javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 17:03:32 +0100
branchmodel
changeset 878 ecbd252fd3a7
parent 877 3392f250c784
parent 830 c3e74d707ba5
child 879 af170d42b5b3
permissions -rw-r--r--
Need to merge in default branch: It contains automatic conversion of boxed types to primitive values which is needed by Knockout to display elements of arrays properly.
     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.demo.calc.staticcompilation;
    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     @Property(name = "history", type = double.class, array = true)
    38 })
    39 public class Calc {
    40     static {
    41         new Calculator().applyBindings();
    42     }
    43     
    44     @On(event = CLICK, id="clear")
    45     static void clear(Calculator c) {
    46         c.setMemory(0);
    47         c.setOperation(null);
    48         c.setDisplay(0);
    49     }
    50     
    51     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    52     static void applyOp(Calculator c, String id) {
    53         c.setMemory(c.getDisplay());
    54         c.setOperation(id);
    55         c.setDisplay(0);
    56     }
    57 
    58     @On(event = MOUSE_OVER, id= { "result" })
    59     static void attemptingIn(Calculator c) {
    60         c.setHover(true);
    61     }
    62     @On(event = MOUSE_OUT, id= { "result" })
    63     static void attemptingOut(Calculator c) {
    64         c.setHover(false);
    65     }
    66     
    67     @On(event = CLICK, id="result")
    68     static void computeTheValue(Calculator c) {
    69         final double newValue = compute(
    70             c.getOperation(), 
    71             c.getMemory(), 
    72             c.getDisplay()
    73         );
    74         c.setDisplay(newValue);
    75         c.getHistory().add(newValue);
    76         c.setMemory(0);
    77     }
    78     
    79     private static double compute(String op, double memory, double display) {
    80         switch (op) {
    81             case "plus": return memory + display;
    82             case "minus": return memory - display;
    83             case "mul": return memory * display;
    84             case "div": return memory / display;
    85             default: throw new IllegalStateException(op);
    86         }
    87     }
    88     
    89     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    90     static void addDigit(String id, Calculator c) {
    91         id = id.substring(1);
    92         
    93         double v = c.getDisplay();
    94         if (v == 0.0) {
    95             c.setDisplay(Integer.parseInt(id));
    96         } else {
    97             String txt = Double.toString(v);
    98             if (txt.endsWith(".0")) {
    99                 txt = txt.substring(0, txt.length() - 2);
   100             }
   101             txt = txt + id;
   102             c.setDisplay(Double.parseDouble(txt));
   103         }
   104     }
   105 
   106     @ComputedProperty
   107     public static String displayPreview(
   108         double display, boolean hover, double memory, String operation
   109     ) {
   110         if (!hover) {
   111             return "Type numbers and perform simple operations! Press '=' to get result.";
   112         }
   113         return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   114     }
   115 }