javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 16:21:14 +0100
branchmodel
changeset 513 35e2c701e514
parent 505 javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml@4198be34b516
child 516 3119b93f1969
permissions -rw-r--r--
Moving to demo package
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!--
     3 
     4     Back 2 Browser Bytecode Translator
     5     Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6 
     7     This program is free software: you can redistribute it and/or modify
     8     it under the terms of the GNU General Public License as published by
     9     the Free Software Foundation, version 2 of the License.
    10 
    11     This program is distributed in the hope that it will be useful,
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14     GNU General Public License for more details.
    15 
    16     You should have received a copy of the GNU General Public License
    17     along with this program. Look for COPYING file in the top folder.
    18     If not, see http://opensource.org/licenses/GPL-2.0.
    19 
    20 -->
    21 <!DOCTYPE html>
    22 <html xmlns="http://www.w3.org/1999/xhtml">
    23     <head>
    24         <title>Simple Calculator in HTML5 and Java</title>
    25 
    26         <style type="text/css">
    27         body {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    28         pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    29         table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    30         .string {color: #e2ce00}
    31         a {color: #e2ce00}
    32         .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
    33         .ST0 {color: #0000ff}
    34         .comment {color: #428bdd}
    35         .keyword-directive {color: #f8bb00}
    36         .tag {color: #f8bb00}
    37         .ST0 {color: #628fb5; background-color: #1b3450}
    38         .sgml-comment {color: #808080}
    39         .value {color: #99006b}
    40         .argument {color: #007c00}
    41         .sgml-declaration {color: #bf9221}
    42         </style>    
    43         </head>
    44     <body>
    45         <h1>Java and HTML5 - Together at Last!</h1>
    46         <table border="0" cellspacing="2">
    47             <tbody>
    48                 <tr>
    49                     <td colspan="4"><input data-bind="value: display" value="0" 
    50                             style="text-align: right"/>
    51                 </td>
    52                 </tr>
    53                 <tr>
    54                     <td><button id="n1">1</button></td>
    55                     <td><button id="n2">2</button></td>
    56                     <td><button id="n3">3</button></td>
    57                     <td><button id="plus">+</button></td>
    58                 </tr>
    59                 <tr>
    60                     <td><button id="n4">4</button></td>
    61                     <td><button id="n5">5</button></td>
    62                     <td><button id="n6">6</button></td>
    63                     <td><button id="minus">-</button></td>
    64                 </tr>
    65                 <tr>
    66                     <td><button id="n7">7</button></td>
    67                     <td><button id="n8">8</button></td>
    68                     <td><button id="n9">9</button></td>
    69                     <td><button id="mul">*</button></td>
    70                 </tr>
    71                 <tr>
    72                     <td><button id="clear">C</button></td>
    73                     <td><button id="n0">0</button></td>
    74                     <td><button id="result">=</button></td>
    75                     <td><button id="div">/</button></td>
    76                 </tr>
    77             </tbody>
    78         </table>
    79         <p>
    80             <div data-bind="text: displayPreview"></div>
    81         </p>
    82         
    83         <script src="/bck2brwsr.js"></script>
    84         <script src="/vm.js"></script>
    85         <script type="text/javascript">
    86             vm.loadClass('org.apidesign.bck2brwsr.demo.calc.Calc');
    87         </script>
    88         
    89         <hr/>
    90     <pre>
    91     <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
    92 
    93     <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    94     <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
    95 
    96     <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>
    97     <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>
    98     <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
    99     <span class="comment"> * </span>
   100     <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>
   101      <span class="comment">*/</span>
   102     @Page(xhtml=<span class="string">&quot;</span><span class="string">Calculator.xhtml</span><span class="string">&quot;</span>)
   103     <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
   104         <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
   105         <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
   106 
   107         @OnClick(id=<span class="string">&quot;</span><span class="string">clear</span><span class="string">&quot;</span>)
   108         <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
   109             memory = <span class="number">0</span>;
   110             operation = <span class="keyword-directive">null</span>;
   111             Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
   112         }
   113 
   114         @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> })
   115         <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
   116             memory = getValue();
   117             operation = op;
   118             Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
   119         }
   120 
   121         @OnClick(id=<span class="string">&quot;</span><span class="string">result</span><span class="string">&quot;</span>)
   122         <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
   123             <span class="keyword-directive">switch</span> (operation) {
   124                 <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>;
   125                 <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>;
   126                 <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>;
   127                 <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>;
   128                 <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
   129             }
   130         }
   131 
   132         @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>}) 
   133         <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
   134             digit = digit.substring(<span class="number">1</span>);
   135             String v = Calculator.DISPLAY.getValue();
   136             <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
   137                 Calculator.DISPLAY.setValue(digit);
   138             } <span class="keyword-directive">else</span> {
   139                 Calculator.DISPLAY.setValue(v + digit);
   140             }
   141         }
   142 
   143         <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) {
   144             StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
   145             sb.append(v);
   146             Calculator.DISPLAY.setValue(sb.toString());
   147         }
   148 
   149         <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
   150             <span class="keyword-directive">try</span> {
   151                 <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
   152             } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
   153                 Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">err</span><span class="string">&quot;</span>);
   154                 <span class="keyword-directive">return</span> <span class="number">0.0</span>;
   155             }
   156         }
   157     }
   158 
   159     </pre>
   160     </body>
   161 </html>