javaquery/demo-static-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml
changeset 373 52386a70f9d9
parent 198 5c1604c5ca9a
child 374 e5b4bd29f268
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javaquery/demo-static-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml	Mon Dec 24 07:51:15 2012 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<!--
     1.6 +
     1.7 +    Back 2 Browser Bytecode Translator
     1.8 +    Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.9 +
    1.10 +    This program is free software: you can redistribute it and/or modify
    1.11 +    it under the terms of the GNU General Public License as published by
    1.12 +    the Free Software Foundation, version 2 of the License.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +    GNU General Public License for more details.
    1.18 +
    1.19 +    You should have received a copy of the GNU General Public License
    1.20 +    along with this program. Look for COPYING file in the top folder.
    1.21 +    If not, see http://opensource.org/licenses/GPL-2.0.
    1.22 +
    1.23 +-->
    1.24 +<!DOCTYPE html>
    1.25 +<html xmlns="http://www.w3.org/1999/xhtml">
    1.26 +    <head>
    1.27 +        <title>Simple Calculator in HTML5 and Java</title>
    1.28 +
    1.29 +        <style type="text/css">
    1.30 +        body {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    1.31 +        pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    1.32 +        table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    1.33 +        .string {color: #e2ce00}
    1.34 +        a {color: #e2ce00}
    1.35 +        .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
    1.36 +        .ST0 {color: #0000ff}
    1.37 +        .comment {color: #428bdd}
    1.38 +        .keyword-directive {color: #f8bb00}
    1.39 +        .tag {color: #f8bb00}
    1.40 +        .ST0 {color: #628fb5; background-color: #1b3450}
    1.41 +        .sgml-comment {color: #808080}
    1.42 +        .value {color: #99006b}
    1.43 +        .argument {color: #007c00}
    1.44 +        .sgml-declaration {color: #bf9221}
    1.45 +        </style>    
    1.46 +        </head>
    1.47 +    <body>
    1.48 +        <h1>Java and HTML5 - Together at Last!</h1>
    1.49 +        <table border="0" cellspacing="2">
    1.50 +            <tbody>
    1.51 +                <tr>
    1.52 +                    <td colspan="4"><input id="display" value="0" 
    1.53 +                            style="text-align: right"/>
    1.54 +                </td>
    1.55 +                </tr>
    1.56 +                <tr>
    1.57 +                    <td><button id="n1">1</button></td>
    1.58 +                    <td><button id="n2">2</button></td>
    1.59 +                    <td><button id="n3">3</button></td>
    1.60 +                    <td><button id="plus">+</button></td>
    1.61 +                </tr>
    1.62 +                <tr>
    1.63 +                    <td><button id="n4">4</button></td>
    1.64 +                    <td><button id="n5">5</button></td>
    1.65 +                    <td><button id="n6">6</button></td>
    1.66 +                    <td><button id="minus">-</button></td>
    1.67 +                </tr>
    1.68 +                <tr>
    1.69 +                    <td><button id="n7">7</button></td>
    1.70 +                    <td><button id="n8">8</button></td>
    1.71 +                    <td><button id="n9">9</button></td>
    1.72 +                    <td><button id="mul">*</button></td>
    1.73 +                </tr>
    1.74 +                <tr>
    1.75 +                    <td><button id="clear">C</button></td>
    1.76 +                    <td><button id="n0">0</button></td>
    1.77 +                    <td><button id="result">=</button></td>
    1.78 +                    <td><button id="div">/</button></td>
    1.79 +                </tr>
    1.80 +            </tbody>
    1.81 +        </table>
    1.82 +
    1.83 +        <script src="bootjava.js"/>
    1.84 +        
    1.85 +        <hr/>
    1.86 +    <pre>
    1.87 +    <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
    1.88 +
    1.89 +    <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    1.90 +    <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.91 +
    1.92 +    <span class="comment">/**</span> <span class="comment">HTML5</span><span class="comment"> &amp; </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>
    1.93 +    <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>
    1.94 +    <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
    1.95 +    <span class="comment"> * </span>
    1.96 +    <span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">Jaroslav</span> <span class="comment">Tulach</span> <span class="ST0">&lt;jaroslav.tulach@apidesign.org&gt;</span>
    1.97 +     <span class="comment">*/</span>
    1.98 +    @Page(xhtml=<span class="string">&quot;</span><span class="string">Calculator.xhtml</span><span class="string">&quot;</span>)
    1.99 +    <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
   1.100 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
   1.101 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
   1.102 +
   1.103 +        @OnClick(id=<span class="string">&quot;</span><span class="string">clear</span><span class="string">&quot;</span>)
   1.104 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
   1.105 +            memory = <span class="number">0</span>;
   1.106 +            operation = <span class="keyword-directive">null</span>;
   1.107 +            Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
   1.108 +        }
   1.109 +
   1.110 +        @OnClick(id= { <span class="string">&quot;</span><span class="string">plus</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">minus</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">mul</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">div</span><span class="string">&quot;</span> })
   1.111 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
   1.112 +            memory = getValue();
   1.113 +            operation = op;
   1.114 +            Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
   1.115 +        }
   1.116 +
   1.117 +        @OnClick(id=<span class="string">&quot;</span><span class="string">result</span><span class="string">&quot;</span>)
   1.118 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
   1.119 +            <span class="keyword-directive">switch</span> (operation) {
   1.120 +                <span class="keyword-directive">case</span> <span class="string">&quot;</span><span class="string">plus</span><span class="string">&quot;</span>: setValue(memory + getValue()); <span class="keyword-directive">break</span>;
   1.121 +                <span class="keyword-directive">case</span> <span class="string">&quot;</span><span class="string">minus</span><span class="string">&quot;</span>: setValue(memory - getValue()); <span class="keyword-directive">break</span>;
   1.122 +                <span class="keyword-directive">case</span> <span class="string">&quot;</span><span class="string">mul</span><span class="string">&quot;</span>: setValue(memory * getValue()); <span class="keyword-directive">break</span>;
   1.123 +                <span class="keyword-directive">case</span> <span class="string">&quot;</span><span class="string">div</span><span class="string">&quot;</span>: setValue(memory / getValue()); <span class="keyword-directive">break</span>;
   1.124 +                <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
   1.125 +            }
   1.126 +        }
   1.127 +
   1.128 +        @OnClick(id={<span class="string">&quot;</span><span class="string">n0</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n1</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n2</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n3</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n4</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n5</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n6</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n7</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n8</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">n9</span><span class="string">&quot;</span>}) 
   1.129 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
   1.130 +            digit = digit.substring(<span class="number">1</span>);
   1.131 +            String v = Calculator.DISPLAY.getValue();
   1.132 +            <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
   1.133 +                Calculator.DISPLAY.setValue(digit);
   1.134 +            } <span class="keyword-directive">else</span> {
   1.135 +                Calculator.DISPLAY.setValue(v + digit);
   1.136 +            }
   1.137 +        }
   1.138 +
   1.139 +        <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) {
   1.140 +            StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
   1.141 +            sb.append(v);
   1.142 +            Calculator.DISPLAY.setValue(sb.toString());
   1.143 +        }
   1.144 +
   1.145 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
   1.146 +            <span class="keyword-directive">try</span> {
   1.147 +                <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
   1.148 +            } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
   1.149 +                Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">err</span><span class="string">&quot;</span>);
   1.150 +                <span class="keyword-directive">return</span> <span class="number">0.0</span>;
   1.151 +            }
   1.152 +        }
   1.153 +    }
   1.154 +
   1.155 +    </pre>
   1.156 +    </body>
   1.157 +</html>