Less verbose. Concentrating all operations into single method. Using string switch to determine what operation to run.
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 22 Nov 2012 00:18:34 +0100
changeset 197e7bb314eec32
parent 196 e7ea061e669c
child 198 5c1604c5ca9a
Less verbose. Concentrating all operations into single method. Using string switch to determine what operation to run.
javaquery/demo-calculator/pom.xml
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/pom.xml	Thu Nov 22 00:08:57 2012 +0100
     1.2 +++ b/javaquery/demo-calculator/pom.xml	Thu Nov 22 00:18:34 2012 +0100
     1.3 @@ -51,8 +51,8 @@
     1.4              <artifactId>maven-compiler-plugin</artifactId>
     1.5              <version>2.3.2</version>
     1.6              <configuration>
     1.7 -               <source>1.6</source>
     1.8 -               <target>1.6</target>
     1.9 +               <source>1.7</source>
    1.10 +               <target>1.7</target>
    1.11              </configuration>
    1.12           </plugin>
    1.13        </plugins>
     2.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Thu Nov 22 00:08:57 2012 +0100
     2.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Thu Nov 22 00:18:34 2012 +0100
     2.3 @@ -22,19 +22,43 @@
     2.4  
     2.5  @Page(xhtml="Calculator.xhtml")
     2.6  public class App {
     2.7 -    private static final int OP_PLUS = 1;
     2.8 -    private static final int OP_MINUS = 2;
     2.9 -    private static final int OP_MUL = 3;
    2.10 -    private static final int OP_DIV = 4;
    2.11 -    
    2.12 -    static double memory = 0;
    2.13 -    static int operation = 0;
    2.14 -    
    2.15 -    
    2.16 +    private static double memory;
    2.17 +    private static String operation;
    2.18      
    2.19      @OnClick(id="clear")
    2.20      static void clear() {
    2.21 -        setValue(0.0);
    2.22 +        memory = 0;
    2.23 +        operation = null;
    2.24 +        Calculator.DISPLAY.setValue("0");
    2.25 +    }
    2.26 +    
    2.27 +    @OnClick(id= { "plus", "minus", "mul", "div" })
    2.28 +    static void applyOp(String op) {
    2.29 +        memory = getValue();
    2.30 +        operation = op;
    2.31 +        Calculator.DISPLAY.setValue("0");
    2.32 +    }
    2.33 +    
    2.34 +    @OnClick(id="result")
    2.35 +    static void computeTheValue() {
    2.36 +        switch (operation) {
    2.37 +            case "plus": setValue(memory + getValue()); break;
    2.38 +            case "minus": setValue(memory - getValue()); break;
    2.39 +            case "mul": setValue(memory * getValue()); break;
    2.40 +            case "div": setValue(memory / getValue()); break;
    2.41 +            default: throw new IllegalStateException(operation);
    2.42 +        }
    2.43 +    }
    2.44 +    
    2.45 +    @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    2.46 +    static void addDigit(String digit) {
    2.47 +        digit = digit.substring(1);
    2.48 +        String v = Calculator.DISPLAY.getValue();
    2.49 +        if (getValue() == 0.0) {
    2.50 +            Calculator.DISPLAY.setValue(digit);
    2.51 +        } else {
    2.52 +            Calculator.DISPLAY.setValue(v + digit);
    2.53 +        }
    2.54      }
    2.55      
    2.56      private static void setValue(double v) {
    2.57 @@ -42,50 +66,13 @@
    2.58          sb.append(v);
    2.59          Calculator.DISPLAY.setValue(sb.toString());
    2.60      }
    2.61 -    
    2.62 +
    2.63      private static double getValue() {
    2.64 -        return Double.parseDouble(Calculator.DISPLAY.getValue());
    2.65 -    }
    2.66 -    
    2.67 -    @OnClick(id="plus")
    2.68 -    static void plus() {
    2.69 -        memory = getValue();
    2.70 -        operation = OP_PLUS;
    2.71 -        setValue(0.0);
    2.72 -    }
    2.73 -    
    2.74 -    @OnClick(id="minus")
    2.75 -    static void minus() {
    2.76 -        memory = getValue();
    2.77 -        operation = OP_MINUS;
    2.78 -        setValue(0.0);
    2.79 -    }
    2.80 -    
    2.81 -    @OnClick(id="mul")
    2.82 -    static void mul() {
    2.83 -        memory = getValue();
    2.84 -        operation = OP_MUL;
    2.85 -        setValue(0.0);
    2.86 -    }
    2.87 -    
    2.88 -    @OnClick(id="result")
    2.89 -    static void computeTheValue() {
    2.90 -        switch (operation) {
    2.91 -            case 0: break;
    2.92 -            case OP_PLUS: setValue(memory + getValue()); break;
    2.93 -            case OP_MINUS: setValue(memory - getValue()); break;
    2.94 -            case OP_MUL: setValue(memory * getValue()); break;
    2.95 -        }
    2.96 -    }
    2.97 -    
    2.98 -    @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    2.99 -    static void addDigit(String digit) {
   2.100 -        digit = digit.substring(1);
   2.101 -        String v = Calculator.DISPLAY.getValue();
   2.102 -        if ("0".equals(v) || v == null) {
   2.103 -            Calculator.DISPLAY.setValue(digit);
   2.104 -        } else {
   2.105 -            Calculator.DISPLAY.setValue(v + digit);
   2.106 +        try {
   2.107 +            return Double.parseDouble(Calculator.DISPLAY.getValue());
   2.108 +        } catch (NumberFormatException ex) {
   2.109 +            Calculator.DISPLAY.setValue("err");
   2.110 +            return 0.0;
   2.111          }
   2.112      }
   2.113  }
     3.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml	Thu Nov 22 00:08:57 2012 +0100
     3.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml	Thu Nov 22 00:18:34 2012 +0100
     3.3 @@ -22,12 +22,32 @@
     3.4  <html xmlns="http://www.w3.org/1999/xhtml">
     3.5      <head>
     3.6          <title>Simple Calculator in HTML5 and Java</title>
     3.7 -    </head>
     3.8 +
     3.9 +        <style type="text/css">
    3.10 +        body {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    3.11 +        pre {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    3.12 +        table {color: #ffffff; background-color: #121e31; font-family: Monospaced}
    3.13 +        .string {color: #e2ce00}
    3.14 +        .ST1 {color: #0000cc; font-family: Monospaced; font-weight: bold}
    3.15 +        .ST0 {color: #0000ff}
    3.16 +        .comment {color: #428bdd}
    3.17 +        .keyword-directive {color: #f8bb00}
    3.18 +        .tag {color: #f8bb00}
    3.19 +        .ST0 {color: #628fb5; background-color: #1b3450}
    3.20 +        .sgml-comment {color: #808080}
    3.21 +        .value {color: #99006b}
    3.22 +        .argument {color: #007c00}
    3.23 +        .sgml-declaration {color: #bf9221}
    3.24 +        </style>    
    3.25 +        </head>
    3.26      <body>
    3.27 +        <h1>Java &amp; HTML5</h1>
    3.28          <table border="0" cellspacing="2">
    3.29              <tbody>
    3.30                  <tr>
    3.31 -                    <td colspan="4"><input id="display" value="0"/></td>
    3.32 +                    <td colspan="4"><input id="display" value="0" 
    3.33 +                            style="text-align: right"/>
    3.34 +                </td>
    3.35                  </tr>
    3.36                  <tr>
    3.37                      <td><button id="n1">1</button></td>
    3.38 @@ -57,5 +77,12 @@
    3.39          </table>
    3.40  
    3.41          <script src="bootjava.js"/>
    3.42 +        
    3.43 +        <hr/>
    3.44 +        <p>
    3.45 +        More information at the <a href="http://wiki.apidesign.org/wiki/Bck2Brwsr">
    3.46 +            Bck2Brwsr
    3.47 +        </a> project page.
    3.48 +        </p>
    3.49      </body>
    3.50  </html>