javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 28 May 2014 10:47:18 +0200
branchclosure
changeset 1609 752f48257d4a
parent 890 c8810910c311
child 1787 ea12a3bb4b33
permissions -rw-r--r--
All classes loaded through the 'vm' object will have 'invoke' method that can be used to invoke static methods like main(String[]), so our examples no longer need to rely on initialization in static initializers.
     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 java.util.List;
    21 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    22 import org.apidesign.bck2brwsr.htmlpage.api.On;
    23 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    24 import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
    25 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    26 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    27 
    28 /** HTML5 & Java demo showing the power of 
    29  * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    30  * as well as other goodies.
    31  * 
    32  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    33  */
    34 @Page(xhtml="Calculator.xhtml", properties = {
    35     @Property(name = "memory", type = double.class),
    36     @Property(name = "display", type = double.class),
    37     @Property(name = "operation", type = String.class),
    38     @Property(name = "hover", type = boolean.class),
    39     @Property(name = "history", type = double.class, array = true)
    40 })
    41 public class Calc {
    42     public static void main(String... args) throws Exception {
    43         new Calculator().applyBindings().setOperation("plus");
    44     }
    45     
    46     @On(event = CLICK, id="clear")
    47     static void clear(Calculator c) {
    48         c.setMemory(0);
    49         c.setOperation(null);
    50         c.setDisplay(0);
    51     }
    52     
    53     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    54     static void applyOp(Calculator c, String id) {
    55         c.setMemory(c.getDisplay());
    56         c.setOperation(id);
    57         c.setDisplay(0);
    58     }
    59 
    60     @On(event = MOUSE_OVER, id= { "result" })
    61     static void attemptingIn(Calculator c) {
    62         c.setHover(true);
    63     }
    64     @On(event = MOUSE_OUT, id= { "result" })
    65     static void attemptingOut(Calculator c) {
    66         c.setHover(false);
    67     }
    68     
    69     @On(event = CLICK, id="result")
    70     static void computeTheValue(Calculator c) {
    71         final double newValue = compute(
    72             c.getOperation(), 
    73             c.getMemory(), 
    74             c.getDisplay()
    75         );
    76         c.setDisplay(newValue);
    77         if (!c.getHistory().contains(newValue)) {
    78             c.getHistory().add(newValue);
    79         }
    80         c.setMemory(0);
    81     }
    82     
    83     @OnFunction
    84     static void recoverMemory(Calculator c, double data) {
    85         c.setDisplay(data);
    86     }
    87 
    88     @OnFunction
    89     static void removeMemory(Calculator c, double data) {
    90         c.getHistory().remove(data);
    91     }
    92     
    93     private static double compute(String op, double memory, double display) {
    94         switch (op) {
    95             case "plus": return memory + display;
    96             case "minus": return memory - display;
    97             case "mul": return memory * display;
    98             case "div": return memory / display;
    99             default: throw new IllegalStateException(op);
   100         }
   101     }
   102     
   103     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
   104     static void addDigit(String id, Calculator c) {
   105         id = id.substring(1);
   106         
   107         double v = c.getDisplay();
   108         if (v == 0.0) {
   109             c.setDisplay(Integer.parseInt(id));
   110         } else {
   111             String txt = Double.toString(v);
   112             if (txt.endsWith(".0")) {
   113                 txt = txt.substring(0, txt.length() - 2);
   114             }
   115             txt = txt + id;
   116             c.setDisplay(Double.parseDouble(txt));
   117         }
   118     }
   119 
   120     @ComputedProperty
   121     public static String displayPreview(
   122         double display, boolean hover, double memory, String operation
   123     ) {
   124         if (!hover) {
   125             return "Type numbers and perform simple operations! Press '=' to get result.";
   126         }
   127         return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   128     }
   129     
   130     @ComputedProperty
   131     static boolean emptyHistory(List<?> history) {
   132         return history.isEmpty();
   133     }
   134 }