Java and HTML5 - From JAR File!


Unlike the previous version of the HTML5 and Java calculator this one is much more than a static compilation to JavaScript. This one is using real JAR file with real Java classes. This means that the same code that runs in your browser can also run on your server.

$ unzip -l demo.calculator.jar 
Archive:  demo.calculator.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
      124  2012-11-28 15:23   META-INF/MANIFEST.MF
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$3.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$14.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$15.class
      633  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$9.class
     2835  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator.class
      633  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$4.class
      606  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$6.class
      633  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$5.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$11.class
     2153  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/App.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$13.class
      596  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$1.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$10.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$12.class
      634  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$2.class
      635  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$16.class
      633  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$8.class
      633  2012-11-28 15:23   org/apidesign/bck2brwsr/mavenhtml/Calculator$7.class
      702  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/Title.class
      486  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/Page.class
      440  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/OnClick.class
      791  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/Button.class
     1486  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/Element.class
     1136  2012-11-28 16:27   org/apidesign/bck2brwsr/htmlpage/api/Input.class
        

    package org.apidesign.bck2brwsr.mavenhtml;

    import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    import org.apidesign.bck2brwsr.htmlpage.api.Page;

    /** HTML5 & Java demo showing the power of annotation processors
     * as well as other goodies, including type-safe association between
     * an XHTML page and Java.
     * 
     * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     */
    @Page(xhtml="Calculator.xhtml")
    public class App {
        private static double memory;
        private static String operation;

        @OnClick(id="clear")
        static void clear() {
            memory = 0;
            operation = null;
            Calculator.DISPLAY.setValue("0");
        }

        @OnClick(id= { "plus", "minus", "mul", "div" })
        static void applyOp(String op) {
            memory = getValue();
            operation = op;
            Calculator.DISPLAY.setValue("0");
        }

        @OnClick(id="result")
        static void computeTheValue() {
            switch (operation) {
                case "plus": setValue(memory + getValue()); break;
                case "minus": setValue(memory - getValue()); break;
                case "mul": setValue(memory * getValue()); break;
                case "div": setValue(memory / getValue()); break;
                default: throw new IllegalStateException(operation);
            }
        }

        @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
        static void addDigit(String digit) {
            digit = digit.substring(1);
            String v = Calculator.DISPLAY.getValue();
            if (getValue() == 0.0) {
                Calculator.DISPLAY.setValue(digit);
            } else {
                Calculator.DISPLAY.setValue(v + digit);
            }
        }

        private static void setValue(double v) {
            StringBuilder sb = new StringBuilder();
            sb.append(v);
            Calculator.DISPLAY.setValue(sb.toString());
        }

        private static double getValue() {
            try {
                return Double.parseDouble(Calculator.DISPLAY.getValue());
            } catch (NumberFormatException ex) {
                Calculator.DISPLAY.setValue("err");
                return 0.0;
            }
        }
    }