Step 1: Add code to create the File menu and the Exit action. When you created a hello world application using the new RCP application wizard in Eclipse (which I always do to create a skeleton RCP project to build off of for a new application), Eclipse created a ApplicationActionBarAdvisor class that extends ActionBarAdvisor. You need to override 2 of the methods to configure the application's menu to suit the needs of the particular application. During certain strategic points in the workbench's lifecycle, these methods are called and your complete menu is created. In makeActions() the Exit action is created and registered. In fillMenuBar() the File menu is created, the Exit action is added to the File menu, and the File menu is added to the application's menu bar.
package com.blogspot.obscuredcalrity; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; public class ApplicationActionBarAdvisor extends ActionBarAdvisor { IWorkbenchAction exitAction; public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); } protected void makeActions(IWorkbenchWindow window) { exitAction = ActionFactory.QUIT.create(window); //the ActionFactory defines a set of common actions and can be used in the application. register(exitAction); //register the action so they are deleted when the Workbench window is closed } protected void fillMenuBar(IMenuManager menuBar) { MenuManager fileMenu = new MenuManager("&File", "file"); //create a menuManager to take care of all submenus in "File" fileMenu.add(exitAction); //Add the "exit" action menuBar.add(fileMenu);//Add the "File" menu to the menuBar } }
Step 2: Run the application and test if everything worked. Your application should now have a File menu with an Exit action and look something like this:
Piece of Cake!
Next ---> Verify User Intent Before Closing an Eclipse RCP Application
<--- Previous - Add an Icon to an Eclipse RCP Application View
Also see: Eclipse RCP Tutorial Table of Contents
No comments:
Post a Comment