jaroslav@421: package ${package};
jaroslav@141:
jaroslav@893: import java.util.List;
toni@1126: import net.java.html.canvas.IGraphicsContext;
jaroslav@437: import org.apidesign.bck2brwsr.htmlpage.api.*;
jaroslav@435: import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
jaroslav@141: import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@528: import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@528: import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
jaroslav@141:
jaroslav@893: /** This is the controller class for associated index.html page. The Index
jaroslav@893: * is autogenerated by parsing the index.html page. It fields represent individual
jaroslav@893: * elements annotated by "id" in the page.
jaroslav@198: */
jaroslav@744: @Page(xhtml="index.html", className="Index", properties={
jaroslav@893: @Property(name="name", type=String.class),
jaroslav@893: @Property(name="messages", type=String.class, array=true),
jaroslav@528: })
jaroslav@141: public class App {
jaroslav@528: static {
jaroslav@528: Index model = new Index();
jaroslav@528: model.setName("World");
jaroslav@528: model.applyBindings();
jaroslav@528: }
jaroslav@528:
jaroslav@893: /**
jaroslav@893: * @param m the model of the index page creates in static initializer
jaroslav@893: */
jaroslav@435: @On(event = CLICK, id="hello")
jaroslav@528: static void hello(Index m) {
jaroslav@893: display(m.getHelloMessage(), m);
jaroslav@893: m.getMessages().add(m.getHelloMessage());
jaroslav@893: }
jaroslav@893:
jaroslav@893: /** Reacts when mouse moves over the canvas.
jaroslav@893: *
jaroslav@893: * @param m the model of the page
jaroslav@893: * @param x property "x" extracted from the event generated by the browser
jaroslav@893: * @param y property "y" from the mouse event
jaroslav@893: */
jaroslav@893: @On(event = MOUSE_MOVE, id="canvas")
jaroslav@893: static void clearPoint(Index m, int x, int y) {
toni@1126: IGraphicsContext g = m.canvas.getContext();
jaroslav@893: boolean even = (x + y) % 2 == 0;
jaroslav@893: if (even) {
jaroslav@893: g.setFillStyle("blue");
jaroslav@893: } else {
jaroslav@893: g.setFillStyle("red");
jaroslav@893: }
jaroslav@533: g.clearRect(0, 0, 1000, 1000);
jaroslav@533: g.setFont("italic 40px Calibri");
jaroslav@533: g.fillText(m.getHelloMessage(), 10, 40);
jaroslav@528: }
jaroslav@893:
jaroslav@893: /** Callback function called by the KnockOut/Java binding on elements
jaroslav@893: * representing href's with individual messages being their data.
jaroslav@893: *
jaroslav@893: * @param data the data associated with the element
jaroslav@893: * @param m the model of the page
jaroslav@893: */
jaroslav@893: @OnFunction
jaroslav@893: static void display(String data, Index m) {
toni@1126: IGraphicsContext g = m.canvas.getContext();
jaroslav@893: g.clearRect(0, 0, 1000, 1000);
jaroslav@893: g.setFillStyle("black");
jaroslav@893: g.setFont("italic 40px Calibri");
jaroslav@893: g.fillText(data, 10, 40);
jaroslav@893: }
jaroslav@893:
jaroslav@893: /** Callback function.
jaroslav@893: *
jaroslav@893: * @param data data associated with the actual element on the page
jaroslav@893: * @param m the model of the page
jaroslav@893: */
jaroslav@893: @OnFunction
jaroslav@893: static void remove(String data, Index m) {
jaroslav@893: m.getMessages().remove(data);
jaroslav@893: }
jaroslav@528:
jaroslav@528: @ComputedProperty
jaroslav@528: static String helloMessage(String name) {
jaroslav@528: return "Hello " + name + "!";
jaroslav@141: }
jaroslav@893:
jaroslav@893: @ComputedProperty
jaroslav@893: static boolean noMessages(List messages) {
jaroslav@893: return messages.isEmpty();
jaroslav@893: }
jaroslav@141: }