javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
branchmodel
changeset 505 4198be34b516
parent 492 854286e49061
     1.1 --- a/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Sun Jan 20 18:20:18 2013 +0100
     1.2 +++ b/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Mon Jan 21 13:43:40 2013 +0100
     1.3 @@ -17,6 +17,7 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.mavenhtml;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
     1.8  import org.apidesign.bck2brwsr.htmlpage.api.On;
     1.9  import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    1.10  import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.11 @@ -29,34 +30,54 @@
    1.12   * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.13   */
    1.14  @Page(xhtml="Calculator.xhtml", properties = {
    1.15 -    @Property(name = "display", type = double.class)
    1.16 +    @Property(name = "memory", type = double.class),
    1.17 +    @Property(name = "display", type = double.class),
    1.18 +    @Property(name = "operation", type = String.class),
    1.19 +    @Property(name = "hover", type = boolean.class)
    1.20  })
    1.21  public class App {
    1.22 -    private static double memory;
    1.23 -    private static String operation;
    1.24 +    private static final Calculator CALC = new Calculator().applyBindings();
    1.25      
    1.26      @On(event = CLICK, id="clear")
    1.27      static void clear() {
    1.28 -        memory = 0;
    1.29 -        operation = null;
    1.30 -        Calculator.setDisplay(0);
    1.31 +        CALC.setMemory(0);
    1.32 +        CALC.setOperation(null);
    1.33 +        CALC.setDisplay(0);
    1.34      }
    1.35      
    1.36      @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    1.37      static void applyOp(String op) {
    1.38 -        memory = Calculator.getDisplay();
    1.39 -        operation = op;
    1.40 -        Calculator.setDisplay(0);
    1.41 +        CALC.setMemory(CALC.getDisplay());
    1.42 +        CALC.setOperation(op);
    1.43 +        CALC.setDisplay(0);
    1.44 +    }
    1.45 +
    1.46 +    @On(event = MOUSE_OVER, id= { "result" })
    1.47 +    static void attemptingIn(String op) {
    1.48 +        CALC.setHover(true);
    1.49 +    }
    1.50 +    @On(event = MOUSE_OUT, id= { "result" })
    1.51 +    static void attemptingOut(String op) {
    1.52 +        CALC.setHover(false);
    1.53      }
    1.54      
    1.55      @On(event = CLICK, id="result")
    1.56      static void computeTheValue() {
    1.57 -        switch (operation) {
    1.58 -            case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
    1.59 -            case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
    1.60 -            case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
    1.61 -            case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
    1.62 -            default: throw new IllegalStateException(operation);
    1.63 +        CALC.setDisplay(compute(
    1.64 +            CALC.getOperation(), 
    1.65 +            CALC.getMemory(), 
    1.66 +            CALC.getDisplay()
    1.67 +        ));
    1.68 +        CALC.setMemory(0);
    1.69 +    }
    1.70 +    
    1.71 +    private static double compute(String op, double memory, double display) {
    1.72 +        switch (op) {
    1.73 +            case "plus": return memory + display;
    1.74 +            case "minus": return memory - display;
    1.75 +            case "mul": return memory * display;
    1.76 +            case "div": return memory / display;
    1.77 +            default: throw new IllegalStateException(op);
    1.78          }
    1.79      }
    1.80      
    1.81 @@ -64,12 +85,26 @@
    1.82      static void addDigit(String digit) {
    1.83          digit = digit.substring(1);
    1.84          
    1.85 -        double v = Calculator.getDisplay();
    1.86 +        double v = CALC.getDisplay();
    1.87          if (v == 0.0) {
    1.88 -            Calculator.setDisplay(Integer.parseInt(digit));
    1.89 +            CALC.setDisplay(Integer.parseInt(digit));
    1.90          } else {
    1.91 -            String txt = Double.toString(v) + digit;
    1.92 -            Calculator.setDisplay(Double.parseDouble(txt));
    1.93 +            String txt = Double.toString(v);
    1.94 +            if (txt.endsWith(".0")) {
    1.95 +                txt = txt.substring(0, txt.length() - 2);
    1.96 +            }
    1.97 +            txt = txt + digit;
    1.98 +            CALC.setDisplay(Double.parseDouble(txt));
    1.99          }
   1.100      }
   1.101 +
   1.102 +    @ComputedProperty
   1.103 +    public static String displayPreview(
   1.104 +        double display, boolean hover, double memory, String operation
   1.105 +    ) {
   1.106 +        if (!hover) {
   1.107 +            return "Type numbers and perform simple operations! Press '=' to get result.";
   1.108 +        }
   1.109 +        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   1.110 +    }
   1.111  }