Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    focusInItem and getSelectionRange in IE8

    Hi,

    I think I am maybe missing some steps before invoking focusInItem and/or getSelectionRange. The btn2 getSelectionRange part of the code below does not work in IE; it always return null selection range.

    Software:
    -SmartGWT 2.4
    -GWT 2.1.1
    -IE 8, FireFox 3.6.6 and Chrome 8.0.552.237 browsers

    Test_focus.html
    Code:
    <!doctype html>
    
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    	<meta http-equiv="X-UA-Compatible" content="IE=7">
    
        <link type="text/css" rel="stylesheet" href="Test_focus.css">
    
        <title>Test Focus</title>
        
        <script> var isomorphicDir = "test_focus/sc/"; </script>
        <script type="text/javascript" language="javascript" src="test_focus/test_focus.nocache.js"></script>
      </head>
    
      <body>
    
        <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
        
        <noscript>
          <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
            Your web browser must have JavaScript enabled
            in order for this application to display correctly.
          </div>
        </noscript>
    
      </body>
    </html>
    Test_focus.gwt.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <module rename-to='test_focus'>
      <inherits name='com.google.gwt.user.User'/>
      <inherits name='com.smartgwt.SmartGwt'/>
      <inherits name="com.smartclient.theme.enterprise.EnterpriseResources"/>
      <entry-point class='au.com.company.client.Test_focus'/>
      <source path='client'/>
      <source path='shared'/>
    </module>
    Test_focus.java
    Code:
    package au.com.company.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.fields.ButtonItem;
    import com.smartgwt.client.widgets.form.fields.TextAreaItem;
    import com.smartgwt.client.widgets.form.fields.TextItem;
    import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
    import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
    
    public class Test_focus implements EntryPoint
    {
    	DynamicForm form = new DynamicForm();
    	TextItem fld1 = new TextItem("field1", "Field 1");
    	TextItem fld2 = new TextItem("field2", "Field 2");
    	TextAreaItem fld3 = new TextAreaItem("field3", "Field 3");
    	ButtonItem btn1 = new ButtonItem("btn1", "Set Focus on Field 3");
    	ButtonItem btn2 = new ButtonItem("btn2", "getSelectionRange");
    	ButtonItem btn3 = new ButtonItem("btn3", "setSelectionRange");
    
    	public void onModuleLoad()
    	{
    		fld1.setValue("3");
    		fld2.setValue("8");
    		fld3.setValue("This is a test sentence.");
    		
    		btn1.setColSpan(2);
    		btn1.addClickHandler(new ClickHandler()
    		{
    			@Override
    			public void onClick(ClickEvent aEvent)
    			{
    				// Works in IE, Chrome and Firefox.
    				form.focusInItem("field3");
    			}
    		});
    		
    		btn2.setColSpan(2);
    		btn2.addClickHandler(new ClickHandler()
    		{
    			@Override
    			public void onClick(ClickEvent aEvent)
    			{
    				form.focusInItem("field3");
    				
    				int [] selectionRange = fld3.getSelectionRange();
    				
    				// IE always return null.
    				if (selectionRange == null)
    				{
    					SC.say("Debug", "selectionRange is null.");
    					return;
    				}
    
    				// Chrome and Firefox return correct values.
    				SC.say("Debug", "selectionRange [0]= " + selectionRange [0] + ", selectionRange [1]= " + selectionRange [1]);
    			}
    		});
    
    		btn3.setColSpan(2);
    		btn3.addClickHandler(new ClickHandler()
    		{
    			@Override
    			public void onClick(ClickEvent aEvent)
    			{
    				String v1 = (String) fld1.getValue();
    				String v2 = (String) fld2.getValue();
    
    				// Works in IE, Chrome and Firefox.
    				form.focusInItem("field3");
    				fld3.setSelectionRange(Integer.parseInt(v1), Integer.parseInt(v2));
    			}
    		});
    
    		fld3.addClickHandler(new ClickHandler()
    		{			
    			@Override
    			public void onClick(ClickEvent aEvent)
    			{
    				// Works in IE, Chrome and Firefox.
    				int [] selectionRange = fld3.getSelectionRange();
    				
    				SC.say("Debug", "selectionRange [0]= " + selectionRange [0] + ", selectionRange [1]= " + selectionRange [1]);
    			}
    		});
    		
    		form.setFields(fld1, fld2, fld3, btn1, btn2, btn3);
    		form.setAutoFocus(true);
    		form.draw();
    	}
    }
    Could anyone please provide advise on what I need to add or remove from my code?

    Is this an IE-related bug?

    Thanks,
    rtsan
    Last edited by rtsan; 16 Jan 2011, 11:15.

    #2
    I see this fail in all browsers. Selection range cannot be obtained with getSelectionRange on any item that does not have focus. That's what is happening here: pressing the button moves the focus and then tries to get selection from another item.

    Comment


      #3
      I explained the same problem in the following post.
      I provided javascript code that address this issue but I am unable to make it work by creating native methods. It can be done quite easily by the SmartGWT team as they do have access to js methods, etc.

      http://forums.smartclient.com/showthread.php?t=15091

      I'm still waiting for a reply though...

      Comment


        #4
        1) Does this mean the focusInItem method does not work in these scenarios?
        2) How is it done in the RichTextEditor?
        3) Please suggest a work-around or another mechanism.

        I would like to stick with the SmartGWT approach as much as possible because IMO it is clear and simple.

        Thank you for the responses.

        Comment


          #5
          A patch has been made to the getSelectionRange() method to return the caret position immediately after a call to focusInItem() as described in your test case. Full selection range is not available in IE after the control loses focus - this is a limitation of IE.

          This change will be available in the next nightly.

          The workarounds is to always handle selection ranges before the control loses focus. If you will need it later, write the values somewhere to save.

          Comment

          Working...
          X