javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Jan 2013 18:20:18 +0100
branchmodel
changeset 492 854286e49061
parent 435 fb4ed6cc0d4b
child 495 f96c44dac48a
permissions -rw-r--r--
Basic support for knockout.js. It can read and write model 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@435
    20
import org.apidesign.bck2brwsr.htmlpage.api.On;
jaroslav@435
    21
import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
jaroslav@141
    22
import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@492
    23
import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@141
    24
jaroslav@198
    25
/** HTML5 & Java demo showing the power of 
jaroslav@198
    26
 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
jaroslav@198
    27
 * as well as other goodies.
jaroslav@198
    28
 * 
jaroslav@198
    29
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@198
    30
 */
jaroslav@492
    31
@Page(xhtml="Calculator.xhtml", properties = {
jaroslav@492
    32
    @Property(name = "display", type = double.class)
jaroslav@492
    33
})
jaroslav@141
    34
public class App {
jtulach@197
    35
    private static double memory;
jtulach@197
    36
    private static String operation;
jaroslav@141
    37
    
jaroslav@435
    38
    @On(event = CLICK, id="clear")
jaroslav@141
    39
    static void clear() {
jtulach@197
    40
        memory = 0;
jtulach@197
    41
        operation = null;
jaroslav@492
    42
        Calculator.setDisplay(0);
jtulach@197
    43
    }
jtulach@197
    44
    
jaroslav@435
    45
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jtulach@197
    46
    static void applyOp(String op) {
jaroslav@492
    47
        memory = Calculator.getDisplay();
jtulach@197
    48
        operation = op;
jaroslav@492
    49
        Calculator.setDisplay(0);
jtulach@197
    50
    }
jtulach@197
    51
    
jaroslav@435
    52
    @On(event = CLICK, id="result")
jtulach@197
    53
    static void computeTheValue() {
jtulach@197
    54
        switch (operation) {
jaroslav@492
    55
            case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
jaroslav@492
    56
            case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
jaroslav@492
    57
            case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
jaroslav@492
    58
            case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
jtulach@197
    59
            default: throw new IllegalStateException(operation);
jtulach@197
    60
        }
jtulach@197
    61
    }
jtulach@197
    62
    
jaroslav@435
    63
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jtulach@197
    64
    static void addDigit(String digit) {
jtulach@197
    65
        digit = digit.substring(1);
jaroslav@492
    66
        
jaroslav@492
    67
        double v = Calculator.getDisplay();
jaroslav@492
    68
        if (v == 0.0) {
jaroslav@492
    69
            Calculator.setDisplay(Integer.parseInt(digit));
jtulach@197
    70
        } else {
jaroslav@492
    71
            String txt = Double.toString(v) + digit;
jaroslav@492
    72
            Calculator.setDisplay(Double.parseDouble(txt));
jtulach@197
    73
        }
jaroslav@141
    74
    }
jaroslav@141
    75
    
jaroslav@492
    76
    
jaroslav@492
    77
    static {
jaroslav@492
    78
        Calculator.setDisplay(10.0);
jaroslav@492
    79
        Calculator.applyBindings();
jaroslav@141
    80
    }
jaroslav@141
    81
}