How can I add a key binding for a quickMenu similar to the “Refactor” context menu in JDT?

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.

Leave a Comment