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