How can I add a key binding for a quickMenu similar to the "Refactor" context menu in JDT?
Asked Answered
G

2

69

I want to add a shortcut to my eclipse plugin to show a quick menu with existing bindings. It should work like the "Refactor" quick menu in JDT.

Shortcut for quick menu in JDT: Shortcut for "Refactor" quick menu

JDT quick menu:

JDT quickMenu

I already added a binding and a command but it seems there is something missing. The Delete Something entry is also working for the context menu, just the shortcut to the quick menu is missing. Does anybody how to do this?

<extension point="org.eclipse.ui.bindings">
  <key
        commandId="myplugin.refactoring.actions.DeleteSomething"
        schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
        sequence="M1+5">
  </key>
  <key
        commandId="myplugin.refactoring.quickMenu"
        schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
        sequence="M1+9">
  </key>

<extension point="org.eclipse.ui.commands">
  <command
        categoryId="myplugin.category.refactor"
        description="Delete Something"
        id="myplugin.refactoring.actions.DeleteSomething"
        name="Extract Method">
  </command>
  <command
        categoryId="myplugin.category.refactor"
        id="myplugin.refactoring.quickMenu"
        name="Show Refactor Quick Menu">
  </command>
  <category
        id="myplugin.category.refactor"
        name="Refactor">
  </category>

Giuliana answered 6/10, 2011 at 9:37 Comment(1)
are you using e4 or something else?Trapezohedron
C
1

You can also do it like this:

Add a command for the quick menu and set a default handler.

      <command
        defaultHandler="myplugin.refactoring.QuickmenuHandler"
        id="myplugin.refactoring.quickMenu"
        name="Show Refactor Quick Menu">
      </command>

The handler should be able to create the menu. Something like this:

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ...
    Menu menu = new Menu(some parent);
    new MenuItem(menu, SWT.PUSH).setText("...");
    menu.setVisible(true);
    return null;
}

Add a shortcut to the command (as you did):

 <key
    commandId="myplugin.refactoring.quickMenu"
    schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
    sequence="M1+9">
 </key>

Finally bind all of this together in the menu extension point:

   <extension
     point="org.eclipse.ui.menus">
  <menuContribution
        allPopups="false"
        locationURI="popup:ch.arenae.dnp.frame.popup?after=additions">
     <menu
           commandId="myplugin.refactoring.quickMenu"
           label="Refactor">
        <command
              commandId="<first refactoring command>"
              style="push">
        </command>
     </menu>
     ...
  </menuContribution>

The important point is the commandId attribute in the menu element. It is used to display the keyboard shortcut in the menu.

Cabalism answered 17/12, 2014 at 8:21 Comment(1)
Peter, i have implemented a minimal example. While the context menu appears and the shortcut also works to display the quick menu, the shortcut key binding is not displayed in the context menu. Thus, a user has to know the shortcut. Is there some extra step to get the shortcut to be displayed in the context menu?Comb
D
0

You can have a look at how JDT implements the same. For instance, when looking at the Eclipse 3.8.2 source code, you'll see interesting method:

org.eclipse.jdt.ui.actions.RefactorActionGroup.installQuickAccessAction()

which is called when Java editor is opened. This is were programmatic handler association with current editor takes place.

To summarize how it's done in JDT:

  1. First, they have a command declaration in plugin.xml:

    <command name="%ActionDefinition.refactorQuickMenu.name" description="%ActionDefinition.refactorQuickMenu.description" categoryId="org.eclipse.jdt.ui.category.refactoring" id="org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu">

  2. They declare a key binding:

    <key sequence="M2+M3+T" commandId="org.eclipse.jdt.ui.edit.text.java.refactor.quickMenu" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>

  3. They associate this command with a handler once editor is created. The handler itself (org.eclipse.jdt.internal.ui.actions.JDTQuickMenuCreator) takes care of filling the quick menu with items.

You don't have to associate a command with a handler programmatically - another option is using org.eclipse.ui.handlers extension point.

Dannadannel answered 2/12, 2014 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.