javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java
changeset 1619 a7bf87c2c1d9
parent 1574 d51a5533a2e7
parent 1618 f62b42f0b751
child 1620 15c6f1aabe13
     1.1 --- a/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java	Thu May 15 11:38:27 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,146 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, version 2 of the License.
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program. Look for COPYING file in the top folder.
    1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 - */
    1.21 -package org.apidesign.bck2brwsr.demo.calc;
    1.22 -
    1.23 -import java.util.List;
    1.24 -import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    1.25 -import org.apidesign.bck2brwsr.htmlpage.api.On;
    1.26 -import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    1.27 -import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
    1.28 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.29 -import org.apidesign.bck2brwsr.htmlpage.api.Property;
    1.30 -
    1.31 -/** HTML5 & Java demo showing the power of 
    1.32 - * <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
    1.33 - * as well as other goodies.
    1.34 - * 
    1.35 - * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.36 - */
    1.37 -@Page(xhtml="Calculator.xhtml", properties = {
    1.38 -    @Property(name = "memory", type = double.class),
    1.39 -    @Property(name = "display", type = double.class),
    1.40 -    @Property(name = "operation", type = String.class),
    1.41 -    @Property(name = "hover", type = boolean.class),
    1.42 -    @Property(name = "history", type = HistoryImpl.class, array = true)
    1.43 -})
    1.44 -public class Calc {
    1.45 -    static {
    1.46 -        new Calculator().applyBindings().setOperation("plus");
    1.47 -    }
    1.48 -    
    1.49 -    @On(event = CLICK, id="clear")
    1.50 -    static void clear(Calculator c) {
    1.51 -        c.setMemory(0);
    1.52 -        c.setOperation(null);
    1.53 -        c.setDisplay(0);
    1.54 -    }
    1.55 -    
    1.56 -    @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
    1.57 -    static void applyOp(Calculator c, String id) {
    1.58 -        c.setMemory(c.getDisplay());
    1.59 -        c.setOperation(id);
    1.60 -        c.setDisplay(0);
    1.61 -    }
    1.62 -
    1.63 -    @On(event = MOUSE_OVER, id= { "result" })
    1.64 -    static void attemptingIn(Calculator c) {
    1.65 -        c.setHover(true);
    1.66 -    }
    1.67 -    @On(event = MOUSE_OUT, id= { "result" })
    1.68 -    static void attemptingOut(Calculator c) {
    1.69 -        c.setHover(false);
    1.70 -    }
    1.71 -    
    1.72 -    @On(event = CLICK, id="result")
    1.73 -    static void computeTheValue(Calculator c) {
    1.74 -        final double newValue = compute(
    1.75 -            c.getOperation(), 
    1.76 -            c.getMemory(), 
    1.77 -            c.getDisplay()
    1.78 -        );
    1.79 -        c.setDisplay(newValue);
    1.80 -        if (!containsValue(c.getHistory(), newValue)) {
    1.81 -            History h = new History();
    1.82 -            h.setValue(newValue);
    1.83 -            h.setOperation(c.getOperation());
    1.84 -            c.getHistory().add(h);
    1.85 -        }
    1.86 -        c.setMemory(0);
    1.87 -    }
    1.88 -    
    1.89 -    @OnFunction
    1.90 -    static void recoverMemory(Calculator c, History data) {
    1.91 -        c.setDisplay(data.getValue());
    1.92 -    }
    1.93 -
    1.94 -    @OnFunction
    1.95 -    static void removeMemory(Calculator c, History data) {
    1.96 -        c.getHistory().remove(data);
    1.97 -    }
    1.98 -    
    1.99 -    private static double compute(String op, double memory, double display) {
   1.100 -        switch (op) {
   1.101 -            case "plus": return memory + display;
   1.102 -            case "minus": return memory - display;
   1.103 -            case "mul": return memory * display;
   1.104 -            case "div": return memory / display;
   1.105 -            default: throw new IllegalStateException(op);
   1.106 -        }
   1.107 -    }
   1.108 -    
   1.109 -    @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
   1.110 -    static void addDigit(String id, Calculator c) {
   1.111 -        id = id.substring(1);
   1.112 -        
   1.113 -        double v = c.getDisplay();
   1.114 -        if (v == 0.0) {
   1.115 -            c.setDisplay(Integer.parseInt(id));
   1.116 -        } else {
   1.117 -            String txt = Double.toString(v);
   1.118 -            if (txt.endsWith(".0")) {
   1.119 -                txt = txt.substring(0, txt.length() - 2);
   1.120 -            }
   1.121 -            txt = txt + id;
   1.122 -            c.setDisplay(Double.parseDouble(txt));
   1.123 -        }
   1.124 -    }
   1.125 -
   1.126 -    @ComputedProperty
   1.127 -    public static String displayPreview(
   1.128 -        double display, boolean hover, double memory, String operation
   1.129 -    ) {
   1.130 -        if (!hover) {
   1.131 -            return "Type numbers and perform simple operations! Press '=' to get result.";
   1.132 -        }
   1.133 -        return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
   1.134 -    }
   1.135 -    
   1.136 -    @ComputedProperty
   1.137 -    static boolean emptyHistory(List<?> history) {
   1.138 -        return history.isEmpty();
   1.139 -    }
   1.140 -
   1.141 -    private static boolean containsValue(List<History> arr, final double newValue) {
   1.142 -        for (History history : arr) {
   1.143 -            if (history.getValue() == newValue) {
   1.144 -                return true;
   1.145 -            }
   1.146 -        }
   1.147 -        return false;
   1.148 -    }
   1.149 -}