javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
branchmodel
changeset 511 b26510e3e105
parent 510 aaf86ae88f46
child 830 c3e74d707ba5
child 877 3392f250c784
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java	Mon Jan 21 16:04:25 2013 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.demo.calc.staticcompilation;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    1.24 +import org.apidesign.bck2brwsr.htmlpage.api.On;
    1.25 +import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    1.26 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.27 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
    1.28 +
    1.29 +/** HTML5 & Java demo showing the power of 
    1.30 + * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    1.31 + * as well as other goodies.
    1.32 + * 
    1.33 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.34 + */
    1.35 +@Page(xhtml="Calculator.xhtml", properties = {
    1.36 +    @Property(name = "memory", type = double.class),
    1.37 +    @Property(name = "display", type = double.class),
    1.38 +    @Property(name = "operation", type = String.class),
    1.39 +    @Property(name = "hover", type = boolean.class)
    1.40 +})
    1.41 +public class Calc {
    1.42 +    static {
    1.43 +        new Calculator().applyBindings();
    1.44 +    }
    1.45 +    
    1.46 +    @On(event = CLICK, id="clear")
    1.47 +    static void clear(Calculator c) {
    1.48 +        c.setMemory(0);
    1.49 +        c.setOperation(null);
    1.50 +        c.setDisplay(0);
    1.51 +    }
    1.52 +    
    1.53 +    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    1.54 +    static void applyOp(Calculator c, String op) {
    1.55 +        c.setMemory(c.getDisplay());
    1.56 +        c.setOperation(op);
    1.57 +        c.setDisplay(0);
    1.58 +    }
    1.59 +
    1.60 +    @On(event = MOUSE_OVER, id= { "result" })
    1.61 +    static void attemptingIn(Calculator c, String op) {
    1.62 +        c.setHover(true);
    1.63 +    }
    1.64 +    @On(event = MOUSE_OUT, id= { "result" })
    1.65 +    static void attemptingOut(Calculator c, String op) {
    1.66 +        c.setHover(false);
    1.67 +    }
    1.68 +    
    1.69 +    @On(event = CLICK, id="result")
    1.70 +    static void computeTheValue(Calculator c) {
    1.71 +        c.setDisplay(compute(
    1.72 +            c.getOperation(), 
    1.73 +            c.getMemory(), 
    1.74 +            c.getDisplay()
    1.75 +        ));
    1.76 +        c.setMemory(0);
    1.77 +    }
    1.78 +    
    1.79 +    private static double compute(String op, double memory, double display) {
    1.80 +        switch (op) {
    1.81 +            case "plus": return memory + display;
    1.82 +            case "minus": return memory - display;
    1.83 +            case "mul": return memory * display;
    1.84 +            case "div": return memory / display;
    1.85 +            default: throw new IllegalStateException(op);
    1.86 +        }
    1.87 +    }
    1.88 +    
    1.89 +    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    1.90 +    static void addDigit(String digit, Calculator c) {
    1.91 +        digit = digit.substring(1);
    1.92 +        
    1.93 +        double v = c.getDisplay();
    1.94 +        if (v == 0.0) {
    1.95 +            c.setDisplay(Integer.parseInt(digit));
    1.96 +        } else {
    1.97 +            String txt = Double.toString(v);
    1.98 +            if (txt.endsWith(".0")) {
    1.99 +                txt = txt.substring(0, txt.length() - 2);
   1.100 +            }
   1.101 +            txt = txt + digit;
   1.102 +            c.setDisplay(Double.parseDouble(txt));
   1.103 +        }
   1.104 +    }
   1.105 +
   1.106 +    @ComputedProperty
   1.107 +    public static String displayPreview(
   1.108 +        double display, boolean hover, double memory, String operation
   1.109 +    ) {
   1.110 +        if (!hover) {
   1.111 +            return "Type numbers and perform simple operations! Press '=' to get result.";
   1.112 +        }
   1.113 +        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   1.114 +    }
   1.115 +}