您可以访问IEditorPart
通过IWorkbenchPage
.
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
从那里,您可以访问各种其他类,包括编辑器的IEditorInput
, the File
由该编辑器或底层 GUI 加载Control
元素。 (请注意,根据编辑器的类型(文本文件、图表等),您可能必须转换为不同的类。)
FileEditorInput input = (FileEditorInput) editor.getEditorInput();
StyledText editorControl = ((StyledText) editor.getAdapter(Control.class));
String path = input.getFile().getRawLocationURI().getRawPath();
现在,您可以向Control
,例如AKeyAdapter
用于监视相应编辑器中发生的所有击键。
editorControl.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Editing in file " + path);
}
});
或者,如果监控所有击键过多,您可以注册一个IPropertyListener
给编辑。该听众将例如每当编辑器变得“脏”或保存时都会收到通知。的含义propId
可以找到IWorkbenchPartConstants
.
editor.addPropertyListener(new IPropertyListener() {
@Override
public void propertyChanged(Object source, int propId) {
if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
System.out.println("'Dirty' Property Changed");
}
}
});