如何从工作集中获取项目(IProject)?

How to get projects (IProject) from Working Sets?

在接下来的代码中,我默认从项目工作区获取我需要的信息,但我需要为工作集的项目更改它。

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

     // Get the root of the workspace
    IWorkspaceRoot root = workspace.getRoot(); 

    // Get all projects in the workspace
    IProject[] projects = root.getProjects(); 

    // Loop over all projects
    for (IProject project : projects) {
        try {
            if (project.getName().equals("RemoteSystemsTempFiles"))
            {
                System.out.println("Ignored project");
                //setSystemName(project.getName());
            }
            else
            {
                // Load the data model by extracting APIs declaration (Imports)
                printProjectInfo(project);
            }
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }

所以,我找到了一个 answer,但是在 return 循环之后,我收到了很多信息,例如:

- ProjectName (not open) 

- ProjectName
   src
      <default> (...)
        src (...)
    src.tryone (...)
   C:\Program Files\Java\jre1.8.0_31\lib\resources.jar (not open)

return获取该信息的代码是:

     IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();

    IWorkingSet [] allWorkingSets = manager.getAllWorkingSets();

    for (IWorkingSet aWorkingset : allWorkingSets) {

        IAdaptable[] elems = aWorkingset.getElements();
        System.out.println("Working set " + aWorkingset.getName() + " has "+ elems.length + " projects.");
        for (IAdaptable elem : elems) {
                System.out.println("Working set " + aWorkingset.getName()+ " contains " + elem.toString());
        }
    }

所以,简单地说,我想知道如何以与工作区项目相同或类似的方式从工作集中获取 IProjects?

每个 IAdaptable 元素调用

IResource resource = (IResource)elem.getAdapter(IResource.class);

这将 return 元素代表的任何资源(某些工作集包含其他内容,因此结果可能为空)。

然后您可以拨打

IProject project = resource.getProject();