javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 13:43:40 +0100
branchmodel
changeset 505 4198be34b516
parent 435 fb4ed6cc0d4b
child 510 aaf86ae88f46
permissions -rw-r--r--
Moving towards testability: Model class does not have any public fields, rather instance onces
     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() {
    50         PAGE.PG_TITLE.setText("You want this window to be named " + PAGE.PG_TEXT.getValue());
    51     }
    52     
    53     @On(event = CLICK, id={ "pg.title", "pg.text" })
    54     static void click(String id) {
    55         if (!id.equals("pg.title")) {
    56             throw new IllegalStateException();
    57         }
    58         PAGE.PG_TITLE.setText(id);
    59     }
    60 }