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
jaroslav@26
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@26
     4
 *
jaroslav@26
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@26
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@26
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@26
     8
 *
jaroslav@26
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@26
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@26
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@26
    12
 * GNU General Public License for more details.
jaroslav@26
    13
 *
jaroslav@26
    14
 * You should have received a copy of the GNU General Public License
jaroslav@26
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@26
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@26
    17
 */
jaroslav@26
    18
package org.apidesign.bck2brwsr.htmlpage.api;
jaroslav@26
    19
jaroslav@845
    20
import javafx.scene.web.WebEngine;
jaroslav@845
    21
import netscape.javascript.JSObject;
jaroslav@100
    22
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@100
    23
jaroslav@26
    24
/** Represents a generic HTML element.
jaroslav@26
    25
 *
jaroslav@26
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@26
    27
 */
jaroslav@26
    28
public abstract class Element {
jaroslav@26
    29
    private final String id;
jaroslav@26
    30
    
jaroslav@26
    31
    public Element(String id) {
jaroslav@26
    32
        this.id = id;
jaroslav@26
    33
    }
jaroslav@26
    34
    
jaroslav@530
    35
    /** Id of the element in the document.
jaroslav@530
    36
     * @return the id for this element
jaroslav@530
    37
     */
jaroslav@530
    38
    public String getId() {
jaroslav@530
    39
        return id;
jaroslav@530
    40
    }
jaroslav@530
    41
    
jaroslav@26
    42
    abstract void dontSubclass();
jaroslav@27
    43
    
jaroslav@100
    44
    @JavaScriptBody(
jaroslav@100
    45
        args={"el", "property", "value"},
jaroslav@592
    46
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    47
           + "e[property] = value;\n"
jaroslav@100
    48
    )
jaroslav@428
    49
    static native void setAttribute(Element el, String property, Object value);
jaroslav@100
    50
jaroslav@100
    51
    @JavaScriptBody(
jaroslav@100
    52
        args={"el", "property"},
jaroslav@592
    53
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    54
           + "return e[property];\n"
jaroslav@100
    55
    )
jaroslav@428
    56
    static native Object getAttribute(Element el, String property);
jaroslav@28
    57
    
toni@521
    58
    @JavaScriptBody(
toni@521
    59
        args={"el"},
jaroslav@592
    60
        body="return window.document.getElementById(el._id());"
toni@521
    61
    )
toni@521
    62
    static native Object getElementById(Element el);
toni@521
    63
    
jaroslav@28
    64
    /** Executes given runnable when user performs a "click" on the given
jaroslav@28
    65
     * element.
jaroslav@813
    66
     * @param data an array of one element to fill with event parameter (if any)
jaroslav@28
    67
     * @param r the runnable to execute, never null
jaroslav@28
    68
     */
jaroslav@100
    69
    @JavaScriptBody(
jaroslav@443
    70
        args={ "ev", "r" },
jaroslav@592
    71
        body="var e = window.document.getElementById(this._id());\n"
jaroslav@813
    72
           + "e[ev._id()] = function(ev) {\n"
jaroslav@813
    73
        + "  var d = ev ? ev : null;\n"
jaroslav@813
    74
        + "  r.onEvent__VLjava_lang_Object_2(d);\n"
jaroslav@813
    75
        + "};\n"
jaroslav@100
    76
    )
jaroslav@813
    77
    final void on(OnEvent ev, OnHandler r) {
jaroslav@845
    78
        WebEngine e = web();
jaroslav@845
    79
        JSObject add = (JSObject)e.executeScript(
jaroslav@845
    80
              "var x = {}; x.add = new Function('e','attr','r', "
jaroslav@845
    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); } };'"
jaroslav@845
    82
            + "); x;");
jaroslav@845
    83
        add.call("add", id, ev.id, r);
jaroslav@845
    84
    }
jaroslav@845
    85
    
jaroslav@845
    86
    private static WebEngine web() {
jaroslav@845
    87
        return (WebEngine) System.getProperties().get("webEngine");
jaroslav@514
    88
    }
jaroslav@428
    89
jaroslav@428
    90
    /** Shows alert message dialog in a browser.
jaroslav@428
    91
     * @param msg the message to show
jaroslav@428
    92
     */
jaroslav@428
    93
    @JavaScriptBody(args = "msg", body = "alert(msg);")
jaroslav@845
    94
    public static void alert(String msg) {
jaroslav@845
    95
        JSObject obj = (JSObject) web().executeScript("window");
jaroslav@845
    96
        obj.call("alert", msg);
jaroslav@845
    97
    }
jaroslav@436
    98
jaroslav@436
    99
    /** Generic way to query any attribute of this element.
jaroslav@436
   100
     * @param property name of the attribute
jaroslav@436
   101
     */
jaroslav@436
   102
    public final Object getAttribute(String property) {
jaroslav@436
   103
        return getAttribute(this, property);
jaroslav@436
   104
    }
jaroslav@428
   105
    
jaroslav@436
   106
    /** Generic way to change an attribute of this element.
jaroslav@436
   107
     * 
jaroslav@436
   108
     * @param property name of the attribute
jaroslav@436
   109
     * @param value value to associate with the attribute
jaroslav@436
   110
     */
jaroslav@436
   111
    public final void setAttribute(String property, Object value) {
jaroslav@436
   112
        setAttribute(this, property, value);
jaroslav@436
   113
    }
jaroslav@26
   114
}