Just so I remember how to do this next time… JFace apps work a little different to normal SWT applications. In an SWT app, you’d normally register an event listener and check for SWT.Close. If you do that, you might not get the behaviour you expect (I found that my current shell was gone by the time I got the event!).

JFace actually make all this a whole lot easier. You just need to override handleShellCloseEvent() and do your own magic. The default implementation just closes everything down. In PasswordSafeSWT, I wanted to prompt for the user to save their modified safe before exiting, so my implementation looks like this:

    /**
     *
     * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
     */
    protected void handleShellCloseEvent() {
        boolean cancelled = saveAppIfDirty();
        if (cancelled) {
            setReturnCode(OK);
        } else {
            tidyUpOnExit();
            super.handleShellCloseEvent();
        }
    }

And you’re in business. If the user cancels the exit, you just set a ReturnCode of OK and life continues as normal. If you’re happy that things are ok to exit, just invoke the superclass and you’re done.

This took ages to work out since I was doing all sorts of evil in my SWT.Close event listener to try and work around it. In future I’ll just read the Javadoc.