I don't want to create my own editor but I开发者_开发技巧 would like to extend existing editors by hooking into their editing events.
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?
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
}
});
}
Note that
- there are many ways to get the page - e.g. via the current site
- there are just as many ways to get the editor part - e.g. via a handler
- many editors don't have a embedded document - e.g. PDE editors
You can add resource change listener.
IResourceChangeListener listener = new IResourceChangeListener() {
@Override
public void resourceChanged(IResourceChangeEvent arg0) {
System.out.println("Text changed");
}
};
ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);
精彩评论