javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Jan 2013 18:20:18 +0100
branchmodel
changeset 492 854286e49061
parent 435 fb4ed6cc0d4b
child 495 f96c44dac48a
permissions -rw-r--r--
Basic support for knockout.js. It can read and write model 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.On;
    21 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    22 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    23 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    24 
    25 /** HTML5 & Java demo showing the power of 
    26  * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    27  * as well as other goodies.
    28  * 
    29  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    30  */
    31 @Page(xhtml="Calculator.xhtml", properties = {
    32     @Property(name = "display", type = double.class)
    33 })
    34 public class App {
    35     private static double memory;
    36     private static String operation;
    37     
    38     @On(event = CLICK, id="clear")
    39     static void clear() {
    40         memory = 0;
    41         operation = null;
    42         Calculator.setDisplay(0);
    43     }
    44     
    45     @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    46     static void applyOp(String op) {
    47         memory = Calculator.getDisplay();
    48         operation = op;
    49         Calculator.setDisplay(0);
    50     }
    51     
    52     @On(event = CLICK, id="result")
    53     static void computeTheValue() {
    54         switch (operation) {
    55             case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
    56             case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
    57             case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
    58             case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
    59             default: throw new IllegalStateException(operation);
    60         }
    61     }
    62     
    63     @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    64     static void addDigit(String digit) {
    65         digit = digit.substring(1);
    66         
    67         double v = Calculator.getDisplay();
    68         if (v == 0.0) {
    69             Calculator.setDisplay(Integer.parseInt(digit));
    70         } else {
    71             String txt = Double.toString(v) + digit;
    72             Calculator.setDisplay(Double.parseDouble(txt));
    73         }
    74     }
    75     
    76     
    77     static {
    78         Calculator.setDisplay(10.0);
    79         Calculator.applyBindings();
    80     }
    81 }