I have created a simple Eclipse RCP application where I can open multiple editor instances based on user action. I have a separate view (ViewPart) where I listen for the selection changes on the editor.
The problem is that the view is notified only of the selections from the last opened editor, the other editors which were opened earlier don’t provide the events any more/or maybe the view listens only to the last opened editor and nothing else. This happens even if the previously opened editors gain focus.
In other words, only the newest editor in editor area provides selection events, what I want is, when I click on the other editor’s tabs, I want to see the changes on my view when I click one of the previously opened editors.
In my view I use:
IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getActiveWorkbenchWindow().getActivePage().addSelectionListener(DocumentsEditor.ID,(ISelectionListener)this);
where DocumentEditor is one of the editors opened in application editor area.
From the JavaDoc for
ISelectionService.addSelectionListener(String partId, ISelectionListener listener)(emphasis is mine):So don’t use this method when you want to track editor selections. Instead use
ISelectionService.addSelectionListener(ISelectionListener listener)and check the given part inISelectionListener.selectionChanged(IWorkbenchPart part, ISelection selection)usinginstanceof…Small note to the code: The selection service exists on a per window basis, so if you have multiple workbench windows, they each have their own service instance.
For this reason I usually use the following code in my views and editors:
This way the used listener will come from the correct window.