Search code examples
javaeclipseeclipse-rcp

Why is the eclipse swt browser not focused?


I'm working on an eclipse RCP application. I have a preference page which inherits from IWorkbenchPreferencePage. In the preference pages, I have a button which spawns a shell when clicked. The shell is always out of focus and cannot be interacted with until the preference page is disposed.

Is there any way to set the focus on the shell?

Here is some pseudo code of the prefence page:

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

  AuthenticationManager authenticationManager = new AuthenticationManager();

  @Override
  protected void createFieldEditors() {
    final Button login = new Button(getFieldEditorParent(), SWT.PUSH);
    login.setText("Login")
    login.addSelectionListener(new SelectionListener() {
      @Override
      public void widgetSelected(final SelectionEvent se) {
        authenticationManager.run(getShell().getDisplay());
      }
    }
  }
}

public AuthenticationManager {
  public void run(@Nullable Display optionalDisplay) {
    display.asyncExec(() -> {
      // set the url & add listeners
    }
  }
}

Thanks!


Solution

  • I solved this by passing the shell rather than display. I still can't find the appropriate swt documentation for why this works but anyway, here's what my solution looks like now:

    public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
    
      AuthenticationManager authenticationManager = new AuthenticationManager();
    
      @Override
      protected void createFieldEditors() {
        final Button login = new Button(getFieldEditorParent(), SWT.PUSH);
        login.setText("Login")
        login.addSelectionListener(new SelectionListener() {
          @Override
          public void widgetSelected(final SelectionEvent se) {
            authenticationManager.run(getShell());
          }
        }
      }
    }
    
    public AuthenticationManager {
      public void run(@Nullable Shell optionalShell) {
        final Display display;
        if (optionalShell == null) {
            if (PlatformUI.isWorkbenchRunning()) {
                display = PlatformUI.getWorkbench().getDisplay();
            } else {
                display = null;
            }
        } else {
            display = optionalShell.getDisplay();
        }
    
    
        display.syncExec(() -> {
          // set the url & add listeners
        }
      }
    }