javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
branchmodel
changeset 510 aaf86ae88f46
parent 508 46fc57ff6553
child 512 1c3fda8898a1
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Mon Jan 21 15:33:32 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                      }