javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 12 Jan 2013 20:24:30 +0100
changeset 435 fb4ed6cc0d4b
parent 427 12e866a32b40
child 492 854286e49061
permissions -rw-r--r--
Allowing access to all onsmthng events of page elements
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@141
    23
jaroslav@198
    24
/** HTML5 & Java demo showing the power of 
jaroslav@198
    25
 * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
jaroslav@198
    26
 * as well as other goodies.
jaroslav@198
    27
 * 
jaroslav@198
    28
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@198
    29
 */
jaroslav@141
    30
@Page(xhtml="Calculator.xhtml")
jaroslav@141
    31
public class App {
jtulach@197
    32
    private static double memory;
jtulach@197
    33
    private static String operation;
jaroslav@141
    34
    
jaroslav@435
    35
    @On(event = CLICK, id="clear")
jaroslav@141
    36
    static void clear() {
jtulach@197
    37
        memory = 0;
jtulach@197
    38
        operation = null;
jtulach@197
    39
        Calculator.DISPLAY.setValue("0");
jtulach@197
    40
    }
jtulach@197
    41
    
jaroslav@435
    42
    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
jtulach@197
    43
    static void applyOp(String op) {
jtulach@197
    44
        memory = getValue();
jtulach@197
    45
        operation = op;
jtulach@197
    46
        Calculator.DISPLAY.setValue("0");
jtulach@197
    47
    }
jtulach@197
    48
    
jaroslav@435
    49
    @On(event = CLICK, id="result")
jtulach@197
    50
    static void computeTheValue() {
jtulach@197
    51
        switch (operation) {
jtulach@197
    52
            case "plus": setValue(memory + getValue()); break;
jtulach@197
    53
            case "minus": setValue(memory - getValue()); break;
jtulach@197
    54
            case "mul": setValue(memory * getValue()); break;
jtulach@197
    55
            case "div": setValue(memory / getValue()); break;
jtulach@197
    56
            default: throw new IllegalStateException(operation);
jtulach@197
    57
        }
jtulach@197
    58
    }
jtulach@197
    59
    
jaroslav@435
    60
    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jtulach@197
    61
    static void addDigit(String digit) {
jtulach@197
    62
        digit = digit.substring(1);
jtulach@197
    63
        String v = Calculator.DISPLAY.getValue();
jtulach@197
    64
        if (getValue() == 0.0) {
jtulach@197
    65
            Calculator.DISPLAY.setValue(digit);
jtulach@197
    66
        } else {
jtulach@197
    67
            Calculator.DISPLAY.setValue(v + digit);
jtulach@197
    68
        }
jaroslav@141
    69
    }
jaroslav@141
    70
    
jaroslav@141
    71
    private static void setValue(double v) {
jaroslav@141
    72
        StringBuilder sb = new StringBuilder();
jaroslav@141
    73
        sb.append(v);
jaroslav@296
    74
        if (sb.toString().endsWith(".0")) {
jaroslav@296
    75
            final int l = sb.length();
jaroslav@296
    76
            sb.delete(l - 2, l);
jaroslav@296
    77
        }
jaroslav@141
    78
        Calculator.DISPLAY.setValue(sb.toString());
jaroslav@141
    79
    }
jtulach@197
    80
jaroslav@141
    81
    private static double getValue() {
jtulach@197
    82
        try {
jtulach@197
    83
            return Double.parseDouble(Calculator.DISPLAY.getValue());
jtulach@197
    84
        } catch (NumberFormatException ex) {
jtulach@197
    85
            Calculator.DISPLAY.setValue("err");
jtulach@197
    86
            return 0.0;
jaroslav@141
    87
        }
jaroslav@141
    88
    }
jaroslav@141
    89
}