How to add items in popup menu?

2019-01-17 08:57发布

问题:

In eclipse plugin development, I want to add one item (eg: Mystyle ) in popup menu.

For an instance,

Project Explorer --> Right Click --> New --> MyStyle

How can I achieve this in eclipse plugin development?

Regards Mathan

回答1:

Something like this should do it (following this thread):

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
         locationURI=
         "popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
         <command
               commandId="myplugin.command.mycommand"
               icon="icons/etool16/mycommand.png"
               label="Run mycommand"
               mnemonic="M1+P"
               tooltip="Do something with this project">
         </command>
      </menuContribution>
   </extension>

See Menucontribution

Defines an ordered set of additions to the command UI structure. The defined elements will be added into the command UI structure at the location specified by the locationURI element.

This should be the starting point for all contributions into menus, toolbars or trim, wherever they occur in the UI.

  • locationURI - A URI specification that defines the insertion point at which the contained additions will be added.
    The format for the URI is comprised of three basic parts:
    • Scheme: One of "menu", "popup" or "toolbar.
      Indicates the type of the manager used to handle the contributions
    • Id: This is either the id of an existing menu, a view id or the id of the editor 'type'
    • Query: The query format is <placement>=<id> where: <placement> is either "before" or "after" and <id> is the id of an existing menu item

See also org.eclipse.ui.popupMenus, org.eclipse.ui.menus-extension point with a menuContribution that has its locationURI-attribute pointing to popup:org.eclipse.ui.popup.any?after=additions.
Warning, as Prashant Bhate mentions in the comment, that package org.eclipse.ui.popupMenus is deprecated.
See question Missing link between objectContribution and command for more.



回答2:

it took me awhile to solve this exact problem just now, so I'll put up answer to OP's question (add to File->New menu).

Where to Contribute (common.new.menu)

thanks to this post, I discovered that you want to contribute to popup:common.new.menu?after=new. why, you ask? heck if I know; but it works.

  • Stack Overflow - Menu contribution for popupsubmenu?
  • Stack Overflow - Adding an entry in a submenu of a popup

Result

(When C/C++ is active perspective)

(When 'not' C/C++ as active perspective)

Here is my example plugin:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="com.justin.debug.SampleHandler"
            id="com.justin.debug.commands.sampleCommand"
            name="Sample Command">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:common.new.menu?after=new">
         <command
               commandId="com.justin.debug.commands.sampleCommand"
               icon="icons/sample.gif"
               label="New Root Command From Justin"
               style="push">
            <visibleWhen
                  checkEnabled="false">
               <with
                     variable="activeWorkbenchWindow.activePerspective">
                  <equals
                        value="org.eclipse.cdt.ui.CPerspective">
                  </equals>
               </with>
            </visibleWhen>
         </command>
      </menuContribution>
      <menuContribution
            locationURI="popup:common.new.menu?after=new">
         <menu
               id="org.ecilpse.ui.navigator.ProjectExplorer.helloJustin"
               label="Hello Justin">
            <command
                  commandId="com.justin.debug.commands.sampleCommand"
                  icon="icons/sample.gif"
                  label="New Submenu Command From Justin"
                  style="push">
            </command>
         </menu>
      </menuContribution>
   </extension>
</plugin>

I added an important part here, the 'visibleWhen' parameter. This only shows New Root Command From Justin when in the C/C++ perspective. It turns out to be tricky to figure that out, thus my inclusion in example. Here are useful resources I dug up while researching that:

  • Eclipse - Command Core Expressions
  • Eclipse RCP Commands Part 3: visibleWhen and Core Expressions

I dug up the name of the C Perspective org.eclipse.cdt.ui.CPerspective by painfully digging through the plugin.xml of the org.eclipse.cdt.ui plugin.

now if anyone could tell me how to easily look these uri/resources/properties up in the future... omg.