javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Nov 2012 12:09:40 +0100
changeset 142 74c37d9cfdc9
parent 141 63be794c1eeb
child 197 e7bb314eec32
permissions -rw-r--r--
Fixing licenses
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@141
    23
@Page(xhtml="Calculator.xhtml")
jaroslav@141
    24
public class App {
jaroslav@141
    25
    private static final int OP_PLUS = 1;
jaroslav@141
    26
    private static final int OP_MINUS = 2;
jaroslav@141
    27
    private static final int OP_MUL = 3;
jaroslav@141
    28
    private static final int OP_DIV = 4;
jaroslav@141
    29
    
jaroslav@141
    30
    static double memory = 0;
jaroslav@141
    31
    static int operation = 0;
jaroslav@141
    32
    
jaroslav@141
    33
    
jaroslav@141
    34
    
jaroslav@141
    35
    @OnClick(id="clear")
jaroslav@141
    36
    static void clear() {
jaroslav@141
    37
        setValue(0.0);
jaroslav@141
    38
    }
jaroslav@141
    39
    
jaroslav@141
    40
    private static void setValue(double v) {
jaroslav@141
    41
        StringBuilder sb = new StringBuilder();
jaroslav@141
    42
        sb.append(v);
jaroslav@141
    43
        Calculator.DISPLAY.setValue(sb.toString());
jaroslav@141
    44
    }
jaroslav@141
    45
    
jaroslav@141
    46
    private static double getValue() {
jaroslav@141
    47
        return Double.parseDouble(Calculator.DISPLAY.getValue());
jaroslav@141
    48
    }
jaroslav@141
    49
    
jaroslav@141
    50
    @OnClick(id="plus")
jaroslav@141
    51
    static void plus() {
jaroslav@141
    52
        memory = getValue();
jaroslav@141
    53
        operation = OP_PLUS;
jaroslav@141
    54
        setValue(0.0);
jaroslav@141
    55
    }
jaroslav@141
    56
    
jaroslav@141
    57
    @OnClick(id="minus")
jaroslav@141
    58
    static void minus() {
jaroslav@141
    59
        memory = getValue();
jaroslav@141
    60
        operation = OP_MINUS;
jaroslav@141
    61
        setValue(0.0);
jaroslav@141
    62
    }
jaroslav@141
    63
    
jaroslav@141
    64
    @OnClick(id="mul")
jaroslav@141
    65
    static void mul() {
jaroslav@141
    66
        memory = getValue();
jaroslav@141
    67
        operation = OP_MUL;
jaroslav@141
    68
        setValue(0.0);
jaroslav@141
    69
    }
jaroslav@141
    70
    
jaroslav@141
    71
    @OnClick(id="result")
jaroslav@141
    72
    static void computeTheValue() {
jaroslav@141
    73
        switch (operation) {
jaroslav@141
    74
            case 0: break;
jaroslav@141
    75
            case OP_PLUS: setValue(memory + getValue()); break;
jaroslav@141
    76
            case OP_MINUS: setValue(memory - getValue()); break;
jaroslav@141
    77
            case OP_MUL: setValue(memory * getValue()); break;
jaroslav@141
    78
        }
jaroslav@141
    79
    }
jaroslav@141
    80
    
jaroslav@141
    81
    @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
jaroslav@141
    82
    static void addDigit(String digit) {
jaroslav@141
    83
        digit = digit.substring(1);
jaroslav@141
    84
        String v = Calculator.DISPLAY.getValue();
jaroslav@141
    85
        if ("0".equals(v) || v == null) {
jaroslav@141
    86
            Calculator.DISPLAY.setValue(digit);
jaroslav@141
    87
        } else {
jaroslav@141
    88
            Calculator.DISPLAY.setValue(v + digit);
jaroslav@141
    89
        }
jaroslav@141
    90
    }
jaroslav@141
    91
}