Welcome to our active community of Eclipse RCP developers! Visit the tutorials Table of Contents or search for the topics you need help with. Then return the favor by contributing your own tip or trick. Don't hesitate, get sharing right away. It's easy!

Saturday, March 21, 2009

Add Nested Menu Items to an Eclipse RCP Application

This article shows how to add nested menu items to a menu in an Eclipse RCP application and builds off of a clean Hello World Eclipse RCP Application. As shown in the article Add a Menu to an Eclipse RCP Application, it is very easy to add a File menu containing the Exit action. Here the Exit action as well as About and Preferences are added to a submenu called Expand in the File menu to demonstrate nested menu items.

Step 0: Create a HelloWorld RCP application.

Step 1: Add the nested menu actions. Open up the ApplicationActionBarAdvisor class and add the three About, Preferences, and Exit actions as private fields. In makeactions(), define the actions and register them. In the fillMenuBar() method, create two instances of MenuManager, one for the File and one for Expand. Add the three actions to the Expand MenuManager. A Separator is added before the Exit action for the sake of demonstrating this nice menu organizing feature. Next add the Expand MenuManager to the File MenuManager. Finally add the File MenuManager to the menubar.

package com.eclipsercptutorials.nestedmenu;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
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 {

private IWorkbenchAction aboutAction;
private IWorkbenchAction preferencesAction;
private IWorkbenchAction exitAction;

public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}

protected void makeActions(IWorkbenchWindow window) {

//ActionFactory Actions, the ActionFactory defines a set of common actions and can be used in our application.
aboutAction = ActionFactory.ABOUT.create(window);
register(aboutAction); //register the action so it is deleted when the Workbench window is closed
preferencesAction = ActionFactory.PREFERENCES.create(window);
register(preferencesAction);
exitAction = ActionFactory.QUIT.create(window);
register(exitAction);
}

protected void fillMenuBar(IMenuManager menuBar) {

MenuManager fileMenu = new MenuManager("&File", "file"); //create a menuManager to take care of all submenus in "File"

MenuManager expandingMenu = new MenuManager("&Expand", "expand");
expandingMenu.add(aboutAction); //Add the "about" action
expandingMenu.add(preferencesAction); //Add the "preferences" action
expandingMenu.add(new Separator()); //Add a horizontal separator
expandingMenu.add(exitAction); //Add the "exit" action

fileMenu.add(expandingMenu); //Add the expanding menu to the "File" menu
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 new menu containing a group of actions nested in a parent menu:



Piece of cake!

Get the code: com.eclipsercptutorials.nestedmenu.zip
---> Next - Add a custom Menu Action to an Eclipse RCP Application
<--- Previous - Add a Menu 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:

Anonymous said...

You could do this all through plugin.xml using the new command framework api. Just as FYI. The article is good but you could save a lot of time by not writing the above code.

Post a Comment

Wanted: your Eclipse RCP tips and tricks. We created this website as a place to give back to the Eclipse open source community by offering people help in developing their own Rich Client Platform (RCP) applications. We hope you find value in the tutorials found here. Visit the share page for information on how to contribute a unique RCP Application tutorial or code snippet and give back to the community! The style of the tutorials are designed to be quick and easy to create by contributors and to understand by the people learning from them. Import the RCP projects found at the end of the tutorials and export your contributed projects as shown in Import and Export an Eclipse RCP Application Project.


Enter your email address to automatically
receive the newest articles:

 

Eclipse RCP Tutorials - beta Copyright © 2009