如何在我自己的插件中钩入Eclipse编辑器事件? [英] How can I hook into Eclipse editor events in my own plugin?

查看:102
本文介绍了如何在我自己的插件中钩入Eclipse编辑器事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想创建自己的编辑器,但我想通过挂起他们的编辑事件来扩展现有的编辑者。

I don't want to create my own editor but I would like to extend existing editors by hooking into their editing events.

例如,每当文本更改一个文本或xml编辑器,我会得到一个回调,并能够对变化做出反应。

For example whenever the text changes in a text or xml editor I would get a callback and be able to react to the change.

是否存在这样一个合适的扩展点?

Does such a suitable extension point exist?

推荐答案

您可以通过访问 IEditorPart ,使用 getAdapter(IDocument。 class)然后添加一个监听器到这个...

You can do this by accessing the IEditorPart, use getAdapter(IDocument.class) and then add a listener to this...

但这真的是一个黑客...; - )

But this is really a hack... ;-)

编辑:根据要求,这里还有一些代码。

EDIT: On request, here is a little more code.

public void hookToEditor() {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    if (page == null) return;
    IEditorPart editor = page.getActiveEditor();
    if (editor == null) return;
    IDocument doc = (IDocument) editor.getAdapter(IDocument.class);
    if (doc == null) return;

    doc.addDocumentListener(new IDocumentListener() {
        @Override
        public void documentChanged(DocumentEvent event) {
            // Do something
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
            // About to do something
        }
    });
}

请注意,


  • 有很多方法来获取页面 - 例如通过现在的网站

  • ,有很多方法来获得编辑器的部分 - 例如通过处理程序

  • 许多编辑者没有嵌入式文档 - 例如PDE编辑器

这篇关于如何在我自己的插件中钩入Eclipse编辑器事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆