Defining menu entries (commands) at runtime in Eclipse RCP

A common question I receive is how menu entries can be defined at runtime in an RCP application. The following gives an example how this can be done.

Create the RCP project “de.vogella.rcp.commands.runtimecommands” using the “Hello RCP” template.

Define a menu contribution. Maintain the class “de.vogella.rcp.commands.runtimecommands.DefineCommands” in this menu contribution.


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="de.vogella.rcp.commands.runtimecommands.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="RCP Perspective"
            class="de.vogella.rcp.commands.runtimecommands.Perspective"
            id="de.vogella.rcp.commands.runtimecommands.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            class="de.vogella.rcp.commands.runtimecommands.DefineCommands"
            locationURI="menu:org.eclipse.ui.main.menu">
      </menuContribution>
   </extension>

</plugin>

Create the following class.

package de.vogella.rcp.commands.runtimecommands;

import org.eclipse.swt.SWT;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;

public class DefineCommands extends ExtensionContributionFactory {

	@Override
	public void createContributionItems(IServiceLocator serviceLocator,
			IContributionRoot additions) {
		CommandContributionItemParameter p = new CommandContributionItemParameter(
				serviceLocator, "",
				"org.eclipse.ui.file.exit",
				SWT.PUSH);
		p.label = "Exit the application";
		p.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");

		CommandContributionItem item = new CommandContributionItem(p);
		item.setVisible(true);
		additions.addContributionItem(item, null);
	}

}

Run the example, your application should have the Exit command in the menu.

Thanks to Robert Einsle for the tip.

This description has also be added to my Eclipse command tutorial.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , , . Bookmark the permalink.

8 Responses to Defining menu entries (commands) at runtime in Eclipse RCP

  1. chris says:

    Good example i am sure it would be helpful…

    Take a moment and think, why people are asking rather than finding ? is it because eclipse has become too complex with little to not so useful documentation and hard to understand the design ?

    All developers coding for eclipse know this fact for a long time, its becoming hard to understand often take long to master a specific component/area and often you end up talking to people to understand something you may have not yet used.

    Not a good sign for a great product/technology, complexity is the first sign of obscurity

  2. Hi Lars,

    It’s great, i like this kind of example.

    I have made the same example in taking into account an expression object for the addContributionItem method. I show how to notify the WorkbenchWindow instance to evaluate the expression object. Thanks to the sources of Eclipse ;-)

    You can find this example into my training courses about the Eclipse Command API. However these slides are in french. Check the slides number 149 and 150.

    URL : http://mbaron.ftp-developpez.com/eclipse/commands.pdf

    Mickael

  3. Lars Vogel says:

    @Michael: Pas de problème. Thanks for the link. While I find it hard to speak (and write) french I can at least read it a bit. And coding is universal. :-)

  4. Lars,

    You will find all the examples related to the Command API at this link (http://mbaron.ftp-developpez.com/eclipse/commands_examples.zip)

    Mickael

  5. Daniel Milosevic says:

    Cheers!

  6. Matthias says:

    Hi Lars,

    I searched for a solution to this question some time ago and stumbled across the element “dynamic” which can be added to a “menu”. Do you know what the differences are?
    The only difference I see between these two solutions is that the class of your solution is loaded at start up whereas my class is loaded when the menu needs to be displayed (at runtime).

  7. Lars Vogel says:

    @Matthias: I have not yet used the element “dynamic”.

  8. @Matthias: The dynamic items are used to store dynamic contributions, so it is called at every display.

    This is a static solution for use cases like you like to load a list of extensions and display it in the UI as commands that is not possible using the xml interface (as far as I know). If the list of possible extensions relates to the runtime state than might be dynamic needed.

Comments are closed.