每当在 Eclipse 中打开新编辑器时,我如何得到通知?

2022-01-16 00:00:00 eclipse-plugin eclipse eclipse-pde

我有一个视图希望收到所有当前打开的编辑器的通知.我在哪里可以添加监听器来实现这一点?

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?

我希望 WorkbenchPage 或 EditorManager 有一些合适的监听器注册表,但我找不到它.

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

推荐答案

您的视图是否使用 org.eclipse.ui.IPartListener2 ?

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

这就是使用这个 EditorListener,其工作是针对给定视图对编辑器事件(包括打开和关闭)做出反应

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));
    }

<小时>

现在,如果您的 ViewPart 直接实现 IPartListener2,它可以将自己注册到各种 Editors,就像这样 BytecodeReferenceView


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);
    [...]

相关文章