Search code examples
javaeclipseeclipse-plugineclipse-rcpeclipse-cdt

Implementing an ISelectionListener to monitor selected projects in project explorer


I am trying to implement an ISelectionListener in my eclipse plugin, which will track the selections in my project explorer and tell me which projects are selected. So its the same way that the build toolbar button finds out which projects are selected to be build each time. I implement that listener but I can get the projects selected only if the user clicks on the actual project and not in any other items under the project(like source codes, folders, packages...) my code is :

    @Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
    Object[] allselections= null;
    selectedProjects.clear();
    // TODO Auto-generated method stub

    if(selection instanceof IStructuredSelection) {
        //Object element = ((IStructuredSelection)selection).getFirstElement();
        allselections = ((IStructuredSelection)selection).toArray();
        for(Object element: allselections){

            if (element instanceof IResource) {
                selectedProjects.add(((IResource)element).getProject());

        }
    }
};

When the element is not an instanceof IResource and is either a CModelManager, cSource/cxxSource from CDT...and anything else that can be part of a project, is there a generic way to get the project that it belongs to?

For an IResourceChangeListener you can use the IResourceDeltaVisitor and then you can get everything as an IResource instance using: IResource resource = delta.getResource() in the visit(IResourceDelta delta) function. Is there something similar for the ISelectionListener?


Solution

  • http://www.eclipsezone.com/articles/what-is-iadaptable/

    http://wiki.eclipse.org/FAQ_How_do_I_use_IAdaptable_and_IAdapterFactory%3F