Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » enable/disable menu item with dynamic sub menu
enable/disable menu item with dynamic sub menu [message #494194] Thu, 29 October 2009 14:23 Go to next message
Mattias H is currently offline Mattias HFriend
Messages: 1
Registered: October 2009
Junior Member
I have a menu item that I need to enable/disable depending on the selection. This item has a dynamic sub menu.

I get it to work except for one thing: the state of the menu item is not refreshed until the item gets focus or gets a click.

The definition of the menu item:
               label="Add to Favorites">

The definition of the command:
            name="Add KPI to Favorites List">
               name="Favorites list name"

Here's the menu contribution item that gets the items from a service
public class AddToFavoritesListContributionItem extends
		CompoundContributionItem {

	protected IContributionItem[] getContributionItems() {
		IServiceLocator serviceLocator = PlatformUI.getWorkbench()
		List<IContributionItem> items = new ArrayList<IContributionItem>();

		//get all the favorite lists
		IFavoriteKPIsService favoritesService = (IFavoriteKPIsService) serviceLocator
		//add an item for each favorite list
		for (FavoritesList favoList : favoritesService.getFavoritesLists()) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(AddToFavoritesListHandler.FAVORITES_LIST_NAME, favoList.getName());
                        CommandContributionItemParameter param = new CommandContributionItemParameter(
					params, null, null, null, favoList.getName(), null, null,
					CommandContributionItem.STYLE_PUSH, null, true);
			items.add(new CommandContributionItem(param));

		IContributionItem[] itemArray = new IContributionItem[items.size()];
		return itemArray;

Here's how I connect the command, handler and expression:
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {

	protected void makeActions(final IWorkbenchWindow window) {
		IHandlerService handlerService = (IHandlerService) window
		//connect the AddKPIToFavoritesList command to a handler & expression
				new AddToFavoritesListHandler(),
				new OnlyPossbileFavoritesAdditionsSelectedExpression());
	public static class OnlyPossbileFavoritesAdditionsSelectedExpression extends
			Expression {
		public EvaluationResult evaluate(IEvaluationContext context)
				throws CoreException {
			if (!(SelectionUtil.anyFavoriteKPIOrListSelected() || SelectionUtil
				return EvaluationResult.TRUE;
			} else{
				return EvaluationResult.FALSE;

		public void collectExpressionInfo(final ExpressionInfo info) {

Does anyone know why this does not work?
I made some debugging and it seems that the handler is enabled and disabled correctly when the selection changes so that does not seem to be the problem.

From what I can tell the menu item never references the state of the handler directly. Could it be that the menu item is only disabled because all the items in the sub menu are disabled? In that case could I somehow connect the menu item to the state of the handler directly?

Re: enable/disable menu item with dynamic sub menu [message #494813 is a reply to message #494194] Mon, 02 November 2009 17:58 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

It's not a general pattern to enable/disable a submenu. I think you have 2 choices.

1) generate your submenu items *or* a disabled submenu item "<empty>" if no submenu items are avaialble

2) make your submenu invisible if no items are available.


Re: enable/disable menu item with dynamic sub menu [message #1701543 is a reply to message #494813] Tue, 14 July 2015 05:12 Go to previous message
Mohita Gakhar is currently offline Mohita GakharFriend
Messages: 2
Registered: July 2015
Junior Member
How to make a sub-menu invisible if no items are enabled?
Previous Topic:Eclipse Preferences General Node
Next Topic:RCP Application not starting after adding tooling(like jdt)
Goto Forum:

Current Time: Tue Jun 19 05:16:12 GMT 2018

Powered by FUDForum. Page generated in 0.01346 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top