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

时间:2015-05-08 19:37:44

标签: java eclipse-plugin eclipse-jdt working-set

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

    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但是作为循环后的返回,我收到了许多信息,如:

- ProjectName (not open) 

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

返回该信息的代码是:

     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?

1 个答案:

答案 0 :(得分:2)

在每个IAdaptable元素调用上

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

将返回该元素表示的任何资源(某些工作集包含其他内容,因此结果可能为null)。

然后你可以打电话

IProject project = resource.getProject();