javaquery/demo-calculator/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.OnClick;
21 import org.apidesign.bck2brwsr.htmlpage.api.Page;
23 /** HTML5 & Java demo showing the power of
24 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
25 * as well as other goodies.
27 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
29 @Page(xhtml="Calculator.xhtml")
31 private static double memory;
32 private static String operation;
38 Calculator.DISPLAY.setValue("0");
41 @OnClick(id= { "plus", "minus", "mul", "div" })
42 static void applyOp(String op) {
45 Calculator.DISPLAY.setValue("0");
49 static void computeTheValue() {
51 case "plus": setValue(memory + getValue()); break;
52 case "minus": setValue(memory - getValue()); break;
53 case "mul": setValue(memory * getValue()); break;
54 case "div": setValue(memory / getValue()); break;
55 default: throw new IllegalStateException(operation);
59 @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
60 static void addDigit(String digit) {
61 digit = digit.substring(1);
62 String v = Calculator.DISPLAY.getValue();
63 if (getValue() == 0.0) {
64 Calculator.DISPLAY.setValue(digit);
66 Calculator.DISPLAY.setValue(v + digit);
70 private static void setValue(double v) {
71 StringBuilder sb = new StringBuilder();
73 if (sb.toString().endsWith(".0")) {
74 final int l = sb.length();
77 Calculator.DISPLAY.setValue(sb.toString());
80 private static double getValue() {
82 return Double.parseDouble(Calculator.DISPLAY.getValue());
83 } catch (NumberFormatException ex) {
84 Calculator.DISPLAY.setValue("err");