简体   繁体   English

在Eclipse中打开新编辑器时如何收到通知?

[英]How do I get notified whenever a new editor is opened in 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? 我在哪里可以添加一个监听器来实现这一目标?

I was expecting WorkbenchPage or EditorManager to have some appropriate listener registry, but I couldn't find it. 我期待WorkbenchPage或EditorManager有一些适当的监听器注册表,但我找不到它。

Does your view uses a org.eclipse.ui.IPartListener2 ? 您的视图是否使用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) 这就是使用这个EditorListener的东西 ,它的工作是对给定的视图作出反应,以编辑事件(包括打开和关闭)

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 现在,如果您的ViewPart直接实现IPartListener2 ,它可以将自己注册到各种Editors ,就像这个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);
    [...]

I think you're on the right track. 我认为你走在正确的轨道上。 You need to listen to the IWorkbenchPage IPartService events: 您需要监听IWorkbenchPage IPartService事件:

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

    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确地在Eclipse编辑器中获取当前打开的类? - How to get currently opened class in eclipse editor correctly? 如何从活动的Eclipse编辑器中获取当前方法? - How do I get the current method from the active Eclipse editor? 如何使eclipse的网页编辑器正常工作? - How do I get eclipse's web page editor to work? 我如何配置 Eclipse 使其在 Eclipse window 失去焦点时保存所有打开的文档? - How can I configure Eclipse to that it saves all opened documents whenever the Eclipse window loses focus? Eclipse将新打开的编辑器选项卡放在当前活动的编辑器选项卡旁边? - Eclipse place the new opened editor tab next to the currently active one? 如何在Eclipse pydev透视图中删除编辑器的视图权限? - How do I remove view right to the editor in Eclipse pydev perspective? 如何使用Eclipse比较编辑器保存文件 - How do I save a file using the Eclipse compare editor 如何禁用/删除Eclipse Graphiti编辑器的面板? - How do I disable/remove the Palette of an Eclipse Graphiti editor? 如何在Eclipse的编辑器窗口中启用SVN菜单? - How do I enable SVN menu in an editor window in Eclipse? 如何将某些缓存的信息附加到Eclipse编辑器或资源? - How do I attach some cached information to an Eclipse editor or resource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM