javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Mar 2013 14:15:09 +0100
branchmodel
changeset 885 4508b8b6b45b
parent 878 ecbd252fd3a7
child 890 c8810910c311
permissions -rw-r--r--
Dynamic calculator sample supports binding of array values. Modified to execute from classes only (again) and not require the built of whole deployed site.
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@885
    20
import java.util.List;
jaroslav@505
    21
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
jaroslav@435
    22
import org.apidesign.bck2brwsr.htmlpage.api.On;
jaroslav@435
    23
import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
jaroslav@885
    24
import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
jaroslav@141
    25
import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@492
    26
import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@141
    27
jaroslav@198
    28
/** HTML5 & Java demo showing the power of 
jaroslav@198
    29
 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
jaroslav@198
    30
 * as well as other goodies.
jaroslav@198
    31
 * 
jaroslav@198
    32
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@198
    33
 */
jaroslav@492
    34
@Page(xhtml="Calculator.xhtml", properties = {
jaroslav@505
    35
    @Property(name = "memory", type = double.class),
jaroslav@505
    36
    @Property(name = "display", type = double.class),
jaroslav@505
    37
    @Property(name = "operation", type = String.class),
jaroslav@763
    38
    @Property(name = "hover", type = boolean.class),
jaroslav@885
    39
    @Property(name = "history", type = double.class, array = true)
jaroslav@492
    40
})
jaroslav@513
    41
public class Calc {
jaroslav@513
    42
    static {
jaroslav@885
    43
        new Calculator().applyBindings().setOperation("plus");
jaroslav@513
    44
    }
jaroslav@141
    45
    
jaroslav@435
    46
    @On(event = CLICK, id="clear")
jaroslav@513
    47
    static void clear(Calculator c) {
jaroslav@513
    48
        c.setMemory(0);
jaroslav@513
    49
        c.setOperation(null);
jaroslav@513
    50
        c.setDisplay(0);
jtulach@197
    51
    }
jtulach@197
    52
    
jaroslav@435
    53
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jaroslav@831
    54
    static void applyOp(Calculator c, String id) {
jaroslav@513
    55
        c.setMemory(c.getDisplay());
jaroslav@831
    56
        c.setOperation(id);
jaroslav@513
    57
        c.setDisplay(0);
jaroslav@505
    58
    }
jaroslav@505
    59
jaroslav@505
    60
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@831
    61
    static void attemptingIn(Calculator c) {
jaroslav@513
    62
        c.setHover(true);
jaroslav@505
    63
    }
jaroslav@505
    64
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@831
    65
    static void attemptingOut(Calculator c) {
jaroslav@513
    66
        c.setHover(false);
jtulach@197
    67
    }
jtulach@197
    68
    
jaroslav@435
    69
    @On(event = CLICK, id="result")
jaroslav@513
    70
    static void computeTheValue(Calculator c) {
jaroslav@885
    71
        final double newValue = compute(
jaroslav@513
    72
            c.getOperation(), 
jaroslav@513
    73
            c.getMemory(), 
jaroslav@513
    74
            c.getDisplay()
jaroslav@763
    75
        );
jaroslav@885
    76
        c.setDisplay(newValue);
jaroslav@885
    77
        c.getHistory().add(newValue);
jaroslav@513
    78
        c.setMemory(0);
jaroslav@505
    79
    }
jaroslav@505
    80
    
jaroslav@885
    81
    @OnFunction
jaroslav@885
    82
    static void recoverMemory(Calculator c, double data) {
jaroslav@885
    83
        c.setDisplay(data);
jaroslav@885
    84
    }
jaroslav@885
    85
jaroslav@885
    86
    @OnFunction
jaroslav@885
    87
    static void removeMemory(Calculator c, double data) {
jaroslav@885
    88
        c.getHistory().remove(data);
jaroslav@885
    89
    }
jaroslav@885
    90
    
jaroslav@505
    91
    private static double compute(String op, double memory, double display) {
jaroslav@505
    92
        switch (op) {
jaroslav@505
    93
            case "plus": return memory + display;
jaroslav@505
    94
            case "minus": return memory - display;
jaroslav@505
    95
            case "mul": return memory * display;
jaroslav@505
    96
            case "div": return memory / display;
jaroslav@505
    97
            default: throw new IllegalStateException(op);
jtulach@197
    98
        }
jtulach@197
    99
    }
jtulach@197
   100
    
jaroslav@435
   101
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@831
   102
    static void addDigit(String id, Calculator c) {
jaroslav@831
   103
        id = id.substring(1);
jaroslav@492
   104
        
jaroslav@513
   105
        double v = c.getDisplay();
jaroslav@492
   106
        if (v == 0.0) {
jaroslav@831
   107
            c.setDisplay(Integer.parseInt(id));
jtulach@197
   108
        } else {
jaroslav@505
   109
            String txt = Double.toString(v);
jaroslav@505
   110
            if (txt.endsWith(".0")) {
jaroslav@505
   111
                txt = txt.substring(0, txt.length() - 2);
jaroslav@505
   112
            }
jaroslav@831
   113
            txt = txt + id;
jaroslav@513
   114
            c.setDisplay(Double.parseDouble(txt));
jaroslav@141
   115
        }
jaroslav@141
   116
    }
jaroslav@505
   117
jaroslav@505
   118
    @ComputedProperty
jaroslav@505
   119
    public static String displayPreview(
jaroslav@505
   120
        double display, boolean hover, double memory, String operation
jaroslav@505
   121
    ) {
jaroslav@505
   122
        if (!hover) {
jaroslav@505
   123
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@505
   124
        }
jaroslav@505
   125
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@505
   126
    }
jaroslav@885
   127
    
jaroslav@885
   128
    @ComputedProperty
jaroslav@885
   129
    static boolean emptyHistory(List<?> history) {
jaroslav@885
   130
        return history.isEmpty();
jaroslav@885
   131
    }
jaroslav@141
   132
}