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