]> source.dussan.org Git - gwtquery.git/commitdiff
moving back WidgetsUtils, new class GqUi to access protected methods of ui package...
authorManolo Carrasco <manolo@apache.org>
Wed, 9 Mar 2011 18:02:14 +0000 (18:02 +0000)
committerManolo Carrasco <manolo@apache.org>
Wed, 9 Mar 2011 18:02:14 +0000 (18:02 +0000)
16 files changed:
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/DateBoxWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/DisclosurePanelWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ListBoxWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/PasswordTextBoxWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/RichTextWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/SuggestBoxWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TabPanelWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TextBoxWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/user/client/ui/GqUi.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/user/client/ui/WidgetsUtils.java [deleted file]
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index 0b516b22dbc282c45faffb5de12468f80ae81a3a..86623df4a10615863fc11168d4d82cf78e53ed42 100644 (file)
@@ -47,10 +47,12 @@ import com.google.gwt.query.client.js.JsNodeArray;
 import com.google.gwt.query.client.js.JsUtils;\r
 import com.google.gwt.query.client.plugins.Plugin;\r
 import com.google.gwt.query.client.plugins.events.EventsListener;\r
+import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;\r
 import com.google.gwt.user.client.DOM;\r
 import com.google.gwt.user.client.Event;\r
 import com.google.gwt.user.client.EventListener;\r
 import com.google.gwt.user.client.Window;\r
+import com.google.gwt.user.client.ui.GqUi;\r
 import com.google.gwt.user.client.ui.Widget;\r
 \r
 import java.util.ArrayList;\r
@@ -1061,14 +1063,22 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * whenever you create a new iframe and you want to add dynamic content to it.\r
    */\r
   public GQuery empty() {\r
-    // TODO: add memory leak cleanup, remove event handlers, and\r
-    // data caches\r
+    // TODO: add memory leak cleanup, remove event handlers\r
     for (Element e : elements()) {\r
       if (e.getNodeType() == Element.DOCUMENT_NODE) {\r
         emptyDocument(e.<Document> cast());\r
       } else {\r
-        while (e.getFirstChild() != null) {\r
-          e.removeChild(e.getFirstChild());\r
+        Node c = e.getFirstChild();\r
+        while (c != null) {\r
+          $(c).unbind(0);\r
+          removeData(c.<Element>cast(), null);\r
+          Widget w = getAssociatedWidget(e);\r
+          if (w != null) {\r
+            w.removeFromParent();\r
+          } else {\r
+            e.removeChild(c);\r
+            c = e.getFirstChild();\r
+          }\r
         }\r
       }\r
     }\r
@@ -1957,9 +1967,12 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    */\r
   public GQuery remove() {\r
     for (Element e : elements()) {\r
-      // TODO: cleanup event bindings\r
+      $(e).unbind(0);\r
       removeData(e, null);\r
-      if (e.getParentNode() != null) {\r
+      Widget w = getAssociatedWidget(e);\r
+      if (w != null && w.isAttached()) {\r
+        w.removeFromParent();\r
+      } else if (e.getParentNode() != null) {\r
         e.getParentNode().removeChild(e);\r
       }\r
     }\r
@@ -2834,8 +2847,10 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       // e.getOwnerDocument();\r
       if (e.getNodeType() == Node.DOCUMENT_NODE) {\r
         e = e.<Document> cast().getBody();\r
-      }\r
+      }  \r
       for (int j = 0; j < g.size(); j++) {\r
+        Widget w = getAssociatedWidget(g.get(j));\r
+        GqUi.detachWidget(w);\r
         Node n = g.get(j);\r
         if (g.size() > 1) {\r
           n = n.cloneNode(true);\r
@@ -2855,6 +2870,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
             newNodes.addNode(e.getParentNode().insertBefore(n, e));\r
             break;\r
         }\r
+        GqUi.attachWidget(w);\r
       }\r
     }\r
     if (newNodes.size() > g.size()) {\r
index 3df324dce50a83cca11f3b3db9c56e3f436280e0..b32fc82ce91d44cc708c6a051754649bb54b7b23 100755 (executable)
@@ -23,6 +23,7 @@ import com.google.gwt.query.client.GQuery;
 import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.DateBoxWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.DisclosurePanelWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;\r
 import com.google.gwt.query.client.plugins.widgets.DisclosurePanelWidgetFactory.DisclosurePanelOptions;\r
 import com.google.gwt.query.client.plugins.widgets.ListBoxWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.ListBoxWidgetFactory.ListBoxOptions;\r
@@ -43,7 +44,6 @@ import com.google.gwt.user.client.ui.SuggestBox;
 import com.google.gwt.user.client.ui.TabPanel;\r
 import com.google.gwt.user.client.ui.TextBox;\r
 import com.google.gwt.user.client.ui.Widget;\r
-import com.google.gwt.user.client.ui.WidgetsUtils;\r
 import com.google.gwt.user.datepicker.client.DateBox;\r
 \r
 /**\r
index cf4f2e5d5f96e9f39424e4910f6d51fc0e1fe418..69efd26067d62d9300aba2bc30c743f2eb6f7d78 100644 (file)
@@ -223,10 +223,12 @@ public class EventsListener implements EventListener {
         .createArray().cast();
     for (int i = 0; i < elementEvents.length(); i++) {
       BindFunction listener = elementEvents.get(i);
-      
-      if (!listener.hasEventType(eventbits) || (namespace != null && namespace.length() != 0 && !listener.nameSpace.equals(namespace))) {
-        newList.add(listener);
+      boolean matchNS = namespace == null || namespace.isEmpty() || listener.nameSpace.equals(namespace);
+      boolean matchEV = eventbits <= 0 || listener.hasEventType(eventbits);
+      if (matchNS && matchEV) {
+        continue;
       }
+      newList.add(listener);
     }
     elementEvents = newList;
   }
index 93cc7755ee16483e69eba213dbb58274fd76107d..31b1a7a7cd882afddde81d9c4c4495860fc1de9d 100644 (file)
@@ -2,7 +2,6 @@ package com.google.gwt.query.client.plugins.widgets;
 
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.user.client.ui.Button;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link Button} widget. A {@link Button} is created
index 829cbe8a36472ed5e4282c9dc26ce3dfdaf5e3bd..b92130c7fed230ec46939b1568d1c3dfb49e2af7 100644 (file)
@@ -7,7 +7,6 @@ import java.util.Date;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.query.client.GQuery;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 import com.google.gwt.user.datepicker.client.DateBox;
 import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
 
index a272ffdcddc596fa37aaf081778c03341077bd18..3de90040da5c517a807fd44060bea6e6d3f70b00 100644 (file)
@@ -7,7 +7,6 @@ import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.DisclosurePanel;
 import com.google.gwt.user.client.ui.HTMLPanel;
 import com.google.gwt.user.client.ui.Label;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 public class DisclosurePanelWidgetFactory implements
     WidgetFactory<DisclosurePanel> {
index f5b8ad0d885ca75f852e05a2c431377dfe375d39..3698290378a6c40186506779b3b059ede526e962 100644 (file)
@@ -7,7 +7,6 @@ import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.SelectElement;
 import com.google.gwt.query.client.GQuery;
 import com.google.gwt.user.client.ui.ListBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link ListBox} widget.
index 9d6c0fb7157f625df36efc2937d96d9e52d1f573..6e988b4d4a17d307dec2312baf0f1e5cd2c4bdee 100644 (file)
@@ -5,7 +5,6 @@ import static com.google.gwt.query.client.GQuery.$;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.query.client.GQuery;
 import com.google.gwt.user.client.ui.PasswordTextBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link PasswordTextBox} widget. A
index b5c5e46c065289f5fa1027ce05fdb1eccc5c7330..59ec23d1863c3229932d4fe7cd16c62fbc454a84 100644 (file)
@@ -5,7 +5,6 @@ import static com.google.gwt.query.client.GQuery.$;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.RichTextArea;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link RichTextArea} widget. 
index 9b85dae39190b83781316ba787ce61fed58795f8..77b3f39104088315880b6f6ac3a1e9ac1f580dbb 100644 (file)
@@ -9,7 +9,6 @@ import com.google.gwt.query.client.GQuery;
 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
 import com.google.gwt.user.client.ui.SuggestBox;
 import com.google.gwt.user.client.ui.SuggestOracle;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link SuggestBox} widget.
index 1de9c94fbd80a79e8969556da1aba859d691ff85..8143b685f80a0b8ed81eaaedcf660e95e0643c39 100644 (file)
@@ -8,7 +8,6 @@ import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.HTMLPanel;
 import com.google.gwt.user.client.ui.TabPanel;
 import com.google.gwt.user.client.ui.Widget;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link Button} widget. A {@link Button} is created
index fe389da2ba3db1a7ebd8c1b00d25e1df195f34f8..22de997e98d946f1aa3cb6bf6c9ba211cf586098 100644 (file)
@@ -5,7 +5,6 @@ import static com.google.gwt.query.client.GQuery.$;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.query.client.GQuery;
 import com.google.gwt.user.client.ui.TextBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
 
 /**
  * Factory used to create a {@link TextBox} widget. A {@link TextBox} is created
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java
new file mode 100644 (file)
index 0000000..cd73f6a
--- /dev/null
@@ -0,0 +1,69 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.GqUi;
+import com.google.gwt.user.client.ui.Widget;
+
+public class WidgetsUtils {
+
+  private static final String[] appendingTags = {
+    "td", "th", "li"};
+
+  
+  /**
+   * Test if the tag name of the element is one of tag names given in parameter
+   * 
+   * @param tagNames
+   * @return
+   */
+  public static boolean matchesTags(Element e, String... tagNames) {
+
+    assert e != null : "Element cannot be null";
+
+    StringBuilder regExp = new StringBuilder("^(");
+    int tagNameLenght = tagNames != null ? tagNames.length : 0;
+    for (int i = 0; i < tagNameLenght; i++) {
+      regExp.append(tagNames[i].toUpperCase());
+      if (i < tagNameLenght - 1) {
+        regExp.append("|");
+      }
+    }
+    regExp.append(")$");
+
+    return e.getTagName().toUpperCase().matches(regExp.toString());
+
+  }
+
+  /**
+   * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
+   * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
+   *  and the old element classes will be copied to the new element.
+   */
+   private static void replaceOrAppend(Element oldElement, Element newElement) {
+    assert oldElement != null && newElement != null;
+    
+    if(matchesTags(oldElement, appendingTags)){
+      GQuery.$(oldElement).html("").append(newElement);
+    }else{
+      GQuery.$(oldElement).replaceWith(newElement);
+  
+      String c = oldElement.getClassName();
+      if (!c.isEmpty()) {
+        newElement.addClassName(c);
+      }
+    }
+   }
+   
+   /**
+    * Replace a dom element by a widget.
+    * If the widget implements Attachable the method attach will be called.
+    * Old element classes will be copied to the new widget.
+    */
+   public static void replaceOrAppend(Element e, Widget widget)  {
+     assert e != null && widget != null;
+     GqUi.detachWidget(widget);
+     replaceOrAppend(e, widget.getElement());
+     GqUi.attachWidget(widget);
+   }
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/user/client/ui/GqUi.java b/gwtquery-core/src/main/java/com/google/gwt/user/client/ui/GqUi.java
new file mode 100644 (file)
index 0000000..15755f8
--- /dev/null
@@ -0,0 +1,24 @@
+package com.google.gwt.user.client.ui;
+
+
+/**
+ * GQuery Utility class to access protected methods in ui package. 
+ */
+public class GqUi {
+  public static void attachWidget(Widget widget) {
+    if (widget != null) {
+      widget.onAttach();
+      if (widget instanceof RichTextArea != true) {
+        RootPanel.detachOnWindowClose(widget);
+      }
+    }
+  }
+  
+  public static void detachWidget(Widget widget){
+    if (widget != null) {
+      if (widget.isAttached()) {
+        widget.removeFromParent();
+      }
+    }
+  }
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/user/client/ui/WidgetsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/user/client/ui/WidgetsUtils.java
deleted file mode 100644 (file)
index 445bdc8..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-package com.google.gwt.user.client.ui;
-
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.query.client.GQuery;
-
-public class WidgetsUtils {
-
-  private static final String[] appendingTags = {
-    "td", "th", "li"};
-
-  
-  /**
-   * Test if the tag name of the element is one of tag names given in parameter
-   * 
-   * @param tagNames
-   * @return
-   */
-  public static boolean matchesTags(Element e, String... tagNames) {
-
-    assert e != null : "Element cannot be null";
-
-    StringBuilder regExp = new StringBuilder("^(");
-    int tagNameLenght = tagNames != null ? tagNames.length : 0;
-    for (int i = 0; i < tagNameLenght; i++) {
-      regExp.append(tagNames[i].toUpperCase());
-      if (i < tagNameLenght - 1) {
-        regExp.append("|");
-      }
-    }
-    regExp.append(")$");
-
-    return e.getTagName().toUpperCase().matches(regExp.toString());
-
-  }
-
-  /**
-   * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
-   * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
-   *  and the old element classes will be copied to the new element.
-   */
-   private static void replaceOrAppend(Element oldElement, Element newElement) {
-    assert oldElement != null && newElement != null;
-    
-    if(matchesTags(oldElement, appendingTags)){
-      GQuery.$(oldElement).html("").append(newElement);
-    }else{
-      GQuery.$(oldElement).replaceWith(newElement);
-  
-      String c = oldElement.getClassName();
-      if (!c.isEmpty()) {
-        newElement.addClassName(c);
-      }
-    }
-   }
-   
-   /**
-    * Replace a dom element by a widget.
-    * If the widget implements Attachable the method attach will be called.
-    * Old element classes will be copied to the new widget.
-    */
-   public static void replaceOrAppend(Element e, Widget widget)  {
-     assert e != null && widget != null;
-     
-     if (widget.isAttached()) {
-       widget.removeFromParent();
-     }
-
-     replaceOrAppend(e, widget.getElement());
-     
-     widget.onAttach();
-     
-     if (widget instanceof RichTextArea != true) {
-       RootPanel.detachOnWindowClose(widget);
-     }
-   }
-   
-}
index 824abf51f6501c979b19b2feb5bb955c8bdcc1ea..21a278a4ad50e7e347881ee1ab12296a72eed747 100644 (file)
@@ -874,6 +874,24 @@ public class GQueryCoreTest extends GWTTestCase {
 
     assertEquals(2, $(new Label(""), new TextArea()).size());
   }
+  
+  public void testGQueryWidgetManipulation() {
+    
+    String content = "<div class='outer'></div>";
+    $(e).html(content);
+    Button b = new Button("b");
+    RootPanel.get().add(b);
+    assertTrue(b.isAttached());
+    $("button").remove();
+    assertFalse(b.isAttached());
+    
+    Button b2 = new Button("b");
+    $(e).append($(b2));
+    assertTrue(b2.isAttached());
+    
+    $(e).empty();
+    
+  }
 
   public void testGQueryMap() {
     String content = "<p id='1'/><p/><p id='2'/><p id='4'/>";