javaquery/demo-static-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Dec 2012 07:51:15 +0100
changeset 373 52386a70f9d9
parent 296 javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java@fbf8eb98a8ef
permissions -rw-r--r--
Archiving static compilation demo
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.mavenhtml;
    19 
    20 import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    21 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    22 
    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.
    26  * 
    27  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    28  */
    29 @Page(xhtml="Calculator.xhtml")
    30 public class App {
    31     private static double memory;
    32     private static String operation;
    33     
    34     @OnClick(id="clear")
    35     static void clear() {
    36         memory = 0;
    37         operation = null;
    38         Calculator.DISPLAY.setValue("0");
    39     }
    40     
    41     @OnClick(id= { "plus", "minus", "mul", "div" })
    42     static void applyOp(String op) {
    43         memory = getValue();
    44         operation = op;
    45         Calculator.DISPLAY.setValue("0");
    46     }
    47     
    48     @OnClick(id="result")
    49     static void computeTheValue() {
    50         switch (operation) {
    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);
    56         }
    57     }
    58     
    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);
    65         } else {
    66             Calculator.DISPLAY.setValue(v + digit);
    67         }
    68     }
    69     
    70     private static void setValue(double v) {
    71         StringBuilder sb = new StringBuilder();
    72         sb.append(v);
    73         if (sb.toString().endsWith(".0")) {
    74             final int l = sb.length();
    75             sb.delete(l - 2, l);
    76         }
    77         Calculator.DISPLAY.setValue(sb.toString());
    78     }
    79 
    80     private static double getValue() {
    81         try {
    82             return Double.parseDouble(Calculator.DISPLAY.getValue());
    83         } catch (NumberFormatException ex) {
    84             Calculator.DISPLAY.setValue("err");
    85             return 0.0;
    86         }
    87     }
    88 }