javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Mar 2013 09:29:03 +0100
changeset 830 c3e74d707ba5
parent 511 b26510e3e105
child 878 ecbd252fd3a7
permissions -rw-r--r--
String referencing id needs to be named id now. Update to new spec version.
jaroslav@142
     1
/**
jaroslav@142
     2
 * Back 2 Browser Bytecode Translator
jaroslav@142
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@142
     4
 *
jaroslav@142
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@142
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@142
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@142
     8
 *
jaroslav@142
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@142
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@142
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@142
    12
 * GNU General Public License for more details.
jaroslav@142
    13
 *
jaroslav@142
    14
 * You should have received a copy of the GNU General Public License
jaroslav@142
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@142
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@142
    17
 */
jaroslav@511
    18
package org.apidesign.bck2brwsr.demo.calc.staticcompilation;
jaroslav@141
    19
jaroslav@498
    20
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
jaroslav@435
    21
import org.apidesign.bck2brwsr.htmlpage.api.On;
jaroslav@435
    22
import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
jaroslav@141
    23
import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@492
    24
import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@141
    25
jaroslav@198
    26
/** HTML5 & Java demo showing the power of 
jaroslav@198
    27
 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
jaroslav@198
    28
 * as well as other goodies.
jaroslav@198
    29
 * 
jaroslav@198
    30
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@198
    31
 */
jaroslav@492
    32
@Page(xhtml="Calculator.xhtml", properties = {
jaroslav@498
    33
    @Property(name = "memory", type = double.class),
jaroslav@498
    34
    @Property(name = "display", type = double.class),
jaroslav@498
    35
    @Property(name = "operation", type = String.class),
jaroslav@498
    36
    @Property(name = "hover", type = boolean.class)
jaroslav@492
    37
})
jaroslav@511
    38
public class Calc {
jaroslav@510
    39
    static {
jaroslav@510
    40
        new Calculator().applyBindings();
jaroslav@510
    41
    }
jaroslav@505
    42
    
jaroslav@435
    43
    @On(event = CLICK, id="clear")
jaroslav@510
    44
    static void clear(Calculator c) {
jaroslav@510
    45
        c.setMemory(0);
jaroslav@510
    46
        c.setOperation(null);
jaroslav@510
    47
        c.setDisplay(0);
jtulach@197
    48
    }
jtulach@197
    49
    
jaroslav@435
    50
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jaroslav@830
    51
    static void applyOp(Calculator c, String id) {
jaroslav@510
    52
        c.setMemory(c.getDisplay());
jaroslav@830
    53
        c.setOperation(id);
jaroslav@510
    54
        c.setDisplay(0);
jtulach@197
    55
    }
jaroslav@498
    56
jaroslav@498
    57
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@830
    58
    static void attemptingIn(Calculator c) {
jaroslav@510
    59
        c.setHover(true);
jaroslav@498
    60
    }
jaroslav@498
    61
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@830
    62
    static void attemptingOut(Calculator c) {
jaroslav@510
    63
        c.setHover(false);
jaroslav@498
    64
    }
jtulach@197
    65
    
jaroslav@435
    66
    @On(event = CLICK, id="result")
jaroslav@510
    67
    static void computeTheValue(Calculator c) {
jaroslav@510
    68
        c.setDisplay(compute(
jaroslav@510
    69
            c.getOperation(), 
jaroslav@510
    70
            c.getMemory(), 
jaroslav@510
    71
            c.getDisplay()
jaroslav@498
    72
        ));
jaroslav@510
    73
        c.setMemory(0);
jaroslav@498
    74
    }
jaroslav@498
    75
    
jaroslav@498
    76
    private static double compute(String op, double memory, double display) {
jaroslav@498
    77
        switch (op) {
jaroslav@498
    78
            case "plus": return memory + display;
jaroslav@498
    79
            case "minus": return memory - display;
jaroslav@498
    80
            case "mul": return memory * display;
jaroslav@498
    81
            case "div": return memory / display;
jaroslav@498
    82
            default: throw new IllegalStateException(op);
jtulach@197
    83
        }
jtulach@197
    84
    }
jtulach@197
    85
    
jaroslav@435
    86
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@830
    87
    static void addDigit(String id, Calculator c) {
jaroslav@830
    88
        id = id.substring(1);
jaroslav@492
    89
        
jaroslav@510
    90
        double v = c.getDisplay();
jaroslav@492
    91
        if (v == 0.0) {
jaroslav@830
    92
            c.setDisplay(Integer.parseInt(id));
jtulach@197
    93
        } else {
jaroslav@495
    94
            String txt = Double.toString(v);
jaroslav@495
    95
            if (txt.endsWith(".0")) {
jaroslav@495
    96
                txt = txt.substring(0, txt.length() - 2);
jaroslav@495
    97
            }
jaroslav@830
    98
            txt = txt + id;
jaroslav@510
    99
            c.setDisplay(Double.parseDouble(txt));
jtulach@197
   100
        }
jaroslav@141
   101
    }
jaroslav@498
   102
jaroslav@498
   103
    @ComputedProperty
jaroslav@498
   104
    public static String displayPreview(
jaroslav@498
   105
        double display, boolean hover, double memory, String operation
jaroslav@498
   106
    ) {
jaroslav@498
   107
        if (!hover) {
jaroslav@498
   108
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@498
   109
        }
jaroslav@498
   110
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@498
   111
    }
jaroslav@141
   112
}