javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.core.JavaScriptBody;
    22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    23 import org.apidesign.bck2brwsr.htmlpage.api.On;
    24 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    25 import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
    26 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    27 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    28 
    29 /** HTML5 & Java demo showing the power of 
    30  * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    31  * as well as other goodies.
    32  * 
    33  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    34  */
    35 @Page(xhtml="Calculator.xhtml", properties = {
    36     @Property(name = "memory", type = double.class),
    37     @Property(name = "display", type = double.class),
    38     @Property(name = "operation", type = String.class),
    39     @Property(name = "hover", type = boolean.class),
    40     @Property(name = "history", type = double.class, array = true)
    41 })
    42 public class Calc {
    43     public static void main(String... args) throws Exception {
    44         new Calculator().applyBindings().setOperation("plus");
    45         notifyFinish();
    46     }
    47     
    48     @On(event = CLICK, id="clear")
    49     static void clear(Calculator c) {
    50         c.setMemory(0);
    51         c.setOperation(null);
    52         c.setDisplay(0);
    53     }
    54     
    55     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    56     static void applyOp(Calculator c, String id) {
    57         c.setMemory(c.getDisplay());
    58         c.setOperation(id);
    59         c.setDisplay(0);
    60     }
    61 
    62     @On(event = MOUSE_OVER, id= { "result" })
    63     static void attemptingIn(Calculator c) {
    64         c.setHover(true);
    65     }
    66     @On(event = MOUSE_OUT, id= { "result" })
    67     static void attemptingOut(Calculator c) {
    68         c.setHover(false);
    69     }
    70     
    71     @On(event = CLICK, id="result")
    72     static void computeTheValue(Calculator c) {
    73         final double newValue = compute(
    74             c.getOperation(), 
    75             c.getMemory(), 
    76             c.getDisplay()
    77         );
    78         c.setDisplay(newValue);
    79         if (!c.getHistory().contains(newValue)) {
    80             c.getHistory().add(newValue);
    81         }
    82         c.setMemory(0);
    83     }
    84     
    85     @OnFunction
    86     static void recoverMemory(Calculator c, double data) {
    87         c.setDisplay(data);
    88     }
    89 
    90     @OnFunction
    91     static void removeMemory(Calculator c, double data) {
    92         c.getHistory().remove(data);
    93     }
    94     
    95     private static double compute(String op, double memory, double display) {
    96         switch (op) {
    97             case "plus": return memory + display;
    98             case "minus": return memory - display;
    99             case "mul": return memory * display;
   100             case "div": return memory / display;
   101             default: throw new IllegalStateException(op);
   102         }
   103     }
   104     
   105     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
   106     static void addDigit(String id, Calculator c) {
   107         id = id.substring(1);
   108         
   109         double v = c.getDisplay();
   110         if (v == 0.0) {
   111             c.setDisplay(Integer.parseInt(id));
   112         } else {
   113             String txt = Double.toString(v);
   114             if (txt.endsWith(".0")) {
   115                 txt = txt.substring(0, txt.length() - 2);
   116             }
   117             txt = txt + id;
   118             c.setDisplay(Double.parseDouble(txt));
   119         }
   120     }
   121 
   122     @ComputedProperty
   123     public static String displayPreview(
   124         double display, boolean hover, double memory, String operation
   125     ) {
   126         if (!hover) {
   127             return "Type numbers and perform simple operations! Press '=' to get result.";
   128         }
   129         return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   130     }
   131     
   132     @ComputedProperty
   133     static boolean emptyHistory(List<?> history) {
   134         return history.isEmpty();
   135     }
   136 
   137     @JavaScriptBody(args = {  }, body =
   138         "var xhttp = new XMLHttpRequest();\n" +
   139         "xhttp.open('GET', '/?exit=true', true);\n" +
   140         "xhttp.send();\n"
   141     )
   142     private static native void notifyFinish();
   143 }