Eclipse插件-访问编辑器 [英] Eclipse plugin - accessing the editor
问题描述
因此,我目前正在为Eclipse IDE开发一个插件。简而言之,该插件是协作式实时代码编辑器,其中的编辑器是eclipse的(类似于Google文档,但带有代码和eclipse)。这意味着在安装插件时,我可以使用我的Gmail帐户将eclipse连接到合作伙伴的蚀。当我开始在机器上进行编码时,我的伙伴会看到我写的内容,反之亦然。
So, I’m currently developing a plugin for the eclipse IDE. In a nutshell, the plugin is a collaborative real time code editor where the editor is eclipse (which is something like Google documents but with the code and on eclipse). Meaning that when I install the plugin, I would be able to connect -using my Gmail account- eclipse to the partner’s eclipse. And when I start coding on my machine, my partner would be seeing what I write and vice versa.
我目前面临的问题是访问eclipse的编辑器。例如,我必须监视活动文档中的所有更改,以便每次发生更改时,都会将此更改通知另一伙伴的IDE。
The problem I’m currently facing is accessing eclipse’s editor. For example, I have to monitor all the changes in the active document so that every time a change happens, the other partner’s IDE would be notified with this change.
我找到并了解了 IDcoumentProvider , IDocument 和 IEditorInput 类,已以某种方式连接,但我无法理解此连接或如何使用它。因此,如果有人可以解释这种联系,我将非常感激。另外还有没有其他方法可以实现我的目标?
I found and read about the IDcoumentProvider, IDocument and IEditorInput classes and they’re somehow connected but I can’t understand this connection or how to use it. So if someone can explain this connection I would really appreciate it. Also if there is another way to achieve my goal?
推荐答案
您可以通过 IEditorPart
> IWorkbenchPage 。
You can access the IEditorPart
via the IWorkbenchPage
.
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
从那里,您可以访问其他各种类,包括编辑器的 IEditorInput
,该编辑器加载的 File
或基础GUI Control
元素。 (请注意,根据编辑器的类型(文本文件,图表等),您可能不得不转换为不同的类。)
From there, you have access to various other classes, including the editor's IEditorInput
, the File
loaded by that editor, or the underlying GUI Control
element. (Note that depending on the kind of editor (text files, diagram, etc.) you may have to cast to different classes.)
FileEditorInput input = (FileEditorInput) editor.getEditorInput();
StyledText editorControl = ((StyledText) editor.getAdapter(Control.class));
String path = input.getFile().getRawLocationURI().getRawPath();
现在,您可以将监听器添加到 Control
,例如 KeyAdapter
用于监视在相应编辑器中发生的所有按键。
Now, you can add a listener to the Control
, e.g. a KeyAdapter
for monitoring all key strokes occurring in the respective editor.
editorControl.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Editing in file " + path);
}
});
或者,如果监视所有键击太多,则可以注册 IPropertyListener
编辑器。该听众将例如每当编辑者出现脏或保存时,都会收到通知。 propId
的含义可以在 IWorkbenchPartConstants
中找到。
Or, if monitoring all key strokes is too much, you can register an IPropertyListener
to the editor. This listener will e.g. be notified whenever the editor gets 'dirty' or when it is saved. The meaning of propId
can be found in IWorkbenchPartConstants
.
editor.addPropertyListener(new IPropertyListener() {
@Override
public void propertyChanged(Object source, int propId) {
if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
System.out.println("'Dirty' Property Changed");
}
}
});
这篇关于Eclipse插件-访问编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!