Fixing HTMLTable#setWidget() to remove the widget or text when null is passed as the widget. Currently, the widget is not removed.
Review at http://gwt-code-reviews.appspot.com/1358803
Review by: rjrjr@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9737 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/HTMLTable.java b/user/src/com/google/gwt/user/client/ui/HTMLTable.java
index 65c3853..5155ffe 100644
--- a/user/src/com/google/gwt/user/client/ui/HTMLTable.java
+++ b/user/src/com/google/gwt/user/client/ui/HTMLTable.java
@@ -1091,19 +1091,20 @@
* within the Grid's bounding box.
* </p>
*
- * @param widget The widget to be added
+ * @param widget The widget to be added, or null to clear the cell
* @param row the cell's row
* @param column the cell's column
* @throws IndexOutOfBoundsException
*/
public void setWidget(int row, int column, Widget widget) {
prepareCell(row, column);
+
+ // Removes any existing widget.
+ Element td = cleanCell(row, column, true);
+
if (widget != null) {
widget.removeFromParent();
- // Removes any existing widget.
- Element td = cleanCell(row, column, true);
-
// Logical attach.
widgetMap.put(widget);
diff --git a/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java b/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java
index 998500b..ef021e8 100644
--- a/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java
+++ b/user/test/com/google/gwt/user/client/ui/HTMLTableTestBase.java
@@ -241,6 +241,29 @@
formatter.setWidth(0, 2, "100%");
}
+ public void testSetWidgetNull() {
+ HTMLTable t = getTable(1, 2);
+
+ // Set some text and a widget.
+ Label content = new Label("widget");
+ t.setText(0, 0, "hello world");
+ t.setWidget(0, 1, content);
+ assertEquals("hello world", t.getText(0, 0));
+ assertEquals(content, t.getWidget(0, 1));
+
+ // Set the text cell to a null widget.
+ t.setWidget(0, 0, null);
+ assertEquals("text should be cleared when the widget is set to null", "",
+ t.getText(0, 0));
+ assertEquals("widget should be cleared when set to null", content,
+ t.getWidget(0, 1));
+
+ // Set the widget cell to a null widget.
+ t.setWidget(0, 1, null);
+ assertEquals("", t.getText(0, 0));
+ assertNull(t.getWidget(0, 1));
+ }
+
public void testSafeHtml() {
HTMLTable table = getTable(1, 1);
table.setHTML(0, 0, SafeHtmlUtils.fromSafeConstant(html));