1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Jan 21 15:56:54 2013 +0100
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Jan 21 15:57:30 2013 +0100
1.3 @@ -97,7 +97,7 @@
1.4 w.append("final class ").append(className).append(" {\n");
1.5 w.append(" private static boolean locked;\n");
1.6 w.append(" public ").append(className).append("() {\n");
1.7 - if (!initializeOnClick((TypeElement) e, w, pp)) {
1.8 + if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
1.9 return false;
1.10 }
1.11 w.append(" }\n");
1.12 @@ -169,7 +169,9 @@
1.13 return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
1.14 }
1.15
1.16 - private boolean initializeOnClick(TypeElement type, Writer w, ProcessPage pp) throws IOException {
1.17 + private boolean initializeOnClick(
1.18 + String className, TypeElement type, Writer w, ProcessPage pp
1.19 + ) throws IOException {
1.20 TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
1.21 { //for (Element clazz : pe.getEnclosedElements()) {
1.22 // if (clazz.getKind() != ElementKind.CLASS) {
1.23 @@ -184,15 +186,28 @@
1.24 return false;
1.25 }
1.26 ExecutableElement ee = (ExecutableElement)method;
1.27 - boolean hasParam;
1.28 - if (ee.getParameters().isEmpty()) {
1.29 - hasParam = false;
1.30 - } else {
1.31 - if (ee.getParameters().size() != 1 || ee.getParameters().get(0).asType() != stringType) {
1.32 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method should either have no arguments or one String argument", ee);
1.33 + StringBuilder params = new StringBuilder();
1.34 + {
1.35 + boolean first = true;
1.36 + for (VariableElement ve : ee.getParameters()) {
1.37 + if (!first) {
1.38 + params.append(", ");
1.39 + }
1.40 + first = false;
1.41 + if (ve.asType() == stringType) {
1.42 + params.append('"').append(id).append('"');
1.43 + continue;
1.44 + }
1.45 + if (ve.asType().toString().equals(className)) {
1.46 + params.append(className).append(".this");
1.47 + continue;
1.48 + }
1.49 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
1.50 + "@On method can only accept String or " + className + " arguments",
1.51 + ee
1.52 + );
1.53 return false;
1.54 }
1.55 - hasParam = true;
1.56 }
1.57 if (!ee.getModifiers().contains(Modifier.STATIC)) {
1.58 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee);
1.59 @@ -206,9 +221,7 @@
1.60 append(").perform(new Runnable() { public void run() {\n");
1.61 w.append(" ").append(type.getSimpleName().toString()).
1.62 append('.').append(ee.getSimpleName()).append("(");
1.63 - if (hasParam) {
1.64 - w.append("\"").append(id).append("\"");
1.65 - }
1.66 + w.append(params);
1.67 w.append(");\n");
1.68 w.append(" }});\n");
1.69 }
2.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Mon Jan 21 15:56:54 2013 +0100
2.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Mon Jan 21 15:57:30 2013 +0100
2.3 @@ -46,8 +46,11 @@
2.4 private static final TestPage PAGE = new TestPage();
2.5
2.6 @On(event = CLICK, id="pg.button")
2.7 - static void updateTitle() {
2.8 - PAGE.PG_TITLE.setText("You want this window to be named " + PAGE.PG_TEXT.getValue());
2.9 + static void updateTitle(TestPage ref) {
2.10 + if (PAGE != ref) {
2.11 + throw new IllegalStateException("Both references should be the same. " + ref + " != " + PAGE);
2.12 + }
2.13 + ref.PG_TITLE.setText("You want this window to be named " + ref.PG_TEXT.getValue());
2.14 }
2.15
2.16 @On(event = CLICK, id={ "pg.title", "pg.text" })
3.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 15:56:54 2013 +0100
3.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Mon Jan 21 15:57:30 2013 +0100
3.3 @@ -36,39 +36,41 @@
3.4 @Property(name = "hover", type = boolean.class)
3.5 })
3.6 public class App {
3.7 - private static final Calculator CALC = new Calculator().applyBindings();
3.8 + static {
3.9 + new Calculator().applyBindings();
3.10 + }
3.11
3.12 @On(event = CLICK, id="clear")
3.13 - static void clear() {
3.14 - CALC.setMemory(0);
3.15 - CALC.setOperation(null);
3.16 - CALC.setDisplay(0);
3.17 + static void clear(Calculator c) {
3.18 + c.setMemory(0);
3.19 + c.setOperation(null);
3.20 + c.setDisplay(0);
3.21 }
3.22
3.23 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
3.24 - static void applyOp(String op) {
3.25 - CALC.setMemory(CALC.getDisplay());
3.26 - CALC.setOperation(op);
3.27 - CALC.setDisplay(0);
3.28 + static void applyOp(Calculator c, String op) {
3.29 + c.setMemory(c.getDisplay());
3.30 + c.setOperation(op);
3.31 + c.setDisplay(0);
3.32 }
3.33
3.34 @On(event = MOUSE_OVER, id= { "result" })
3.35 - static void attemptingIn(String op) {
3.36 - CALC.setHover(true);
3.37 + static void attemptingIn(Calculator c, String op) {
3.38 + c.setHover(true);
3.39 }
3.40 @On(event = MOUSE_OUT, id= { "result" })
3.41 - static void attemptingOut(String op) {
3.42 - CALC.setHover(false);
3.43 + static void attemptingOut(Calculator c, String op) {
3.44 + c.setHover(false);
3.45 }
3.46
3.47 @On(event = CLICK, id="result")
3.48 - static void computeTheValue() {
3.49 - CALC.setDisplay(compute(
3.50 - CALC.getOperation(),
3.51 - CALC.getMemory(),
3.52 - CALC.getDisplay()
3.53 + static void computeTheValue(Calculator c) {
3.54 + c.setDisplay(compute(
3.55 + c.getOperation(),
3.56 + c.getMemory(),
3.57 + c.getDisplay()
3.58 ));
3.59 - CALC.setMemory(0);
3.60 + c.setMemory(0);
3.61 }
3.62
3.63 private static double compute(String op, double memory, double display) {
3.64 @@ -82,19 +84,19 @@
3.65 }
3.66
3.67 @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
3.68 - static void addDigit(String digit) {
3.69 + static void addDigit(String digit, Calculator c) {
3.70 digit = digit.substring(1);
3.71
3.72 - double v = CALC.getDisplay();
3.73 + double v = c.getDisplay();
3.74 if (v == 0.0) {
3.75 - CALC.setDisplay(Integer.parseInt(digit));
3.76 + c.setDisplay(Integer.parseInt(digit));
3.77 } else {
3.78 String txt = Double.toString(v);
3.79 if (txt.endsWith(".0")) {
3.80 txt = txt.substring(0, txt.length() - 2);
3.81 }
3.82 txt = txt + digit;
3.83 - CALC.setDisplay(Double.parseDouble(txt));
3.84 + c.setDisplay(Double.parseDouble(txt));
3.85 }
3.86 }
3.87