json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
branchunion
changeset 506 4b66c5141f84
parent 505 501413c8638c
     1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Wed Jan 29 15:49:48 2014 +0100
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Thu Jan 30 11:36:32 2014 +0100
     1.3 @@ -266,15 +266,15 @@
     1.4          w.append("  private static Class<" + inPckName(e) + "> modelFor() { return null; }\n");
     1.5          w.append("  private ").append(className).append("(net.java.html.BrwsrCtx context) {\n");
     1.6          w.append("    this.proto = TYPE.createProto(this, context);\n");
     1.7 -        for (Prprt p : props) {
     1.8 +        for (int i = 0; i < props.length; i++) {
     1.9 +            Prprt p = props[i];
    1.10              if (p.array()) {
    1.11                  final String tn = typeName(e, p);
    1.12                  String[] gs = toGetSet(p.name(), tn, p.array());
    1.13                  w.write("    this.prop_" + p.name() + " = proto.createList(\""
    1.14                      + p.name() + "\"");
    1.15                  if (functionDeps.containsKey(p.name())) {
    1.16 -                    int index = Arrays.asList(functionDeps.keySet().toArray()).indexOf(p.name());
    1.17 -                    w.write(", " + index);
    1.18 +                    w.write(", " + i);
    1.19                  } else {
    1.20                      w.write(", -1");
    1.21                  }
    1.22 @@ -404,9 +404,9 @@
    1.23          w.append("    @Override public void onChange(" + className + " model, int type) {\n");
    1.24          w.append("      switch (type) {\n");
    1.25          {
    1.26 -            String[] arr = functionDeps.keySet().toArray(new String[0]);
    1.27 -            for (int i = 0; i < arr.length; i++) {
    1.28 -                Collection<String> onChange = functionDeps.get(arr[i]);
    1.29 +            for (int i = 0; i < props.length; i++) {
    1.30 +                String pn = props[i].name();
    1.31 +                Collection<String> onChange = functionDeps.get(pn);
    1.32                  if (onChange != null) {
    1.33                      w.append("      case " + i + ":\n");
    1.34                      for (String call : onChange) {
    1.35 @@ -416,8 +416,7 @@
    1.36                  }
    1.37              }
    1.38          }
    1.39 -        w.append("    }\n");
    1.40 -        w.append("      throw new UnsupportedOperationException();\n");
    1.41 +        w.append("      }\n");
    1.42          w.append("    }\n");
    1.43          w.append(onReceiveType);
    1.44          w.append("    @Override public " + className + " read(net.java.html.BrwsrCtx c, Object json) { return new " + className + "(c, json); }\n");
    1.45 @@ -638,15 +637,15 @@
    1.46                  w.append("  }\n");
    1.47                  w.append("  private ").append(className).append("(net.java.html.BrwsrCtx context, Object union, Object json) {\n");
    1.48                  w.append("    this.proto = TYPE.createProto(this, context);\n");
    1.49 -                for (Prprt p : props) {
    1.50 +                for (int i = 0; i < props.length; i++) {
    1.51 +                    Prprt p = props[i];
    1.52                      if (p.array()) {
    1.53                          final String tn = typeName(e, p);
    1.54                          String[] gs = toGetSet(p.name(), tn, p.array());
    1.55                          w.write("    this.prop_" + p.name() + " = proto.createList(\""
    1.56                              + p.name() + "\"");
    1.57                          if (functionDeps.containsKey(p.name())) {
    1.58 -                            int index = Arrays.asList(functionDeps.keySet().toArray()).indexOf(p.name());
    1.59 -                            w.write(", " + index);
    1.60 +                            w.write(", " + i);
    1.61                          } else {
    1.62                              w.write(", -1");
    1.63                          }
    1.64 @@ -731,6 +730,7 @@
    1.65                      // end of enum
    1.66                      w.append(") {\n");
    1.67                      w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class), union);\n");
    1.68 +                    w.append("    ((" + em.className() + ")union).proto.onValueHasMutated(this.proto);\n");
    1.69                      for (Prprt p : props) {
    1.70                          if (p.array()) {
    1.71                              continue;
    1.72 @@ -827,9 +827,9 @@
    1.73                  w.append("    @Override public void onChange(" + className + " model, int type) {\n");
    1.74                  w.append("      switch (type) {\n");
    1.75                  {
    1.76 -                    String[] arr = functionDeps.keySet().toArray(new String[0]);
    1.77 -                    for (int i = 0; i < arr.length; i++) {
    1.78 -                        Collection<String> onChange = functionDeps.get(arr[i]);
    1.79 +                    for (int i = 0; i < allProps.size(); i++) {
    1.80 +                        String pn = allProps.get(i).name();
    1.81 +                        Collection<String> onChange = functionDeps.get(pn);
    1.82                          if (onChange != null) {
    1.83                              w.append("      case " + i + ":\n");
    1.84                              for (String call : onChange) {
    1.85 @@ -839,8 +839,7 @@
    1.86                          }
    1.87                      }
    1.88                  }
    1.89 -                w.append("    }\n");
    1.90 -                w.append("      throw new UnsupportedOperationException();\n");
    1.91 +                w.append("      }\n");
    1.92                  w.append("    }\n");
    1.93                  w.append(onReceiveType);
    1.94                  w.append("    @Override public " + className + " read(net.java.html.BrwsrCtx c, Object json) {\n");
    1.95 @@ -1011,6 +1010,7 @@
    1.96                      if (props.length > 0) {
    1.97                          w.write("    public " + className + "(" + em.className() + " union) {\n");
    1.98                          w.append("      this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class), union, null);\n");
    1.99 +                        w.append("      ((" + em.className() + ")union).proto.onValueHasMutated(this.proto);\n");
   1.100                          w.write("    }\n");
   1.101                      }
   1.102                      generateEnumConstantModel(w, ec, sharedProps);
   1.103 @@ -2010,6 +2010,7 @@
   1.104          w.write("  }\n");
   1.105          w.write("  private " + className + " clone(net.java.html.BrwsrCtx ctx) {\n");
   1.106          w.write("    Object newUnion = null;\n");
   1.107 +        w.write("    org.apidesign.html.json.spi.Proto newProto = null;\n");
   1.108          for (Element ec : e.getEnclosedElements()) {
   1.109              if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
   1.110                  continue;
   1.111 @@ -2018,9 +2019,13 @@
   1.112              if (em == null) {
   1.113                  continue;
   1.114              }
   1.115 -            w.write("    if (union instanceof " + em.className() + ") newUnion = ((" + em.className() + ")union).clone(ctx);\n");
   1.116 +            w.write("    if (union instanceof " + em.className() + ") {\n");
   1.117 +            w.write("      newUnion = ((" + em.className() + ")union).clone(ctx);\n");
   1.118 +            w.write("      newProto = ((" + em.className() + ")newUnion).proto;\n");
   1.119 +            w.write("    }\n");
   1.120          }
   1.121          w.write("    " + className + " ret = new " + className + "(ctx, newUnion);\n");
   1.122 +        w.write("    newProto.onValueHasMutated(ret.proto);\n");
   1.123          for (Prprt p : props) {
   1.124              if (!p.array()) {
   1.125                  boolean isModel[] = { false };