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