javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Mar 2013 09:24:26 +0100
changeset 892 16fd25f3a75d
parent 813 2fa85847ccf7
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Keep case of id tag when generating name of the field
     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;
    19 
    20 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    21 import org.apidesign.bck2brwsr.htmlpage.api.On;
    22 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    23 
    24 /** Trivial demo for the bck2brwsr project. First of all start
    25  * with <a href="TestPage.html">your XHTML page</a>. Include there
    26  * a script that will <em>boot Java</em> in your browser.
    27  * <p>
    28  * Then use <code>@Page</code> annotation to 
    29  * generate a Java representation of elements with IDs in that page.
    30  * Depending on the type of the elements, they will have different 
    31  * methods (e.g. <code>PG_TITLE</code> has <code>setText</code>, etc.).
    32  * Use <code>@OnClick</code> annotation to associate behavior
    33  * with existing elements. Use the generated elements
    34  * (<code>PG_TITLE</code>, <code>PG_TEXT</code>) to modify the page.
    35  * <p>
    36  * Everything is type-safe. As soon as somebody modifies the page and
    37  * removes the IDs or re-assigns them to wrong elements. Java compiler
    38  * will emit an error.
    39  * <p>
    40  * Welcome to the type-safe HTML5 world!
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 @Page(xhtml="TestPage.html")
    45 public class PageController {
    46     private static final TestPage PAGE = new TestPage();
    47     
    48     @On(event = CLICK, id="pg.button")
    49     static void updateTitle(TestPage ref) {
    50         if (PAGE != ref) {
    51             throw new IllegalStateException("Both references should be the same. " + ref + " != " + PAGE);
    52         }
    53         ref.pg_title.setText("You want this window to be named " + ref.pg_text.getValue());
    54     }
    55     
    56     @On(event = CLICK, id={ "pg.title", "pg.text" })
    57     static void click(String id) {
    58         if (!id.equals("pg.title")) {
    59             throw new IllegalStateException();
    60         }
    61         PAGE.pg_title.setText(id);
    62     }
    63     
    64     @On(event = CLICK, id={ "pg.canvas" })
    65     static void clickCanvas(String id, double layerX) {
    66         PAGE.pg_canvas.setWidth((int) layerX);
    67     }
    68 }