Jump to: navigation, search

Platform Text

Revision as of 09:29, 14 June 2008 by Francisu.ieee.org (Talk | contribs) (New page: Platform Text consists mostly of the JFace support for text editing (the Text Viewer and is subclasses) coupled with the text editor support in org.eclipse.ui.workbench.texteditor. This ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Platform Text consists mostly of the JFace support for text editing (the Text Viewer and is subclasses) coupled with the text editor support in org.eclipse.ui.workbench.texteditor. This allows RCP applications to use the same text editor infrastructure that is enjoyed by JDT and other languages.

The [documentation] provides a good overview about using the text editor support, but is currently missing some of the material shown below. Hopefully at some point this can be moved into the documentation.

Using the SourceViewer with Annotations separately from a TextEditor

Here is some example code that shows how you can use a SourceViewer with annotation support without using a TextEditor. You may want this in cases where you wish to simply show some text as part of a larger editor that's not a TextEditor.

A complete project for this is found [here].

This shows the main class which is a simple editor partition that has the SourceViewer as it's main object. This also allows the annotation to be configured through the normal means and (for that) depends on the plugin.xml (found in the above zip file) declarations.

package sourceviewsample;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.jface.text.source.OverviewRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.editors.text.EditorsPlugin;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;

 * An example of using a SourceViewer with Annotation support outside of the
 * TextEditor class. This annotations can be configured in the preferences if
 * the markerAnnotationSpecification is setup in the plugin.xml.
 * To execute this, run as an Eclipse Application and then open a file using
 * Open with.. -> Other, and select Sample Editor. You will see the text that
 * comes in this example and the highlight.
public class SampleEditor extends EditorPart
    public static final String              ANNO_TYPE          = "com.mycompany.element";
    public static final String              ANNO_KEY_HIGHLIGHT = "annotateElemHighlight";
    public static final String              ANNO_KEY_OVERVIEW  = "annotateElemOverviewRuler";
    public static final String              ANNO_KEY_VERTICAL  = "annotateElemVertialRuler";
    public static final String              ANNO_KEY_TEXT      = "annotateElemText";
    public static final String              ANNO_KEY_COLOR     = "annotateElemColor";

    protected SourceViewer                  _sourceViewer;
    protected SourceViewerDecorationSupport _sds;
    protected IDocument                     _document;
    protected AnnotationModel               _annotationModel;

    protected String                        _docString         = "this\nis\na\ntest\ndocument";

    public SampleEditor()

    public void createPartControl(Composite parent)
        int VERTICAL_RULER_WIDTH = 12;

        int styles = SWT.V_SCROLL
            | SWT.H_SCROLL
            | SWT.MULTI
            | SWT.BORDER
            | SWT.FULL_SELECTION;
        ISharedTextColors sharedColors = EditorsPlugin.getDefault()
        IOverviewRuler overviewRuler = new OverviewRuler(null, VERTICAL_RULER_WIDTH,
        CompositeRuler ruler = new CompositeRuler(VERTICAL_RULER_WIDTH);

        _document = new Document();

        _annotationModel = new AnnotationModel();

        _sourceViewer = new SourceViewer(parent,
        _sourceViewer.configure(new SourceViewerConfiguration());

        _sds = new SourceViewerDecorationSupport(_sourceViewer,

        AnnotationPreference ap = new AnnotationPreference();


        _sourceViewer.setDocument(_document, _annotationModel);

        _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,

        ruler.addDecorator(0, new LineNumberRulerColumn());

        Annotation annotation = new Annotation(false);
        Position position = new Position(0, 4);
        _annotationModel.addAnnotation(annotation, position);

    public void dispose()
        // The _sourceViewer goes away automatically when the editor goes
        // away because it's hooked to the controls

    // This stuff below is just needed to make the EditorPart happy

    public void doSave(IProgressMonitor monitor)

    public void doSaveAs()

    public void init(IEditorSite site, IEditorInput input)
        throws PartInitException

    public boolean isDirty()
        return false;

    public boolean isSaveAsAllowed()
        return false;

    public void setFocus()