javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 13:43:40 +0100
branchmodel
changeset 505 4198be34b516
parent 492 854286e49061
permissions -rw-r--r--
Moving towards testability: Model class does not have any public fields, rather instance onces
     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.ComputedProperty;
    21 import org.apidesign.bck2brwsr.htmlpage.api.On;
    22 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    23 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    24 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    25 
    26 /** HTML5 & Java demo showing the power of 
    27  * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    28  * as well as other goodies.
    29  * 
    30  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    31  */
    32 @Page(xhtml="Calculator.xhtml", properties = {
    33     @Property(name = "memory", type = double.class),
    34     @Property(name = "display", type = double.class),
    35     @Property(name = "operation", type = String.class),
    36     @Property(name = "hover", type = boolean.class)
    37 })
    38 public class App {
    39     private static final Calculator CALC = new Calculator().applyBindings();
    40     
    41     @On(event = CLICK, id="clear")
    42     static void clear() {
    43         CALC.setMemory(0);
    44         CALC.setOperation(null);
    45         CALC.setDisplay(0);
    46     }
    47     
    48     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    49     static void applyOp(String op) {
    50         CALC.setMemory(CALC.getDisplay());
    51         CALC.setOperation(op);
    52         CALC.setDisplay(0);
    53     }
    54 
    55     @On(event = MOUSE_OVER, id= { "result" })
    56     static void attemptingIn(String op) {
    57         CALC.setHover(true);
    58     }
    59     @On(event = MOUSE_OUT, id= { "result" })
    60     static void attemptingOut(String op) {
    61         CALC.setHover(false);
    62     }
    63     
    64     @On(event = CLICK, id="result")
    65     static void computeTheValue() {
    66         CALC.setDisplay(compute(
    67             CALC.getOperation(), 
    68             CALC.getMemory(), 
    69             CALC.getDisplay()
    70         ));
    71         CALC.setMemory(0);
    72     }
    73     
    74     private static double compute(String op, double memory, double display) {
    75         switch (op) {
    76             case "plus": return memory + display;
    77             case "minus": return memory - display;
    78             case "mul": return memory * display;
    79             case "div": return memory / display;
    80             default: throw new IllegalStateException(op);
    81         }
    82     }
    83     
    84     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    85     static void addDigit(String digit) {
    86         digit = digit.substring(1);
    87         
    88         double v = CALC.getDisplay();
    89         if (v == 0.0) {
    90             CALC.setDisplay(Integer.parseInt(digit));
    91         } else {
    92             String txt = Double.toString(v);
    93             if (txt.endsWith(".0")) {
    94                 txt = txt.substring(0, txt.length() - 2);
    95             }
    96             txt = txt + digit;
    97             CALC.setDisplay(Double.parseDouble(txt));
    98         }
    99     }
   100 
   101     @ComputedProperty
   102     public static String displayPreview(
   103         double display, boolean hover, double memory, String operation
   104     ) {
   105         if (!hover) {
   106             return "Type numbers and perform simple operations! Press '=' to get result.";
   107         }
   108         return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   109     }
   110 }