javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 14 Jan 2013 11:30:56 +0100
branchUseFunctionCall
changeset 443 9359b006782b
parent 436 42f0ad9e4152
child 514 f761555d8312
child 521 5e9d7f92d5d2
permissions -rw-r--r--
Using 'this' in @JavaScriptBody instance methods
     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     abstract void dontSubclass();
    34     
    35     @JavaScriptBody(
    36         args={"el", "property", "value"},
    37         body="var e = window.document.getElementById(el.fld_id);\n"
    38            + "e[property] = value;\n"
    39     )
    40     static native void setAttribute(Element el, String property, Object value);
    41 
    42     @JavaScriptBody(
    43         args={"el", "property"},
    44         body="var e = window.document.getElementById(el.fld_id);\n"
    45            + "return e[property];\n"
    46     )
    47     static native Object getAttribute(Element el, String property);
    48     
    49     /** Executes given runnable when user performs a "click" on the given
    50      * element.
    51      * @param r the runnable to execute, never null
    52      */
    53     @JavaScriptBody(
    54         args={ "ev", "r" },
    55         body="var e = window.document.getElementById(this.fld_id);\n"
    56            + "e[ev.fld_id] = function() { r.run__V(); };\n"
    57     )
    58     final native void on(OnEvent ev, Runnable r);
    59 
    60     /** Shows alert message dialog in a browser.
    61      * @param msg the message to show
    62      */
    63     @JavaScriptBody(args = "msg", body = "alert(msg);")
    64     public static native void alert(String msg);
    65 
    66     /** Generic way to query any attribute of this element.
    67      * @param property name of the attribute
    68      */
    69     public final Object getAttribute(String property) {
    70         return getAttribute(this, property);
    71     }
    72     
    73     /** Generic way to change an attribute of this element.
    74      * 
    75      * @param property name of the attribute
    76      * @param value value to associate with the attribute
    77      */
    78     public final void setAttribute(String property, Object value) {
    79         setAttribute(this, property, value);
    80     }
    81 }