javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
branchmodel
changeset 492 854286e49061
parent 435 fb4ed6cc0d4b
child 495 f96c44dac48a
     1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Sat Jan 12 20:24:30 2013 +0100
     1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Sun Jan 20 18:20:18 2013 +0100
     1.3 @@ -20,6 +20,7 @@
     1.4  import org.apidesign.bck2brwsr.htmlpage.api.On;
     1.5  import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
     1.6  import org.apidesign.bck2brwsr.htmlpage.api.Page;
     1.7 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
     1.8  
     1.9  /** HTML5 & Java demo showing the power of 
    1.10   * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    1.11 @@ -27,7 +28,9 @@
    1.12   * 
    1.13   * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.14   */
    1.15 -@Page(xhtml="Calculator.xhtml")
    1.16 +@Page(xhtml="Calculator.xhtml", properties = {
    1.17 +    @Property(name = "display", type = double.class)
    1.18 +})
    1.19  public class App {
    1.20      private static double memory;
    1.21      private static String operation;
    1.22 @@ -36,23 +39,23 @@
    1.23      static void clear() {
    1.24          memory = 0;
    1.25          operation = null;
    1.26 -        Calculator.DISPLAY.setValue("0");
    1.27 +        Calculator.setDisplay(0);
    1.28      }
    1.29      
    1.30      @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    1.31      static void applyOp(String op) {
    1.32 -        memory = getValue();
    1.33 +        memory = Calculator.getDisplay();
    1.34          operation = op;
    1.35 -        Calculator.DISPLAY.setValue("0");
    1.36 +        Calculator.setDisplay(0);
    1.37      }
    1.38      
    1.39      @On(event = CLICK, id="result")
    1.40      static void computeTheValue() {
    1.41          switch (operation) {
    1.42 -            case "plus": setValue(memory + getValue()); break;
    1.43 -            case "minus": setValue(memory - getValue()); break;
    1.44 -            case "mul": setValue(memory * getValue()); break;
    1.45 -            case "div": setValue(memory / getValue()); break;
    1.46 +            case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
    1.47 +            case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
    1.48 +            case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
    1.49 +            case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
    1.50              default: throw new IllegalStateException(operation);
    1.51          }
    1.52      }
    1.53 @@ -60,30 +63,19 @@
    1.54      @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    1.55      static void addDigit(String digit) {
    1.56          digit = digit.substring(1);
    1.57 -        String v = Calculator.DISPLAY.getValue();
    1.58 -        if (getValue() == 0.0) {
    1.59 -            Calculator.DISPLAY.setValue(digit);
    1.60 +        
    1.61 +        double v = Calculator.getDisplay();
    1.62 +        if (v == 0.0) {
    1.63 +            Calculator.setDisplay(Integer.parseInt(digit));
    1.64          } else {
    1.65 -            Calculator.DISPLAY.setValue(v + digit);
    1.66 +            String txt = Double.toString(v) + digit;
    1.67 +            Calculator.setDisplay(Double.parseDouble(txt));
    1.68          }
    1.69      }
    1.70      
    1.71 -    private static void setValue(double v) {
    1.72 -        StringBuilder sb = new StringBuilder();
    1.73 -        sb.append(v);
    1.74 -        if (sb.toString().endsWith(".0")) {
    1.75 -            final int l = sb.length();
    1.76 -            sb.delete(l - 2, l);
    1.77 -        }
    1.78 -        Calculator.DISPLAY.setValue(sb.toString());
    1.79 -    }
    1.80 -
    1.81 -    private static double getValue() {
    1.82 -        try {
    1.83 -            return Double.parseDouble(Calculator.DISPLAY.getValue());
    1.84 -        } catch (NumberFormatException ex) {
    1.85 -            Calculator.DISPLAY.setValue("err");
    1.86 -            return 0.0;
    1.87 -        }
    1.88 +    
    1.89 +    static {
    1.90 +        Calculator.setDisplay(10.0);
    1.91 +        Calculator.applyBindings();
    1.92      }
    1.93  }