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

Bug 515153

Summary: ScrolledComposite should not take focus
Product: [RT] RAP Reporter: Matthias Boehm <Matthias.Boehm>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Matthias.Boehm
Version: 3.0   
Target Milestone: 3.2 M7   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/#/c/94929/
Whiteboard:
Attachments:
Description Flags
a simple application that demonstrates the bug none

Description Matthias Boehm CLA 2017-04-12 03:30:59 EDT
Created attachment 267765 [details]
a simple application that demonstrates the bug

I try to create a ScrolledComposite with the "NO_FOCUS" style, as in this snippet:

protected void createContents(Composite parent)
{
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.NO_FOCUS);

    Button button = new Button(parent, SWT.NONE);
    button.setText("abc");
}

But when pressing the TAB key, focus jumps to the button, and away from the button again, and to the button again, and so on. This does not occur if instead of the ScrolledComposite a simple Composite is used:

protected void createContents(Composite parent)
{
    Composite c = new Composite(parent, SWT.NO_FOCUS);

    Button button = new Button(parent, SWT.NONE);
    button.setText("abc");
}

Here the focus stays at the button after pressing the TAB key. 

I found already the cause for that problem: In the constructor of the class "ScrolledComposite", the super constructor is called like this:

super( parent, checkStyle( style ) );

where "checkStyle" is defined as follows:

private static int checkStyle( int style ) {
    int mask
      = SWT.H_SCROLL
      | SWT.V_SCROLL
      | SWT.BORDER
      | SWT.LEFT_TO_RIGHT;
//      | SWT.RIGHT_TO_LEFT;
    return style & mask;
  }

So checkStyle takes away the NO_FOCUS style. 

A simple complete application that demonstrates this bug is attached.
Comment 1 Matthias Boehm CLA 2017-04-12 04:24:24 EDT
I searched for "SWT.NO_FOCUS" and found it in the following code:

package org.eclipse.rap.rwt.internal.lifecycle;

public class ControlLCAUtil {

  ...

  // TODO [rh] Eliminate instance checks. Let the respective classes always return NO_FOCUS
  private static boolean takesFocus( Control control ) {
    boolean result = true;
    result &= ( control.getStyle() & SWT.NO_FOCUS ) == 0;
    result &= control.getClass() != Composite.class;
    result &= control.getClass() != SashForm.class;
    return result;
  }

  ...

}


Perhaps this is a place where the bug could be fixed (instead of in ScrolledComposite itself)?
Comment 2 Ivan Furnadjiev CLA 2017-04-12 04:44:07 EDT
The original SWT ScrolledComposite has this style mask:
---
static int checkStyle (int style) {
	int mask = SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
	return style & mask;
}
---
SWT.NO_FOCUS is not supported by ScrolledComposite implementation in both SWT and RAP.
Comment 3 Matthias Boehm CLA 2017-04-12 05:08:05 EDT
Excuse me for reopening this bug, but in SWT the behaviour is as expected, and different to RWT, as you can see with this simple program:

package nofocusforscrolledcompositeswt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class EntryPoint {

    public static void main(String[] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        
        shell.setLayout(new GridLayout(2, false));
        
        // in SWT, scrolled composite does indeed get no focus!
        ScrolledComposite sc = new ScrolledComposite(shell, SWT.NO_FOCUS);
                
        Button button = new Button(shell, SWT.NONE);
        button.setText("abc");
                
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();

    }

}

Here the scrolled Composite does indeed not get the focus, and the focus stays on the button when pressing the TAB key.
Comment 4 Ivan Furnadjiev CLA 2017-04-12 05:24:29 EDT
I'll check the behaviour, but NO_FOCUS style flag is NOT documented as supported in SWT ScrolledComposite:
http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fcustom%2FScrolledComposite.html
Only H_SCROLL, V_SCROLL are supported.
Comment 5 Matthias Boehm CLA 2017-04-12 06:05:21 EDT
By the way, in SWT the ScrolledComposite *never* gets the focus, even if the SWT.NO_FOCUS flag is *not* specified.
Comment 6 Ivan Furnadjiev CLA 2017-04-12 08:59:52 EDT
OK... Agree.