jaroslav@26: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@26: * jaroslav@26: * This program is free software: you can redistribute it and/or modify jaroslav@26: * it under the terms of the GNU General Public License as published by jaroslav@26: * the Free Software Foundation, version 2 of the License. jaroslav@26: * jaroslav@26: * This program is distributed in the hope that it will be useful, jaroslav@26: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@26: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@26: * GNU General Public License for more details. jaroslav@26: * jaroslav@26: * You should have received a copy of the GNU General Public License jaroslav@26: * along with this program. Look for COPYING file in the top folder. jaroslav@26: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@26: */ jaroslav@26: package org.apidesign.bck2brwsr.htmlpage.api; jaroslav@26: jaroslav@100: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@100: jaroslav@26: /** Represents a generic HTML element. jaroslav@26: * jaroslav@26: * @author Jaroslav Tulach jaroslav@26: */ jaroslav@26: public abstract class Element { jaroslav@26: private final String id; jaroslav@26: jaroslav@26: public Element(String id) { jaroslav@26: this.id = id; jaroslav@26: } jaroslav@26: jaroslav@530: /** Id of the element in the document. jaroslav@530: * @return the id for this element jaroslav@530: */ jaroslav@530: public String getId() { jaroslav@530: return id; jaroslav@530: } jaroslav@530: jaroslav@871: abstract void dontSubclass(); jaroslav@27: jaroslav@100: @JavaScriptBody( jaroslav@100: args={"el", "property", "value"}, jaroslav@592: body="var e = window.document.getElementById(el._id());\n" jaroslav@100: + "e[property] = value;\n" jaroslav@100: ) jaroslav@428: static native void setAttribute(Element el, String property, Object value); jaroslav@100: jaroslav@100: @JavaScriptBody( jaroslav@100: args={"el", "property"}, jaroslav@592: body="var e = window.document.getElementById(el._id());\n" jaroslav@100: + "return e[property];\n" jaroslav@100: ) jaroslav@428: static native Object getAttribute(Element el, String property); jaroslav@28: toni@521: @JavaScriptBody( toni@521: args={"el"}, jaroslav@592: body="return window.document.getElementById(el._id());" toni@521: ) toni@521: static native Object getElementById(Element el); toni@521: jaroslav@28: /** Executes given runnable when user performs a "click" on the given jaroslav@28: * element. jaroslav@813: * @param data an array of one element to fill with event parameter (if any) jaroslav@28: * @param r the runnable to execute, never null jaroslav@28: */ jaroslav@100: @JavaScriptBody( jaroslav@443: args={ "ev", "r" }, jaroslav@592: body="var e = window.document.getElementById(this._id());\n" jaroslav@813: + "e[ev._id()] = function(ev) {\n" jaroslav@813: + " var d = ev ? ev : null;\n" jaroslav@813: + " r.onEvent__VLjava_lang_Object_2(d);\n" jaroslav@813: + "};\n" jaroslav@100: ) jaroslav@813: final void on(OnEvent ev, OnHandler r) { jaroslav@514: } jaroslav@428: jaroslav@428: /** Shows alert message dialog in a browser. jaroslav@428: * @param msg the message to show jaroslav@428: */ jaroslav@428: @JavaScriptBody(args = "msg", body = "alert(msg);") jaroslav@428: public static native void alert(String msg); jaroslav@436: jaroslav@436: /** Generic way to query any attribute of this element. jaroslav@436: * @param property name of the attribute jaroslav@436: */ jaroslav@436: public final Object getAttribute(String property) { jaroslav@436: return getAttribute(this, property); jaroslav@436: } jaroslav@428: jaroslav@436: /** Generic way to change an attribute of this element. jaroslav@436: * jaroslav@436: * @param property name of the attribute jaroslav@436: * @param value value to associate with the attribute jaroslav@436: */ jaroslav@436: public final void setAttribute(String property, Object value) { jaroslav@436: setAttribute(this, property, value); jaroslav@436: } jaroslav@26: }