[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: JFace Dialog - change default focus

Joe Gagnon wrote:
Matthew Hall wrote:

You can cause the filter to be removed as the dialog is closed by adding a SWT.Dispose listener to the composite created in createDialogArea:

composite.addListener(SWT.Dispose, new Listener() {
   public void handleEvent(Event e) {
     display.removeListener(SWT.Traverse, overrideTraversalListener);
   }
});

We have overridden the buttonPressed() method (for other reasons) and this is where I'm removing the listener. Is this an acceptable alternative?


 protected void buttonPressed(int buttonId) {
   ...

   log.debug("Removing traverse filter");
   getShell().getDisplay().removeFilter(SWT.Traverse, listener);

   super.buttonPressed(buttonId);
 }

What if the OK button is enabled but your field validators prevent the window from closing? The traversals will return to normal behavior and your interface will be inconsistent. I would remove the traversal override when the dialog area is disposed.


Another approach to knowing when to convert Enter-traversals to Tab-traversals is to overload createButton() to keep a list of all buttons in the button bar:

protected Button createButton (Composite parent, int id, String label, boolean defaultButton) {
Button button = super.createButton(parent, id, label, defaultButton);
buttons.add(result);
return button;
}


Then override traversal for all but those buttons:

Listener traversalOverrideListener = new Listener() {
  public void handleEvent(Event e) {
    if ( e.detail == SWT.TRAVERSE_RETURN &&
         display.getActiveShell() == shell &&
         !buttons.contains(e.widget) ) {
      event.detail = SWT.TRAVERSE_NEXT;
    }
  }
}

Matthew