javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
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;
24 /** HTML5 & Java demo showing the power of
25 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
26 * as well as other goodies.
28 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
30 @Page(xhtml="Calculator.xhtml")
32 private static double memory;
33 private static String operation;
35 @On(event = CLICK, id="clear")
39 Calculator.DISPLAY.setValue("0");
42 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
43 static void applyOp(String op) {
46 Calculator.DISPLAY.setValue("0");
49 @On(event = CLICK, id="result")
50 static void computeTheValue() {
52 case "plus": setValue(memory + getValue()); break;
53 case "minus": setValue(memory - getValue()); break;
54 case "mul": setValue(memory * getValue()); break;
55 case "div": setValue(memory / getValue()); break;
56 default: throw new IllegalStateException(operation);
60 @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
61 static void addDigit(String digit) {
62 digit = digit.substring(1);
63 String v = Calculator.DISPLAY.getValue();
64 if (getValue() == 0.0) {
65 Calculator.DISPLAY.setValue(digit);
67 Calculator.DISPLAY.setValue(v + digit);
71 private static void setValue(double v) {
72 StringBuilder sb = new StringBuilder();
74 if (sb.toString().endsWith(".0")) {
75 final int l = sb.length();
78 Calculator.DISPLAY.setValue(sb.toString());
81 private static double getValue() {
83 return Double.parseDouble(Calculator.DISPLAY.getValue());
84 } catch (NumberFormatException ex) {
85 Calculator.DISPLAY.setValue("err");