javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
branchmodel
changeset 492 854286e49061
parent 491 14268cd404a4
child 496 a06c98795b01
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Sun Jan 20 14:29:10 2013 +0100
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Sun Jan 20 18:20:18 2013 +0100
     1.3 @@ -92,6 +92,7 @@
     1.4                      w.append("package " + pkg + ";\n");
     1.5                      w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
     1.6                      w.append("class ").append(className).append(" {\n");
     1.7 +                    w.append("  private ").append(className).append("() {}\n");
     1.8                      for (String id : pp.ids()) {
     1.9                          String tag = pp.tagNameForId(id);
    1.10                          String type = type(tag);
    1.11 @@ -104,8 +105,27 @@
    1.12                          return false;
    1.13                      }
    1.14                      w.append("  }\n");
    1.15 -                    generateProperties(w, p.properties());
    1.16 -                    generateComputedProperties(w, e.getEnclosedElements());
    1.17 +                    List<String> propsGetSet = new ArrayList<String>();
    1.18 +                    generateProperties(w, p.properties(), propsGetSet);
    1.19 +                    generateComputedProperties(w, e.getEnclosedElements(), propsGetSet);
    1.20 +                    if (!propsGetSet.isEmpty()) {
    1.21 +                        w.write("public static void applyBindings() {\n");
    1.22 +                        w.write("  org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(");
    1.23 +                        w.write(className + ".class, new " + className + "(), ");
    1.24 +                        w.write("new String[] {\n");
    1.25 +                        String sep = "";
    1.26 +                        for (String n : propsGetSet) {
    1.27 +                            w.write(sep);
    1.28 +                            if (n == null) {
    1.29 +                                w.write("    null");
    1.30 +                            } else {
    1.31 +                                w.write("    \"" + n + "\"");
    1.32 +                            }
    1.33 +                            sep = ",\n";
    1.34 +                        }
    1.35 +                        w.write("\n  });\n}\n");
    1.36 +                        //w.write("static { applyBindings(); }\n");
    1.37 +                    }
    1.38                      w.append("}\n");
    1.39                  } finally {
    1.40                      w.close();
    1.41 @@ -237,11 +257,13 @@
    1.42          return e.getEnclosingElement();
    1.43      }
    1.44  
    1.45 -    private static void generateProperties(Writer w, Property[] properties) throws IOException {
    1.46 +    private static void generateProperties(
    1.47 +        Writer w, Property[] properties, Collection<String> props
    1.48 +    ) throws IOException {
    1.49          for (Property p : properties) {
    1.50 -            String[] gs = toGetSet(p.name(), null);
    1.51 +            final String tn = typeName(p);
    1.52 +            String[] gs = toGetSet(p.name(), tn);
    1.53  
    1.54 -            final String tn = typeName(p);
    1.55              w.write("private static " + tn + " prop_" + p.name() + ";\n");
    1.56              w.write("public static " + tn + " " + gs[0] + "() {\n");
    1.57              w.write("  return prop_" + p.name() + ";\n");
    1.58 @@ -249,10 +271,16 @@
    1.59              w.write("public static void " + gs[1] + "(" + tn + " v) {\n");
    1.60              w.write("  prop_" + p.name() + " = v;\n");
    1.61              w.write("}\n");
    1.62 +            
    1.63 +            props.add(p.name());
    1.64 +            props.add(gs[2]);
    1.65 +            props.add(gs[3]);
    1.66          }
    1.67      }
    1.68  
    1.69 -    private void generateComputedProperties(Writer w, Collection<? extends Element> arr) throws IOException {
    1.70 +    private void generateComputedProperties(
    1.71 +        Writer w, Collection<? extends Element> arr, Collection<String> props
    1.72 +    ) throws IOException {
    1.73          for (Element e : arr) {
    1.74              if (e.getKind() != ElementKind.METHOD) {
    1.75                  continue;
    1.76 @@ -261,30 +289,47 @@
    1.77                  continue;
    1.78              }
    1.79              ExecutableElement ee = (ExecutableElement)e;
    1.80 -            String[] gs = toGetSet(ee.getSimpleName().toString(), null);
    1.81 +            final String tn = ee.getReturnType().toString();
    1.82 +            String[] gs = toGetSet(ee.getSimpleName().toString(), tn);
    1.83  
    1.84 -            final String tn = ee.getReturnType().toString();
    1.85              w.write("public static " + tn + " " + gs[0] + "() {\n");
    1.86              w.write("  return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
    1.87              String sep = "";
    1.88              for (VariableElement pe : ee.getParameters()) {
    1.89 -                String[] call = toGetSet(pe.getSimpleName().toString(), null);
    1.90 +                String[] call = toGetSet(pe.getSimpleName().toString(), pe.asType().toString());
    1.91                  w.write(sep);
    1.92                  w.write(call[0] + "()");
    1.93                  sep = ", ";
    1.94              }
    1.95              w.write(");\n");
    1.96              w.write("}\n");
    1.97 +            
    1.98 +            props.add(e.getSimpleName().toString());
    1.99 +            props.add(gs[2]);
   1.100 +            props.add(null);
   1.101          }
   1.102      }
   1.103  
   1.104 -    private static String[] toGetSet(String name, Object type) {
   1.105 +    private static String[] toGetSet(String name, String type) {
   1.106          String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
   1.107 -//        if (p.type() == boolean.class) {
   1.108 -//            return new String[] { "is" + n, "set" + n };
   1.109 -//        } else {
   1.110 -        return new String[]{"get" + n, "set" + n};
   1.111 -//        }
   1.112 +        String bck2brwsrType = "L" + type.replace('.', '_') + "_2";
   1.113 +        if ("int".equals(type)) {
   1.114 +            bck2brwsrType = "I";
   1.115 +        }
   1.116 +        if ("double".equals(type)) {
   1.117 +            bck2brwsrType = "D";
   1.118 +        }
   1.119 +        String pref = "get";
   1.120 +        if ("boolean".equals(type)) {
   1.121 +            pref = "is";
   1.122 +            bck2brwsrType = "Z";
   1.123 +        }
   1.124 +        return new String[]{
   1.125 +            pref + n, 
   1.126 +            "set" + n, 
   1.127 +            pref + n + "__" + bck2brwsrType,
   1.128 +            "set" + n + "__V" + bck2brwsrType
   1.129 +        };
   1.130      }
   1.131  
   1.132      private static String typeName(Property p) {