diff -r f6a165f7f00f -r 5751fe604e6b javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Mon Dec 24 08:04:45 2012 +0100 @@ -0,0 +1,158 @@ + + + + + + Simple Calculator in HTML5 and Java + + + + +

Java and HTML5 - Together at Last!

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + +
+
+    package org.apidesign.bck2brwsr.mavenhtml;
+
+    import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
+    import org.apidesign.bck2brwsr.htmlpage.api.Page;
+
+    /** HTML5 & Java demo showing the power of annotation processors
+     * as well as other goodies, including type-safe association between
+     * an XHTML page and Java.
+     * 
+     * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
+     */
+    @Page(xhtml="Calculator.xhtml")
+    public class App {
+        private static double memory;
+        private static String operation;
+
+        @OnClick(id="clear")
+        static void clear() {
+            memory = 0;
+            operation = null;
+            Calculator.DISPLAY.setValue("0");
+        }
+
+        @OnClick(id= { "plus", "minus", "mul", "div" })
+        static void applyOp(String op) {
+            memory = getValue();
+            operation = op;
+            Calculator.DISPLAY.setValue("0");
+        }
+
+        @OnClick(id="result")
+        static void computeTheValue() {
+            switch (operation) {
+                case "plus": setValue(memory + getValue()); break;
+                case "minus": setValue(memory - getValue()); break;
+                case "mul": setValue(memory * getValue()); break;
+                case "div": setValue(memory / getValue()); break;
+                default: throw new IllegalStateException(operation);
+            }
+        }
+
+        @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
+        static void addDigit(String digit) {
+            digit = digit.substring(1);
+            String v = Calculator.DISPLAY.getValue();
+            if (getValue() == 0.0) {
+                Calculator.DISPLAY.setValue(digit);
+            } else {
+                Calculator.DISPLAY.setValue(v + digit);
+            }
+        }
+
+        private static void setValue(double v) {
+            StringBuilder sb = new StringBuilder();
+            sb.append(v);
+            Calculator.DISPLAY.setValue(sb.toString());
+        }
+
+        private static double getValue() {
+            try {
+                return Double.parseDouble(Calculator.DISPLAY.getValue());
+            } catch (NumberFormatException ex) {
+                Calculator.DISPLAY.setValue("err");
+                return 0.0;
+            }
+        }
+    }
+
+    
+ +