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 项目时才会调用该方法,所以我需要做的就是获取当前选择,然后获取其绝对路径.有任何想法吗?或者有更好的方法吗?

mjguo8888 回答:Eclipse Plugin:如何获取当前选中项目的路径

这应该让你更接近(如果不能为你完全解决它;))

    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window != null)
    {
        IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
        Object firstElement = selection.getFirstElement();
        if (firstElement instanceof IAdaptable)
        {
            IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
            IPath path = project.getFullPath();
            System.out.println(path);
        }
    }

这篇关于Eclipse Plugin:如何获取当前选中项目的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!

本文链接:https://www.f2er.com/3175751.html

大家都在问