[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Re: How can I insert a string into an editor with an external menuitem?

Leen,

Eclipse allows graphical editors as well as textual editors, so you'll need
to check whether the current editor is an ITextEditor.
If it is, you can ask for its document provider, which can give you the
document for the editor's input.

Try something like:

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window != null) {
 IWorkbenchPage page = window.getActivePage();
 if (page != null) {
  IEditorPart e = page.getActiveEditor();
  if (e instanceof ITextEditor) {
   ITextEditor te = (ITextEditor) e;
   IDocument document = te.getDocumentProvider().getDocument(te.getInput());
   ...
  }
 }
}

This is assuming that you have no context whatsoever when your action runs.
I recommend structuring it, if you can, so that the action has as much
context as possible so you avoid using the getActive* calls, which may
return null if the active window, page or editor cannot be determined.  For
example, the active window cannot be determined if the workbench is not the
front-most window.

Note that the IDocumentProvider is there to provide the decoupling needed
for document sharing.  If two editors are open on the same file, they share
the same IDocumentProvider and therefore the same IDocument, and
modifications in one editor will show up in the other.

Hope this helps,
Nick