javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 07:00:56 +0100
branchmodel
changeset 498 607f062485cc
parent 495 f96c44dac48a
child 505 4198be34b516
permissions -rw-r--r--
Notify change in computed properties
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@141
    18
package org.apidesign.bck2brwsr.mavenhtml;
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@141
    38
public class App {
jaroslav@435
    39
    @On(event = CLICK, id="clear")
jaroslav@141
    40
    static void clear() {
jaroslav@498
    41
        Calculator.setMemory(0);
jaroslav@498
    42
        Calculator.setOperation(null);
jaroslav@492
    43
        Calculator.setDisplay(0);
jtulach@197
    44
    }
jtulach@197
    45
    
jaroslav@435
    46
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jtulach@197
    47
    static void applyOp(String op) {
jaroslav@498
    48
        Calculator.setMemory(Calculator.getDisplay());
jaroslav@498
    49
        Calculator.setOperation(op);
jaroslav@492
    50
        Calculator.setDisplay(0);
jtulach@197
    51
    }
jaroslav@498
    52
jaroslav@498
    53
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@498
    54
    static void attemptingIn(String op) {
jaroslav@498
    55
        Calculator.setHover(true);
jaroslav@498
    56
    }
jaroslav@498
    57
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@498
    58
    static void attemptingOut(String op) {
jaroslav@498
    59
        Calculator.setHover(false);
jaroslav@498
    60
    }
jtulach@197
    61
    
jaroslav@435
    62
    @On(event = CLICK, id="result")
jtulach@197
    63
    static void computeTheValue() {
jaroslav@498
    64
        Calculator.setDisplay(compute(
jaroslav@498
    65
            Calculator.getOperation(), 
jaroslav@498
    66
            Calculator.getMemory(), 
jaroslav@498
    67
            Calculator.getDisplay()
jaroslav@498
    68
        ));
jaroslav@498
    69
        Calculator.setMemory(0);
jaroslav@498
    70
    }
jaroslav@498
    71
    
jaroslav@498
    72
    private static double compute(String op, double memory, double display) {
jaroslav@498
    73
        switch (op) {
jaroslav@498
    74
            case "plus": return memory + display;
jaroslav@498
    75
            case "minus": return memory - display;
jaroslav@498
    76
            case "mul": return memory * display;
jaroslav@498
    77
            case "div": return memory / display;
jaroslav@498
    78
            default: throw new IllegalStateException(op);
jtulach@197
    79
        }
jtulach@197
    80
    }
jtulach@197
    81
    
jaroslav@435
    82
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jtulach@197
    83
    static void addDigit(String digit) {
jtulach@197
    84
        digit = digit.substring(1);
jaroslav@492
    85
        
jaroslav@492
    86
        double v = Calculator.getDisplay();
jaroslav@492
    87
        if (v == 0.0) {
jaroslav@492
    88
            Calculator.setDisplay(Integer.parseInt(digit));
jtulach@197
    89
        } else {
jaroslav@495
    90
            String txt = Double.toString(v);
jaroslav@495
    91
            if (txt.endsWith(".0")) {
jaroslav@495
    92
                txt = txt.substring(0, txt.length() - 2);
jaroslav@495
    93
            }
jaroslav@495
    94
            txt = txt + digit;
jaroslav@492
    95
            Calculator.setDisplay(Double.parseDouble(txt));
jtulach@197
    96
        }
jaroslav@141
    97
    }
jaroslav@498
    98
jaroslav@498
    99
    @ComputedProperty
jaroslav@498
   100
    public static String displayPreview(
jaroslav@498
   101
        double display, boolean hover, double memory, String operation
jaroslav@498
   102
    ) {
jaroslav@498
   103
        if (!hover) {
jaroslav@498
   104
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@498
   105
        }
jaroslav@498
   106
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@498
   107
    }
jaroslav@492
   108
    
jaroslav@492
   109
    static {
jaroslav@492
   110
        Calculator.applyBindings();
jaroslav@141
   111
    }
jaroslav@141
   112
}