1.1 --- a/javaquery/demo-calculator/pom.xml Mon Jan 21 15:57:30 2013 +0100
1.2 +++ b/javaquery/demo-calculator/pom.xml Mon Jan 21 16:04:25 2013 +0100
1.3 @@ -42,7 +42,7 @@
1.4 <configuration>
1.5 <executable>xdg-open</executable>
1.6 <arguments>
1.7 - <argument>${project.build.directory}/classes/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml</argument>
1.8 + <argument>${project.build.directory}/classes/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml</argument>
1.9 </arguments>
1.10 </configuration>
1.11 </plugin>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java Mon Jan 21 16:04:25 2013 +0100
2.3 @@ -0,0 +1,112 @@
2.4 +/**
2.5 + * Back 2 Browser Bytecode Translator
2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 + *
2.8 + * This program is free software: you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License as published by
2.10 + * the Free Software Foundation, version 2 of the License.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program. Look for COPYING file in the top folder.
2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 + */
2.21 +package org.apidesign.bck2brwsr.demo.calc.staticcompilation;
2.22 +
2.23 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
2.24 +import org.apidesign.bck2brwsr.htmlpage.api.On;
2.25 +import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
2.26 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
2.27 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
2.28 +
2.29 +/** HTML5 & Java demo showing the power of
2.30 + * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
2.31 + * as well as other goodies.
2.32 + *
2.33 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.34 + */
2.35 +@Page(xhtml="Calculator.xhtml", properties = {
2.36 + @Property(name = "memory", type = double.class),
2.37 + @Property(name = "display", type = double.class),
2.38 + @Property(name = "operation", type = String.class),
2.39 + @Property(name = "hover", type = boolean.class)
2.40 +})
2.41 +public class Calc {
2.42 + static {
2.43 + new Calculator().applyBindings();
2.44 + }
2.45 +
2.46 + @On(event = CLICK, id="clear")
2.47 + static void clear(Calculator c) {
2.48 + c.setMemory(0);
2.49 + c.setOperation(null);
2.50 + c.setDisplay(0);
2.51 + }
2.52 +
2.53 + @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
2.54 + static void applyOp(Calculator c, String op) {
2.55 + c.setMemory(c.getDisplay());
2.56 + c.setOperation(op);
2.57 + c.setDisplay(0);
2.58 + }
2.59 +
2.60 + @On(event = MOUSE_OVER, id= { "result" })
2.61 + static void attemptingIn(Calculator c, String op) {
2.62 + c.setHover(true);
2.63 + }
2.64 + @On(event = MOUSE_OUT, id= { "result" })
2.65 + static void attemptingOut(Calculator c, String op) {
2.66 + c.setHover(false);
2.67 + }
2.68 +
2.69 + @On(event = CLICK, id="result")
2.70 + static void computeTheValue(Calculator c) {
2.71 + c.setDisplay(compute(
2.72 + c.getOperation(),
2.73 + c.getMemory(),
2.74 + c.getDisplay()
2.75 + ));
2.76 + c.setMemory(0);
2.77 + }
2.78 +
2.79 + private static double compute(String op, double memory, double display) {
2.80 + switch (op) {
2.81 + case "plus": return memory + display;
2.82 + case "minus": return memory - display;
2.83 + case "mul": return memory * display;
2.84 + case "div": return memory / display;
2.85 + default: throw new IllegalStateException(op);
2.86 + }
2.87 + }
2.88 +
2.89 + @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
2.90 + static void addDigit(String digit, Calculator c) {
2.91 + digit = digit.substring(1);
2.92 +
2.93 + double v = c.getDisplay();
2.94 + if (v == 0.0) {
2.95 + c.setDisplay(Integer.parseInt(digit));
2.96 + } else {
2.97 + String txt = Double.toString(v);
2.98 + if (txt.endsWith(".0")) {
2.99 + txt = txt.substring(0, txt.length() - 2);
2.100 + }
2.101 + txt = txt + digit;
2.102 + c.setDisplay(Double.parseDouble(txt));
2.103 + }
2.104 + }
2.105 +
2.106 + @ComputedProperty
2.107 + public static String displayPreview(
2.108 + double display, boolean hover, double memory, String operation
2.109 + ) {
2.110 + if (!hover) {
2.111 + return "Type numbers and perform simple operations! Press '=' to get result.";
2.112 + }
2.113 + return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
2.114 + }
2.115 +}
3.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 15:57:30 2013 +0100
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,112 +0,0 @@
3.4 -/**
3.5 - * Back 2 Browser Bytecode Translator
3.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 - *
3.8 - * This program is free software: you can redistribute it and/or modify
3.9 - * it under the terms of the GNU General Public License as published by
3.10 - * the Free Software Foundation, version 2 of the License.
3.11 - *
3.12 - * This program is distributed in the hope that it will be useful,
3.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 - * GNU General Public License for more details.
3.16 - *
3.17 - * You should have received a copy of the GNU General Public License
3.18 - * along with this program. Look for COPYING file in the top folder.
3.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 - */
3.21 -package org.apidesign.bck2brwsr.mavenhtml;
3.22 -
3.23 -import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
3.24 -import org.apidesign.bck2brwsr.htmlpage.api.On;
3.25 -import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
3.26 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
3.27 -import org.apidesign.bck2brwsr.htmlpage.api.Property;
3.28 -
3.29 -/** HTML5 & Java demo showing the power of
3.30 - * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
3.31 - * as well as other goodies.
3.32 - *
3.33 - * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.34 - */
3.35 -@Page(xhtml="Calculator.xhtml", properties = {
3.36 - @Property(name = "memory", type = double.class),
3.37 - @Property(name = "display", type = double.class),
3.38 - @Property(name = "operation", type = String.class),
3.39 - @Property(name = "hover", type = boolean.class)
3.40 -})
3.41 -public class App {
3.42 - static {
3.43 - new Calculator().applyBindings();
3.44 - }
3.45 -
3.46 - @On(event = CLICK, id="clear")
3.47 - static void clear(Calculator c) {
3.48 - c.setMemory(0);
3.49 - c.setOperation(null);
3.50 - c.setDisplay(0);
3.51 - }
3.52 -
3.53 - @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
3.54 - static void applyOp(Calculator c, String op) {
3.55 - c.setMemory(c.getDisplay());
3.56 - c.setOperation(op);
3.57 - c.setDisplay(0);
3.58 - }
3.59 -
3.60 - @On(event = MOUSE_OVER, id= { "result" })
3.61 - static void attemptingIn(Calculator c, String op) {
3.62 - c.setHover(true);
3.63 - }
3.64 - @On(event = MOUSE_OUT, id= { "result" })
3.65 - static void attemptingOut(Calculator c, String op) {
3.66 - c.setHover(false);
3.67 - }
3.68 -
3.69 - @On(event = CLICK, id="result")
3.70 - static void computeTheValue(Calculator c) {
3.71 - c.setDisplay(compute(
3.72 - c.getOperation(),
3.73 - c.getMemory(),
3.74 - c.getDisplay()
3.75 - ));
3.76 - c.setMemory(0);
3.77 - }
3.78 -
3.79 - private static double compute(String op, double memory, double display) {
3.80 - switch (op) {
3.81 - case "plus": return memory + display;
3.82 - case "minus": return memory - display;
3.83 - case "mul": return memory * display;
3.84 - case "div": return memory / display;
3.85 - default: throw new IllegalStateException(op);
3.86 - }
3.87 - }
3.88 -
3.89 - @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
3.90 - static void addDigit(String digit, Calculator c) {
3.91 - digit = digit.substring(1);
3.92 -
3.93 - double v = c.getDisplay();
3.94 - if (v == 0.0) {
3.95 - c.setDisplay(Integer.parseInt(digit));
3.96 - } else {
3.97 - String txt = Double.toString(v);
3.98 - if (txt.endsWith(".0")) {
3.99 - txt = txt.substring(0, txt.length() - 2);
3.100 - }
3.101 - txt = txt + digit;
3.102 - c.setDisplay(Double.parseDouble(txt));
3.103 - }
3.104 - }
3.105 -
3.106 - @ComputedProperty
3.107 - public static String displayPreview(
3.108 - double display, boolean hover, double memory, String operation
3.109 - ) {
3.110 - if (!hover) {
3.111 - return "Type numbers and perform simple operations! Press '=' to get result.";
3.112 - }
3.113 - return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
3.114 - }
3.115 -}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Mon Jan 21 16:04:25 2013 +0100
4.3 @@ -0,0 +1,156 @@
4.4 +<?xml version="1.0" encoding="UTF-8"?>
4.5 +<!--
4.6 +
4.7 + Back 2 Browser Bytecode Translator
4.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.9 +
4.10 + This program is free software: you can redistribute it and/or modify
4.11 + it under the terms of the GNU General Public License as published by
4.12 + the Free Software Foundation, version 2 of the License.
4.13 +
4.14 + This program is distributed in the hope that it will be useful,
4.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + GNU General Public License for more details.
4.18 +
4.19 + You should have received a copy of the GNU General Public License
4.20 + along with this program. Look for COPYING file in the top folder.
4.21 + If not, see http://opensource.org/licenses/GPL-2.0.
4.22 +
4.23 +-->
4.24 +<!DOCTYPE html>
4.25 +<html xmlns="http://www.w3.org/1999/xhtml">
4.26 + <head>
4.27 + <title>Simple Calculator in HTML5 and Java</title>
4.28 +
4.29 + <style type="text/css">
4.30 + body {color: #ffffff; background-color: #121e31; font-family: Monospaced}
4.31 + pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
4.32 + table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
4.33 + .string {color: #e2ce00}
4.34 + a {color: #e2ce00}
4.35 + .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
4.36 + .ST0 {color: #0000ff}
4.37 + .comment {color: #428bdd}
4.38 + .keyword-directive {color: #f8bb00}
4.39 + .tag {color: #f8bb00}
4.40 + .ST0 {color: #628fb5; background-color: #1b3450}
4.41 + .sgml-comment {color: #808080}
4.42 + .value {color: #99006b}
4.43 + .argument {color: #007c00}
4.44 + .sgml-declaration {color: #bf9221}
4.45 + </style>
4.46 + </head>
4.47 + <body>
4.48 + <h1>Java and HTML5 - Together at Last!</h1>
4.49 + <table border="0" cellspacing="2">
4.50 + <tbody>
4.51 + <tr>
4.52 + <td colspan="4"><input data-bind="value: display" value="0"
4.53 + style="text-align: right"/>
4.54 + </td>
4.55 + </tr>
4.56 + <tr>
4.57 + <td><button id="n1">1</button></td>
4.58 + <td><button id="n2">2</button></td>
4.59 + <td><button id="n3">3</button></td>
4.60 + <td><button id="plus">+</button></td>
4.61 + </tr>
4.62 + <tr>
4.63 + <td><button id="n4">4</button></td>
4.64 + <td><button id="n5">5</button></td>
4.65 + <td><button id="n6">6</button></td>
4.66 + <td><button id="minus">-</button></td>
4.67 + </tr>
4.68 + <tr>
4.69 + <td><button id="n7">7</button></td>
4.70 + <td><button id="n8">8</button></td>
4.71 + <td><button id="n9">9</button></td>
4.72 + <td><button id="mul">*</button></td>
4.73 + </tr>
4.74 + <tr>
4.75 + <td><button id="clear">C</button></td>
4.76 + <td><button id="n0">0</button></td>
4.77 + <td><button id="result">=</button></td>
4.78 + <td><button id="div">/</button></td>
4.79 + </tr>
4.80 + </tbody>
4.81 + </table>
4.82 + <p>
4.83 + <div data-bind="text: displayPreview"></div>
4.84 + </p>
4.85 + <script src="bootjava.js"/>
4.86 +
4.87 + <hr/>
4.88 + <pre>
4.89 + <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
4.90 +
4.91 + <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
4.92 + <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
4.93 +
4.94 + <span class="comment">/**</span> <span class="comment">HTML5</span><span class="comment"> & </span><span class="comment">Java</span> <span class="comment">demo</span> <span class="comment">showing</span> <span class="comment">the</span> <span class="comment">power</span> <span class="comment">of</span> <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
4.95 + <span class="comment"> * </span><span class="comment">as</span> <span class="comment">well</span> <span class="comment">as</span> <span class="comment">other</span> <span class="comment">goodies</span><span class="comment">, including type-safe association between</span>
4.96 + <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
4.97 + <span class="comment"> * </span>
4.98 + <span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">Jaroslav</span> <span class="comment">Tulach</span> <span class="ST0"><jaroslav.tulach@apidesign.org></span>
4.99 + <span class="comment">*/</span>
4.100 + @Page(xhtml=<span class="string">"</span><span class="string">Calculator.xhtml</span><span class="string">"</span>)
4.101 + <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
4.102 + <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
4.103 + <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
4.104 +
4.105 + @OnClick(id=<span class="string">"</span><span class="string">clear</span><span class="string">"</span>)
4.106 + <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
4.107 + memory = <span class="number">0</span>;
4.108 + operation = <span class="keyword-directive">null</span>;
4.109 + Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
4.110 + }
4.111 +
4.112 + @OnClick(id= { <span class="string">"</span><span class="string">plus</span><span class="string">"</span>, <span class="string">"</span><span class="string">minus</span><span class="string">"</span>, <span class="string">"</span><span class="string">mul</span><span class="string">"</span>, <span class="string">"</span><span class="string">div</span><span class="string">"</span> })
4.113 + <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
4.114 + memory = getValue();
4.115 + operation = op;
4.116 + Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
4.117 + }
4.118 +
4.119 + @OnClick(id=<span class="string">"</span><span class="string">result</span><span class="string">"</span>)
4.120 + <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
4.121 + <span class="keyword-directive">switch</span> (operation) {
4.122 + <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">plus</span><span class="string">"</span>: setValue(memory + getValue()); <span class="keyword-directive">break</span>;
4.123 + <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">minus</span><span class="string">"</span>: setValue(memory - getValue()); <span class="keyword-directive">break</span>;
4.124 + <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">mul</span><span class="string">"</span>: setValue(memory * getValue()); <span class="keyword-directive">break</span>;
4.125 + <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">div</span><span class="string">"</span>: setValue(memory / getValue()); <span class="keyword-directive">break</span>;
4.126 + <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
4.127 + }
4.128 + }
4.129 +
4.130 + @OnClick(id={<span class="string">"</span><span class="string">n0</span><span class="string">"</span>, <span class="string">"</span><span class="string">n1</span><span class="string">"</span>, <span class="string">"</span><span class="string">n2</span><span class="string">"</span>, <span class="string">"</span><span class="string">n3</span><span class="string">"</span>, <span class="string">"</span><span class="string">n4</span><span class="string">"</span>, <span class="string">"</span><span class="string">n5</span><span class="string">"</span>, <span class="string">"</span><span class="string">n6</span><span class="string">"</span>, <span class="string">"</span><span class="string">n7</span><span class="string">"</span>, <span class="string">"</span><span class="string">n8</span><span class="string">"</span>, <span class="string">"</span><span class="string">n9</span><span class="string">"</span>})
4.131 + <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
4.132 + digit = digit.substring(<span class="number">1</span>);
4.133 + String v = Calculator.DISPLAY.getValue();
4.134 + <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
4.135 + Calculator.DISPLAY.setValue(digit);
4.136 + } <span class="keyword-directive">else</span> {
4.137 + Calculator.DISPLAY.setValue(v + digit);
4.138 + }
4.139 + }
4.140 +
4.141 + <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> setValue(<span class="keyword-directive">double</span> v) {
4.142 + StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
4.143 + sb.append(v);
4.144 + Calculator.DISPLAY.setValue(sb.toString());
4.145 + }
4.146 +
4.147 + <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
4.148 + <span class="keyword-directive">try</span> {
4.149 + <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
4.150 + } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
4.151 + Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">err</span><span class="string">"</span>);
4.152 + <span class="keyword-directive">return</span> <span class="number">0.0</span>;
4.153 + }
4.154 + }
4.155 + }
4.156 +
4.157 + </pre>
4.158 + </body>
4.159 +</html>
5.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Mon Jan 21 15:57:30 2013 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,156 +0,0 @@
5.4 -<?xml version="1.0" encoding="UTF-8"?>
5.5 -<!--
5.6 -
5.7 - Back 2 Browser Bytecode Translator
5.8 - Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.9 -
5.10 - This program is free software: you can redistribute it and/or modify
5.11 - it under the terms of the GNU General Public License as published by
5.12 - the Free Software Foundation, version 2 of the License.
5.13 -
5.14 - This program is distributed in the hope that it will be useful,
5.15 - but WITHOUT ANY WARRANTY; without even the implied warranty of
5.16 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.17 - GNU General Public License for more details.
5.18 -
5.19 - You should have received a copy of the GNU General Public License
5.20 - along with this program. Look for COPYING file in the top folder.
5.21 - If not, see http://opensource.org/licenses/GPL-2.0.
5.22 -
5.23 --->
5.24 -<!DOCTYPE html>
5.25 -<html xmlns="http://www.w3.org/1999/xhtml">
5.26 - <head>
5.27 - <title>Simple Calculator in HTML5 and Java</title>
5.28 -
5.29 - <style type="text/css">
5.30 - body {color: #ffffff; background-color: #121e31; font-family: Monospaced}
5.31 - pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
5.32 - table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
5.33 - .string {color: #e2ce00}
5.34 - a {color: #e2ce00}
5.35 - .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
5.36 - .ST0 {color: #0000ff}
5.37 - .comment {color: #428bdd}
5.38 - .keyword-directive {color: #f8bb00}
5.39 - .tag {color: #f8bb00}
5.40 - .ST0 {color: #628fb5; background-color: #1b3450}
5.41 - .sgml-comment {color: #808080}
5.42 - .value {color: #99006b}
5.43 - .argument {color: #007c00}
5.44 - .sgml-declaration {color: #bf9221}
5.45 - </style>
5.46 - </head>
5.47 - <body>
5.48 - <h1>Java and HTML5 - Together at Last!</h1>
5.49 - <table border="0" cellspacing="2">
5.50 - <tbody>
5.51 - <tr>
5.52 - <td colspan="4"><input data-bind="value: display" value="0"
5.53 - style="text-align: right"/>
5.54 - </td>
5.55 - </tr>
5.56 - <tr>
5.57 - <td><button id="n1">1</button></td>
5.58 - <td><button id="n2">2</button></td>
5.59 - <td><button id="n3">3</button></td>
5.60 - <td><button id="plus">+</button></td>
5.61 - </tr>
5.62 - <tr>
5.63 - <td><button id="n4">4</button></td>
5.64 - <td><button id="n5">5</button></td>
5.65 - <td><button id="n6">6</button></td>
5.66 - <td><button id="minus">-</button></td>
5.67 - </tr>
5.68 - <tr>
5.69 - <td><button id="n7">7</button></td>
5.70 - <td><button id="n8">8</button></td>
5.71 - <td><button id="n9">9</button></td>
5.72 - <td><button id="mul">*</button></td>
5.73 - </tr>
5.74 - <tr>
5.75 - <td><button id="clear">C</button></td>
5.76 - <td><button id="n0">0</button></td>
5.77 - <td><button id="result">=</button></td>
5.78 - <td><button id="div">/</button></td>
5.79 - </tr>
5.80 - </tbody>
5.81 - </table>
5.82 - <p>
5.83 - <div data-bind="text: displayPreview"></div>
5.84 - </p>
5.85 - <script src="bootjava.js"/>
5.86 -
5.87 - <hr/>
5.88 - <pre>
5.89 - <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
5.90 -
5.91 - <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
5.92 - <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
5.93 -
5.94 - <span class="comment">/**</span> <span class="comment">HTML5</span><span class="comment"> & </span><span class="comment">Java</span> <span class="comment">demo</span> <span class="comment">showing</span> <span class="comment">the</span> <span class="comment">power</span> <span class="comment">of</span> <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
5.95 - <span class="comment"> * </span><span class="comment">as</span> <span class="comment">well</span> <span class="comment">as</span> <span class="comment">other</span> <span class="comment">goodies</span><span class="comment">, including type-safe association between</span>
5.96 - <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
5.97 - <span class="comment"> * </span>
5.98 - <span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">Jaroslav</span> <span class="comment">Tulach</span> <span class="ST0"><jaroslav.tulach@apidesign.org></span>
5.99 - <span class="comment">*/</span>
5.100 - @Page(xhtml=<span class="string">"</span><span class="string">Calculator.xhtml</span><span class="string">"</span>)
5.101 - <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
5.102 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
5.103 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
5.104 -
5.105 - @OnClick(id=<span class="string">"</span><span class="string">clear</span><span class="string">"</span>)
5.106 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
5.107 - memory = <span class="number">0</span>;
5.108 - operation = <span class="keyword-directive">null</span>;
5.109 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
5.110 - }
5.111 -
5.112 - @OnClick(id= { <span class="string">"</span><span class="string">plus</span><span class="string">"</span>, <span class="string">"</span><span class="string">minus</span><span class="string">"</span>, <span class="string">"</span><span class="string">mul</span><span class="string">"</span>, <span class="string">"</span><span class="string">div</span><span class="string">"</span> })
5.113 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
5.114 - memory = getValue();
5.115 - operation = op;
5.116 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
5.117 - }
5.118 -
5.119 - @OnClick(id=<span class="string">"</span><span class="string">result</span><span class="string">"</span>)
5.120 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
5.121 - <span class="keyword-directive">switch</span> (operation) {
5.122 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">plus</span><span class="string">"</span>: setValue(memory + getValue()); <span class="keyword-directive">break</span>;
5.123 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">minus</span><span class="string">"</span>: setValue(memory - getValue()); <span class="keyword-directive">break</span>;
5.124 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">mul</span><span class="string">"</span>: setValue(memory * getValue()); <span class="keyword-directive">break</span>;
5.125 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">div</span><span class="string">"</span>: setValue(memory / getValue()); <span class="keyword-directive">break</span>;
5.126 - <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
5.127 - }
5.128 - }
5.129 -
5.130 - @OnClick(id={<span class="string">"</span><span class="string">n0</span><span class="string">"</span>, <span class="string">"</span><span class="string">n1</span><span class="string">"</span>, <span class="string">"</span><span class="string">n2</span><span class="string">"</span>, <span class="string">"</span><span class="string">n3</span><span class="string">"</span>, <span class="string">"</span><span class="string">n4</span><span class="string">"</span>, <span class="string">"</span><span class="string">n5</span><span class="string">"</span>, <span class="string">"</span><span class="string">n6</span><span class="string">"</span>, <span class="string">"</span><span class="string">n7</span><span class="string">"</span>, <span class="string">"</span><span class="string">n8</span><span class="string">"</span>, <span class="string">"</span><span class="string">n9</span><span class="string">"</span>})
5.131 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
5.132 - digit = digit.substring(<span class="number">1</span>);
5.133 - String v = Calculator.DISPLAY.getValue();
5.134 - <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
5.135 - Calculator.DISPLAY.setValue(digit);
5.136 - } <span class="keyword-directive">else</span> {
5.137 - Calculator.DISPLAY.setValue(v + digit);
5.138 - }
5.139 - }
5.140 -
5.141 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> setValue(<span class="keyword-directive">double</span> v) {
5.142 - StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
5.143 - sb.append(v);
5.144 - Calculator.DISPLAY.setValue(sb.toString());
5.145 - }
5.146 -
5.147 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
5.148 - <span class="keyword-directive">try</span> {
5.149 - <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
5.150 - } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
5.151 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">err</span><span class="string">"</span>);
5.152 - <span class="keyword-directive">return</span> <span class="number">0.0</span>;
5.153 - }
5.154 - }
5.155 - }
5.156 -
5.157 - </pre>
5.158 - </body>
5.159 -</html>