0

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 Answer 1

0

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.

7
  • The instance check and the setSelection do not seem to make a difference. As for the TreeSelection, I am failing to get a TreeSelection from an IResource... Jul 4, 2017 at 12:24
  • You don't get TreeSelection from IResource, you build it using a TreePath. Exactly what the project explorer wants in the path I am not sure. Added a bit more to the answer.
    – greg-449
    Jul 4, 2017 at 12:34
  • I have succesfully modified the code to recieve a tree selection but it still displays exactly the same behavior, if the tree was not open, it will not select the element. If the usery manually expands the tree at least once the code will jump to the element even if the tree is collapsed. Jul 4, 2017 at 13:58
  • Well you might be able to do something with expandToLevel, but this is not what the selection provider interface is for and there is no guarantee that this is possible at all.
    – greg-449
    Jul 4, 2017 at 14:40
  • neither the Viewer class nor the selection provider provide the expandToLevel method Jul 4, 2017 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.