javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 14 Mar 2013 09:22:28 +0100
branchfx
changeset 845 859804c78010
parent 813 2fa85847ccf7
child 969 df08556c5c7c
permissions -rw-r--r--
Hacky way (relies on singleton) to get FX working. Can attach @On(event=CLICK) and show alert in FX WebView now
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage.api;
    19 
    20 import javafx.scene.web.WebEngine;
    21 import netscape.javascript.JSObject;
    22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    23 
    24 /** Represents a generic HTML element.
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public abstract class Element {
    29     private final String id;
    30     
    31     public Element(String id) {
    32         this.id = id;
    33     }
    34     
    35     /** Id of the element in the document.
    36      * @return the id for this element
    37      */
    38     public String getId() {
    39         return id;
    40     }
    41     
    42     abstract void dontSubclass();
    43     
    44     @JavaScriptBody(
    45         args={"el", "property", "value"},
    46         body="var e = window.document.getElementById(el._id());\n"
    47            + "e[property] = value;\n"
    48     )
    49     static native void setAttribute(Element el, String property, Object value);
    50 
    51     @JavaScriptBody(
    52         args={"el", "property"},
    53         body="var e = window.document.getElementById(el._id());\n"
    54            + "return e[property];\n"
    55     )
    56     static native Object getAttribute(Element el, String property);
    57     
    58     @JavaScriptBody(
    59         args={"el"},
    60         body="return window.document.getElementById(el._id());"
    61     )
    62     static native Object getElementById(Element el);
    63     
    64     /** Executes given runnable when user performs a "click" on the given
    65      * element.
    66      * @param data an array of one element to fill with event parameter (if any)
    67      * @param r the runnable to execute, never null
    68      */
    69     @JavaScriptBody(
    70         args={ "ev", "r" },
    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"
    75         + "};\n"
    76     )
    77     final void on(OnEvent ev, OnHandler r) {
    78         WebEngine e = web();
    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); } };'"
    82             + "); x;");
    83         add.call("add", id, ev.id, r);
    84     }
    85     
    86     private static WebEngine web() {
    87         return (WebEngine) System.getProperties().get("webEngine");
    88     }
    89 
    90     /** Shows alert message dialog in a browser.
    91      * @param msg the message to show
    92      */
    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);
    97     }
    98 
    99     /** Generic way to query any attribute of this element.
   100      * @param property name of the attribute
   101      */
   102     public final Object getAttribute(String property) {
   103         return getAttribute(this, property);
   104     }
   105     
   106     /** Generic way to change an attribute of this element.
   107      * 
   108      * @param property name of the attribute
   109      * @param value value to associate with the attribute
   110      */
   111     public final void setAttribute(String property, Object value) {
   112         setAttribute(this, property, value);
   113     }
   114 }