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

Bug 410144

Summary: Current focus is lost after opening and closing a MessageBox
Product: [RT] RAP Reporter: Anamaria Pradais <caprice_1_7>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P2    
Version: 2.1   
Target Milestone: 2.2 M1   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Anamaria Pradais CLA 2013-06-07 04:55:22 EDT
In a class Utilities I have the following code that is meant to open a pop-up box and show a validation failed message when a button from a certain Shell is clicked (as a Save button in a form that performs validation before actually taking a desired action):
public static void showErrorMessage(Shell parent, final String msg) {
 final Shell parentInst = parent;
 logger.error(msg);
 parent.getDisplay().asyncExec(new Runnable() {
  @Override
  public void run() {
   MessageBox messageBox = new MessageBox(parentInst, SWT.ERROR | SWT.OK);
   messageBox.setMessage(msg);
   DialogUtil.open(messageBox, null);
  }
 });
}
Before opening the MessageBox the focus is forced to move to the Text field that failed validation and select all content. After closing the MessageBox this focus in the underlying Shell should be resumed. This used to work perfectly in version 1.5 but doesn't work anymore after updating to 2.1M2. 

Tried to fix it like this:
public static void showErrorMessage(Shell parent, final String msg) {
 final Shell parentInst = parent;
 final Control focusedControl = parent.getDisplay().getFocusControl();// checked this and is what I expected
 parent.getDisplay().asyncExec(new Runnable() {
  @Override
  public void run() {
   MessageBox messageBox = new MessageBox(parentInst, SWT.ERROR | SWT.OK);
   messageBox.setMessage(msg);
   DialogUtil.open(messageBox, new DialogCallback() {
    @Override
    public void dialogClosed(int returnCode) {
     if (null != focusedControl)
      focusedControl.forceFocus(); //not working and returns false
    }
   });
  }
 });
}

After some further investigations I've determined that the method call focusedControl.isActive() which is used in forceFocus() returns false causing the forceFocus to have no effect because even if the MessageBox is closing when this action is executed it is probably still considered as being on top of the Shell where I would want the focus to return.
Comment 1 Ivan Furnadjiev CLA 2013-06-10 03:47:54 EDT
Reproducible with Controls Demo -> Button Tab. Focus the text field in the "Default Button" group. Press enter to open the massage dialog. Press enter again to close the dialog - focus is not on the Text.
Comment 2 Anamaria Pradais CLA 2013-06-10 04:48:26 EDT
Managed to fix it like this meanwhile (sorry if it wasn't really a bug):

[...]
DialogUtil.open(messageBox, new DialogCallback() {
 @Override
 public void dialogClosed(int returnCode) {
  if (null != focusedControl) {
    parentInst.forceActive();
    focusedControl.forceFocus();					
  }
 }
});
Comment 3 Ivan Furnadjiev CLA 2013-06-11 08:00:00 EDT
(In reply to comment #1)
> Reproducible with Controls Demo -> Button Tab. Focus the text field in the
> "Default Button" group. Press enter to open the massage dialog. Press enter
> again to close the dialog - focus is not on the Text.
The Controls Demo -> Button Tab behaves correctly - when default button is triggered the focus is moved on it (verified with SWT). To reproduce the issue use Controls Demo -> Dialogs Tab. Click on "Message Box" button for examples. After closing the MessageBox the focus is not back to "Message Box" button.
Comment 4 Ivan Furnadjiev CLA 2013-06-11 10:35:48 EDT
Fixed in master with commit e1d2c5b6a150ff398b98050a71bdece5dd95fdd7.