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;
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.
15 @Page(xhtml="index.html", className="Index", properties={
16 @Property(name="name", type=String.class),
17 @Property(name="messages", type=String.class, array=true),
21 Index model = new Index();
22 model.setName("World");
23 model.applyBindings();
27 * @param m the model of the index page creates in static initializer
29 @On(event = CLICK, id="hello")
30 static void hello(Index m) {
31 display(m.getHelloMessage(), m);
32 m.getMessages().add(m.getHelloMessage());
35 /** Reacts when mouse moves over the canvas.
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
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;
46 g.setFillStyle("blue");
48 g.setFillStyle("red");
50 g.clearRect(0, 0, 1000, 1000);
51 g.setFont("italic 40px Calibri");
52 g.fillText(m.getHelloMessage(), 10, 40);
55 /** Callback function called by the KnockOut/Java binding on elements
56 * representing href's with individual messages being their data.
58 * @param data the data associated with the element
59 * @param m the model of the page
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);
70 /** Callback function.
72 * @param data data associated with the actual element on the page
73 * @param m the model of the page
76 static void remove(String data, Index m) {
77 m.getMessages().remove(data);
81 static String helloMessage(String name) {
82 return "Hello " + name + "!";
86 static boolean noMessages(List<String> messages) {
87 return messages.isEmpty();