Introducing History model class with two defined properties and one derived model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 31 Mar 2013 12:26:07 +0200
branchmodel
changeset 91052cb50cea1df
parent 909 e51a474fcf79
child 911 fdf840690861
Introducing History model class with two defined properties and one derived
javaquery/demo-calculator-dynamic/nbactions.xml
javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java
javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/HistoryImpl.java
javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml
     1.1 --- a/javaquery/demo-calculator-dynamic/nbactions.xml	Sun Mar 31 12:01:38 2013 +0200
     1.2 +++ b/javaquery/demo-calculator-dynamic/nbactions.xml	Sun Mar 31 12:26:07 2013 +0200
     1.3 @@ -23,7 +23,7 @@
     1.4              <actionName>run</actionName>
     1.5              <goals>
     1.6                  <goal>process-classes</goal>
     1.7 -                <goal>org.apidesign.bck2brwsr:mojo:0.5-SNAPSHOT:brwsr</goal>
     1.8 +                <goal>org.apidesign.bck2brwsr:mojo:0.6-SNAPSHOT:brwsr</goal>
     1.9              </goals>
    1.10          </action>
    1.11      </actions>
     2.1 --- a/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java	Sun Mar 31 12:01:38 2013 +0200
     2.2 +++ b/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java	Sun Mar 31 12:26:07 2013 +0200
     2.3 @@ -36,7 +36,7 @@
     2.4      @Property(name = "display", type = double.class),
     2.5      @Property(name = "operation", type = String.class),
     2.6      @Property(name = "hover", type = boolean.class),
     2.7 -    @Property(name = "history", type = double.class, array = true)
     2.8 +    @Property(name = "history", type = HistoryImpl.class, array = true)
     2.9  })
    2.10  public class Calc {
    2.11      static {
    2.12 @@ -74,19 +74,22 @@
    2.13              c.getDisplay()
    2.14          );
    2.15          c.setDisplay(newValue);
    2.16 -        if (!c.getHistory().contains(newValue)) {
    2.17 -            c.getHistory().add(newValue);
    2.18 +        if (!containsValue(c.getHistory(), newValue)) {
    2.19 +            History h = new History();
    2.20 +            h.setValue(newValue);
    2.21 +            h.setOperation(c.getOperation());
    2.22 +            c.getHistory().add(h);
    2.23          }
    2.24          c.setMemory(0);
    2.25      }
    2.26      
    2.27      @OnFunction
    2.28 -    static void recoverMemory(Calculator c, double data) {
    2.29 -        c.setDisplay(data);
    2.30 +    static void recoverMemory(Calculator c, History data) {
    2.31 +        c.setDisplay(data.getValue());
    2.32      }
    2.33  
    2.34      @OnFunction
    2.35 -    static void removeMemory(Calculator c, double data) {
    2.36 +    static void removeMemory(Calculator c, History data) {
    2.37          c.getHistory().remove(data);
    2.38      }
    2.39      
    2.40 @@ -131,4 +134,13 @@
    2.41      static boolean emptyHistory(List<?> history) {
    2.42          return history.isEmpty();
    2.43      }
    2.44 +
    2.45 +    private static boolean containsValue(List<History> arr, final double newValue) {
    2.46 +        for (History history : arr) {
    2.47 +            if (history.getValue() == newValue) {
    2.48 +                return true;
    2.49 +            }
    2.50 +        }
    2.51 +        return false;
    2.52 +    }
    2.53  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/HistoryImpl.java	Sun Mar 31 12:26:07 2013 +0200
     3.3 @@ -0,0 +1,37 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.demo.calc;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    3.24 +import org.apidesign.bck2brwsr.htmlpage.api.Model;
    3.25 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +@Model(className = "History", properties = {
    3.32 +    @Property(name = "value", type = double.class),
    3.33 +    @Property(name = "operation", type = String.class)
    3.34 +})
    3.35 +public class HistoryImpl {
    3.36 +    @ComputedProperty
    3.37 +    static String resultOf(String operation) {
    3.38 +        return "result of " + operation;
    3.39 +    }
    3.40 +}
     4.1 --- a/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml	Sun Mar 31 12:01:38 2013 +0200
     4.2 +++ b/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml	Sun Mar 31 12:26:07 2013 +0200
     4.3 @@ -83,9 +83,10 @@
     4.4          <div data-bind="if: emptyHistory">No results yet.</div>
     4.5          <ul data-bind="foreach: history">
     4.6              <li>
     4.7 -                <span data-bind="text: $data"></span> -
     4.8 +                <span data-bind="text: $data.value"></span> -
     4.9                  <a href="#" data-bind="click: $root.recoverMemory">Use</a>
    4.10 -                <a href="#" data-bind="click: $root.removeMemory">Remove</a>
    4.11 +                <a href="#" data-bind="click: $root.removeMemory">Remove</a> -
    4.12 +                <span data-bind="text: $data.resultOf"></span>
    4.13              </li>
    4.14          </ul>
    4.15