Adding the Java source code for the calculator on the HTML page for demostration purposes
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 22 Nov 2012 10:38:38 +0100
changeset 1985c1604c5ca9a
parent 197 e7bb314eec32
child 199 866e4f4820c7
child 221 3ee23267706c
Adding the Java source code for the calculator on the HTML page for demostration purposes
javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml
     1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Thu Nov 22 00:18:34 2012 +0100
     1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Thu Nov 22 10:38:38 2012 +0100
     1.3 @@ -20,6 +20,12 @@
     1.4  import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
     1.5  import org.apidesign.bck2brwsr.htmlpage.api.Page;
     1.6  
     1.7 +/** HTML5 & Java demo showing the power of 
     1.8 + * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
     1.9 + * as well as other goodies.
    1.10 + * 
    1.11 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.12 + */
    1.13  @Page(xhtml="Calculator.xhtml")
    1.14  public class App {
    1.15      private static double memory;
     2.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml	Thu Nov 22 00:18:34 2012 +0100
     2.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml	Thu Nov 22 10:38:38 2012 +0100
     2.3 @@ -28,6 +28,7 @@
     2.4          pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
     2.5          table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
     2.6          .string {color: #e2ce00}
     2.7 +        a {color: #e2ce00}
     2.8          .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
     2.9          .ST0 {color: #0000ff}
    2.10          .comment {color: #428bdd}
    2.11 @@ -41,7 +42,7 @@
    2.12          </style>    
    2.13          </head>
    2.14      <body>
    2.15 -        <h1>Java &amp; HTML5</h1>
    2.16 +        <h1>Java and HTML5 - Together at Last!</h1>
    2.17          <table border="0" cellspacing="2">
    2.18              <tbody>
    2.19                  <tr>
    2.20 @@ -79,10 +80,75 @@
    2.21          <script src="bootjava.js"/>
    2.22          
    2.23          <hr/>
    2.24 -        <p>
    2.25 -        More information at the <a href="http://wiki.apidesign.org/wiki/Bck2Brwsr">
    2.26 -            Bck2Brwsr
    2.27 -        </a> project page.
    2.28 -        </p>
    2.29 +    <pre>
    2.30 +    <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
    2.31 +
    2.32 +    <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    2.33 +    <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
    2.34 +
    2.35 +    <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>
    2.36 +    <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>
    2.37 +    <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
    2.38 +    <span class="comment"> * </span>
    2.39 +    <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>
    2.40 +     <span class="comment">*/</span>
    2.41 +    @Page(xhtml=<span class="string">&quot;</span><span class="string">Calculator.xhtml</span><span class="string">&quot;</span>)
    2.42 +    <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
    2.43 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
    2.44 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
    2.45 +
    2.46 +        @OnClick(id=<span class="string">&quot;</span><span class="string">clear</span><span class="string">&quot;</span>)
    2.47 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
    2.48 +            memory = <span class="number">0</span>;
    2.49 +            operation = <span class="keyword-directive">null</span>;
    2.50 +            Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
    2.51 +        }
    2.52 +
    2.53 +        @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> })
    2.54 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
    2.55 +            memory = getValue();
    2.56 +            operation = op;
    2.57 +            Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">0</span><span class="string">&quot;</span>);
    2.58 +        }
    2.59 +
    2.60 +        @OnClick(id=<span class="string">&quot;</span><span class="string">result</span><span class="string">&quot;</span>)
    2.61 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
    2.62 +            <span class="keyword-directive">switch</span> (operation) {
    2.63 +                <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>;
    2.64 +                <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>;
    2.65 +                <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>;
    2.66 +                <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>;
    2.67 +                <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
    2.68 +            }
    2.69 +        }
    2.70 +
    2.71 +        @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>}) 
    2.72 +        <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
    2.73 +            digit = digit.substring(<span class="number">1</span>);
    2.74 +            String v = Calculator.DISPLAY.getValue();
    2.75 +            <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
    2.76 +                Calculator.DISPLAY.setValue(digit);
    2.77 +            } <span class="keyword-directive">else</span> {
    2.78 +                Calculator.DISPLAY.setValue(v + digit);
    2.79 +            }
    2.80 +        }
    2.81 +
    2.82 +        <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) {
    2.83 +            StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
    2.84 +            sb.append(v);
    2.85 +            Calculator.DISPLAY.setValue(sb.toString());
    2.86 +        }
    2.87 +
    2.88 +        <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
    2.89 +            <span class="keyword-directive">try</span> {
    2.90 +                <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
    2.91 +            } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
    2.92 +                Calculator.DISPLAY.setValue(<span class="string">&quot;</span><span class="string">err</span><span class="string">&quot;</span>);
    2.93 +                <span class="keyword-directive">return</span> <span class="number">0.0</span>;
    2.94 +            }
    2.95 +        }
    2.96 +    }
    2.97 +
    2.98 +    </pre>
    2.99      </body>
   2.100  </html>