javaquery/demo-static-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javaquery/demo-static-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Dec 24 07:51:15 2012 +0100
1.3 @@ -0,0 +1,88 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.mavenhtml;
1.22 +
1.23 +import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
1.24 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.25 +
1.26 +/** HTML5 & Java demo showing the power of
1.27 + * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
1.28 + * as well as other goodies.
1.29 + *
1.30 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.31 + */
1.32 +@Page(xhtml="Calculator.xhtml")
1.33 +public class App {
1.34 + private static double memory;
1.35 + private static String operation;
1.36 +
1.37 + @OnClick(id="clear")
1.38 + static void clear() {
1.39 + memory = 0;
1.40 + operation = null;
1.41 + Calculator.DISPLAY.setValue("0");
1.42 + }
1.43 +
1.44 + @OnClick(id= { "plus", "minus", "mul", "div" })
1.45 + static void applyOp(String op) {
1.46 + memory = getValue();
1.47 + operation = op;
1.48 + Calculator.DISPLAY.setValue("0");
1.49 + }
1.50 +
1.51 + @OnClick(id="result")
1.52 + static void computeTheValue() {
1.53 + switch (operation) {
1.54 + case "plus": setValue(memory + getValue()); break;
1.55 + case "minus": setValue(memory - getValue()); break;
1.56 + case "mul": setValue(memory * getValue()); break;
1.57 + case "div": setValue(memory / getValue()); break;
1.58 + default: throw new IllegalStateException(operation);
1.59 + }
1.60 + }
1.61 +
1.62 + @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
1.63 + static void addDigit(String digit) {
1.64 + digit = digit.substring(1);
1.65 + String v = Calculator.DISPLAY.getValue();
1.66 + if (getValue() == 0.0) {
1.67 + Calculator.DISPLAY.setValue(digit);
1.68 + } else {
1.69 + Calculator.DISPLAY.setValue(v + digit);
1.70 + }
1.71 + }
1.72 +
1.73 + private static void setValue(double v) {
1.74 + StringBuilder sb = new StringBuilder();
1.75 + sb.append(v);
1.76 + if (sb.toString().endsWith(".0")) {
1.77 + final int l = sb.length();
1.78 + sb.delete(l - 2, l);
1.79 + }
1.80 + Calculator.DISPLAY.setValue(sb.toString());
1.81 + }
1.82 +
1.83 + private static double getValue() {
1.84 + try {
1.85 + return Double.parseDouble(Calculator.DISPLAY.getValue());
1.86 + } catch (NumberFormatException ex) {
1.87 + Calculator.DISPLAY.setValue("err");
1.88 + return 0.0;
1.89 + }
1.90 + }
1.91 +}