Hacky way (relies on singleton) to get FX working. Can attach @On(event=CLICK) and show alert in FX WebView now
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 abstract 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() {
42 abstract void dontSubclass();
45 args={"el", "property", "value"},
46 body="var e = window.document.getElementById(el._id());\n"
47 + "e[property] = value;\n"
49 static native void setAttribute(Element el, String property, Object value);
52 args={"el", "property"},
53 body="var e = window.document.getElementById(el._id());\n"
54 + "return e[property];\n"
56 static native Object getAttribute(Element el, String property);
60 body="return window.document.getElementById(el._id());"
62 static native Object getElementById(Element el);
64 /** Executes given runnable when user performs a "click" on the given
66 * @param data an array of one element to fill with event parameter (if any)
67 * @param r the runnable to execute, never null
71 body="var e = window.document.getElementById(this._id());\n"
72 + "e[ev._id()] = function(ev) {\n"
73 + " var d = ev ? ev : null;\n"
74 + " r.onEvent__VLjava_lang_Object_2(d);\n"
77 final void on(OnEvent ev, OnHandler r) {
79 JSObject add = (JSObject)e.executeScript(
80 "var x = {}; x.add = new Function('e','attr','r', "
81 + " '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); } };'"
83 add.call("add", id, ev.id, r);
86 private static WebEngine web() {
87 return (WebEngine) System.getProperties().get("webEngine");
90 /** Shows alert message dialog in a browser.
91 * @param msg the message to show
93 @JavaScriptBody(args = "msg", body = "alert(msg);")
94 public static void alert(String msg) {
95 JSObject obj = (JSObject) web().executeScript("window");
96 obj.call("alert", msg);
99 /** Generic way to query any attribute of this element.
100 * @param property name of the attribute
102 public final Object getAttribute(String property) {
103 return getAttribute(this, property);
106 /** Generic way to change an attribute of this element.
108 * @param property name of the attribute
109 * @param value value to associate with the attribute
111 public final void setAttribute(String property, Object value) {
112 setAttribute(this, property, value);