Eclipse Plugin:如何获取当前所选项目的路径
我正在编写一个 Eclipse 插件,它将在 Java 项目的上下文菜单中显示一个菜单项。我已经编写了plugin.xml如下:
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
<dynamic
class="uk.co.dajohnston.plugin.classpathswitcher.menu.MenuContribution"
id="uk.co.dajohnston.plugin.classpathSwitcher.switchMenuContribution">
<visibleWhen>
<with
variable="activeMenuSelection">
<iterate>
<adapt
type="org.eclipse.jdt.core.IJavaProject">
</adapt>
</iterate>
<count
value="1">
</count>
</with>
</visibleWhen>
</dynamic>
</menuContribution>
</extension>
</plugin>
所以我现在尝试编写扩展CompoundContributionItem
的MenuContribution
类,以便我可以创建一个动态菜单及其内容菜单将基于 Java 项目根目录中存在的一组文件。但我一直试图从 getContributionItems 方法中获取根目录的路径。
根据plugin.xml 文件,我可以保证只有选择单个Java 项目时才会调用该方法,因此我需要做的就是获取当前选择,然后获取其绝对路径。有什么想法吗?或者有更好的方法来做到这一点吗?
I am writing an Eclipse plugin that will display a menu item in the context menu for a Java Project. I have written the plugin.xml as follows:
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
<dynamic
class="uk.co.dajohnston.plugin.classpathswitcher.menu.MenuContribution"
id="uk.co.dajohnston.plugin.classpathSwitcher.switchMenuContribution">
<visibleWhen>
<with
variable="activeMenuSelection">
<iterate>
<adapt
type="org.eclipse.jdt.core.IJavaProject">
</adapt>
</iterate>
<count
value="1">
</count>
</with>
</visibleWhen>
</dynamic>
</menuContribution>
</extension>
</plugin>
So I am now trying to write the MenuContribution
class which extends CompoundContributionItem
so that I can create a dynamic menu and the contents of this menu are going to be based on a set of files that exist in the Java Project's root directory. But I am stuck trying to get the path to the root directory from within the getContributionItems
method.
Based on the plugin.xml file I can be guarenteed that the method will only be called if a single Java Project is selected, so all I need to do is get the current selection and then get its absolute path. Any ideas? Or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该会让你更接近(如果不能为你完全解决它;))
This should get you closer (if not solve it completely for you ;))
自从我写了前面的答案以来,我又编写了几个实用程序插件,现在有了 Eclipse Navigator Popups 的通用启动器。文件如下:
基本清单文件
MANIFEST.MF
基本属性文件
build.properties
插件文件
特别注意;的 扩展点中的 链接到 在 扩展点中,这允许我们将多个菜单贡献链接到单个处理程序。
另外值得注意的是,存在多个 locationURI,因为它们可以从一个角度更改为另一个角度。
plugin.xml
要了解有关当前选择的插件的更多信息,请单击文件并使用 ALT-SHIFT-F1 进行插件选择间谍
使用 ALT-SHIFT-F2 进行插件菜单间谍。 请注意,在右键单击并选择感兴趣的弹出菜单项之前,请按 ALT-SHIFT-F2 组合。
最后是插件的代码。
NavigatorPopupHandler.java
以及在其自己的java文件上运行插件的结果。
Since I wrote the previous answer I have written another couple of utility plugins and now have a generic starter for Eclipse Navigator Popups. The files are as follows:
Basic manifest file
MANIFEST.MF
Basic properties file
build.properties
Plugin file
In particular note how <command commandId=...> of the <menuContribution...> <menu..> in the <extension point="org.eclipse.ui.menus"> extension point links to the <handler commandId=...> in the <extension point="org.eclipse.ui.handlers"> extension point this permits us to link multiple menu contributions to a single handler.
Also of note is the fact that there are multiple locationURI's as these can change from one perspective to another.
plugin.xml
To find out more about the plugin for the current selection click on a file and use ALT-SHIFT-F1 for the Plug-in Selection Spy
Use ALT-SHIFT-F2 for the Plug-in Menu Spy. Note press the ALT-SHIFT-F2 combination before right clicking and selecting the popup menu item of interest.
Finally the code for the plug-in.
NavigatorPopupHandler.java
And the result of running the plug-in on its own java file.
我花了很长的一天时间研究这里的答案和很多其他答案,发现最终让我到达那里的是做一个 Java 反射循环来研究从树中选择的类和接口结构航海家。类内省循环处于其最终的休息位置,但最初更接近执行方法的顶部。
Had a very long day working my way through the answers here and a whole lot of other answers and found the bit that finally got me there was to do a Java Reflection loop to study the Class and Interface structure of the selection from the Tree in the navigator. The Class introspection loop is in its final resting place but was originally much closer to the top of the execute method.