javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Sun Jan 20 21:01:10 2013 +0100
1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 07:00:56 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,52 @@
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 -
1.25 @On(event = CLICK, id="clear")
1.26 static void clear() {
1.27 - memory = 0;
1.28 - operation = null;
1.29 + Calculator.setMemory(0);
1.30 + Calculator.setOperation(null);
1.31 Calculator.setDisplay(0);
1.32 }
1.33
1.34 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
1.35 static void applyOp(String op) {
1.36 - memory = Calculator.getDisplay();
1.37 - operation = op;
1.38 + Calculator.setMemory(Calculator.getDisplay());
1.39 + Calculator.setOperation(op);
1.40 Calculator.setDisplay(0);
1.41 }
1.42 +
1.43 + @On(event = MOUSE_OVER, id= { "result" })
1.44 + static void attemptingIn(String op) {
1.45 + Calculator.setHover(true);
1.46 + }
1.47 + @On(event = MOUSE_OUT, id= { "result" })
1.48 + static void attemptingOut(String op) {
1.49 + Calculator.setHover(false);
1.50 + }
1.51
1.52 @On(event = CLICK, id="result")
1.53 static void computeTheValue() {
1.54 - switch (operation) {
1.55 - case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break;
1.56 - case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break;
1.57 - case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break;
1.58 - case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break;
1.59 - default: throw new IllegalStateException(operation);
1.60 + Calculator.setDisplay(compute(
1.61 + Calculator.getOperation(),
1.62 + Calculator.getMemory(),
1.63 + Calculator.getDisplay()
1.64 + ));
1.65 + Calculator.setMemory(0);
1.66 + }
1.67 +
1.68 + private static double compute(String op, double memory, double display) {
1.69 + switch (op) {
1.70 + case "plus": return memory + display;
1.71 + case "minus": return memory - display;
1.72 + case "mul": return memory * display;
1.73 + case "div": return memory / display;
1.74 + default: throw new IllegalStateException(op);
1.75 }
1.76 }
1.77
1.78 @@ -76,10 +95,18 @@
1.79 Calculator.setDisplay(Double.parseDouble(txt));
1.80 }
1.81 }
1.82 -
1.83 +
1.84 + @ComputedProperty
1.85 + public static String displayPreview(
1.86 + double display, boolean hover, double memory, String operation
1.87 + ) {
1.88 + if (!hover) {
1.89 + return "Type numbers and perform simple operations! Press '=' to get result.";
1.90 + }
1.91 + return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
1.92 + }
1.93
1.94 static {
1.95 - Calculator.setDisplay(10.0);
1.96 Calculator.applyBindings();
1.97 }
1.98 }