javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 17:03:32 +0100
branchmodel
changeset 878 ecbd252fd3a7
parent 877 3392f250c784
parent 830 c3e74d707ba5
child 879 af170d42b5b3
permissions -rw-r--r--
Need to merge in default branch: It contains automatic conversion of boxed types to primitive values which is needed by Knockout to display elements of arrays properly.
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@877
    36
    @Property(name = "hover", type = boolean.class),
jaroslav@877
    37
    @Property(name = "history", type = double.class, array = true)
jaroslav@492
    38
})
jaroslav@511
    39
public class Calc {
jaroslav@510
    40
    static {
jaroslav@510
    41
        new Calculator().applyBindings();
jaroslav@510
    42
    }
jaroslav@505
    43
    
jaroslav@435
    44
    @On(event = CLICK, id="clear")
jaroslav@510
    45
    static void clear(Calculator c) {
jaroslav@510
    46
        c.setMemory(0);
jaroslav@510
    47
        c.setOperation(null);
jaroslav@510
    48
        c.setDisplay(0);
jtulach@197
    49
    }
jtulach@197
    50
    
jaroslav@435
    51
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jaroslav@830
    52
    static void applyOp(Calculator c, String id) {
jaroslav@510
    53
        c.setMemory(c.getDisplay());
jaroslav@830
    54
        c.setOperation(id);
jaroslav@510
    55
        c.setDisplay(0);
jtulach@197
    56
    }
jaroslav@498
    57
jaroslav@498
    58
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@830
    59
    static void attemptingIn(Calculator c) {
jaroslav@510
    60
        c.setHover(true);
jaroslav@498
    61
    }
jaroslav@498
    62
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@830
    63
    static void attemptingOut(Calculator c) {
jaroslav@510
    64
        c.setHover(false);
jaroslav@498
    65
    }
jtulach@197
    66
    
jaroslav@435
    67
    @On(event = CLICK, id="result")
jaroslav@510
    68
    static void computeTheValue(Calculator c) {
jaroslav@877
    69
        final double newValue = compute(
jaroslav@510
    70
            c.getOperation(), 
jaroslav@510
    71
            c.getMemory(), 
jaroslav@510
    72
            c.getDisplay()
jaroslav@877
    73
        );
jaroslav@877
    74
        c.setDisplay(newValue);
jaroslav@877
    75
        c.getHistory().add(newValue);
jaroslav@510
    76
        c.setMemory(0);
jaroslav@498
    77
    }
jaroslav@498
    78
    
jaroslav@498
    79
    private static double compute(String op, double memory, double display) {
jaroslav@498
    80
        switch (op) {
jaroslav@498
    81
            case "plus": return memory + display;
jaroslav@498
    82
            case "minus": return memory - display;
jaroslav@498
    83
            case "mul": return memory * display;
jaroslav@498
    84
            case "div": return memory / display;
jaroslav@498
    85
            default: throw new IllegalStateException(op);
jtulach@197
    86
        }
jtulach@197
    87
    }
jtulach@197
    88
    
jaroslav@435
    89
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@830
    90
    static void addDigit(String id, Calculator c) {
jaroslav@830
    91
        id = id.substring(1);
jaroslav@492
    92
        
jaroslav@510
    93
        double v = c.getDisplay();
jaroslav@492
    94
        if (v == 0.0) {
jaroslav@830
    95
            c.setDisplay(Integer.parseInt(id));
jtulach@197
    96
        } else {
jaroslav@495
    97
            String txt = Double.toString(v);
jaroslav@495
    98
            if (txt.endsWith(".0")) {
jaroslav@495
    99
                txt = txt.substring(0, txt.length() - 2);
jaroslav@495
   100
            }
jaroslav@830
   101
            txt = txt + id;
jaroslav@510
   102
            c.setDisplay(Double.parseDouble(txt));
jtulach@197
   103
        }
jaroslav@141
   104
    }
jaroslav@498
   105
jaroslav@498
   106
    @ComputedProperty
jaroslav@498
   107
    public static String displayPreview(
jaroslav@498
   108
        double display, boolean hover, double memory, String operation
jaroslav@498
   109
    ) {
jaroslav@498
   110
        if (!hover) {
jaroslav@498
   111
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@498
   112
        }
jaroslav@498
   113
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@498
   114
    }
jaroslav@141
   115
}