How do I get notified whenever a new editor is opened in Eclipse?
Asked Answered
R

2

5

I have a view which would like to be notified about all the currently opened editors. Where can I add a listener to achieve this?

I was expecting WorkbenchPage or EditorManager to have some appropriate listener registry, but I couldn't find it.

Redwing answered 12/2, 2009 at 17:7 Comment(0)
B
8

Does your view uses a org.eclipse.ui.IPartListener2 ?

That is what is using this EditorListener, whose job is to react, for a given view, to Editor events (including open and close)

public class EditorListener implements ISelectionListener, IFileBufferListener,
IPartListener2 {
    protected BytecodeOutlineView view;

    EditorListener(BytecodeOutlineView view){
        this.view = view;
    }

[...] 

    /**
     * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
     */
    public void partOpened(IWorkbenchPartReference partRef) {
        view.handlePartVisible(partRef.getPart(false));
    }

Now if your ViewPart directly implements an IPartListener2, it can register itself to the various Editors, like this BytecodeReferenceView

public class BytecodeReferenceView extends ViewPart implements IPartListener2, ISelectionListener {

    [...]

    public void createPartControl(Composite parent) {
        browser = new Browser(parent, SWT.BORDER);
        browser.setText(BytecodeOutlinePlugin.getResourceString(NLS_PREFIX
            + "empty.selection.text"));
        final IWorkbenchWindow workbenchWindow = getSite().getWorkbenchWindow();
        workbenchWindow.getPartService().addPartListener(this);
    [...]
Bellman answered 12/2, 2009 at 21:19 Comment(1)
do you have an example how I can add this EditorListener to the default Java code editor in Eclipse?Sexcentenary
D
2

I think you're on the right track. You need to listen to the IWorkbenchPage IPartService events:

page.addPartListener(new IPartListener() {
    partOpened(IWorkbenchPart part) {
        ...
    }

    ...
});
Deference answered 12/2, 2009 at 17:15 Comment(3)
doesn't seem to do anything for meRedwing
ah... I was not reading your comment properly. I had added it to the page, not to the page's partService.Redwing
You probably didn't misread it - I thought you could just listen to the page directly. Apparently not.Deference

© 2022 - 2024 — McMap. All rights reserved.