如何添加一个监听器到Eclipse中的默认代码编辑器? [英] How to add a listener to the default code editor in Eclipse?

查看:1124
本文介绍了如何添加一个监听器到Eclipse中的默认代码编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个监听器到Eclipse中的默认代码编辑器。每当新的代码文件被打开或关闭时都应该被触发。有什么可用的吗?我做了google搜索几个小时,但可以找到一个工作的解决方案。



我特别关注默认的Java代码编辑器。但是一旦我知道如何勾选这样的编辑器,添加额外的编辑器就不应该是一个大问题。我到目前为止的主要问题是,我不知道我应该怎么处理我可以得到的IEditorDescriptor。

解决方案

您可以使用PartService注册一个IPartListener2,例如:

  PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getPartService() .addPartListener(aListener)

还有其他注册IPartListener的方法,例如,如果你有一个引用到 IWorkbenchPage



当编辑器打开或关闭时,部件侦听器被通知。对于 IPartListener2 ,您将获得一个 IWorkbenchPartReference ,您可以使用它来检查涉及的是哪种编辑器。例如,要检测标准Java编辑器,您可以检查ID org.eclipse.jdt.ui.CompilationUnitEditor


I want to add a listener to the default code editor in Eclipse. It should be triggered whenever a new code file is opened or closed. Is there anything available like that? I did a google search for hours, but could find a working solution.

I'm especially focusing the default Java code editor. But once I know how to hook into such an editor it shouldn't be a big problem to add additional ones. The main problem I have so far is, that I don't know what I should do with the IEditorDescriptor I could get.

解决方案

You can register an IPartListener2 with the PartService, for example:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(aListener)

There are other ways to register the IPartListener, for example if you have a reference to an IWorkbenchPage.

The part listener is notified when editors are opened or closed, among other things. For IPartListener2, you get an IWorkbenchPartReference which you can use to check what kind of editor is involved. For example, to detect the standard Java editor, you can check for the ID org.eclipse.jdt.ui.CompilationUnitEditor.

这篇关于如何添加一个监听器到Eclipse中的默认代码编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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