Fixed an issue where TextBoxBase.setSelectionRange() can throw an error if the element is not attached to the page or not visible.  Also fixed an error in TextBoxBase.getSelectedText() where it throws an exception in IE if the cursorPos is -1, which can happen if the element is not attached.

Patch by: jlabanca
Review by: ecc
Issue: 1385

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3666 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/TextBoxBase.java b/user/src/com/google/gwt/user/client/ui/TextBoxBase.java
index 5011cac..69da0d0 100644
--- a/user/src/com/google/gwt/user/client/ui/TextBoxBase.java
+++ b/user/src/com/google/gwt/user/client/ui/TextBoxBase.java
@@ -121,7 +121,11 @@
    * @return the selected text, or an empty string if none is selected
    */
   public String getSelectedText() {
-    int start = getCursorPos(), length = getSelectionLength();
+    int start = getCursorPos();
+    if (start < 0) {
+      return "";
+    }
+    int length = getSelectionLength();
     return getText().substring(start, start + length);
   }
 
@@ -179,7 +183,8 @@
   /**
    * Selects all of the text in the box.
    * 
-   * This will only work when the widget is attached to the document.
+   * This will only work when the widget is attached to the document and not
+   * hidden.
    */
   public void selectAll() {
     int length = getText().length();
@@ -191,6 +196,9 @@
   /**
    * Sets the cursor position.
    * 
+   * This will only work when the widget is attached to the document and not
+   * hidden.
+   * 
    * @param pos the new cursor position
    */
   public void setCursorPos(int pos) {
@@ -233,7 +241,8 @@
   /**
    * Sets the range of text to be selected.
    * 
-   * This will only work when the widget is attached to the document.
+   * This will only work when the widget is attached to the document and not
+   * hidden.
    * 
    * @param pos the position of the first character to be selected
    * @param length the number of characters to be selected
diff --git a/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java b/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java
index dd3b780..2ebaac1 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java
@@ -45,6 +45,10 @@
   }
 
   public native void setSelectionRange(Element elem, int pos, int length) /*-{
-    elem.setSelectionRange(pos, pos + length);
+    try {
+      elem.setSelectionRange(pos, pos + length);
+    } catch (e) {
+      // Firefox throws exception if TextBox is not visible, even if attached
+    }
   }-*/;
 }
diff --git a/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java b/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java
index 76fdb1d..375fd69 100644
--- a/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java
+++ b/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java
@@ -84,5 +84,13 @@
 
     // Check for setting 0;
     area.setSelectionRange(0, 0);
+
+    // Issue 1996: Cannot select text if TextBox is hidden
+    {
+      TextBoxBase area2 = createTextBoxBase();
+      area2.setVisible(false);
+      RootPanel.get().add(area2);
+      area.selectAll();
+    }
   }
 }