Tuesday, December 16, 2008

Verify User Intent Before Closing an Eclipse RCP Application

Have you ever accidentally closed an application only to lose all the data you were working with or abort some long running calculation half way through? On one particular application I was working on, it would have been devastating to accidentally close the application so I needed to add a dialog box that asked the user to verify that s/he really wanted to close the application. I've also used some applications before with this feature, and found it annoying when I was asked every single time whether or not I was really sure I wanted to close the application. Use it sparingly. The best solution would be to add a preference to the application where the user could decide if s/he wanted the reminder or not. I'll add a tutorial on how to do that soon. Anyway, It took me a while to figure out how to add a confirmation dialog box, but it's actually quite easy. Here's how...



Step 0: Create a Hello World Application.

Step 1: Add code to create the dialog box when the close application action is triggered. In the ApplicationWorkbenchAdvisor class that was automatically generated when you created a hello world application using the new RCP application wizard in Eclipse, you need to override the preShutdown() method. The method returns a boolean where, if true, the application will shut down, and if false, the application will continue running. You need to add code in that method that opens up a dialog box, gets user input, and returns what the user selected. Eclipse's JFace library contains a nice little class called MessageDialog where we can easily make a dialog box pop up, ask a question, and get the answer. We use the MessageDialog.openQuestion() method and pass it three parameters: a Shell, the dialog box's title, and the question we want to ask.


package com.blogspot.obscuredclarity;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

private static final String PERSPECTIVE_ID = "com.timmolter.helloWorld.perspective";

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
return new ApplicationWorkbenchWindowAdvisor(configurer);
}

public String getInitialWindowPerspectiveId() {
return PERSPECTIVE_ID;
}

public boolean preShutdown(){

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
String dialogBoxTitle = "Question";
String question = "Are you sure you want to close this application?";
return MessageDialog.openQuestion(shell, dialogBoxTitle, question);

}
}




Step 2: Run the application and test if everything worked. Your application should now have a dialog box that verifies user intent when the application is closed and look something like this:


Piece of Cake!

Next ---> Add Nested Menu Items and Custom Actions to an Eclipse RCP Application
<--- Previous - Add a Menu to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

5 comments:

Anonymous said...

heyy..in my appl. d dialog box doesnt pop up...also cld u tell me how to add my own buttons to a menu 'TO DO'....d buttons r defined in my view class....

Unknown said...

Thank you very much. this is very helpful. I've read all you posts, I can tell you that I could understand RCP applications very well. and the good thing is that you also give theoretical explanations about what is going on behind the scene which we dont find in many websites.
But I was wondering if you can show us how to add multiple views in the perspective instead of using only one stand alone view?

Thank you very much

Tim Molter said...

Oualid, Thanks for the comment. I'm glad I could help you out figuring out RCP! Unfortunately, I'm not using RCP anymore and I have absolutely no spare time to show you how to add multiple views in the perspective. Sorry!!

Alex said...

Hi, I started to work with eclipse plugins, so I have one question!
I've created the ApplicationWorkbenchAdvisor class and I have the Activator class, where and how can I register the event to trigger the close event?
Thanks in advance

Tim Molter said...

Alex, sorry, I'm not sure. :(