Difference between revisions of "FAQ How do I get started with creating a custom text editor?"

From Eclipsepedia

Jump to: navigation, search
 
Line 6: Line 6:
 
you need to do nothing more; simply subclassing <tt>AbstractTextEditor</tt>
 
you need to do nothing more; simply subclassing <tt>AbstractTextEditor</tt>
 
will give you a generic text editor implementation right out of the box.
 
will give you a generic text editor implementation right out of the box.
 
  
 
To customize your editor, you need to create your own subclass of  
 
To customize your editor, you need to create your own subclass of  
Line 14: Line 13:
 
method in the configuration.  Browse through the methods of this class to get
 
method in the configuration.  Browse through the methods of this class to get
 
an idea of the kinds of customization you can add.
 
an idea of the kinds of customization you can add.
 
  
 
Another entry point for editor customization is the document provider.  The editor&#146;s
 
Another entry point for editor customization is the document provider.  The editor&#146;s
Line 24: Line 22:
 
or a document that uses a different character encoding.  This is also a good place for  
 
or a document that uses a different character encoding.  This is also a good place for  
 
adding listeners to the document so you can be notified when it changes.
 
adding listeners to the document so you can be notified when it changes.
 
  
 
You&#146;ll also want to customize the actions available to your editor.
 
You&#146;ll also want to customize the actions available to your editor.
Line 36: Line 33:
 
of <tt>createActions</tt>.  Look in the package <tt>org.eclipse.ui.texteditor</tt>
 
of <tt>createActions</tt>.  Look in the package <tt>org.eclipse.ui.texteditor</tt>
 
for more available actions.
 
for more available actions.
 
  
 
The FAQs in this chapter will use a running example of a simple HTML editor. We
 
The FAQs in this chapter will use a running example of a simple HTML editor. We
Line 44: Line 40:
 
be compatible with everyone&#146;s browser.   
 
be compatible with everyone&#146;s browser.   
 
Still, it&#146;s nice to have some syntax highlighting,
 
Still, it&#146;s nice to have some syntax highlighting,
Content Assist, and other time-saving features, so we wrote a simple HTML editor
+
Content Assist, and other time-saving features, so we wrote a simple HTML editor for Eclipse.   
for Eclipse.   
+
The fact that this book is written in HTML gave us added incentive:
+
another opportunity to eat our own dog food.
+
As with all other examples in this book, complete source is available on
+
the accompanying CD-ROM, and on
+
this book&#146;s Web site (http://eclipsefaq.org).
+
  
Here is the skeleton of the <tt>HTMLEditor</tt> class, showing the  
+
Here is the skeleton of the <tt>HTMLEditor</tt> class, showing the customization entry points:
customization entry points:
+
 
<pre>
 
<pre>
 
public class HTMLEditor extends AbstractTextEditor {
 
public class HTMLEditor extends AbstractTextEditor {

Revision as of 17:05, 22 February 2007

Follow the steps inFAQ_How_do_I_create_my_own_editor? to create a platform editor extension, but instead of the basic EditorPart, subclass AbstractTextEditor. You don’t need to override createPartControl this time, because the abstract editor builds the visual representation for you. In fact, you need to do nothing more; simply subclassing AbstractTextEditor will give you a generic text editor implementation right out of the box.

To customize your editor, you need to create your own subclass of SourceViewerConfiguration defined in package org.eclipse.jface.text.source when the editor is created. This class is the locus of all editor customization. Just about every time you want to add a feature to a text editor, you start by subclassing a method in the configuration. Browse through the methods of this class to get an idea of the kinds of customization you can add.

Another entry point for editor customization is the document provider. The editor’s document provider is a factory method for supplying the model object (an IDocument) that represents the editor’s contents. The document provider’s main function is to transform an IEditorInput into an appropriate IDocument. By subclassing the generic document provider, you can create a customized document, such as a document that is divided into multiple partitions or a document that uses a different character encoding. This is also a good place for adding listeners to the document so you can be notified when it changes.

You’ll also want to customize the actions available to your editor. The abstract text editor provides some actions, but if you want to add extra tools, you’ll need actions for them. This is done by overriding the method createActions in your editor. Be sure to call super.createActions to allow the abstract editor to install the default set of text-editing actions, such as Cut, Copy, Paste, and Undo. The editor framework supplies more actions that are not automatically added to the abstract editor, but you can add them from your implementation of createActions. Look in the package org.eclipse.ui.texteditor for more available actions.

The FAQs in this chapter will use a running example of a simple HTML editor. We have used a simple text editor for writing HTML because it’s the only editor that gives you complete control over the contents and won’t insert all those funny tags that most editors insert to ensure that your pages won’t be compatible with everyone’s browser. Still, it’s nice to have some syntax highlighting, Content Assist, and other time-saving features, so we wrote a simple HTML editor for Eclipse.

Here is the skeleton of the HTMLEditor class, showing the customization entry points:

public class HTMLEditor extends AbstractTextEditor {
   public HTMLEditor() {
      //install the source configuration
      setSourceViewerConfiguration(new HTMLConfiguration());
      //install the document provider
      setDocumentProvider(new HTMLDocumentProvider());
   }
   protected void createActions() {
      super.createActions();
      //... add other editor actions here
   }
}

See Also:

FAQ_What_support_is_there_for_creating_custom_text_editors?

Go to Platform Plug-in Developer Guide > Programmer’s Guide > Editors > Configuring a source viewer.


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.