javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 07:00:56 +0100
branchmodel
changeset 498 607f062485cc
parent 495 f96c44dac48a
child 505 4198be34b516
permissions -rw-r--r--
Notify change in computed properties
     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     @On(event = CLICK, id="clear")
    40     static void clear() {
    41         Calculator.setMemory(0);
    42         Calculator.setOperation(null);
    43         Calculator.setDisplay(0);
    44     }
    45     
    46     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    47     static void applyOp(String op) {
    48         Calculator.setMemory(Calculator.getDisplay());
    49         Calculator.setOperation(op);
    50         Calculator.setDisplay(0);
    51     }
    52 
    53     @On(event = MOUSE_OVER, id= { "result" })
    54     static void attemptingIn(String op) {
    55         Calculator.setHover(true);
    56     }
    57     @On(event = MOUSE_OUT, id= { "result" })
    58     static void attemptingOut(String op) {
    59         Calculator.setHover(false);
    60     }
    61     
    62     @On(event = CLICK, id="result")
    63     static void computeTheValue() {
    64         Calculator.setDisplay(compute(
    65             Calculator.getOperation(), 
    66             Calculator.getMemory(), 
    67             Calculator.getDisplay()
    68         ));
    69         Calculator.setMemory(0);
    70     }
    71     
    72     private static double compute(String op, double memory, double display) {
    73         switch (op) {
    74             case "plus": return memory + display;
    75             case "minus": return memory - display;
    76             case "mul": return memory * display;
    77             case "div": return memory / display;
    78             default: throw new IllegalStateException(op);
    79         }
    80     }
    81     
    82     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    83     static void addDigit(String digit) {
    84         digit = digit.substring(1);
    85         
    86         double v = Calculator.getDisplay();
    87         if (v == 0.0) {
    88             Calculator.setDisplay(Integer.parseInt(digit));
    89         } else {
    90             String txt = Double.toString(v);
    91             if (txt.endsWith(".0")) {
    92                 txt = txt.substring(0, txt.length() - 2);
    93             }
    94             txt = txt + digit;
    95             Calculator.setDisplay(Double.parseDouble(txt));
    96         }
    97     }
    98 
    99     @ComputedProperty
   100     public static String displayPreview(
   101         double display, boolean hover, double memory, String operation
   102     ) {
   103         if (!hover) {
   104             return "Type numbers and perform simple operations! Press '=' to get result.";
   105         }
   106         return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   107     }
   108     
   109     static {
   110         Calculator.applyBindings();
   111     }
   112 }