Adding SafeHtml versions of addItem/insertItem to Tree and TreeItem.

Review at http://gwt-code-reviews.appspot.com/1010801

Review by: pdr@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9077 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 d5cc644..b468940 100644
--- a/user/src/com/google/gwt/user/client/ui/Tree.java
+++ b/user/src/com/google/gwt/user/client/ui/Tree.java
@@ -55,6 +55,7 @@
 import com.google.gwt.i18n.client.LocaleInfo;
 import com.google.gwt.resources.client.ClientBundle;
 import com.google.gwt.resources.client.ImageResource;
+import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.Event;
@@ -331,6 +332,16 @@
   }
 
   /**
+   * Adds a simple tree item containing the specified html.
+   * 
+   * @param itemHtml the html of the item to be added
+   * @return the item that was added
+   */
+  public TreeItem addItem(SafeHtml itemHtml) {
+    return root.addItem(itemHtml);
+  }
+
+  /**
    * Adds an item to the root level of this tree.
    * 
    * @param item the item to be added
@@ -494,6 +505,19 @@
   }
 
   /**
+   * Inserts a child tree item at the specified index containing the specified
+   * html.
+   * 
+   * @param beforeIndex the index where the item will be inserted
+   * @param itemHtml the html of the item to be added
+   * @return the item that was added
+   * @throws IndexOutOfBoundsException if the index is out of range
+   */
+  public TreeItem insertItem(int beforeIndex, SafeHtml itemHtml) {
+    return root.insertItem(beforeIndex, itemHtml);
+  }
+
+  /**
    * Inserts an item into the root level of this tree.
    * 
    * @param beforeIndex the index where the item will be inserted
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 6aed8a5..1244015 100644
--- a/user/src/com/google/gwt/user/client/ui/TreeItem.java
+++ b/user/src/com/google/gwt/user/client/ui/TreeItem.java
@@ -338,6 +338,18 @@
   }
 
   /**
+   * Adds a child tree item containing the specified html.
+   * 
+   * @param itemHtml the item's HTML
+   * @return the item that was added
+   */
+  public TreeItem addItem(SafeHtml itemHtml) {
+    TreeItem ret = new TreeItem(itemHtml);
+    addItem(ret);
+    return ret;
+  }
+
+  /**
    * Adds another item as a child to this one.
    * 
    * @param item the item to be added
@@ -473,6 +485,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 itemHtml the item's HTML
+   * @return the item that was added
+   * @throws IndexOutOfBoundsException if the index is out of range
+   */
+  public TreeItem insertItem(int beforeIndex, SafeHtml itemHtml)
+      throws IndexOutOfBoundsException {
+    TreeItem ret = new TreeItem(itemHtml);
+    insertItem(beforeIndex, ret);
+    return ret;
+  }
+
+  /**
    * Inserts an item as a child to this one.
    * 
    * @param beforeIndex the index where the item will be inserted
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 df4ebf3..da6a325 100644
--- a/user/test/com/google/gwt/user/client/ui/TreeItemTest.java
+++ b/user/test/com/google/gwt/user/client/ui/TreeItemTest.java
@@ -47,6 +47,12 @@
     assertEquals(a, item.getChild(1));
   }
 
+  public void testAddItemSafeHtml() {
+    TreeItem item = new TreeItem("foo");
+    TreeItem child = item.addItem(SafeHtmlUtils.fromSafeConstant(html));
+    assertEquals(html, child.getHTML().toLowerCase());
+  }
+
   public void testInsert() {
     TreeItem item = new TreeItem();
     TreeItem b = item.addItem("b");
@@ -124,6 +130,12 @@
     }
   }
 
+  public void testInsertItemSafeHtml() {
+    TreeItem item = new TreeItem("foo");
+    TreeItem child = item.insertItem(0, SafeHtmlUtils.fromSafeConstant(html));
+    assertEquals(html, child.getHTML().toLowerCase());
+  }
+
   public void testSafeHtmlConstructor() {
     TreeItem item = new TreeItem(SafeHtmlUtils.fromSafeConstant(html));
     
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 0e04aa0..e283fb8 100644
--- a/user/test/com/google/gwt/user/client/ui/TreeTest.java
+++ b/user/test/com/google/gwt/user/client/ui/TreeTest.java
@@ -16,6 +16,7 @@
 package com.google.gwt.user.client.ui;
 
 import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.safehtml.shared.SafeHtmlUtils;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Element;
 
@@ -26,6 +27,8 @@
  */
 public class TreeTest extends GWTTestCase {
 
+  private static final String html = "<b>hello</b><i>world</i>";
+
   static class Adder implements HasWidgetsTester.WidgetAdder {
     public void addChild(HasWidgets container, Widget child) {
       ((Tree) container).addItem(child);
@@ -37,6 +40,12 @@
     return "com.google.gwt.user.DebugTest";
   }
 
+  public void testAddItemSafeHtml() {
+    Tree t = new Tree();
+    TreeItem item = t.addItem(SafeHtmlUtils.fromSafeConstant(html));
+    assertEquals(html, item.getHTML().toLowerCase());
+  }
+
   public void testAttachDetachOrder() {
     HasWidgetsTester.testAll(new Tree(), new Adder(), true);
   }
@@ -111,6 +120,12 @@
     assertEquals(wti, t.getItem(1));
   }
 
+  public void testInsertItemSafeHtml() {
+    Tree t = new Tree();
+    TreeItem item = t.insertItem(0, SafeHtmlUtils.fromSafeConstant(html));
+    assertEquals(html, item.getHTML().toLowerCase());
+  }
+
   public void testIterator() {
     Tree tree = new Tree();
     Iterator<TreeItem> iter = tree.treeItemIterator();