java - 在 Project Explorer (Eclipse) 中查找项目位置

标签 java eclipse eclipse-plugin project-explorer

我在上下文菜单中添加了一个按钮,仅当您在“项目资源管理器”窗口中单击鼠标右键时才执行。我希望能够获取“项目资源管理器”窗口中的每个项目,获取“项目名称”和“项目位置”,然后将其单独放入一个对象中,然后返回新的对象数组。

我找不到访问所需信息的方法。

插件.xml

<extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu">
      <command commandId="helloworld.sample"
        label="Hello World" style="push">
      </command>
    </menuContribution>
  </extension>
  <extension point="org.eclipse.ui.commands">
    <command defaultHandler="helloworld.handler.samplehandler"
      id="helloworld.sample" name="Sample">
    </command>
  </extension>

示例处理程序.java

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IWorkbenchPart viewpart  = page.findView("org.eclipse.ui.navigator.ProjectExplorer");

Tree tree = (Tree) ((TreeViewer) viewpart.getSite().getSelectionProvider()).getControl(); 
TreeItem[] selTree = tree.getSelection();

我觉得 tree 或 selTree 似乎有我想要的信息,但我找不到它。有什么帮助吗?

最佳答案

使用来自选择提供程序的选择数据:

IStructuredSelection sel =   
     (IStructuredSelection)viewpart.getSite().getSelectionProvider().getSelection(); 

Object selObj = sel.getFirstElement();

IProject project =
   (IProject)Platform.getAdapterManager().getAdapter(selObj, IProject.class);

... might return null if selection is not the project

String name = project.getName();

IPath location = project.getLocation();

要获取所有项目,请使用:

IWorkspace workspace = ResourcesPlugin.getPlugin().getWorkspace();

IProject [] projects = workspace.getRoot().getProjects();

关于java - 在 Project Explorer (Eclipse) 中查找项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438337/

相关文章:

eclipse-plugin - gef4 的旧版本?

java - Collectors.reducing 到列表

java.rmi.ConnectException : Connection refused to host when RMI server is child process of RMI client

java - 如何替换列表中的多个项目?

java - Eclipse 运行配置的默认值

Eclipse pmd插件握手失败

Eclipse 中依赖于 super 项目的 Java 子项目

eclipse-plugin - 如何在 Eclipse 项目资源管理器上下文菜单中添加项目

java - 以编程方式设置类路径文件 Java

c# - 在连接 Sql Server 数据库时,Java 和 C# 有什么区别?