Find Project locations in Project Explorer (Eclipse)

前端 未结 1 1357
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 15:18

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 wind

1条回答
  •  不知归路
    2021-01-27 15:45

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

    0 讨论(0)
提交回复
热议问题