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