javaeclipse-plugineclipse-jdtworking-set

How to get projects (IProject) from Working Sets?


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.

    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?


Solution

  • On each IAdaptable element call

    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).

    You can then call

    IProject project = resource.getProject();