javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
Basic support for knockout.js. It can read and write model properties.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.mavenhtml;
20 import org.apidesign.bck2brwsr.htmlpage.api.On;
21 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
22 import org.apidesign.bck2brwsr.htmlpage.api.Page;
23 import org.apidesign.bck2brwsr.htmlpage.api.Property;
25 /** HTML5 & Java demo showing the power of
26 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
27 * as well as other goodies.
29 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
31 @Page(xhtml="Calculator.xhtml", properties = {
32 @Property(name = "display", type = double.class)
35 private static double memory;
36 private static String operation;
38 @On(event = CLICK, id="clear")
42 Calculator.setDisplay(0);
45 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
46 static void applyOp(String op) {
47 memory = Calculator.getDisplay();
49 Calculator.setDisplay(0);
52 @On(event = CLICK, id="result")
53 static void computeTheValue() {
55 case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
56 case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
57 case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
58 case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
59 default: throw new IllegalStateException(operation);
63 @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
64 static void addDigit(String digit) {
65 digit = digit.substring(1);
67 double v = Calculator.getDisplay();
69 Calculator.setDisplay(Integer.parseInt(digit));
71 String txt = Double.toString(v) + digit;
72 Calculator.setDisplay(Double.parseDouble(txt));
78 Calculator.setDisplay(10.0);
79 Calculator.applyBindings();