Skip to main content



      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 07:59 Go to next message
Eclipse UserFriend
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 http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_ui_menus.html 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 -->
			defaultHandler="x.y.z.injection.ExtensionFactory:a.b.c.rcp.handlers.EditorQuickAccessHandler"
			categoryId="a.b.c.rcp.category.categoryid"
			name="%commands.quickmenu.name"
			description="%commands.quickmenu.description">
		</command>
	</extension>
<!-- The keyboard shortcut is specified here -->
	<extension point="org.eclipse.ui.bindings">
		<key
			commandId="a.b.c.rcp.commands.quickMenu"
			contextId="org.eclipse.jdt.ui.javaEditorScope"
			schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
			sequence="M1+M2+M3+H"/>
	</extension>
<!-- The contribution to the context menu -->
	<extension point="org.eclipse.ui.menus">
		<menuContribution locationURI="popup:#CompilationUnitEditorContext?before=group.copy"
			allPopups="true">
			<menu id="a.b.c.rcp.menu.menuid"
				label="%menu..label"
				icon="icons/view16/icon.png"
				
				<!-- Supposedly this line of code should make the shortcut visible in the context menu, but has no effect -->
				commandId="a.b.c.rcp.commands.quickMenu"> 

				<dynamic id="a.b.c.rcp.dynamic.dynamicid"
					class="a.b.c.rcp.MenuContributions"/>
			</menu>
		</menuContribution>
	</extension>


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

public class EditorQuickAccessHandler extends AbstractHandler {

    public EditorQuickAccessHandler() {
    }

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

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

        quickMenu.createMenu();

        return null;
    }

}


Is there anything obviously wrong here?

Thanks

[Updated on: Mon, 16 November 2015 08:00] by Moderator

Re: Getting shortcut to appear next to context menu item [message #1715390 is a reply to message #1714760] Mon, 23 November 2015 03:50 Go to previous message
Eclipse UserFriend
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;

@Override
    public void start(BundleContext context) throws Exception {
        super.start(context);

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

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


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);
        }

        @Override
        public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
            if (!PlatformUI.isWorkbenchRunning()) {
                return;
            }

// 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.
            manager.setActionDefinitionId("a.b.c.rcp.commands.quickMenu"); 

// you can also have an image appear in the context menu
            manager.setImageDescriptor(myImageDescriptor);

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

// 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() {
                                         @Override
                                         public boolean isEnabled() { return false; }
                                         @Override
                                         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

    @Override
    public void stop(BundleContext context) throws Exception {
        if (!PlatformUI.isWorkbenchRunning()) {
            return;
        }

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

        menuService.removeContributionFactory(compilationUnitEditorContributionFactory);
        compilationUnitEditorContributionFactory = null;

        super.stop(context);
    }


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 Eclipse.app
Goto Forum:
  


Current Time: Mon Mar 17 20:51:57 EDT 2025

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

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

Back to the top