javaeclipseeclipse-pluginpackage-explorer

How to programmatically change the selection within package explorer


I am currently developing a plugin for eclipse that analyzes dependencies and references between projects within the Eclipse Workspace and displays them in its own View in a UML-like diagram.

To increase the usefulness of my plugin, I wish to add interactivity to the diagram by allowing users to open a project in the package explorer and if applicable open it in an editor by clicking on the graph displayed.

However, my problem is that while I know how to obtain a given selection from the package explorer, I have not been able to find a way to change the selection or simply open up a project in the package explorer programmatically.

Does anyone have a solution for this problem?


Solution

  • I have found the solution. Eclipse does offer direct access to the package explorer in org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart , but it is discouraged.

    import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
    
    ...
    
    PackageExplorerPart part= PackageExplorerPart.getFromActivePerspective();
    IResource resource =  /*any IResource to be selected in the explorer*/;
    
    part.selectAndReveal(resource);
    

    This will highlight whatever IResource resource is and expand the tree as necessary.