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

Bug 398378

Summary: RAP vs SWT SetVisible/SetFocus different behaviours
Product: [RT] RAP Reporter: John Gymer <jgymer>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 2.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description John Gymer CLA 2013-01-17 06:57:24 EST
Using RAP 2.0 M4 (with some nightlies applied), there is a difference in behaviour between SWT and RAP with regard to programmatic setting of visibility, focus and active on shells.

The snippet below demonstrates it:

package bug.snippet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Bugsy {
	private static Display display;
	private static Shell shell;
	private static Shell shell2;
	private static Text txt;
	private static Text txt2;
	private static Button but;
	private static Button but2;
	
	public static void main(String args[]) {
		System.out.println("BugSnippy Starting...");
		
		// create Shell1
		display = new Display();
		shell = new Shell(display, SWT.TITLE|SWT.MAX|SWT.MIN|SWT.RESIZE|SWT.CLOSE);
		shell.setText("Shell 1");
		shell.setSize(300, 150);
		shell.setLocation(20, 20);
		shell.setBackground(new Color(null, new RGB(255,128,128)));
		FormLayout layout = new FormLayout();
		shell.setLayout(layout);

		// add Text entry field onto Shell1
		txt = new Text(shell, SWT.SINGLE|SWT.BORDER);
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.top = new FormAttachment(0, 5);
		fd.width = 100;
		fd.height = 24;
		txt.setLayoutData(fd);

		// add "SHOW-Shell2" Button to Shell1
		but = new Button(shell, SWT.BORDER|SWT.PUSH);
		but.setText("Show Shell 2");
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.top = new FormAttachment(0, 40);
		fd.width = 100;
		fd.height = 24;
		but.setLayoutData(fd);
		
		// create Shell2
		shell2 = new Shell(display, SWT.TITLE|SWT.MAX|SWT.MIN|SWT.RESIZE|SWT.CLOSE);
		shell2.setText("Shell 2");
		shell2.setSize(300, 150);
		shell2.setLocation(80, 80);
		shell2.setBackground(new Color(null, new RGB(128,255,128)));
		FormLayout layout2 = new FormLayout();
		shell2.setLayout(layout2);

		// add Text entry field onto Shell2
		txt2 = new Text(shell2, SWT.SINGLE|SWT.BORDER);
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.top = new FormAttachment(0, 5);
		fd.width = 100;
		fd.height = 24;
		txt2.setLayoutData(fd);

		// add "HideMe" Button to Shell2
		but2 = new Button(shell2, SWT.BORDER|SWT.PUSH);
		but2.setText("Hide Shell 2");
		fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.top = new FormAttachment(0, 40);
		fd.width = 100;
		fd.height = 24;
		but2.setLayoutData(fd);

		// define listener to button in Shell 1 which will re-show Shell2 when pressed
		but.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				shell2.setVisible(true);
				shell2.setActive();
				txt2.setFocus();
			}
		});
		
		// define listener to button in Shell 2 which will hide Shell2 when pressed
		but2.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				shell2.setVisible(false);
			}
		});

		shell.open();
		shell2.open();

		while (!shell.isDisposed() && !shell2.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		
		System.out.println("BugSnippy Done!");
	}

}



... with simple RAP entry point:

package bug.snippet;

import org.eclipse.rap.rwt.application.EntryPoint;

public class RAPEntryPoint_BugSnip implements EntryPoint {
	@Override
	public int createUI() {
		System.out.println("Starting RAP");
		Bugsy.main(null);
		System.out.println("RAP Done");

		return(0);
	}
}



... explanation of snippet...

Basically we have 2 Shells, with a field and button on each. The positions of the Shells overlap a little (not that this makes any difference - just highlights the problem more clearly).

In Shell 2 you press the 'Hide' button which causes Shell 2 to get hidden with setVisible(false). Then in Shell 1 you press the 'Show' button, which makes Shell2 become visible again. The intention is that Shell2 becomes (a) visible (b) active (c) keyboard focus goes to 'txt2' in Shell2.

In SWT, this works ok with:
				shell2.setVisible(true);
				shell2.setActive();
				txt2.setFocus();

In RAP, the above logic only changes keyboard focus to txt2 - it does NOT make shell2 active, or bring it to the front.

Changing RAP to the following results in shell2 coming to the front, and the window getting focus, but I cannot then change focus to txt2:
				shell2.setVisible(true);
				shell2.forceActive();
				shell2.forceFocus();

Final combination, adding setFocus on txt2, puts keyboard focus in the right place, but shell2 is no longer brought to the front, so the cursor in txt2 is active, but the window containing it is behind, so looks wierd (you can type, see the caret, but it is behind shell1!!):
				shell2.setVisible(true);
				shell2.forceActive();
				shell2.forceFocus();
				txt2.setFocus();

Let me know if you need more.
Thanks, John
Comment 1 John Gymer CLA 2013-01-24 06:05:03 EST
After further investigation, although there is a difference between SWT and RAP, it appears to be a reasonable one. In SWT we get away with the logic, but in RAP we do not because the Shell has not actually become active inline with the code, so the request to change focus is not honoured.
The work-around is to set keyboard focus to the Text field within a SWT.Activate event listener on the Shell, which works perfectly in both SWT and RAP!
Bug can be ignored/closed as WONTFIX.
Thanks, John
Comment 2 Ivan Furnadjiev CLA 2013-02-08 03:27:07 EST
Hi John, I agree with you... closing as WONTFIX as you suggested.