Dynamic menu items in Eclipse

I wanted to add a dynamic menu contribution item to the popup menu of project explorer. I could not immediately find out how to do this, so I decided to document it here. Eventually it turned out to be quite easy.

First you have to add the menu contribution to your plugin.xml. I wanted to add an extra menu item to the project explorer so I used "popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions" as locationURI. Next you only have to specify a class that will create the menu item and specify an id.

<extension point="org.eclipse.ui.menus">
  <menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
    <dynamic
              class="com.sigasi.MydynamicMenu"
              id="com.sigasi.myDynamicMenu">
    </dynamic>
  </menuContribution>
</extension>

Next you have to create the Java class that extends org.eclipse.jface.action.ContributionItem (You can use autocomplete for this).

The method that you have to override for a dynamic menu is fill(Menu menu, int index).
In this method you have to create the new (dynamic) MenuItem. You can also simply return if (e.g. based on the selection) you do not want to show a menu item. In the example code below I simply display the current date in the dynamic menu item.

Do not forget to add a addSelectionListener to the MenuItem, to start some action when the menu is selected.

That's it.

The complete java source:

package com.sigasi;
 
import java.util.Date;
 
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
 
public class MyDynamicMenu extends ContributionItem {
 
	public MyDynamicMenu() {
	}
 
	public MyDynamicMenu(String id) {
		super(id);
	}
 
	@Override
	public void fill(Menu menu, int index) {
		//Here you could get selection and decide what to do
		//You can also simply return if you do not want to show a menu
 
		//create the menu item
		MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
		menuItem.setText("My menu item (" + new Date() + ")");
		menuItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				//what to do when menu is subsequently selected.
				System.err.println("Dynamic menu selected");
			}
		});
	}
}

Comments

get the location URI

Hi,

I just want to add a little information on how to get the locationUri.
Since Eclipse 3.5, you can use the menu plug-in spy Alt+shift+F2.

Regards,

Aurelien Pupier

Thanks for this! Works like a

Thanks for this! Works like a charm.

You suggest that we could get

You suggest that we could get the selection and decide what to do, but how? I can't find a simple way to find the item(s) that were selected. Is there something I am missing?

You can get the selection

You can get the selection with following code:

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getActivePage().getSelection();

Dynamic Menu

If the dynamic menu might change from time to time (which i think is the idea!!) you must override isDynamic, or else fill() only gets called once.:

public class ActionDynamicMenu extends ContributionItem {
 
	@Override
	public boolean isDynamic() {
		System.err.println("Dynamic Check");
		return true;
	}
 
      @Override
	public void fill(Menu menu, int index) {
		System.err.println("Fill Dynamic Menu");
                // Now you can fill based on the selection
       }
 
}