Jump to: navigation, search

Difference between revisions of "FAQ How do I insert text in the active text editor?"

 
 
Line 28: Line 28:
 
== See Also: ==
 
== See Also: ==
  
[[FAQ_How_do_I_use_the_text_document_model%3F]]
+
[[FAQ How do I use the text document model?]]
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
 
 +
{{Template: FAQ Tagline}}

Latest revision as of 03:44, 19 January 2007

Text editors have no public API to insert text. Furthermore, they do not expose their StyledText widget used to edit the underlying document. Therefore, inserting text in the currently active editor is not trivial. One could start with the SWT shell, search the widget containment hierarchy, and eventually locate the widget to enter text into. Fortunately, an easier way is available.

Text editors obtain a document model by using a document provider. This provider functions as synchronizer for multiple editors, notifying each when the other changes the document. By acting as one of the editors on a document, one can easily insert text into the editor of choice. The next code snippet locates the active editor, gets its document provider, requests the underlying document, and inserts some text into it:

   IWorkbenchPage page = ...;
   IEditorPart part = page.getActiveEditor();
   if (!(part instanceof AbstractTextEditor)
      return;
   ITextEditor editor = (ITextEditor)part;
   IDocumentProvider dp = editor.getDocumentProvider();
   IDocument doc = dp.getDocument(editor.getEditorInput());
   int offset = doc.getLineOffset(doc.getNumberOfLines()-4);
   doc.replace(offset, 0, pasteText+"\n");

The provider will notify all other editors to update their presentation as a result.

See Also:

FAQ How do I use the text document model?



This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.