0
votes

I have the following problem with my RCP Eclipse application: I am trying to set selection on an IResource within a Project Explorer tree. Code I am using is:

final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final IViewPart viewPart = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
final ISelectionProvider selProvider = viewPart.getSite().getSelectionProvider();
selProvider.setSelection(new StructuredSelection(mrcReportRuleOccurrence.getIResource()));

The code seems to work only if the leaf was already once expanded in the tree. If the tree is completely collapsed, the code does not make eclipse expand the containing folder and select the desired file in the project explorer. Does anyone know how to bring this about?

1

1 Answers

0
votes

You could try checking if the selection provider is a Viewer and using the setSelection(selection. reveal) method:

if (selProvider instanceof Viewer) {
  ((Viewer)selProvider).setSelection(selection, true);
}

It may also be necessary to use a TreeSelection with a TreePath for the selection rather than just StructuredSelection.

TreePath path = new TreePath(.... array of objects ...);

TreeSelection selection = new TreeSelection(path);

Where 'array of objects' is the path through the tree to the resource.

But selecting items in the tree of a different view is not what the ISelectionProvider interface is for. There is no guarantee that this can be done using this interface.