javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
author Jan Horvath <jan.horvath@oracle.com>
Thu, 21 Mar 2013 15:45:42 +0100
changeset 866 9b4751828ceb
parent 813 2fa85847ccf7
child 871 6168fb585ab4
permissions -rw-r--r--
Dynamically generate classes representing elements on the HTML @Page
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@100
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@100
    21
jaroslav@26
    22
/** Represents a generic HTML element.
jaroslav@26
    23
 *
jaroslav@26
    24
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@26
    25
 */
jaroslav@26
    26
public abstract class Element {
jaroslav@26
    27
    private final String id;
jaroslav@26
    28
    
jaroslav@26
    29
    public Element(String id) {
jaroslav@26
    30
        this.id = id;
jaroslav@26
    31
    }
jaroslav@26
    32
    
jaroslav@530
    33
    /** Id of the element in the document.
jaroslav@530
    34
     * @return the id for this element
jaroslav@530
    35
     */
jaroslav@530
    36
    public String getId() {
jaroslav@530
    37
        return id;
jaroslav@530
    38
    }
jaroslav@530
    39
    
jan@866
    40
    public String getText() {
jan@866
    41
        return (String)getAttribute("innerHTML");
jan@866
    42
    }
jan@866
    43
jan@866
    44
    public void setText(String text) {
jan@866
    45
        setAttribute("innerHTML", text);
jan@866
    46
    }
jaroslav@27
    47
    
jaroslav@100
    48
    @JavaScriptBody(
jaroslav@100
    49
        args={"el", "property", "value"},
jaroslav@592
    50
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    51
           + "e[property] = value;\n"
jaroslav@100
    52
    )
jaroslav@428
    53
    static native void setAttribute(Element el, String property, Object value);
jaroslav@100
    54
jaroslav@100
    55
    @JavaScriptBody(
jaroslav@100
    56
        args={"el", "property"},
jaroslav@592
    57
        body="var e = window.document.getElementById(el._id());\n"
jaroslav@100
    58
           + "return e[property];\n"
jaroslav@100
    59
    )
jaroslav@428
    60
    static native Object getAttribute(Element el, String property);
jaroslav@28
    61
    
toni@521
    62
    @JavaScriptBody(
toni@521
    63
        args={"el"},
jaroslav@592
    64
        body="return window.document.getElementById(el._id());"
toni@521
    65
    )
toni@521
    66
    static native Object getElementById(Element el);
toni@521
    67
    
jaroslav@28
    68
    /** Executes given runnable when user performs a "click" on the given
jaroslav@28
    69
     * element.
jaroslav@813
    70
     * @param data an array of one element to fill with event parameter (if any)
jaroslav@28
    71
     * @param r the runnable to execute, never null
jaroslav@28
    72
     */
jaroslav@100
    73
    @JavaScriptBody(
jaroslav@443
    74
        args={ "ev", "r" },
jaroslav@592
    75
        body="var e = window.document.getElementById(this._id());\n"
jaroslav@813
    76
           + "e[ev._id()] = function(ev) {\n"
jaroslav@813
    77
        + "  var d = ev ? ev : null;\n"
jaroslav@813
    78
        + "  r.onEvent__VLjava_lang_Object_2(d);\n"
jaroslav@813
    79
        + "};\n"
jaroslav@100
    80
    )
jaroslav@813
    81
    final void on(OnEvent ev, OnHandler r) {
jaroslav@514
    82
    }
jaroslav@428
    83
jaroslav@428
    84
    /** Shows alert message dialog in a browser.
jaroslav@428
    85
     * @param msg the message to show
jaroslav@428
    86
     */
jaroslav@428
    87
    @JavaScriptBody(args = "msg", body = "alert(msg);")
jaroslav@428
    88
    public static native void alert(String msg);
jaroslav@436
    89
jaroslav@436
    90
    /** Generic way to query any attribute of this element.
jaroslav@436
    91
     * @param property name of the attribute
jaroslav@436
    92
     */
jaroslav@436
    93
    public final Object getAttribute(String property) {
jaroslav@436
    94
        return getAttribute(this, property);
jaroslav@436
    95
    }
jaroslav@428
    96
    
jaroslav@436
    97
    /** Generic way to change an attribute of this element.
jaroslav@436
    98
     * 
jaroslav@436
    99
     * @param property name of the attribute
jaroslav@436
   100
     * @param value value to associate with the attribute
jaroslav@436
   101
     */
jaroslav@436
   102
    public final void setAttribute(String property, Object value) {
jaroslav@436
   103
        setAttribute(this, property, value);
jaroslav@436
   104
    }
jaroslav@26
   105
}