javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Nov 2012 12:09:40 +0100
changeset 142 74c37d9cfdc9
parent 141 63be794c1eeb
child 197 e7bb314eec32
permissions -rw-r--r--
Fixing licenses
     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 final int OP_PLUS = 1;
    26     private static final int OP_MINUS = 2;
    27     private static final int OP_MUL = 3;
    28     private static final int OP_DIV = 4;
    29     
    30     static double memory = 0;
    31     static int operation = 0;
    32     
    33     
    34     
    35     @OnClick(id="clear")
    36     static void clear() {
    37         setValue(0.0);
    38     }
    39     
    40     private static void setValue(double v) {
    41         StringBuilder sb = new StringBuilder();
    42         sb.append(v);
    43         Calculator.DISPLAY.setValue(sb.toString());
    44     }
    45     
    46     private static double getValue() {
    47         return Double.parseDouble(Calculator.DISPLAY.getValue());
    48     }
    49     
    50     @OnClick(id="plus")
    51     static void plus() {
    52         memory = getValue();
    53         operation = OP_PLUS;
    54         setValue(0.0);
    55     }
    56     
    57     @OnClick(id="minus")
    58     static void minus() {
    59         memory = getValue();
    60         operation = OP_MINUS;
    61         setValue(0.0);
    62     }
    63     
    64     @OnClick(id="mul")
    65     static void mul() {
    66         memory = getValue();
    67         operation = OP_MUL;
    68         setValue(0.0);
    69     }
    70     
    71     @OnClick(id="result")
    72     static void computeTheValue() {
    73         switch (operation) {
    74             case 0: break;
    75             case OP_PLUS: setValue(memory + getValue()); break;
    76             case OP_MINUS: setValue(memory - getValue()); break;
    77             case OP_MUL: setValue(memory * getValue()); break;
    78         }
    79     }
    80     
    81     @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    82     static void addDigit(String digit) {
    83         digit = digit.substring(1);
    84         String v = Calculator.DISPLAY.getValue();
    85         if ("0".equals(v) || v == null) {
    86             Calculator.DISPLAY.setValue(digit);
    87         } else {
    88             Calculator.DISPLAY.setValue(v + digit);
    89         }
    90     }
    91 }