javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Nov 2012 12:06:45 +0100
changeset 141 63be794c1eeb
child 142 74c37d9cfdc9
permissions -rw-r--r--
Adding calculator demo to the repository
     1 package org.apidesign.bck2brwsr.mavenhtml;
     2 
     3 import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
     4 import org.apidesign.bck2brwsr.htmlpage.api.Page;
     5 
     6 @Page(xhtml="Calculator.xhtml")
     7 public class App {
     8     private static final int OP_PLUS = 1;
     9     private static final int OP_MINUS = 2;
    10     private static final int OP_MUL = 3;
    11     private static final int OP_DIV = 4;
    12     
    13     static double memory = 0;
    14     static int operation = 0;
    15     
    16     
    17     
    18     @OnClick(id="clear")
    19     static void clear() {
    20         setValue(0.0);
    21     }
    22     
    23     private static void setValue(double v) {
    24         StringBuilder sb = new StringBuilder();
    25         sb.append(v);
    26         Calculator.DISPLAY.setValue(sb.toString());
    27     }
    28     
    29     private static double getValue() {
    30         return Double.parseDouble(Calculator.DISPLAY.getValue());
    31     }
    32     
    33     @OnClick(id="plus")
    34     static void plus() {
    35         memory = getValue();
    36         operation = OP_PLUS;
    37         setValue(0.0);
    38     }
    39     
    40     @OnClick(id="minus")
    41     static void minus() {
    42         memory = getValue();
    43         operation = OP_MINUS;
    44         setValue(0.0);
    45     }
    46     
    47     @OnClick(id="mul")
    48     static void mul() {
    49         memory = getValue();
    50         operation = OP_MUL;
    51         setValue(0.0);
    52     }
    53     
    54     @OnClick(id="result")
    55     static void computeTheValue() {
    56         switch (operation) {
    57             case 0: break;
    58             case OP_PLUS: setValue(memory + getValue()); break;
    59             case OP_MINUS: setValue(memory - getValue()); break;
    60             case OP_MUL: setValue(memory * getValue()); break;
    61         }
    62     }
    63     
    64     @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    65     static void addDigit(String digit) {
    66         digit = digit.substring(1);
    67         String v = Calculator.DISPLAY.getValue();
    68         if ("0".equals(v) || v == null) {
    69             Calculator.DISPLAY.setValue(digit);
    70         } else {
    71             Calculator.DISPLAY.setValue(v + digit);
    72         }
    73     }
    74 }