Commands Part 7: Adding standard commands
Posted On May 12, 2009 at by Prakash G.R.In the earlier installments, we have seen adding our commands to menus and toolbars. But what about the standard menu items like Cut, Copy, Paste etc? We'll see how to add these to a context menu of the Sample View.
Create the Sample View through the PDE's Extension Point Wizard. In the SampleView class, notice these lines:
private void hookContextMenu() {
// rest of the code...
getSite().registerContextMenu(menuMgr, viewer);
}
private void fillContextMenu(IMenuManager manager) {
//rest of the code
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
The first one registers this context menu, so that other plugins can contribute their commands to it. The second one adds a separator, which serves as a place holder/location where the contribution goes. So the SampleView is ready to accept the contributions, lets now contribute:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:com.eclipse_tips.commands.part7.views.SampleView?after=additions">
<command commandId="org.eclipse.ui.edit.cut"/>
<command commandId="org.eclipse.ui.edit.copy"/>
<command commandId="org.eclipse.ui.edit.paste"/>
</menuContribution>
</extension>
The id for the context menu, is by default the View's id, and we add our contribution after the separator. We can add any command, either the Platform defined or our own. Result:

Now we have the menu items, images, shortcut keys everything in place (you ought to love the command framework :-)), except that the items are not enabled. Its because there is no handler for these commands for the given context. There are multiple ways to contribute the handler, since we are adding this to our view, lets use the activePartId variable:
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.eclipse_tips.commands.part7.handlers.CutHandler"
commandId="org.eclipse.ui.edit.cut">
<activeWhen>
<with
variable="activePartId">
<equals
value="com.eclipse_tips.commands.part7.views.SampleView">
</equals>
</with>
</activeWhen>
</handler>
Other commands also have similar handlers. Now:

There you go. In the similar way you can add all the standard commands to any menu/context menu you prefer. Just make sure that you have the handler for the commands with the right activeWhen & enabledWhen expression.
The standard command Ids can be found in the IWorkbenchCommandConstants class (available in 3.5)

Create the Sample View through the PDE's Extension Point Wizard. In the SampleView class, notice these lines:
private void hookContextMenu() {
// rest of the code...
getSite().registerContextMenu(menuMgr, viewer);
}
private void fillContextMenu(IMenuManager manager) {
//rest of the code
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
The first one registers this context menu, so that other plugins can contribute their commands to it. The second one adds a separator, which serves as a place holder/location where the contribution goes. So the SampleView is ready to accept the contributions, lets now contribute:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:com.eclipse_tips.commands.part7.views.SampleView?after=additions">
<command commandId="org.eclipse.ui.edit.cut"/>
<command commandId="org.eclipse.ui.edit.copy"/>
<command commandId="org.eclipse.ui.edit.paste"/>
</menuContribution>
</extension>
The id for the context menu, is by default the View's id, and we add our contribution after the separator. We can add any command, either the Platform defined or our own. Result:

Now we have the menu items, images, shortcut keys everything in place (you ought to love the command framework :-)), except that the items are not enabled. Its because there is no handler for these commands for the given context. There are multiple ways to contribute the handler, since we are adding this to our view, lets use the activePartId variable:
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.eclipse_tips.commands.part7.handlers.CutHandler"
commandId="org.eclipse.ui.edit.cut">
<activeWhen>
<with
variable="activePartId">
<equals
value="com.eclipse_tips.commands.part7.views.SampleView">
</equals>
</with>
</activeWhen>
</handler>
Other commands also have similar handlers. Now:

There you go. In the similar way you can add all the standard commands to any menu/context menu you prefer. Just make sure that you have the handler for the commands with the right activeWhen & enabledWhen expression.
The standard command Ids can be found in the IWorkbenchCommandConstants class (available in 3.5)

Tired of unwanted and irrelevant results in Google? Try Eclipse Search, the Customized Seach Engine that gives you only relevant results - Powered by Google! It has plugins for Eclipse and Firefox/IE
Hi Prakash,
A while ago, I looked into commands in a project, that uses EMF for the model part.
Now EMF uses its own command framework with a class hierarchy of its own. I saw no obvious way to use those command frameworks together or just one of both.
Any hints how your blog posts fit into a project that uses EMF?
Is command delegation the way to go?
Cheers,
Gerd
Hi Prakash,
Is it possible to mix old RetargetActions (i.e. as defined in ActionFactory) and commands?
I've a GEF editor in my app and so still make use of the old style actions - I'm also contributing custom commands OK, but if I try and supply a handler (with activeWhen) for command id org.eclipse.ui.edit.copy for another view, it just will note take!
I could replace the ActionFactory.EDIT in the ApplicationActionBarAdvisor with a CommandContributionItem, but then maybe I should completely remove the old style actions? However given the GEF editor I think that might be a can of worms?
Any suggestions? This interoperability issue might be worth a new blog post even?!
Thanks,
Justin