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
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.
This answer extends what the accepted answer states but takes it further for folks who mind the "Discouraged Access" warning on the use of PackageExplorerPart
.
Exact warning (more for easier searching off Google) that you see is
Discouraged access: The type
PackageExplorerPart
is not accessible due to restriction on required library/eclipse_install_path/eclipse/plugins/org.eclipse.jdt.ui_3.9.1.v20130820-1427.jar
Code Sample:
final IWorkbenchPart activePart = getActivePart();
if (activePart != null && activePart instanceof IPackagesViewPart) {
((IPackagesViewPart) activePart).selectAndReveal(newElement);
}
Supporting Code:
private IWorkbenchPart getActivePart() {
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
if (activeWindow != null) {
final IWorkbenchPage activePage = activeWindow.getActivePage();
if (activePage != null) {
return activePage.getActivePart();
}
}
return null;
}