javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 12 Jan 2013 20:24:30 +0100
changeset 435 fb4ed6cc0d4b
parent 427 12e866a32b40
child 492 854286e49061
permissions -rw-r--r--
Allowing access to all onsmthng events of page elements
     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.On;
    21 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    22 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    23 
    24 /** HTML5 & Java demo showing the power of 
    25  * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    26  * as well as other goodies.
    27  * 
    28  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    29  */
    30 @Page(xhtml="Calculator.xhtml")
    31 public class App {
    32     private static double memory;
    33     private static String operation;
    34     
    35     @On(event = CLICK, id="clear")
    36     static void clear() {
    37         memory = 0;
    38         operation = null;
    39         Calculator.DISPLAY.setValue("0");
    40     }
    41     
    42     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    43     static void applyOp(String op) {
    44         memory = getValue();
    45         operation = op;
    46         Calculator.DISPLAY.setValue("0");
    47     }
    48     
    49     @On(event = CLICK, id="result")
    50     static void computeTheValue() {
    51         switch (operation) {
    52             case "plus": setValue(memory + getValue()); break;
    53             case "minus": setValue(memory - getValue()); break;
    54             case "mul": setValue(memory * getValue()); break;
    55             case "div": setValue(memory / getValue()); break;
    56             default: throw new IllegalStateException(operation);
    57         }
    58     }
    59     
    60     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    61     static void addDigit(String digit) {
    62         digit = digit.substring(1);
    63         String v = Calculator.DISPLAY.getValue();
    64         if (getValue() == 0.0) {
    65             Calculator.DISPLAY.setValue(digit);
    66         } else {
    67             Calculator.DISPLAY.setValue(v + digit);
    68         }
    69     }
    70     
    71     private static void setValue(double v) {
    72         StringBuilder sb = new StringBuilder();
    73         sb.append(v);
    74         if (sb.toString().endsWith(".0")) {
    75             final int l = sb.length();
    76             sb.delete(l - 2, l);
    77         }
    78         Calculator.DISPLAY.setValue(sb.toString());
    79     }
    80 
    81     private static double getValue() {
    82         try {
    83             return Double.parseDouble(Calculator.DISPLAY.getValue());
    84         } catch (NumberFormatException ex) {
    85             Calculator.DISPLAY.setValue("err");
    86             return 0.0;
    87         }
    88     }
    89 }