jaroslav@142: /** jaroslav@142: * Back 2 Browser Bytecode Translator jaroslav@142: * Copyright (C) 2012 Jaroslav Tulach jaroslav@142: * jaroslav@142: * This program is free software: you can redistribute it and/or modify jaroslav@142: * it under the terms of the GNU General Public License as published by jaroslav@142: * the Free Software Foundation, version 2 of the License. jaroslav@142: * jaroslav@142: * This program is distributed in the hope that it will be useful, jaroslav@142: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@142: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@142: * GNU General Public License for more details. jaroslav@142: * jaroslav@142: * You should have received a copy of the GNU General Public License jaroslav@142: * along with this program. Look for COPYING file in the top folder. jaroslav@142: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@142: */ jaroslav@141: package org.apidesign.bck2brwsr.mavenhtml; jaroslav@141: jaroslav@141: import org.apidesign.bck2brwsr.htmlpage.api.OnClick; jaroslav@141: import org.apidesign.bck2brwsr.htmlpage.api.Page; jaroslav@141: jaroslav@198: /** HTML5 & Java demo showing the power of jaroslav@198: * annotation processors jaroslav@198: * as well as other goodies. jaroslav@198: * jaroslav@198: * @author Jaroslav Tulach jaroslav@198: */ jaroslav@141: @Page(xhtml="Calculator.xhtml") jaroslav@141: public class App { jtulach@197: private static double memory; jtulach@197: private static String operation; jaroslav@141: jaroslav@141: @OnClick(id="clear") jaroslav@141: static void clear() { jtulach@197: memory = 0; jtulach@197: operation = null; jtulach@197: Calculator.DISPLAY.setValue("0"); jtulach@197: } jtulach@197: jtulach@197: @OnClick(id= { "plus", "minus", "mul", "div" }) jtulach@197: static void applyOp(String op) { jtulach@197: memory = getValue(); jtulach@197: operation = op; jtulach@197: Calculator.DISPLAY.setValue("0"); jtulach@197: } jtulach@197: jtulach@197: @OnClick(id="result") jtulach@197: static void computeTheValue() { jtulach@197: switch (operation) { jtulach@197: case "plus": setValue(memory + getValue()); break; jtulach@197: case "minus": setValue(memory - getValue()); break; jtulach@197: case "mul": setValue(memory * getValue()); break; jtulach@197: case "div": setValue(memory / getValue()); break; jtulach@197: default: throw new IllegalStateException(operation); jtulach@197: } jtulach@197: } jtulach@197: jtulach@197: @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) jtulach@197: static void addDigit(String digit) { jtulach@197: digit = digit.substring(1); jtulach@197: String v = Calculator.DISPLAY.getValue(); jtulach@197: if (getValue() == 0.0) { jtulach@197: Calculator.DISPLAY.setValue(digit); jtulach@197: } else { jtulach@197: Calculator.DISPLAY.setValue(v + digit); jtulach@197: } jaroslav@141: } jaroslav@141: jaroslav@141: private static void setValue(double v) { jaroslav@141: StringBuilder sb = new StringBuilder(); jaroslav@141: sb.append(v); jaroslav@296: if (sb.toString().endsWith(".0")) { jaroslav@296: final int l = sb.length(); jaroslav@296: sb.delete(l - 2, l); jaroslav@296: } jaroslav@141: Calculator.DISPLAY.setValue(sb.toString()); jaroslav@141: } jtulach@197: jaroslav@141: private static double getValue() { jtulach@197: try { jtulach@197: return Double.parseDouble(Calculator.DISPLAY.getValue()); jtulach@197: } catch (NumberFormatException ex) { jtulach@197: Calculator.DISPLAY.setValue("err"); jtulach@197: return 0.0; jaroslav@141: } jaroslav@141: } jaroslav@141: }