Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse RCP: Set active part programmatically or get selection of inactive part

in my Eclipse Plugin I have this workflow:

  1. Get the currently selected project in the Package Explorer
  2. Do something
  3. Get the currently selected project in the Package Explorer (same as 1)
  4. Do something different

1 (and 3) are realized like that:

ISelectionService selectionService = PlatformUI.getWorkbench()
    .getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
[...]

Now, the problem is that before 1, the Package Explorer is selected, because that's the only way to trigger the workflow. But step 2 changes the active part because it refreshes a TreeView which makes it the active part. When I now try to run 3, which is the same method as 1, I have a problem: The Package Explorer is no longer the activePart of the selectionService and therefore selection is null.

My questions are: Is there any way to get the ISelectionService for a particular View which is not the active one? If not, is there a way to programmatically set the active part before executing step 3?

Btw that's an Eclipse 3.x plugin.

like image 774
Jdv Avatar asked Sep 10 '25 01:09

Jdv


1 Answers

If you find the IViewPart for the package explorer you can access its ISelectionProvider directly using:

IViewPart part = .. find package explorer view part

IViewSite viewSite = part.getViewSite();

ISelectionProvider provider = viewSite.getSelectionProvider();

ISelection selection = provider.getSelection();
like image 140
greg-449 Avatar answered Sep 12 '25 14:09

greg-449