Author: [EMAIL PROTECTED]
Date: Thu Sep 18 13:12:44 2008
New Revision: 3666

Modified:
    trunk/user/src/com/google/gwt/user/client/ui/TextBoxBase.java
    trunk/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java
    trunk/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java

Log:
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

Modified: trunk/user/src/com/google/gwt/user/client/ui/TextBoxBase.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/ui/TextBoxBase.java       
(original)
+++ trunk/user/src/com/google/gwt/user/client/ui/TextBoxBase.java       Thu Sep 
 
18 13:12:44 2008
@@ -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

Modified: trunk/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java   
(original)
+++ trunk/user/src/com/google/gwt/user/client/ui/impl/TextBoxImpl.java  Thu  
Sep 18 13:12:44 2008
@@ -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
+    }
    }-*/;
  }

Modified:  
trunk/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java
==============================================================================
--- trunk/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java      
 
(original)
+++ trunk/user/test/com/google/gwt/user/client/ui/TextBoxBaseTestBase.java      
 
Thu Sep 18 13:12:44 2008
@@ -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();
+    }
    }
  }

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

Reply via email to