Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 419920

Summary: [Text] ENTER does not trigger widgetDefaultSelected under some constellations
Product: [RT] RAP Reporter: Holger Staudacher <holger.staudacher>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: P3    
Version: 2.2   
Target Milestone: 2.2 M3   
Hardware: PC   
OS: Mac OS X   
Whiteboard:

Description Holger Staudacher CLA 2013-10-20 04:25:02 EDT
Don't know if I have used SWT wrong but I think this is bug.

After pressing a button that creates a Text add runtime the new text can't gain focus. This means a listener that is registered on the new Text never receives widgetDefaultSelected.

Steps to reproduce:
The snippet below has a text that is created during start. Enter some text and press enter. The event is send to the server. Afterwards click the button to create a new Text. Enter the new Text and add some text. Press enter and you will not receive any event on the server.

public class TextBug extends AbstractEntryPoint {

  @Override
  protected void createContents( Composite parent ) {
    final Composite container = new Composite( parent, SWT.NONE );
    container.setLayout( new RowLayout( SWT.VERTICAL ) );
    createText( container );

    Button createTextButton = new Button( container, SWT.PUSH );
    createTextButton.setText( "Create new Text" );
    createTextButton.addSelectionListener( new SelectionAdapter() {
      @Override
      public void widgetSelected( SelectionEvent e ) {
        createText( container );
      }
    } );
  }

  private void createText( Composite container ) {
    final Text text = new Text( container, SWT.BORDER );
    text.setMessage( "Type text, press enter and check console" );
    text.addSelectionListener( new SelectionListener() {

      public void widgetSelected( SelectionEvent e ) {
        System.out.println( "Text selected with text " + text.getText() );
      }

      public void widgetDefaultSelected( SelectionEvent e ) {
        System.out.println( "Text default selected with text " + text.getText() );
      }
    } );
    container.getParent().layout( true );
  }
}
Comment 1 Holger Staudacher CLA 2013-10-20 04:29:44 EDT
A workaround is to register a MouseDownListener and set the focus programatically:

    text.addListener( SWT.MouseDown, new Listener() {

      @Override
      public void handleEvent( Event event ) {
        text.setFocus();
      }
    } );
Comment 2 Ivan Furnadjiev CLA 2013-10-21 07:06:25 EDT
The problem is the following. When you click on a button, it temporary becomes the Shell default button (same in SWT). Press ENTER on a text with shell default button set does not trigger the widget default selection on the Text, but the default button. The default button is set *only* by the server-side. What's happening:
1. Click on the Button - it becomes shell default button and text is created
2. Click on the Text - Text is the new focus control on the client (you can type on it), but the shell default button is still set. No request is sent as there is no listeners interested on focus change.
3. Press ENTER - default Button is triggered instead widget default selected on the Text.
If you add empty focus listener to the Text - everything is working as expected as request is sent after Text gains the focus on the client and default button is reset to null.
Comment 3 Ivan Furnadjiev CLA 2013-10-25 03:55:36 EDT
Fixed in master with commit 6a05959c8387989c8617585c34df28b88d79c2b0.
Comment 4 Holger Staudacher CLA 2013-10-25 04:19:49 EDT
Nice, thanks Ivan