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:
<extension
         point="org.eclipse.ui.menus">
		 ...
         <menu
               id="%.AddKPIToFavoritesList"
               label="Add to Favorites">
            <dynamic
                  class="%.AddToFavoritesListContributionItem"
                  id="%.AddToFavoritesListMenu">
            </dynamic>
         </menu>
         ...
   </extension>


The definition of the command:
<extension
         point="org.eclipse.ui.commands">
		 ...
      <command
            id="%.AddKPIToFavoritesList"
            name="Add KPI to Favorites List">
         <commandParameter
               id="favoritesListName"
               name="Favorites list name"
               optional="true">
         </commandParameter>
      </command>
	  ...
   </extension>


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

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

		//get all the favorite lists
		IFavoriteKPIsService favoritesService = (IFavoriteKPIsService) serviceLocator
				.getService(IFavoriteKPIsService.class);
		//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(
					serviceLocator,
					AddToFavoritesListHandler.FAVORITES_LIST_NAME,
					"%.AddKPIToFavoritesList",
					params, null, null, null, favoList.getName(), null, null,
					CommandContributionItem.STYLE_PUSH, null, true);
			items.add(new CommandContributionItem(param));
		}

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


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

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

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

		@Override
		public void collectExpressionInfo(final ExpressionInfo info) {
			info.markDefaultVariableAccessed();
			info.addVariableNameAccess("activeMenuSelection");
		}
	}


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?

thanks
Mattias
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.

PW


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: Fri Apr 19 22:10:04 GMT 2024

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

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

Back to the top