# HG changeset patch # User Jaroslav Tulach # Date 1358748056 -3600 # Node ID 607f062485cc04a4424107b038b68cbce2d7327f # Parent a06c98795b013a88c1d629b615c88127f7cab85d Notify change in computed properties diff -r a06c98795b01 -r 607f062485cc javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Sun Jan 20 21:01:46 2013 +0100 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Jan 21 07:00:56 2013 +0100 @@ -24,8 +24,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Set; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Completion; @@ -106,8 +109,9 @@ } w.append(" }\n"); List propsGetSet = new ArrayList(); - generateProperties(w, p.properties(), propsGetSet); - generateComputedProperties(w, e.getEnclosedElements(), propsGetSet); + Map> propsDeps = new HashMap>(); + generateComputedProperties(w, e.getEnclosedElements(), propsGetSet, propsDeps); + generateProperties(w, p.properties(), propsGetSet, propsDeps); w.append(" private static org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n"); if (!propsGetSet.isEmpty()) { w.write("public static void applyBindings() {\n"); @@ -259,7 +263,8 @@ } private static void generateProperties( - Writer w, Property[] properties, Collection props + Writer w, Property[] properties, Collection props, + Map> deps ) throws IOException { for (Property p : properties) { final String tn = typeName(p); @@ -271,7 +276,15 @@ w.write("}\n"); w.write("public static void " + gs[1] + "(" + tn + " v) {\n"); w.write(" prop_" + p.name() + " = v;\n"); - w.write(" if (ko != null) ko.valueHasMutated(\"" + p.name() + "\");\n"); + w.write(" if (ko != null) {\n"); + w.write(" ko.valueHasMutated(\"" + p.name() + "\");\n"); + final Collection dependants = deps.get(p.name()); + if (dependants != null) { + for (String depProp : dependants) { + w.write(" ko.valueHasMutated(\"" + depProp + "\");\n"); + } + } + w.write(" }\n"); w.write("}\n"); props.add(p.name()); @@ -280,8 +293,9 @@ } } - private void generateComputedProperties( - Writer w, Collection arr, Collection props + private boolean generateComputedProperties( + Writer w, Collection arr, Collection props, + Map> deps ) throws IOException { for (Element e : arr) { if (e.getKind() != ElementKind.METHOD) { @@ -292,16 +306,25 @@ } ExecutableElement ee = (ExecutableElement)e; final String tn = ee.getReturnType().toString(); - String[] gs = toGetSet(ee.getSimpleName().toString(), tn); - + final String sn = ee.getSimpleName().toString(); + String[] gs = toGetSet(sn, tn); + w.write("public static " + tn + " " + gs[0] + "() {\n"); w.write(" return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "("); String sep = ""; for (VariableElement pe : ee.getParameters()) { - String[] call = toGetSet(pe.getSimpleName().toString(), pe.asType().toString()); + final String dn = pe.getSimpleName().toString(); + String[] call = toGetSet(dn, pe.asType().toString()); w.write(sep); w.write(call[0] + "()"); sep = ", "; + + Collection depends = deps.get(dn); + if (depends == null) { + depends = new LinkedHashSet(); + deps.put(dn, depends); + } + depends.add(sn); } w.write(");\n"); w.write("}\n"); @@ -310,6 +333,8 @@ props.add(gs[2]); props.add(null); } + + return true; } private static String[] toGetSet(String name, String type) { @@ -326,11 +351,12 @@ pref = "is"; bck2brwsrType = "Z"; } + final String nu = n.replace('.', '_'); return new String[]{ pref + n, "set" + n, - pref + n + "__" + bck2brwsrType, - "set" + n + "__V" + bck2brwsrType + pref + nu + "__" + bck2brwsrType, + "set" + nu + "__V" + bck2brwsrType }; } @@ -338,10 +364,7 @@ try { return p.type().getName(); } catch (MirroredTypeException ex) { - if (ex.getTypeMirror().getKind().isPrimitive()) { - return ex.getTypeMirror().toString(); - } - throw ex; + return ex.getTypeMirror().toString(); } } } diff -r a06c98795b01 -r 607f062485cc javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Sun Jan 20 21:01:46 2013 +0100 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 07:00:56 2013 +0100 @@ -17,6 +17,7 @@ */ package org.apidesign.bck2brwsr.mavenhtml; +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty; import org.apidesign.bck2brwsr.htmlpage.api.On; import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*; import org.apidesign.bck2brwsr.htmlpage.api.Page; @@ -29,34 +30,52 @@ * @author Jaroslav Tulach */ @Page(xhtml="Calculator.xhtml", properties = { - @Property(name = "display", type = double.class) + @Property(name = "memory", type = double.class), + @Property(name = "display", type = double.class), + @Property(name = "operation", type = String.class), + @Property(name = "hover", type = boolean.class) }) public class App { - private static double memory; - private static String operation; - @On(event = CLICK, id="clear") static void clear() { - memory = 0; - operation = null; + Calculator.setMemory(0); + Calculator.setOperation(null); Calculator.setDisplay(0); } @On(event = CLICK, id= { "plus", "minus", "mul", "div" }) static void applyOp(String op) { - memory = Calculator.getDisplay(); - operation = op; + Calculator.setMemory(Calculator.getDisplay()); + Calculator.setOperation(op); Calculator.setDisplay(0); } + + @On(event = MOUSE_OVER, id= { "result" }) + static void attemptingIn(String op) { + Calculator.setHover(true); + } + @On(event = MOUSE_OUT, id= { "result" }) + static void attemptingOut(String op) { + Calculator.setHover(false); + } @On(event = CLICK, id="result") static void computeTheValue() { - switch (operation) { - case "plus": Calculator.setDisplay(memory + Calculator.getDisplay()); break; - case "minus": Calculator.setDisplay(memory - Calculator.getDisplay()); break; - case "mul": Calculator.setDisplay(memory * Calculator.getDisplay()); break; - case "div": Calculator.setDisplay(memory / Calculator.getDisplay()); break; - default: throw new IllegalStateException(operation); + Calculator.setDisplay(compute( + Calculator.getOperation(), + Calculator.getMemory(), + Calculator.getDisplay() + )); + Calculator.setMemory(0); + } + + private static double compute(String op, double memory, double display) { + switch (op) { + case "plus": return memory + display; + case "minus": return memory - display; + case "mul": return memory * display; + case "div": return memory / display; + default: throw new IllegalStateException(op); } } @@ -76,10 +95,18 @@ Calculator.setDisplay(Double.parseDouble(txt)); } } - + + @ComputedProperty + public static String displayPreview( + double display, boolean hover, double memory, String operation + ) { + if (!hover) { + return "Type numbers and perform simple operations! Press '=' to get result."; + } + return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display); + } static { - Calculator.setDisplay(10.0); Calculator.applyBindings(); } } diff -r a06c98795b01 -r 607f062485cc javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Sun Jan 20 21:01:46 2013 +0100 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Mon Jan 21 07:00:56 2013 +0100 @@ -76,7 +76,9 @@ - +

+

+