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

时间:2023-01-14 11:59:26

In the next code, I am getting the information I need from projects by default Workspace, but I need to change it for Working Sets' projects.

在下一个代码中,我从默认的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();
        }
    }

So, I found an answer but as return after loop the I receive many information like:

所以,我找到了一个答案,但作为循环后返回,我收到许多信息,如:

- ProjectName (not open) 

or

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

The code that is returning that information is:

返回该信息的代码是:

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

So, briefly, I would like to know how can I get the IProjects from Working sets, in the same way or similar way I did with the workspace projects?

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

1 个解决方案

#1


On each IAdaptable element call

在每个IAdaptable元素调用上

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

which will return any resource that the element represents (some working sets contain other things so the result might be null).

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

You can then call

然后你可以打电话

IProject project = resource.getProject();

#1


On each IAdaptable element call

在每个IAdaptable元素调用上

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

which will return any resource that the element represents (some working sets contain other things so the result might be null).

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

You can then call

然后你可以打电话

IProject project = resource.getProject();