javaeclipseeclipse-pluginselectionworking-set

Eclipse Plugin: get contents of working set


I have an eclipse plugin which currently is able to get selected projects in the workspace using

IStructuredSelection structured = (IStructuredSelection) iServiceServiceObject.getSelection("org.eclipse.jdt.ui.PackageExplorer");
Object[] selectedProjectObjects = structured.toArray();
    for (Object projectObj : selectedProjectObjects) {
    IAdapterManager adapterManager = Platform.getAdapterManager();
    IResource resource = (IResource) adapterManager.getAdapter(projectObj, IResource.class);
    if (resource instanceof IProject) {
        IProject project = (IProject) resource;
        // using the project in some way

I would like to add the function of also being able to select a whole working set and process all the projects in it. Is there a way to say

if(resource instanceof WorkingSetClass){
    WorkingSetClass wsc = (WorkingSetClass) 
    IProject[] projects = wsc.getProjects()

The problem is, the doc says "All Known Subinterfaces (of IResource): IContainer, IFile, IFolder, IProject, IWorkspaceRoot". I tried with IContainer and its members() method instead of the pseudo-codish WorkingSetClass and getProjects(), but instanceof returns false.

Long story short, how do I get the projects inside a selected working set in package manager?


Solution

  • The working set interface org.eclipse.ui.IWorkingSet is not related to the IResource interfaces.

    You could try a separate adapter manager call:

    IWorkingSet workingSet = (IWorkingSet)adapterManager.getAdapter(projectObg, IWorkingSet.class);
    
    IAdaptable [] elements = workingSet.getElements();
    

    The elements in a working set can be projects but may be other things. Loop through the elements and check for instanceof IProject.