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:
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....
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
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!!
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
Alex, sorry, I'm not sure. :(
Post a Comment