javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Dec 2015 08:22:59 +0100
changeset 1850 23babc7d5bb8
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Can tell the server to exit
jaroslav@142
     1
/**
jaroslav@142
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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@879
    20
import java.util.List;
jaroslav@1850
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@498
    22
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
jaroslav@435
    23
import org.apidesign.bck2brwsr.htmlpage.api.On;
jaroslav@435
    24
import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
jaroslav@879
    25
import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
jaroslav@141
    26
import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@492
    27
import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@141
    28
jaroslav@198
    29
/** HTML5 & Java demo showing the power of 
jaroslav@198
    30
 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
jaroslav@198
    31
 * as well as other goodies.
jaroslav@198
    32
 * 
jaroslav@198
    33
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@198
    34
 */
jaroslav@492
    35
@Page(xhtml="Calculator.xhtml", properties = {
jaroslav@498
    36
    @Property(name = "memory", type = double.class),
jaroslav@498
    37
    @Property(name = "display", type = double.class),
jaroslav@498
    38
    @Property(name = "operation", type = String.class),
jaroslav@877
    39
    @Property(name = "hover", type = boolean.class),
jaroslav@877
    40
    @Property(name = "history", type = double.class, array = true)
jaroslav@492
    41
})
jaroslav@511
    42
public class Calc {
jaroslav@1609
    43
    public static void main(String... args) throws Exception {
jaroslav@879
    44
        new Calculator().applyBindings().setOperation("plus");
jaroslav@1850
    45
        notifyFinish();
jaroslav@510
    46
    }
jaroslav@505
    47
    
jaroslav@435
    48
    @On(event = CLICK, id="clear")
jaroslav@510
    49
    static void clear(Calculator c) {
jaroslav@510
    50
        c.setMemory(0);
jaroslav@510
    51
        c.setOperation(null);
jaroslav@510
    52
        c.setDisplay(0);
jtulach@197
    53
    }
jtulach@197
    54
    
jaroslav@435
    55
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jaroslav@830
    56
    static void applyOp(Calculator c, String id) {
jaroslav@510
    57
        c.setMemory(c.getDisplay());
jaroslav@830
    58
        c.setOperation(id);
jaroslav@510
    59
        c.setDisplay(0);
jtulach@197
    60
    }
jaroslav@498
    61
jaroslav@498
    62
    @On(event = MOUSE_OVER, id= { "result" })
jaroslav@830
    63
    static void attemptingIn(Calculator c) {
jaroslav@510
    64
        c.setHover(true);
jaroslav@498
    65
    }
jaroslav@498
    66
    @On(event = MOUSE_OUT, id= { "result" })
jaroslav@830
    67
    static void attemptingOut(Calculator c) {
jaroslav@510
    68
        c.setHover(false);
jaroslav@498
    69
    }
jtulach@197
    70
    
jaroslav@435
    71
    @On(event = CLICK, id="result")
jaroslav@510
    72
    static void computeTheValue(Calculator c) {
jaroslav@877
    73
        final double newValue = compute(
jaroslav@510
    74
            c.getOperation(), 
jaroslav@510
    75
            c.getMemory(), 
jaroslav@510
    76
            c.getDisplay()
jaroslav@877
    77
        );
jaroslav@877
    78
        c.setDisplay(newValue);
jaroslav@890
    79
        if (!c.getHistory().contains(newValue)) {
jaroslav@890
    80
            c.getHistory().add(newValue);
jaroslav@890
    81
        }
jaroslav@510
    82
        c.setMemory(0);
jaroslav@498
    83
    }
jaroslav@498
    84
    
jaroslav@879
    85
    @OnFunction
jaroslav@879
    86
    static void recoverMemory(Calculator c, double data) {
jaroslav@879
    87
        c.setDisplay(data);
jaroslav@879
    88
    }
jaroslav@879
    89
jaroslav@879
    90
    @OnFunction
jaroslav@879
    91
    static void removeMemory(Calculator c, double data) {
jaroslav@879
    92
        c.getHistory().remove(data);
jaroslav@879
    93
    }
jaroslav@879
    94
    
jaroslav@498
    95
    private static double compute(String op, double memory, double display) {
jaroslav@498
    96
        switch (op) {
jaroslav@498
    97
            case "plus": return memory + display;
jaroslav@498
    98
            case "minus": return memory - display;
jaroslav@498
    99
            case "mul": return memory * display;
jaroslav@498
   100
            case "div": return memory / display;
jaroslav@498
   101
            default: throw new IllegalStateException(op);
jtulach@197
   102
        }
jtulach@197
   103
    }
jtulach@197
   104
    
jaroslav@435
   105
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@830
   106
    static void addDigit(String id, Calculator c) {
jaroslav@830
   107
        id = id.substring(1);
jaroslav@492
   108
        
jaroslav@510
   109
        double v = c.getDisplay();
jaroslav@492
   110
        if (v == 0.0) {
jaroslav@830
   111
            c.setDisplay(Integer.parseInt(id));
jtulach@197
   112
        } else {
jaroslav@495
   113
            String txt = Double.toString(v);
jaroslav@495
   114
            if (txt.endsWith(".0")) {
jaroslav@495
   115
                txt = txt.substring(0, txt.length() - 2);
jaroslav@495
   116
            }
jaroslav@830
   117
            txt = txt + id;
jaroslav@510
   118
            c.setDisplay(Double.parseDouble(txt));
jtulach@197
   119
        }
jaroslav@141
   120
    }
jaroslav@498
   121
jaroslav@498
   122
    @ComputedProperty
jaroslav@498
   123
    public static String displayPreview(
jaroslav@498
   124
        double display, boolean hover, double memory, String operation
jaroslav@498
   125
    ) {
jaroslav@498
   126
        if (!hover) {
jaroslav@498
   127
            return "Type numbers and perform simple operations! Press '=' to get result.";
jaroslav@498
   128
        }
jaroslav@498
   129
        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
jaroslav@498
   130
    }
jaroslav@879
   131
    
jaroslav@879
   132
    @ComputedProperty
jaroslav@879
   133
    static boolean emptyHistory(List<?> history) {
jaroslav@879
   134
        return history.isEmpty();
jaroslav@879
   135
    }
jaroslav@1850
   136
jaroslav@1850
   137
    @JavaScriptBody(args = {  }, body =
jaroslav@1850
   138
        "var xhttp = new XMLHttpRequest();\n" +
jaroslav@1850
   139
        "xhttp.open('GET', '/?exit=true', true);\n" +
jaroslav@1850
   140
        "xhttp.send();\n"
jaroslav@1850
   141
    )
jaroslav@1850
   142
    private static native void notifyFinish();
jaroslav@141
   143
}