Deprecating TreeItem.addItem/insertItem(String html) in favor of methods that take SafeHtml. The current versions are misleading because they claim to take itemText but actually set innerHTML. The javadoc of the deprecated methods has been updated to indicate that they take HTML, not text.
Review at http://gwt-code-reviews.appspot.com/1666803
Review by: jat@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10920 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/Tree.java b/user/src/com/google/gwt/user/client/ui/Tree.java
index 61c9a4c..1bb3ef1 100644
--- a/user/src/com/google/gwt/user/client/ui/Tree.java
+++ b/user/src/com/google/gwt/user/client/ui/Tree.java
@@ -344,7 +344,9 @@
*
* @param itemHtml the text of the item to be added
* @return the item that was added
+ * @deprecated use {@link #addItem(SafeHtml)} instead
*/
+ @Deprecated
public TreeItem addItem(String itemHtml) {
return root.addItem(itemHtml);
}
@@ -561,15 +563,17 @@
/**
* Inserts a child tree item at the specified index containing the specified
- * text.
+ * html.
*
* @param beforeIndex the index where the item will be inserted
- * @param itemText the text to be added
+ * @param itemHtml the html to be added
* @return the item that was added
* @throws IndexOutOfBoundsException if the index is out of range
+ * @deprecated use {@link #insertItem(int, SafeHtml)} instead
*/
- public TreeItem insertItem(int beforeIndex, String itemText) {
- return root.insertItem(beforeIndex, itemText);
+ @Deprecated
+ public TreeItem insertItem(int beforeIndex, String itemHtml) {
+ return root.insertItem(beforeIndex, itemHtml);
}
/**
@@ -609,6 +613,19 @@
return root.insertItem(beforeIndex, widget);
}
+ /**
+ * Inserts a child tree item at the specified index containing the specified
+ * text.
+ *
+ * @param beforeIndex the index where the item will be inserted
+ * @param itemText the text of the item to be added
+ * @return the item that was added
+ * @throws IndexOutOfBoundsException if the index is out of range
+ */
+ public TreeItem insertTextItem(int beforeIndex, String itemText) {
+ return root.insertTextItem(beforeIndex, itemText);
+ }
+
@Override
public boolean isAnimationEnabled() {
return isAnimationEnabled;
diff --git a/user/src/com/google/gwt/user/client/ui/TreeItem.java b/user/src/com/google/gwt/user/client/ui/TreeItem.java
index 8ed1d46..01ae2ec 100644
--- a/user/src/com/google/gwt/user/client/ui/TreeItem.java
+++ b/user/src/com/google/gwt/user/client/ui/TreeItem.java
@@ -288,7 +288,9 @@
* Constructs a tree item with the given HTML.
*
* @param html the item's HTML
+ * @deprecated use {@link #TreeItem(SafeHtml)} instead
*/
+ @Deprecated
public TreeItem(String html) {
this();
setHTML(html);
@@ -336,7 +338,9 @@
*
* @param itemHtml the text to be added
* @return the item that was added
+ * @deprecated use {@link #addItem(SafeHtml)} instead
*/
+ @Deprecated
public TreeItem addItem(String itemHtml) {
TreeItem ret = new TreeItem(itemHtml);
addItem(ret);
@@ -349,6 +353,7 @@
* @param itemHtml the item's HTML
* @return the item that was added
*/
+ @Override
public TreeItem addItem(SafeHtml itemHtml) {
TreeItem ret = new TreeItem(itemHtml);
addItem(ret);
@@ -360,6 +365,7 @@
*
* @param item the item to be added
*/
+ @Override
public void addItem(TreeItem item) {
// If this is the item's parent, removing the item will affect the child
// count.
@@ -372,6 +378,7 @@
*
* @param isItem the wrapper of item to be added
*/
+ @Override
public void addItem(IsTreeItem isItem) {
TreeItem item = isItem.asTreeItem();
addItem(item);
@@ -383,6 +390,7 @@
* @param widget the widget to be added
* @return the item that was added
*/
+ @Override
public TreeItem addItem(Widget widget) {
TreeItem ret = new TreeItem(widget);
addItem(ret);
@@ -395,6 +403,7 @@
* @param itemText the text of the item to be added
* @return the item that was added
*/
+ @Override
public TreeItem addTextItem(String itemText) {
TreeItem ret = new TreeItem();
ret.setText(itemText);
@@ -402,6 +411,7 @@
return ret;
}
+ @Override
public TreeItem asTreeItem() {
return this;
}
@@ -448,6 +458,7 @@
return children.indexOf(child);
}
+ @Override
public String getHTML() {
return DOM.getInnerHTML(contentElem);
}
@@ -470,6 +481,7 @@
return open;
}
+ @Override
public String getText() {
return DOM.getInnerText(contentElem);
}
@@ -503,23 +515,25 @@
/**
* Inserts a child tree item at the specified index containing the specified
- * text.
+ * html.
*
* @param beforeIndex the index where the item will be inserted
- * @param itemText the text to be added
+ * @param itemHtml the html that the item will contain
* @return the item that was added
* @throws IndexOutOfBoundsException if the index is out of range
+ * @deprecated use {@link #insertItem(int, SafeHtml)} instead
*/
- public TreeItem insertItem(int beforeIndex, String itemText)
+ @Deprecated
+ public TreeItem insertItem(int beforeIndex, String itemHtml)
throws IndexOutOfBoundsException {
- TreeItem ret = new TreeItem(itemText);
+ TreeItem ret = new TreeItem(itemHtml);
insertItem(beforeIndex, ret);
return ret;
}
/**
* Inserts a child tree item at the specified index containing the specified
- * text.
+ * html.
*
* @param beforeIndex the index where the item will be inserted
* @param itemHtml the item's HTML
@@ -603,6 +617,22 @@
}
/**
+ * Inserts a child tree item at the specified index containing the specified
+ * text.
+ *
+ * @param beforeIndex the index where the item will be inserted
+ * @param itemText the item's text
+ * @return the item that was added
+ * @throws IndexOutOfBoundsException if the index is out of range
+ */
+ public TreeItem insertTextItem(int beforeIndex, String itemText) {
+ TreeItem ret = new TreeItem();
+ ret.setText(itemText);
+ insertItem(beforeIndex, ret);
+ return ret;
+ }
+
+ /**
* Determines whether this item is currently selected.
*
* @return <code>true</code> if it is selected
@@ -630,6 +660,7 @@
*
* @param item the item to be removed
*/
+ @Override
public void removeItem(TreeItem item) {
// Validate.
if (children == null || !children.contains(item)) {
@@ -661,6 +692,7 @@
*
* @param isItem the wrapper of item to be removed
*/
+ @Override
public void removeItem(IsTreeItem isItem) {
if (isItem != null) {
TreeItem item = isItem.asTreeItem();
@@ -671,17 +703,20 @@
/**
* Removes all of this item's children.
*/
+ @Override
public void removeItems() {
while (getChildCount() > 0) {
removeItem(getChild(0));
}
}
+ @Override
public void setHTML(String html) {
setWidget(null);
DOM.setInnerHTML(contentElem, html);
}
+ @Override
public void setHTML(SafeHtml html) {
setHTML(html.asString());
}
@@ -731,6 +766,7 @@
}
}
+ @Override
public void setText(String text) {
setWidget(null);
DOM.setInnerText(contentElem, text);
diff --git a/user/test/com/google/gwt/user/client/ui/TreeItemTest.java b/user/test/com/google/gwt/user/client/ui/TreeItemTest.java
index 8d18db8..2d724de 100644
--- a/user/test/com/google/gwt/user/client/ui/TreeItemTest.java
+++ b/user/test/com/google/gwt/user/client/ui/TreeItemTest.java
@@ -164,7 +164,17 @@
TreeItem child = item.insertItem(0, SafeHtmlUtils.fromSafeConstant(HTML));
assertEquals(HTML, child.getHTML().toLowerCase());
}
-
+
+ public void testInsertTextItem() {
+ TreeItem root = new TreeItem("foo");
+ String text = "Some<br>text";
+ TreeItem item = root.insertTextItem(0, text);
+ assertEquals(text, item.getText());
+ // Normalize the html for ancient safari 3
+ String html = item.getHTML().replace(">", ">");
+ assertEquals("Some<br>text", html);
+ }
+
/**
* Test for {@link TreeItem#removeItem(IsTreeItem)}.
*/
diff --git a/user/test/com/google/gwt/user/client/ui/TreeTest.java b/user/test/com/google/gwt/user/client/ui/TreeTest.java
index fdbf29f..212b901 100644
--- a/user/test/com/google/gwt/user/client/ui/TreeTest.java
+++ b/user/test/com/google/gwt/user/client/ui/TreeTest.java
@@ -30,6 +30,7 @@
private static final String html = "<b>hello</b><i>world</i>";
static class Adder implements HasWidgetsTester.WidgetAdder {
+ @Override
public void addChild(HasWidgets container, Widget child) {
((Tree) container).addItem(child);
}
@@ -93,7 +94,7 @@
String text = "Some<br>text";
TreeItem item = t.addTextItem(text);
assertEquals(text, item.getText());
- // Normalize the html for ancient safari 3
+ // Safari 3 leaves > in the HTML
String html = item.getHTML().replace(">", ">");
assertEquals("Some<br>text", html);
}
@@ -178,6 +179,16 @@
assertEquals(html, item.getHTML().toLowerCase());
}
+ public void testInsertTextItem() {
+ Tree t = createTree();
+ String text = "Some<br>text";
+ TreeItem item = t.insertTextItem(0, text);
+ assertEquals(text, item.getText());
+ // Safari 3 leaves > in the HTML
+ String html = item.getHTML().replace(">", ">");
+ assertEquals("Some<br>text", html);
+ }
+
public void testIterator() {
Tree tree = createTree();
Iterator<TreeItem> iter = tree.treeItemIterator();