Revision: 8675
Author: rj...@google.com
Date: Tue Aug 31 07:18:57 2010
Log: Changes required for reliable editing of "ui:field" attribute in
template and corresponding @UiField in Java. Solved problems:

1. When GWT Designer calls createAndBindUi(), it can not provide
"owner", because we can not instantiate it, and it is not interface,
so we can not mock it too. So, we use "null" and this causes NPE
during assigning widget into fields. At design time we don't need
these fields, so null check is probably safe.

2. At design time we create ClassLoader only once and can not refresh
Java type.  So, when user asks to remove "ui:field" we update both
template and Java, but generator does not know about Java change. Same
for rename.

Patch by konstantin.scheg...@gmail.com
Review by rj...@google.com

http://gwt-code-reviews.appspot.com/792801/show

Review by: robertvaw...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=8675

Modified:
 /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtils.java
 /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsImpl.java
 /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsStub.java
 /trunk/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java
 /trunk/user/test/com/google/gwt/uibinder/rebind/DesignTimeUtilsTest.java

=======================================
--- /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtils.java Fri Jul 23 14:20:15 2010 +++ /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtils.java Tue Aug 31 07:18:57 2010
@@ -33,6 +33,13 @@
    */
   String getImplName(String implName);

+  /**
+ * @return the source to check that "owner" is not <code>null</code>. Problem + * is that at design time we render template without owner, so can not
+   *         provide it.
+   */
+  String getOwnerCheck();
+
   /**
    * @return the path of given {...@link Element}.
    */
@@ -66,6 +73,16 @@
    */
   void rememberPathForElements(Document doc);

+  /**
+   * @return <code>true</code> if absence of "ui:field" attribute for
+   *         corresponding "@UiField" declaration is OK. Problem is that at
+ * design time we create {...@link ClassLoader} only once and can not
+   *         refresh Java type. So, when user asks to remove "ui:field" we
+ * update both template and Java, but generator does not know about
+   *         Java change.
+   */
+  boolean shouldIgnoreNoUiFieldAttribute();
+
   /**
    * Writes remembered values of attributes.
    */
=======================================
--- /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsImpl.java Fri Jul 23 14:20:15 2010 +++ /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsImpl.java Tue Aug 31 07:18:57 2010
@@ -50,6 +50,10 @@
   public String getImplName(String implName) {
     return implName + "_designTime" + System.currentTimeMillis();
   }
+
+  public String getOwnerCheck() {
+    return "if (owner != null) ";
+  }

   public String getPath(Element element) {
     return elementPaths.get(element);
@@ -91,6 +95,10 @@
   public void rememberPathForElements(Document doc) {
     rememberPathForElements(doc.getDocumentElement(), "0");
   }
+
+  public boolean shouldIgnoreNoUiFieldAttribute() {
+    return true;
+  }

   public void writeAttributes(Statements writer) {
     for (Map.Entry<String, String> entry : attributes.entrySet()) {
=======================================
--- /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsStub.java Fri Jul 23 14:20:15 2010 +++ /trunk/user/src/com/google/gwt/uibinder/rebind/DesignTimeUtilsStub.java Tue Aug 31 07:18:57 2010
@@ -30,6 +30,10 @@
   public String getImplName(String implName) {
     return implName;
   }
+
+  public String getOwnerCheck() {
+    return "";
+  }

   public String getPath(Element element) {
     return null;
@@ -51,6 +55,10 @@

   public void rememberPathForElements(Document doc) {
   }
+
+  public boolean shouldIgnoreNoUiFieldAttribute() {
+    return false;
+  }

   public void writeAttributes(Statements writer) {
   }
=======================================
--- /trunk/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java Thu Jul 29 11:09:15 2010 +++ /trunk/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java Tue Aug 31 07:18:57 2010
@@ -891,8 +891,8 @@
       /*
        * And initialize the field
        */
-      niceWriter.write("owner.%1$s = %2$s;", ownerField.getName(),
-          templateField);
+      niceWriter.write(designTime.getOwnerCheck() + "owner.%1$s = %2$s;",
+          ownerField.getName(), templateField);
     } else {
       /*
* But with @UiField(provided=true) the user builds it, so reverse the
@@ -1149,9 +1149,11 @@

       } else {
         // ownerField was not found as bundle resource or widget, must die.
-        die("Template %s has no %s attribute for %s.%s#%s", templatePath,
-            getUiFieldAttributeName(), uiOwnerType.getPackage().getName(),
-            uiOwnerType.getName(), fieldName);
+        if (!designTime.shouldIgnoreNoUiFieldAttribute()) {
+          die("Template %s has no %s attribute for %s.%s#%s", templatePath,
+ getUiFieldAttributeName(), uiOwnerType.getPackage().getName(),
+              uiOwnerType.getName(), fieldName);
+        }
       }
     }
   }
=======================================
--- /trunk/user/test/com/google/gwt/uibinder/rebind/DesignTimeUtilsTest.java Thu Jul 29 11:09:15 2010 +++ /trunk/user/test/com/google/gwt/uibinder/rebind/DesignTimeUtilsTest.java Tue Aug 31 07:18:57 2010
@@ -183,6 +183,20 @@
"if (dtObjectHandler != null) dtObjectHandler.handle(\"0/0\", myField);",
         statements.get(0));
   }
+
+  /**
+   * Test for {...@link DesignTimeUtils#getOwnerCheck()}.
+   */
+  public void test_getOwnerCheck_default() throws Exception {
+    assertEquals("", stub.getOwnerCheck());
+  }
+
+  /**
+   * Test for {...@link DesignTimeUtils#getOwnerCheck()}.
+   */
+  public void test_getOwnerCheck_designTime() throws Exception {
+    assertEquals("if (owner != null) ", impl.getOwnerCheck());
+  }

   /**
* Test for {...@link DesignTimeUtils#putAttribute(XMLElement, String, String)}
@@ -310,4 +324,18 @@
       statements.add(String.format(format, args));
     }
   };
-}
+
+  /**
+   * Test for {...@link DesignTimeUtils#shouldIgnoreNoUiFieldAttribute()}.
+   */
+ public void test_shouldIgnoreNoUiFieldAttribute_default() throws Exception {
+    assertFalse(stub.shouldIgnoreNoUiFieldAttribute());
+  }
+
+  /**
+   * Test for {...@link DesignTimeUtils#shouldIgnoreNoUiFieldAttribute()}.
+   */
+ public void test_shouldIgnoreNoUiFieldAttribute_designTime() throws Exception {
+    assertTrue(impl.shouldIgnoreNoUiFieldAttribute());
+  }
+}

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to