This article shows how ask the user if s/he really intends to close a RCP application and builds off of a clean Hello World Eclipse RCP Application. Use this feature sparingly with your applications because it can be an annoying "feature". 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. Look for a tutorial on how to ad the preferences soon.Step 0: Create a HelloWorld RCP 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 during step 0, 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 yes/no 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.eclipsercptutorials.verifyintent;
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.eclipsercptutorials.verifyIntent.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!
Get the code: com.eclipsercptutorials.verifyIntent.zip
---> Next - Import and Export an Eclipse RCP Application Project
<--- Previous - Add a Custom Menu Action to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents
If you enjoyed this post, make sure you subscribe to the Eclipse RCP Tutorials RSS feed to receive new posts in a reader or via email!
1 comments:
I really like your blog. Very useful!
Post a Comment