简体   繁体   English

如何在Eclipse中将侦听器添加到默认代码编辑器?

[英]How to add a listener to the default code editor in Eclipse?

I want to add a listener to the default code editor in Eclipse. 我想将侦听器添加到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. 我特别关注默认的Java代码编辑器。 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. 到目前为止,我遇到的主要问题是,我不知道该如何处理IEditorDescriptor。

You can register an IPartListener2 with the PartService, for example: 您可以向PartService注册IPartListener2,例如:

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

There are other ways to register the IPartListener, for example if you have a reference to an IWorkbenchPage . 还有其他注册IPartListener的方法,例如,如果您有对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. 对于IPartListener2 ,您将获得一个IWorkbenchPartReference ,可用来检查所涉及的编辑器类型。 For example, to detect the standard Java editor, you can check for the ID org.eclipse.jdt.ui.CompilationUnitEditor . 例如,要检测标准的Java编辑器,可以检查ID org.eclipse.jdt.ui.CompilationUnitEditor

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM