javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 22 Nov 2012 00:18:34 +0100
changeset 197 e7bb314eec32
parent 142 74c37d9cfdc9
child 198 5c1604c5ca9a
permissions -rw-r--r--
Less verbose. Concentrating all operations into single method. Using string switch to determine what operation to run.
     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 @Page(xhtml="Calculator.xhtml")
    24 public class App {
    25     private static double memory;
    26     private static String operation;
    27     
    28     @OnClick(id="clear")
    29     static void clear() {
    30         memory = 0;
    31         operation = null;
    32         Calculator.DISPLAY.setValue("0");
    33     }
    34     
    35     @OnClick(id= { "plus", "minus", "mul", "div" })
    36     static void applyOp(String op) {
    37         memory = getValue();
    38         operation = op;
    39         Calculator.DISPLAY.setValue("0");
    40     }
    41     
    42     @OnClick(id="result")
    43     static void computeTheValue() {
    44         switch (operation) {
    45             case "plus": setValue(memory + getValue()); break;
    46             case "minus": setValue(memory - getValue()); break;
    47             case "mul": setValue(memory * getValue()); break;
    48             case "div": setValue(memory / getValue()); break;
    49             default: throw new IllegalStateException(operation);
    50         }
    51     }
    52     
    53     @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    54     static void addDigit(String digit) {
    55         digit = digit.substring(1);
    56         String v = Calculator.DISPLAY.getValue();
    57         if (getValue() == 0.0) {
    58             Calculator.DISPLAY.setValue(digit);
    59         } else {
    60             Calculator.DISPLAY.setValue(v + digit);
    61         }
    62     }
    63     
    64     private static void setValue(double v) {
    65         StringBuilder sb = new StringBuilder();
    66         sb.append(v);
    67         Calculator.DISPLAY.setValue(sb.toString());
    68     }
    69 
    70     private static double getValue() {
    71         try {
    72             return Double.parseDouble(Calculator.DISPLAY.getValue());
    73         } catch (NumberFormatException ex) {
    74             Calculator.DISPLAY.setValue("err");
    75             return 0.0;
    76         }
    77     }
    78 }