2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.htmlpage.api;
20 import javafx.scene.web.WebEngine;
21 import netscape.javascript.JSObject;
22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
24 /** Represents a generic HTML element.
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 public class Element {
29 private final String id;
31 public Element(String id) {
35 /** Id of the element in the document.
36 * @return the id for this element
38 public String getId() {
46 args={"el", "property", "value"},
47 body="var e = window.document.getElementById(el._id());\n"
48 + "e[property] = value;\n"
50 static void setAttribute(Element el, String property, Object value) {
51 JSObject js = (JSObject) web().executeScript("(function () {"
53 + " x.setAttribute = function(id, property, value) { "
54 + " var e = window.document.getElementById(id);\n"
55 + " e[property] = value;"
59 js.call("setAttribute", el.id, property, value);
63 args={"el", "property"},
64 body="var e = window.document.getElementById(el._id());\n"
65 + "return e[property];\n"
67 static Object getAttribute(Element el, String property) {
68 JSObject js = (JSObject) web().executeScript("(function () {"
70 + " x.getAttribute = function(id, property) { "
71 + " var e = window.document.getElementById(id);\n"
72 + " return e[property];"
76 return js.call("getAttribute", el.id, property);
81 body="return window.document.getElementById(el._id());"
83 static native Object getElementById(Element el);
85 /** Executes given runnable when user performs a "click" on the given
87 * @param data an array of one element to fill with event parameter (if any)
88 * @param r the runnable to execute, never null
92 body="var e = window.document.getElementById(this._id());\n"
93 + "e[ev._id()] = function(ev) {\n"
94 + " var d = ev ? ev : null;\n"
95 + " r.onEvent__VLjava_lang_Object_2(d);\n"
98 final void on(OnEvent ev, OnHandler r) {
100 JSObject add = (JSObject)e.executeScript(
101 "var x = {}; x.add = new Function('e','attr','r', "
102 + " 'window.document.getElementById(e)[attr] = function(ev) { var d = ev ? ev : null; try { r.onEvent(d); } catch (x) { alert(\"OK\" + r[\"onEvent\"] + \"e\" + d + x); } };'"
104 add.call("add", id, ev.id, r);
107 private static WebEngine web() {
108 return (WebEngine) System.getProperties().get("webEngine");
111 /** Shows alert message dialog in a browser.
112 * @param msg the message to show
114 @JavaScriptBody(args = "msg", body = "alert(msg);")
115 public static void alert(String msg) {
116 JSObject obj = (JSObject) web().executeScript("window");
117 obj.call("alert", msg);
120 /** Generic way to query any attribute of this element.
121 * @param property name of the attribute
123 public final Object getAttribute(String property) {
124 return getAttribute(this, property);
127 /** Generic way to change an attribute of this element.
129 * @param property name of the attribute
130 * @param value value to associate with the attribute
132 public final void setAttribute(String property, Object value) {
133 setAttribute(this, property, value);