rt/archetype/src/main/resources/archetype-resources/src/main/java/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Mar 2013 09:29:05 +0100
changeset 893 20fb32569339
parent 870 448bed1f6d5a
child 1126 338d80e0344f
permissions -rw-r--r--
Richer example showing Knockout operations with arrays/lists and ability to receive parameters from an event
     1 package ${package};
     2 
     3 import java.util.List;
     4 import org.apidesign.bck2brwsr.htmlpage.api.*;
     5 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
     6 import org.apidesign.bck2brwsr.htmlpage.api.Page;
     7 import org.apidesign.bck2brwsr.htmlpage.api.Property;
     8 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
     9 
    10 /** This is the controller class for associated index.html page. The <code>Index</code>
    11  * is autogenerated by parsing the index.html page. It fields represent individual
    12  * elements annotated by "id" in the page.
    13  */
    14 @Page(xhtml="index.html", className="Index", properties={
    15     @Property(name="name", type=String.class),
    16     @Property(name="messages", type=String.class, array=true),
    17 })
    18 public class App {
    19     static {
    20         Index model = new Index();
    21         model.setName("World");
    22         model.applyBindings();
    23     }
    24     
    25     /** 
    26      * @param m the model of the index page creates in static initializer
    27      */
    28     @On(event = CLICK, id="hello")
    29     static void hello(Index m) {
    30         display(m.getHelloMessage(), m);
    31         m.getMessages().add(m.getHelloMessage());
    32     }
    33 
    34     /** Reacts when mouse moves over the canvas.
    35      * 
    36      * @param m the model of the page
    37      * @param x property "x" extracted from the event generated by the browser
    38      * @param y property "y" from the mouse event
    39      */
    40     @On(event = MOUSE_MOVE, id="canvas")
    41     static void clearPoint(Index m, int x, int y) {
    42         GraphicsContext g = m.canvas.getContext();
    43         boolean even = (x + y) % 2 == 0;
    44         if (even) {
    45             g.setFillStyle("blue");
    46         } else {
    47             g.setFillStyle("red");
    48         }
    49         g.clearRect(0, 0, 1000, 1000);
    50         g.setFont("italic 40px Calibri");
    51         g.fillText(m.getHelloMessage(), 10, 40);
    52     }
    53 
    54     /** Callback function called by the KnockOut/Java binding on elements
    55      * representing href's with individual messages being their data.
    56      * 
    57      * @param data the data associated with the element 
    58      * @param m the model of the page
    59      */
    60     @OnFunction
    61     static void display(String data, Index m) {
    62         GraphicsContext g = m.canvas.getContext();
    63         g.clearRect(0, 0, 1000, 1000);
    64         g.setFillStyle("black");
    65         g.setFont("italic 40px Calibri");
    66         g.fillText(data, 10, 40);
    67     }
    68 
    69     /** Callback function.
    70      * 
    71      * @param data data associated with the actual element on the page
    72      * @param m the model of the page
    73      */
    74     @OnFunction
    75     static void remove(String data, Index m) {
    76         m.getMessages().remove(data);
    77     }
    78     
    79     @ComputedProperty
    80     static String helloMessage(String name) {
    81         return "Hello " + name + "!";
    82     }
    83     
    84     @ComputedProperty
    85     static boolean noMessages(List<String> messages) {
    86         return messages.isEmpty();
    87     }
    88 }