eclipse 如何在弹出菜单中添加项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1342532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:54:01  来源:igfitidea点击:

How to add items in popup menu?

eclipseplugins

提问by VonC

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

在eclipse插件开发中,我想在弹出菜单中添加一项(例如: Mystyle )。

For an instance,

例如,

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

项目资源管理器 --> 右键单击​​ --> 新建 --> MyStyle

How can I achieve this in eclipse plugin development?

如何在 eclipse 插件开发中实现这一点?

Regards Mathan

问候马森

回答by VonC

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 locationURIelement.

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

定义命令 UI 结构的一组有序添加项。定义的元素将被添加到locationURI元素指定位置的命令 UI 结构中。

这应该是对菜单、工具栏或修剪的所有贡献的起点,无论它们出现在 UI 中的何处。

  • locationURI- 定义插入点的 URI 规范,在该插入点将添加包含的添加项。
    URI 的格式由三个基本部分组成:
    • 方案:“菜单”、“弹出”或“工具栏”之一。
      指示用于处理贡献的经理的类型
    • Id:这是现有菜单的 id、视图 id 或编辑器“类型”的 id
    • 查询:查询格式是<placement>=<id><placement>是“before”或“after”,<id>是现有菜单项的id

Show Readme Action

显示自述操作

See also org.eclipse.ui.popupMenus, org.eclipse.ui.menus-extension point with a menuContributionthat has its locationURI-attribute pointing to popup:org.eclipse.ui.popup.any?after=additions.
Warning, as Prashant Bhatementions in the comment, that package org.eclipse.ui.popupMenusis deprecated.
See question Missing link between objectContributionand commandfor more.

也可以看看 org.eclipse.ui.popupMenus, -org.eclipse.ui.menus扩展点,menuContribution其 -locationURI属性指向popup:org.eclipse.ui.popup.any?after=additions
警告,正如Prashant Bhate在评论中提到的,该软件包org.eclipse.ui.popupMenus已被弃用。
有关更多信息,请参阅问题和命令之间的缺少链接objectContribution

回答by J-Dizzle

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).

刚才我花了一段时间才解决这个确切的问题,所以我将回答 OP 的问题(添加到文件->新菜单)。

Where to Contribute (common.new.menu)

贡献的地方(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.

感谢这篇文章,我发现你想为 popup:common.new.menu?after=new 做出贡献。你为什么问?哎呀,如果我知道;但它有效。

Result

结果

(When C/C++ is active perspective)

(当 C/C++ 是主动视角时)

enter image description here

在此处输入图片说明

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

(当“不是”C/C++ 作为主动视角时)

enter image description here

在此处输入图片说明

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 Justinwhen 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:

我在这里添加了一个重要的部分,'visibleWhen' 参数。这仅在 C/C++ 透视图中显示来自 Justin 的新根命令。事实证明,弄清楚这一点很棘手,因此我将其包含在示例中。以下是我在研究时挖掘的有用资源:

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

我通过痛苦地挖掘 org.eclipse.cdt.ui 插件的 plugin.xml挖出了 C Perspective org.eclipse.cdt.ui.CPerspective的名称。

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

现在,如果有人能告诉我如何在未来轻松查看这些 uri/资源/属性......天啊。