javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Tue Jan 22 17:53:05 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,89 +0,0 @@
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.On;
1.24 -import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
1.25 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.26 -
1.27 -/** HTML5 & Java demo showing the power of
1.28 - * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
1.29 - * as well as other goodies.
1.30 - *
1.31 - * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.32 - */
1.33 -@Page(xhtml="Calculator.xhtml")
1.34 -public class App {
1.35 - private static double memory;
1.36 - private static String operation;
1.37 -
1.38 - @On(event = CLICK, id="clear")
1.39 - static void clear() {
1.40 - memory = 0;
1.41 - operation = null;
1.42 - Calculator.DISPLAY.setValue("0");
1.43 - }
1.44 -
1.45 - @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
1.46 - static void applyOp(String op) {
1.47 - memory = getValue();
1.48 - operation = op;
1.49 - Calculator.DISPLAY.setValue("0");
1.50 - }
1.51 -
1.52 - @On(event = CLICK, id="result")
1.53 - static void computeTheValue() {
1.54 - switch (operation) {
1.55 - case "plus": setValue(memory + getValue()); break;
1.56 - case "minus": setValue(memory - getValue()); break;
1.57 - case "mul": setValue(memory * getValue()); break;
1.58 - case "div": setValue(memory / getValue()); break;
1.59 - default: throw new IllegalStateException(operation);
1.60 - }
1.61 - }
1.62 -
1.63 - @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
1.64 - static void addDigit(String digit) {
1.65 - digit = digit.substring(1);
1.66 - String v = Calculator.DISPLAY.getValue();
1.67 - if (getValue() == 0.0) {
1.68 - Calculator.DISPLAY.setValue(digit);
1.69 - } else {
1.70 - Calculator.DISPLAY.setValue(v + digit);
1.71 - }
1.72 - }
1.73 -
1.74 - private static void setValue(double v) {
1.75 - StringBuilder sb = new StringBuilder();
1.76 - sb.append(v);
1.77 - if (sb.toString().endsWith(".0")) {
1.78 - final int l = sb.length();
1.79 - sb.delete(l - 2, l);
1.80 - }
1.81 - Calculator.DISPLAY.setValue(sb.toString());
1.82 - }
1.83 -
1.84 - private static double getValue() {
1.85 - try {
1.86 - return Double.parseDouble(Calculator.DISPLAY.getValue());
1.87 - } catch (NumberFormatException ex) {
1.88 - Calculator.DISPLAY.setValue("err");
1.89 - return 0.0;
1.90 - }
1.91 - }
1.92 -}