DrKaoliN
DrKaoliN

Reputation: 1402

Is there a way to prevent an IWorkbenchWindow from closing?

So I have my IWorkbenchWiondow object, window.
I add this listener to it:

window.addPageListener(new IPageListener()
{
    @Override
    public void pageOpened(IWorkbenchPage page)
    {
        // method stub
    }

    /**
     * Whenever the user tries to close the workbench window, this method gets called.
     */
    @Override
    public void pageClosed(IWorkbenchPage page)
    {
        if (MessageDialog.openQuestion(page.getWorkbenchWindow().getShell(), "Question", "Do you really want to close the application?"))
        {
            // YES, then no problem, close
            return;
        }
        else
        {
            // NO
            System.out.println("Now what?");
        }
    }

    @Override
    public void pageActivated(IWorkbenchPage page)
    {
        // method stub
    }
});

How can I stop the window from closing if the user says No?
Or how can I achieve the same end result?

Upvotes: 2

Views: 323

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21025

As @david said, the IWorkbenchListener has a preShutDown event that allows to veto the shutdown of the entire workbench by returning false.

The workbench is shut down when the last workbench window is closed or through actions such as File > Exit.

If you would like to prevent a single IWorkbenchWindow from being closed, you need to add a close listener to the shell that represents the workbench window.

For example:

Shell shell = window.getShell();
shell.addListener( SWT.Close, new Listener() {
  public void handleEvent( Event event ) {
    MessageBox messageBox = new MessageBox( parentShell, SWT.APPLICATION_MODAL | SWT.YES | SWT.NO );
    messageBox.setText( "Confirmation" );
    messageBox.setMessage( "Close the window?" );
    event.doit = messageBox.open() == SWT.YES;
  }
} );

Setting the doit flag to false will prevent the shell from being closed/disposed.

Upvotes: 2

David G
David G

Reputation: 4014

Caveat: I've only given this technique a minimal test and it appears to work as expected.

From the IWorkBenchWindow get an IWorkbench object.

Add an IWorkbenchListener to the workbench object.

The listener has a preShutdown method that should allow you to veto the close.

Upvotes: 1

Related Questions