Find Project locations in Project Explorer (Eclipse)

心已入冬 提交于 2019-12-02 17:53:22

问题


I've added a button in the context menu to execute only when you right click in the Project Explorer window. I want to be able to grab each project in the Project Explorer window and grab the ProjectName and ProjectLocation and put that into an object by itself and return the new array of objects.

I can't find a way to access the information that I need.

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

I feel as though tree or selTree would have the information I want but I can't find it. Any help?


回答1:


Use the selection data from the selection provider:

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

To get all the projects use:

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

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


来源:https://stackoverflow.com/questions/31438337/find-project-locations-in-project-explorer-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!