Search code examples
javadialogswtjfacewizard

JFace close dialog in SelectionListener


How do I close a Dialog in a SelectionListener? Currently my SelectionListener looks like this:

public void widgetSelected(SelectionEvent e) {
    ICommandService commandService = (ICommandService) PlatformUI.getWorkbench()
            .getService(ICommandService.class);
    IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench()
            .getService(IHandlerService.class);

    Command command = commandService
            .getCommand("com.mainfirst.backoffice.gui.accounting.command.createBookingReversal");

    Map<String, String> params = new HashMap<String, String>();
    params.put("com.mainfirst.backoffice.gui.accounting.reversalCodeParameter",
            String.valueOf(model.getVal().getCode()));

    ParameterizedCommand paraCommand = ParameterizedCommand.generateCommand(command, params);

    try {
        handlerService.executeCommand(paraCommand, null);
    } catch (Exception ex) {
        Status status = new Status(IStatus.ERROR, AccountingActivator.PLUGIN_ID,
                "Error opening reversal wizard", ex);
        StatusManager.getManager().handle(status, StatusManager.SHOW);
        throw new RuntimeException(ex);
    }
    close();
}

As you can see I do call close();, but the Dialog does stay open in the end. Moving the call inside a finally block doesn't help either. My command opens a new Wizard in front of the Dialog. Might I have to close the Dialog before the Wizard opens?


Solution

  • If you want the dialog to close before the command it is running completes you will need to run the command asynchronously. Use Display.asyncExec for this. Use something like:

    public void widgetSelected(SelectionEvent e) {
    
        Display.getDefault().asyncExec(() ->
           {
              ... code to call command
           });
    
        close();
    }
    

    (assuming Java 8, use a Runnable for Java 7 or earlier).