javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 17:03:32 +0100
branchmodel
changeset 878 ecbd252fd3a7
parent 763 ecd7294f1e17
parent 831 513ecb0b76b3
child 885 4508b8b6b45b
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@513
    18
package org.apidesign.bck2brwsr.demo.calc;
jaroslav@141
    19
jaroslav@505
    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@505
    33
    @Property(name = "memory", type = double.class),
jaroslav@505
    34
    @Property(name = "display", type = double.class),
jaroslav@505
    35
    @Property(name = "operation", type = String.class),
jaroslav@763
    36
    @Property(name = "hover", type = boolean.class),
jaroslav@763
    37
    @Property(name = "results", type = String.class, array = true)
jaroslav@492
    38
})
jaroslav@513
    39
public class Calc {
jaroslav@513
    40
    static {
jaroslav@763
    41
        Calculator m = new Calculator();
jaroslav@763
    42
        m.applyBindings();
jaroslav@513
    43
    }
jaroslav@141
    44
    
jaroslav@435
    45
    @On(event = CLICK, id="clear")
jaroslav@513
    46
    static void clear(Calculator c) {
jaroslav@513
    47
        c.setMemory(0);
jaroslav@513
    48
        c.setOperation(null);
jaroslav@513
    49
        c.setDisplay(0);
jtulach@197
    50
    }
jtulach@197
    51
    
jaroslav@435
    52
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jaroslav@831
    53
    static void applyOp(Calculator c, String id) {
jaroslav@513
    54
        c.setMemory(c.getDisplay());
jaroslav@831
    55
        c.setOperation(id);
jaroslav@513
    56
        c.setDisplay(0);
jaroslav@505
    57
    }
jaroslav@505
    58
jaroslav@505
    59
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@831
    60
    static void attemptingIn(Calculator c) {
jaroslav@513
    61
        c.setHover(true);
jaroslav@505
    62
    }
jaroslav@505
    63
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@831
    64
    static void attemptingOut(Calculator c) {
jaroslav@513
    65
        c.setHover(false);
jtulach@197
    66
    }
jtulach@197
    67
    
jaroslav@435
    68
    @On(event = CLICK, id="result")
jaroslav@513
    69
    static void computeTheValue(Calculator c) {
jaroslav@763
    70
        final double val = compute(
jaroslav@513
    71
            c.getOperation(), 
jaroslav@513
    72
            c.getMemory(), 
jaroslav@513
    73
            c.getDisplay()
jaroslav@763
    74
        );
jaroslav@763
    75
        c.getResults().add("another result " + val);
jaroslav@763
    76
        c.setDisplay(val);
jaroslav@513
    77
        c.setMemory(0);
jaroslav@505
    78
    }
jaroslav@505
    79
    
jaroslav@505
    80
    private static double compute(String op, double memory, double display) {
jaroslav@505
    81
        switch (op) {
jaroslav@505
    82
            case "plus": return memory + display;
jaroslav@505
    83
            case "minus": return memory - display;
jaroslav@505
    84
            case "mul": return memory * display;
jaroslav@505
    85
            case "div": return memory / display;
jaroslav@505
    86
            default: throw new IllegalStateException(op);
jtulach@197
    87
        }
jtulach@197
    88
    }
jtulach@197
    89
    
jaroslav@435
    90
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@831
    91
    static void addDigit(String id, Calculator c) {
jaroslav@831
    92
        id = id.substring(1);
jaroslav@492
    93
        
jaroslav@513
    94
        double v = c.getDisplay();
jaroslav@492
    95
        if (v == 0.0) {
jaroslav@831
    96
            c.setDisplay(Integer.parseInt(id));
jtulach@197
    97
        } else {
jaroslav@505
    98
            String txt = Double.toString(v);
jaroslav@505
    99
            if (txt.endsWith(".0")) {
jaroslav@505
   100
                txt = txt.substring(0, txt.length() - 2);
jaroslav@505
   101
            }
jaroslav@831
   102
            txt = txt + id;
jaroslav@513
   103
            c.setDisplay(Double.parseDouble(txt));
jaroslav@141
   104
        }
jaroslav@141
   105
    }
jaroslav@505
   106
jaroslav@505
   107
    @ComputedProperty
jaroslav@505
   108
    public static String displayPreview(
jaroslav@505
   109
        double display, boolean hover, double memory, String operation
jaroslav@505
   110
    ) {
jaroslav@505
   111
        if (!hover) {
jaroslav@505
   112
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@505
   113
        }
jaroslav@505
   114
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@505
   115
    }
jaroslav@141
   116
}