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