ko-archetype/src/main/resources/archetype-resources/src/main/java/App.java
changeset 1201 b6fd8b9ccc7a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko-archetype/src/main/resources/archetype-resources/src/main/java/App.java	Mon May 13 11:39:33 2013 +0200
     1.3 @@ -0,0 +1,88 @@
     1.4 +package ${package};
     1.5 +
     1.6 +import java.util.List;
     1.7 +import org.apidesign.bck2brwsr.htmlpage.api.*;
     1.8 +import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
     1.9 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.10 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
    1.11 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    1.12 +
    1.13 +/** This is the controller class for associated index.html page. The <code>Index</code>
    1.14 + * is autogenerated by parsing the index.html page. It fields represent individual
    1.15 + * elements annotated by "id" in the page.
    1.16 + */
    1.17 +@Page(xhtml="index.html", className="Index", properties={
    1.18 +    @Property(name="name", type=String.class),
    1.19 +    @Property(name="messages", type=String.class, array=true),
    1.20 +})
    1.21 +public class App {
    1.22 +    static {
    1.23 +        Index model = new Index();
    1.24 +        model.setName("World");
    1.25 +        model.applyBindings();
    1.26 +    }
    1.27 +    
    1.28 +    /** 
    1.29 +     * @param m the model of the index page creates in static initializer
    1.30 +     */
    1.31 +    @On(event = CLICK, id="hello")
    1.32 +    static void hello(Index m) {
    1.33 +        display(m.getHelloMessage(), m);
    1.34 +        m.getMessages().add(m.getHelloMessage());
    1.35 +    }
    1.36 +
    1.37 +    /** Reacts when mouse moves over the canvas.
    1.38 +     * 
    1.39 +     * @param m the model of the page
    1.40 +     * @param x property "x" extracted from the event generated by the browser
    1.41 +     * @param y property "y" from the mouse event
    1.42 +     */
    1.43 +    @On(event = MOUSE_MOVE, id="canvas")
    1.44 +    static void clearPoint(Index m, int x, int y) {
    1.45 +        GraphicsContext g = m.canvas.getContext();
    1.46 +        boolean even = (x + y) % 2 == 0;
    1.47 +        if (even) {
    1.48 +            g.setFillStyle("blue");
    1.49 +        } else {
    1.50 +            g.setFillStyle("red");
    1.51 +        }
    1.52 +        g.clearRect(0, 0, 1000, 1000);
    1.53 +        g.setFont("italic 40px Calibri");
    1.54 +        g.fillText(m.getHelloMessage(), 10, 40);
    1.55 +    }
    1.56 +
    1.57 +    /** Callback function called by the KnockOut/Java binding on elements
    1.58 +     * representing href's with individual messages being their data.
    1.59 +     * 
    1.60 +     * @param data the data associated with the element 
    1.61 +     * @param m the model of the page
    1.62 +     */
    1.63 +    @OnFunction
    1.64 +    static void display(String data, Index m) {
    1.65 +        GraphicsContext g = m.canvas.getContext();
    1.66 +        g.clearRect(0, 0, 1000, 1000);
    1.67 +        g.setFillStyle("black");
    1.68 +        g.setFont("italic 40px Calibri");
    1.69 +        g.fillText(data, 10, 40);
    1.70 +    }
    1.71 +
    1.72 +    /** Callback function.
    1.73 +     * 
    1.74 +     * @param data data associated with the actual element on the page
    1.75 +     * @param m the model of the page
    1.76 +     */
    1.77 +    @OnFunction
    1.78 +    static void remove(String data, Index m) {
    1.79 +        m.getMessages().remove(data);
    1.80 +    }
    1.81 +    
    1.82 +    @ComputedProperty
    1.83 +    static String helloMessage(String name) {
    1.84 +        return "Hello " + name + "!";
    1.85 +    }
    1.86 +    
    1.87 +    @ComputedProperty
    1.88 +    static boolean noMessages(List<String> messages) {
    1.89 +        return messages.isEmpty();
    1.90 +    }
    1.91 +}