Eclipse Search


May 12, 2009

Commands Part 7: Adding standard commands

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:

<command commandId="org.eclipse.ui.edit.cut"/>
<command commandId="org.eclipse.ui.edit.copy"/>
<command commandId="org.eclipse.ui.edit.paste"/>

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:


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)


  1. 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?


  2. 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?!


  3. I want to add a comment to help others. I struggled for a few hours with standard commands not enabled for key bindings and global menus, but they worked in the context menu. The (now) obvious error I made was that I used "activeMenuSelection". I now use "selection" and it works perfectly.

    And thanks for the great article!

  4. You had to implement the handler, com.eclipse_tips.commands.part7.handlers.CutHandler, from scratch, right? If not, I'd be curious to see how you made use of any pre-existing functionality for the cut/copy/paste handlers.

  5. @Walk_n_wind
    yes, you have to write from the scratch. You can look at the existing implementations, but don't think it will be generic enough to apply for all the situatios

  6. Note that retarget actions are not (yet) fully integrated with the command framework. You DO get the cut/copy/paste actions into your context menu, and they're working with the correct text labels, shortcuts, icons and everything - but the same actions won't work from the menu bar (Edit -> Cut/Copy/Paste).

    The reason is Eclipse Bug #270007, and there is a workaround: register your action using actionBars.setGlobalActionHandler().

  7. Could you please share the article source code? I am not able to reproduce it?
    If we are adding these standard command, whats the use of handler? It should use eclipse standar handlers?


  8. Hi,

    How to add my own command before any standard command? For example before the Rename command in edit group.