# HG changeset patch # User Jaroslav Tulach # Date 1358780250 -3600 # Node ID aaf86ae88f4646f850e49ac744ee9483d05fcfd3 # Parent ff9e65d1748b8c7a0038f02684432c1380ba6e11 Injection of model diff -r ff9e65d1748b -r aaf86ae88f46 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Jan 21 15:56:54 2013 +0100 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Jan 21 15:57:30 2013 +0100 @@ -97,7 +97,7 @@ w.append("final class ").append(className).append(" {\n"); w.append(" private static boolean locked;\n"); w.append(" public ").append(className).append("() {\n"); - if (!initializeOnClick((TypeElement) e, w, pp)) { + if (!initializeOnClick(className, (TypeElement) e, w, pp)) { return false; } w.append(" }\n"); @@ -169,7 +169,9 @@ return id.toUpperCase(Locale.ENGLISH).replace('.', '_'); } - private boolean initializeOnClick(TypeElement type, Writer w, ProcessPage pp) throws IOException { + private boolean initializeOnClick( + String className, TypeElement type, Writer w, ProcessPage pp + ) throws IOException { TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType(); { //for (Element clazz : pe.getEnclosedElements()) { // if (clazz.getKind() != ElementKind.CLASS) { @@ -184,15 +186,28 @@ return false; } ExecutableElement ee = (ExecutableElement)method; - boolean hasParam; - if (ee.getParameters().isEmpty()) { - hasParam = false; - } else { - if (ee.getParameters().size() != 1 || ee.getParameters().get(0).asType() != stringType) { - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method should either have no arguments or one String argument", ee); + StringBuilder params = new StringBuilder(); + { + boolean first = true; + for (VariableElement ve : ee.getParameters()) { + if (!first) { + params.append(", "); + } + first = false; + if (ve.asType() == stringType) { + params.append('"').append(id).append('"'); + continue; + } + if (ve.asType().toString().equals(className)) { + params.append(className).append(".this"); + continue; + } + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, + "@On method can only accept String or " + className + " arguments", + ee + ); return false; } - hasParam = true; } if (!ee.getModifiers().contains(Modifier.STATIC)) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee); @@ -206,9 +221,7 @@ append(").perform(new Runnable() { public void run() {\n"); w.append(" ").append(type.getSimpleName().toString()). append('.').append(ee.getSimpleName()).append("("); - if (hasParam) { - w.append("\"").append(id).append("\""); - } + w.append(params); w.append(");\n"); w.append(" }});\n"); } diff -r ff9e65d1748b -r aaf86ae88f46 javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Mon Jan 21 15:56:54 2013 +0100 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Mon Jan 21 15:57:30 2013 +0100 @@ -46,8 +46,11 @@ private static final TestPage PAGE = new TestPage(); @On(event = CLICK, id="pg.button") - static void updateTitle() { - PAGE.PG_TITLE.setText("You want this window to be named " + PAGE.PG_TEXT.getValue()); + static void updateTitle(TestPage ref) { + if (PAGE != ref) { + throw new IllegalStateException("Both references should be the same. " + ref + " != " + PAGE); + } + ref.PG_TITLE.setText("You want this window to be named " + ref.PG_TEXT.getValue()); } @On(event = CLICK, id={ "pg.title", "pg.text" }) diff -r ff9e65d1748b -r aaf86ae88f46 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 Mon Jan 21 15:56:54 2013 +0100 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 15:57:30 2013 +0100 @@ -36,39 +36,41 @@ @Property(name = "hover", type = boolean.class) }) public class App { - private static final Calculator CALC = new Calculator().applyBindings(); + static { + new Calculator().applyBindings(); + } @On(event = CLICK, id="clear") - static void clear() { - CALC.setMemory(0); - CALC.setOperation(null); - CALC.setDisplay(0); + static void clear(Calculator c) { + c.setMemory(0); + c.setOperation(null); + c.setDisplay(0); } @On(event = CLICK, id= { "plus", "minus", "mul", "div" }) - static void applyOp(String op) { - CALC.setMemory(CALC.getDisplay()); - CALC.setOperation(op); - CALC.setDisplay(0); + static void applyOp(Calculator c, String op) { + c.setMemory(c.getDisplay()); + c.setOperation(op); + c.setDisplay(0); } @On(event = MOUSE_OVER, id= { "result" }) - static void attemptingIn(String op) { - CALC.setHover(true); + static void attemptingIn(Calculator c, String op) { + c.setHover(true); } @On(event = MOUSE_OUT, id= { "result" }) - static void attemptingOut(String op) { - CALC.setHover(false); + static void attemptingOut(Calculator c, String op) { + c.setHover(false); } @On(event = CLICK, id="result") - static void computeTheValue() { - CALC.setDisplay(compute( - CALC.getOperation(), - CALC.getMemory(), - CALC.getDisplay() + static void computeTheValue(Calculator c) { + c.setDisplay(compute( + c.getOperation(), + c.getMemory(), + c.getDisplay() )); - CALC.setMemory(0); + c.setMemory(0); } private static double compute(String op, double memory, double display) { @@ -82,19 +84,19 @@ } @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) - static void addDigit(String digit) { + static void addDigit(String digit, Calculator c) { digit = digit.substring(1); - double v = CALC.getDisplay(); + double v = c.getDisplay(); if (v == 0.0) { - CALC.setDisplay(Integer.parseInt(digit)); + c.setDisplay(Integer.parseInt(digit)); } else { String txt = Double.toString(v); if (txt.endsWith(".0")) { txt = txt.substring(0, txt.length() - 2); } txt = txt + digit; - CALC.setDisplay(Double.parseDouble(txt)); + c.setDisplay(Double.parseDouble(txt)); } }