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