Revision: 9735
Author: jlaba...@google.com
Date: Mon Feb 14 07:13:41 2011
Log: Add HTMLPanel.wrap(Element).
http://gwt-code-reviews.appspot.com/1360801/show

Issue: 3511
Author: tbroyer
Review by: jlabanca

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

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

=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java Fri Dec 17 06:27:00 2010 +++ /trunk/user/src/com/google/gwt/user/client/ui/HTMLPanel.java Mon Feb 14 07:13:41 2011
@@ -40,6 +40,28 @@
   public static String createUniqueId() {
     return Document.get().createUniqueId();
   }
+
+  /**
+   * Creates an HTML panel that wraps an existing element.
+   *
+ * This element must already be attached to the document. If the element is
+   * removed from the document, you must call
+   * {@link RootPanel#detachNow(Widget)}.
+   *
+   * @param element the element to be wrapped
+   */
+  public static HTMLPanel wrap(Element element) {
+    // Assert that the element is attached.
+    assert Document.get().getBody().isOrHasChild(element);
+
+    HTMLPanel html = new HTMLPanel(element);
+
+    // Mark it attached and remember it for cleanup.
+    html.onAttach();
+    RootPanel.detachOnWindowClose(html);
+
+    return html;
+  }

   /**
    * Creates an HTML panel with the specified HTML contents inside a DIV
@@ -100,7 +122,16 @@
     setElement(scratchDiv.getFirstChildElement());
     getElement().removeFromParent();
   }
-
+
+  /**
+   * Construct a new {@link HTMLPanel} with the specified element.
+   *
+   * @param elem the element at the root of the panel
+   */
+  private HTMLPanel(Element elem) {
+    setElement(elem);
+  }
+
   /**
    * Adds a child widget to the panel.
    *
=======================================
--- /trunk/user/test/com/google/gwt/user/client/ui/ElementWrappingTest.java Wed Mar 11 16:11:41 2009 +++ /trunk/user/test/com/google/gwt/user/client/ui/ElementWrappingTest.java Mon Feb 14 07:13:41 2011
@@ -149,6 +149,18 @@
     assertExistsAndAttached(html);
     assertEquals("myHTML", html.getHTML());
   }
+
+  /**
+   * Tests {@link HTMLPanel#wrap(Element)}.
+   */
+  public void testHTMLPanel() {
+ ensureDiv().setInnerHTML("<div id='foo'>my<div id='bar'>HTML</div></div>");
+    Element bar = Document.get().getElementById("bar");
+    HTMLPanel html = HTMLPanel.wrap(Document.get().getElementById("foo"));
+
+    assertExistsAndAttached(html);
+    assertTrue(html.getElement().isOrHasChild(bar));
+  }

   /**
    * Tests {@link Image#wrap(Element)}.

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

Reply via email to