Revision: 7598
Author: awi...@google.com
Date: Thu Feb 18 10:19:29 2010
Log: Add new method HTMLPanel.add(Widget, Element) (using gwt.dom.client.Element) Add overload HTMLPanel.addAndReplaceElement(Widget, Element), marked final so that anyone actually overriding these methods does so on the only one that works.
Deprecated HTMLPanel.addAndReplaceElement(Widget, gwt.user.client.Element)

http://code.google.com/p/google-web-toolkit/source/detail?r=7598

Modified:
 /trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java
 /trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java

=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java Tue Oct 6 14:43:09 2009 +++ /trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java Thu Feb 18 10:19:29 2010
@@ -17,8 +17,7 @@

 import com.google.gwt.dom.client.DivElement;
 import com.google.gwt.dom.client.Document;
-import com.google.gwt.user.client.DOM;
-import com.google.gwt.user.client.Element;
+import com.google.gwt.dom.client.Element;

 import java.util.NoSuchElementException;

@@ -38,7 +37,7 @@
    * @return a new unique identifier
    */
   public static String createUniqueId() {
-    return DOM.createUniqueId();
+    return Document.get().createUniqueId();
   }

   /**
@@ -104,7 +103,18 @@
       throw new NoSuchElementException(id);
     }

-    super.add(widget, elem);
+    add(widget, elem);
+  }
+
+  /**
+   * Adds a child widget to the panel, contained within an HTML element.
+   *
+   * @param widget the widget to be added
+   * @param elem the element within which it will be contained
+   */
+  public void add(Widget widget, Element elem) {
+    com.google.gwt.user.client.Element clientElem = elem.cast();
+    super.add(widget, clientElem);
   }

   /**
@@ -113,7 +123,22 @@
    * @param widget the widget to be added
    * @param toReplace the element to be replaced by the widget
    */
-  public void addAndReplaceElement(Widget widget, Element toReplace) {
+  @SuppressWarnings("deprecation")
+ public final void addAndReplaceElement(Widget widget, Element toReplace) {
+    com.google.gwt.user.client.Element clientElem = toReplace.cast();
+    addAndReplaceElement(widget, clientElem);
+  }
+
+  /**
+   * Adds a child widget to the panel, replacing the HTML element.
+   *
+   * @param widget the widget to be added
+   * @param toReplace the element to be replaced by the widget
+   * @deprecated use {...@link #addAndReplaceElement(Widget, Element)}
+   */
+  @Deprecated
+  public void addAndReplaceElement(Widget widget,
+      com.google.gwt.user.client.Element toReplace) {
// Logic pulled from super.add(), replacing the element rather than adding.
     widget.removeFromParent();
     getChildren().add(widget);
@@ -148,8 +173,9 @@
    * @param id the id of the element to be found
* @return the element with the given id, or <code>null</code> if none is found
    */
-  public Element getElementById(String id) {
- return isAttached() ? DOM.getElementById(id) : attachToDomAndGetElement(id);
+  public com.google.gwt.user.client.Element getElementById(String id) {
+ Element elem = isAttached() ? Document.get().getElementById(id) : attachToDomAndGetElement(id);
+    return elem.cast();
   }

   /**
@@ -164,27 +190,27 @@
   private Element attachToDomAndGetElement(String id) {
     // If the hidden DIV has not been created, create it.
     if (hiddenDiv == null) {
-      hiddenDiv = DOM.createDiv();
+      hiddenDiv = Document.get().createDivElement();
       UIObject.setVisible(hiddenDiv, false);
       RootPanel.getBodyElement().appendChild(hiddenDiv);
     }

// Hang on to the panel's original parent and sibling elements so that it
     // can be replaced.
-    Element origParent = DOM.getParent(getElement());
-    Element origSibling = DOM.getNextSibling(getElement());
+    Element origParent = getElement().getParentElement();
+    Element origSibling = getElement().getNextSiblingElement();

     // Attach the panel's element to the hidden div.
-    DOM.appendChild(hiddenDiv, getElement());
+    hiddenDiv.appendChild(getElement());

     // Now that we're attached to the DOM, we can use getElementById.
-    Element child = DOM.getElementById(id);
+    Element child = Document.get().getElementById(id);

     // Put the panel's element back where it was.
     if (origParent != null) {
-      DOM.insertBefore(origParent, getElement(), origSibling);
+      origParent.insertBefore(getElement(), origSibling);
     } else {
-      DOM.removeChild(hiddenDiv, getElement());
+      hiddenDiv.removeChild(getElement());
     }

     return child;
=======================================
--- /trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java Tue Oct 6 14:43:09 2009 +++ /trunk/user/test/com/google/gwt/user/client/ui/HTMLPanelTest.java Thu Feb 18 10:19:29 2010
@@ -46,6 +46,22 @@
     assertEquals("a", labelA.getElement().getParentElement().getId());
     assertEquals("b", labelB.getElement().getParentElement().getId());
   }
+
+  /**
+   * Tests {...@link HTMLPanel#add(Widget, Element)}.
+   */
+  public void testAddToElement() {
+    Label labelA = new Label("A"), labelB = new Label("B");
+ HTMLPanel p = new HTMLPanel("<div class=\"a\"></div><div class=\"b\"></div>");
+    Element first = p.getElement().getFirstChildElement();
+    Element second = first.getNextSiblingElement();
+
+    p.add(labelA, first);
+    p.add(labelB, second);
+    // Ensure that both Label's have the correct parent.
+ assertEquals("a", labelA.getElement().getParentElement().getClassName()); + assertEquals("b", labelB.getElement().getParentElement().getClassName());
+  }

   /**
    * This is meant to catch an issue created by a faulty optimization. To
@@ -122,7 +138,8 @@
   /**
* Ensures that addAndReplaceChild() puts the widget in exactly the right place in the DOM.
    */
-  public void testAddAndReplaceElementForElement() {
+  @SuppressWarnings("deprecation")
+  public void testAddAndReplaceElementForUserElement() {
HTMLPanel hp = new HTMLPanel("<div id='parent'>foo<span id='placeholder'></span>bar</div>");

     RootPanel.get().add(hp);
@@ -139,6 +156,27 @@
     Node next = button.getElement().getNextSibling();
     assertEquals("bar", next.getNodeValue());
   }
+
+  /**
+ * Ensures that addAndReplaceChild() puts the widget in exactly the right place in the DOM.
+   */
+  public void testAddAndReplaceElementForElement() {
+ HTMLPanel hp = new HTMLPanel("<div id='parent'>foo<span id='placeholder'></span>bar</div>");
+
+    RootPanel.get().add(hp);
+    Element placeholder = hp.getElementById("placeholder");
+
+    Button button = new Button("my button");
+    hp.addAndReplaceElement(button, placeholder);
+
+    assertEquals("parent", button.getElement().getParentElement().getId());
+
+    Node prev = button.getElement().getPreviousSibling();
+    assertEquals("foo", prev.getNodeValue());
+
+    Node next = button.getElement().getNextSibling();
+    assertEquals("bar", next.getNodeValue());
+  }

   /**
    * Tests table root tag.

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

Reply via email to