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

How do I get notified whenever a new editor is opened in Eclipse?(每当在 Eclipse 中打开新编辑器时,我如何得到通知?)

本文介绍了每当在 Eclipse 中打开新编辑器时,我如何得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于每当在 Eclipse 中打开新编辑器时,我如何得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:每当在 Eclipse 中打开新编辑器时,我如何得到通知?

基础教程推荐