Thursday, December 11, 2008

Add a Menu to an Eclipse RCP Application

As shown in the article Hello World with Eclipse RCP - Your First Application, it is quite simple to create and run your own very basic Java RCP application using Eclipse. In this article I will show you how to add a "Menu" to your application. In particular, I will add the "Exit" action to a "File" menu, and it is very simple. It only requires a few lines of code in one class. The application's menu is used to provide the user with a logical and organized interface to access all the global functions of the application. For example, almost all applications have a File--->Exit or a Help---> About menu. An Eclipse RCP application menu can be as simple or elaborate as needed, and it can contain icons, dividers, and expanding submenus. The following steps demonstrate how to add the Exit action to a File menu to an Eclipse RCP application.

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) {

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: