Problems with adding commands to the toolbar. [message #856192] |
Wed, 25 April 2012 13:02 |
Lorand Szakacs Messages: 1 Registered: April 2012 |
Junior Member |
|
|
Hello,
I have 4 commands, for brevity let's give them the ids: c1, c2, c3, c4.
In my plugin I hook up to the following extensions:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:es_commands">
<dynamic
class="MenuContribution"
id="dynamicMenu">
</dynamic>
</menuContribution>
<menuContribution
allPopups="false"
class="ToolBarContributionFactory"
locationURI="toolbar:toolbar.commands">
</menuContribution>
So basically:
- ToolBarContributionFactory extends ExtensionContributionFactory;
- MenuContribution extends CompoundContributionItem
- (I tried adding </dynamic><dynamic> to the toolbar; it wouldn't even display, hence the difference in approach)
Both classes contribute 4 newly constructed CommandContributionItem objects, associated with the commands mentioned before.
The problem:
The entries in the menu function properly, i.e. they are enabled/disabled as I'd expect according to the isEnabled() method of their respective handlers.
The entries in the toolbar are not updated properly. They start out enabled, even though they shouldn't be (the ones in the menu aren't).
I added a SelectionListener to the ISelectionService in which I force a refresh of the buttons:
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection)
{
CommandService commandService = (ICommandService) PlatformUI.getWorkbench()
.getService(ICommandService.class);
Map<String, IWorkbenchWindow> filter = new HashMap<String, IWorkbenchWindow>();
filter.put(IServiceScopes.WINDOW_SCOPE, PlatformUI.getWorkbench()
.getActiveWorkbenchWindow());
commandService.refreshElements("c1", filter);
//and so on for all commands
}
The plugin doesn't seem to load before I click the CommandsMenu. After that, if I click on an item in the treeviewer the buttons are displayed properly.
Another weird thing is that once I add bindings to my commands (Ctrl+1, ...; with "org.eclipse.ui.contexts.window" as a context), the toolbar items are always enabled, the ones in the menu still work properly.
I would greatly appreciate an elegant solution to the problem, not only a quick hack to my approach.(I'm also fairly new to RCP and I'd like to learn new stuff). Preferably without actions.
And yes, I am sure that I associated the same commands to the items.
Failed approaches:
- Tried adding actionSets to the Menu and toolbar. But had trouble with:
public class ToolbarActionDelegate implements IWorkbenchWindowActionDelegate
{
@Override
public void run(IAction action)
{
//values always are "c1", "c2", etc...
String actionDefinitionId = action.getActionDefinitionId();
CommandUtil.executeCommand(actionDefinitionId);
}
@Override
public void selectionChanged(IAction action, ISelection selection)
{
String commandID = action.getActionDefinitionId();
ICommandService cs = (ICommandService) PlatformUI.getWorkbench()
.getService(ICommandService.class);
Command command = cs.getCommand(commandID);
boolean result = command.isEnabled();
action.setEnabled(result);
}
}
Once the action set is added the relation between "cx" commands and their handlers got obscured and the isEnabled() or execute() methods would never run.(I put breakpoints and logging to check it);
- adding the items to the toolbar individually instead of using an ExtensionContributionFactory exhibited the same problem as described above.
Thank you for your help!
|
|
|
|
Powered by
FUDForum. Page generated in 0.02379 seconds