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