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

时间:2015-07-15 18:30:44

标签: java eclipse eclipse-plugin project-explorer

我在上下文菜单中添加了一个按钮,只有在Project Explorer窗口中右键单击才能执行。我希望能够在Project Explorer窗口中获取每个项目并获取ProjectName和ProjectLocation并将其自身放入一个对象中并返回新的对象数组。

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

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

samplehandler.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();

我觉得树或selTree会有我想要的信息,但我无法找到它。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

使用选择提供者的选择数据:

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();