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 @Page(xhtml="Calculator.xhtml")
25 private static final int OP_PLUS = 1;
26 private static final int OP_MINUS = 2;
27 private static final int OP_MUL = 3;
28 private static final int OP_DIV = 4;
30 static double memory = 0;
31 static int operation = 0;
40 private static void setValue(double v) {
41 StringBuilder sb = new StringBuilder();
43 Calculator.DISPLAY.setValue(sb.toString());
46 private static double getValue() {
47 return Double.parseDouble(Calculator.DISPLAY.getValue());
72 static void computeTheValue() {
75 case OP_PLUS: setValue(memory + getValue()); break;
76 case OP_MINUS: setValue(memory - getValue()); break;
77 case OP_MUL: setValue(memory * getValue()); break;
81 @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
82 static void addDigit(String digit) {
83 digit = digit.substring(1);
84 String v = Calculator.DISPLAY.getValue();
85 if ("0".equals(v) || v == null) {
86 Calculator.DISPLAY.setValue(digit);
88 Calculator.DISPLAY.setValue(v + digit);