Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Getting shortcut to appear next to context menu item
Getting shortcut to appear next to context menu item [message #1714760] Mon, 16 November 2015 12:59 Go to next message
Simon Laffoy is currently offline Simon LaffoyFriend
Messages: 19
Registered: January 2015
Junior Member
I have added a command to the editor context menu which has a sub menu. I have also added a shortcut key binding which allows the user to open that submenu without calling the context menu first.

Now I wish for that keyboard shortcut to be visible beside the top level context menu item. According to I just need to specify the commandId associated with my keybinding in my menu contribution, but this is not working. Apparently the specified command "should have a handler that can launch a quickmenu version of this menu.", which is exactly what my command does do.

Unfortunately I still cannot see the shortcut in the context menu. The shortcut works fine and the context menu works fine, but I just cannot get the shortcut to appear in the context menu. What am I doing wrong? Here is the relevant xml ...

<!-- The command used by the shortcut -->
	<extension point="org.eclipse.ui.commands">
		<command id="a.b.c.rcp.commands.quickMenu"
			<!-- The handler (code below) which launches the quick access menu -->
<!-- The keyboard shortcut is specified here -->
	<extension point="org.eclipse.ui.bindings">
<!-- The contribution to the context menu -->
	<extension point="org.eclipse.ui.menus">
		<menuContribution locationURI="popup:#CompilationUnitEditorContext?before=group.copy"
			<menu id=""
				<!-- Supposedly this line of code should make the shortcut visible in the context menu, but has no effect -->

				<dynamic id="a.b.c.rcp.dynamic.dynamicid"

The code for the handler EditorQuickAccessHandler is as follows. ...

public class EditorQuickAccessHandler extends AbstractHandler {

    public EditorQuickAccessHandler() {

    public Object execute(ExecutionEvent event) throws ExecutionException {
        QuickMenuCreator quickMenu = new QuickMenuCreator() {

            protected void fillMenu(IMenuManager menu) {
                MenuContributions contributions = new MenuContributions();
                for (IContributionItem item : contributions.getContributionItems()) {


        return null;


Is there anything obviously wrong here?


[Updated on: Mon, 16 November 2015 13:00]

Report message to a moderator

Re: Getting shortcut to appear next to context menu item [message #1715390 is a reply to message #1714760] Mon, 23 November 2015 08:50 Go to previous message
Simon Laffoy is currently offline Simon LaffoyFriend
Messages: 19
Registered: January 2015
Junior Member
OK, I figured it out programmatically.

The first step is to create an Activator class for your plugin overriding AbstractUIPlugin. Don't forget to set this class as the activator of your plugin in plugin.xml

Bundle-Activator: my.pkg.MyPluginActivator

Within this class you will override the start and stop methods of the superclass. In the start method you will register the context menu contributions and in the stop method you will deregister them.

private AbstractContributionFactory compilationUnitEditorContributionFactory = null;

    public void start(BundleContext context) throws Exception {

        final IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
        if (menuService == null) {

        compilationUnitEditorContributionFactory = new MyEditorContributionFactory(
                popup:#CompilationUnitEditorContext?before=group.copy, Constants.MY_PLUGIN_ID);

MyEditorContributionFactory extends the AbstractContributionFactory class. It is responsible for creating the top level menu entry, populating its submenu and displaying the short cut.

    private class MyEditorContributionFactory extends AbstractContributionFactory {

        public MyEditorContributionFactory(String location, String namespace) {
            super(location, namespace);

        public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
            if (!PlatformUI.isWorkbenchRunning()) {

// create the top level menu entry with some user readable text
            MenuManager manager = new MenuManager(Messages.MENU_LABEL);

// this is the command id of the shortcut key binding specified in the first post of this thread. This line displays the shortcut in the context menu.

// you can also have an image appear in the context menu

            MyEditorMenuContributions myContributions = new MyEditorMenuContributions();
            IContributionItem[] items = myContributions.getContributionItems();
            for (IContributionItem item : items) {

// in the case that there are no contribution items, the top level menu item will not appear.
// In my case, I want the top level menu item to appear anyway, just so the user knows it exists.
// So I add an action to the manager which does nothing.
            if (items.length == 0) {
                manager.add(new Action() {
                                         public boolean isEnabled() { return false; }
                                         public String getText() { return Messages.MENU_LABEL_NO_APPLICABLE_QUERIES; }

            additions.addContributionItem(manager, null);

The class MyEditorMEnuContributions extends the CompoundContributionItem class and overrides the getContributionItems() method to return the contribution items we want to appear in the sub menu.

To dergister the menu contribution, just override the stop method of the AbstractUiPlugin class

    public void stop(BundleContext context) throws Exception {
        if (!PlatformUI.isWorkbenchRunning()) {

        IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
        if (menuService == null) {

        compilationUnitEditorContributionFactory = null;


In my first post I had three bits of xml. One each for the extension points org.eclipse.ui.commands, org.eclipse.ui.bindings and org.eclipse.ui.menus. The first two of these are still required, but the third, for org.eclipse.ui.menus, can be removed.
Previous Topic:New workspaces being persistently corrupted
Next Topic:Redistributing a customized
Goto Forum:

Current Time: Wed Aug 22 00:27:26 GMT 2018

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

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

Back to the top