javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 14 Apr 2013 11:51:58 +0200
branchfx
changeset 978 5aa8bc3c98b0
parent 969 df08556c5c7c
child 1011 9cc253aa9405
permissions -rw-r--r--
Need to be able to instantiate Element to access <span id>
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@978
    28
public 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@978
    42
    void dontSubclass() {
jaroslav@978
    43
    }
jaroslav@27
    44
    
jaroslav@100
    45
    @JavaScriptBody(
jaroslav@100
    46
        args={"el", "property", "value"},
jaroslav@592
    47
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    48
           + "e[property] = value;\n"
jaroslav@100
    49
    )
jaroslav@428
    50
    static native void setAttribute(Element el, String property, Object value);
jaroslav@100
    51
jaroslav@100
    52
    @JavaScriptBody(
jaroslav@100
    53
        args={"el", "property"},
jaroslav@592
    54
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    55
           + "return e[property];\n"
jaroslav@100
    56
    )
jaroslav@428
    57
    static native Object getAttribute(Element el, String property);
jaroslav@28
    58
    
toni@521
    59
    @JavaScriptBody(
toni@521
    60
        args={"el"},
jaroslav@592
    61
        body="return window.document.getElementById(el._id());"
toni@521
    62
    )
toni@521
    63
    static native Object getElementById(Element el);
toni@521
    64
    
jaroslav@28
    65
    /** Executes given runnable when user performs a "click" on the given
jaroslav@28
    66
     * element.
jaroslav@813
    67
     * @param data an array of one element to fill with event parameter (if any)
jaroslav@28
    68
     * @param r the runnable to execute, never null
jaroslav@28
    69
     */
jaroslav@100
    70
    @JavaScriptBody(
jaroslav@443
    71
        args={ "ev", "r" },
jaroslav@592
    72
        body="var e = window.document.getElementById(this._id());\n"
jaroslav@813
    73
           + "e[ev._id()] = function(ev) {\n"
jaroslav@813
    74
        + "  var d = ev ? ev : null;\n"
jaroslav@813
    75
        + "  r.onEvent__VLjava_lang_Object_2(d);\n"
jaroslav@813
    76
        + "};\n"
jaroslav@100
    77
    )
jaroslav@813
    78
    final void on(OnEvent ev, OnHandler r) {
jaroslav@845
    79
        WebEngine e = web();
jaroslav@845
    80
        JSObject add = (JSObject)e.executeScript(
jaroslav@845
    81
              "var x = {}; x.add = new Function('e','attr','r', "
jaroslav@845
    82
            + "  '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
    83
            + "); x;");
jaroslav@845
    84
        add.call("add", id, ev.id, r);
jaroslav@845
    85
    }
jaroslav@845
    86
    
jaroslav@845
    87
    private static WebEngine web() {
jaroslav@845
    88
        return (WebEngine) System.getProperties().get("webEngine");
jaroslav@514
    89
    }
jaroslav@428
    90
jaroslav@428
    91
    /** Shows alert message dialog in a browser.
jaroslav@428
    92
     * @param msg the message to show
jaroslav@428
    93
     */
jaroslav@428
    94
    @JavaScriptBody(args = "msg", body = "alert(msg);")
jaroslav@845
    95
    public static void alert(String msg) {
jaroslav@845
    96
        JSObject obj = (JSObject) web().executeScript("window");
jaroslav@845
    97
        obj.call("alert", msg);
jaroslav@845
    98
    }
jaroslav@436
    99
jaroslav@436
   100
    /** Generic way to query any attribute of this element.
jaroslav@436
   101
     * @param property name of the attribute
jaroslav@436
   102
     */
jaroslav@436
   103
    public final Object getAttribute(String property) {
jaroslav@436
   104
        return getAttribute(this, property);
jaroslav@436
   105
    }
jaroslav@428
   106
    
jaroslav@436
   107
    /** Generic way to change an attribute of this element.
jaroslav@436
   108
     * 
jaroslav@436
   109
     * @param property name of the attribute
jaroslav@436
   110
     * @param value value to associate with the attribute
jaroslav@436
   111
     */
jaroslav@436
   112
    public final void setAttribute(String property, Object value) {
jaroslav@436
   113
        setAttribute(this, property, value);
jaroslav@436
   114
    }
jaroslav@26
   115
}