Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Platform Text"

(Using the SourceViewer with Annotations separately from a TextEditor)
Line 2: Line 2:
 
org.eclipse.ui.workbench.texteditor.  This allows RCP applications to use the same text editor infrastructure that is enjoyed by [[JDT]] and other languages.
 
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 [[http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/editors.htm 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.
+
The [http://help.eclipse.org/help33/topic/org.eclipse.platform.doc.isv/guide/editors.htm Platform Plugin Developer Guide] 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 ==
 
== Using the SourceViewer with Annotations separately from a TextEditor ==
Line 8: Line 8:
 
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.
 
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 [http://www.oaklandsoftware.com/eclipse/sourceviewsample.zip here].
+
A complete project for this is found [http://wiki.eclipse.org/images/0/04/Sourceviewsample.zip 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
 
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

Revision as of 10:33, 14 June 2008

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 Platform Plugin Developer Guide 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()
    {
        super();
    }
 
    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()
                .getSharedTextColors();
        IOverviewRuler overviewRuler = new OverviewRuler(null, VERTICAL_RULER_WIDTH,
                                                         sharedColors);
        CompositeRuler ruler = new CompositeRuler(VERTICAL_RULER_WIDTH);
 
        _document = new Document();
        _document.set(_docString);
 
        _annotationModel = new AnnotationModel();
        _annotationModel.connect(_document);
 
        _sourceViewer = new SourceViewer(parent,
                                         ruler,
                                         overviewRuler,
                                         true,
                                         styles);
        _sourceViewer.configure(new SourceViewerConfiguration());
 
        _sds = new SourceViewerDecorationSupport(_sourceViewer,
                                                 overviewRuler,
                                                 null,
                                                 sharedColors);
 
        AnnotationPreference ap = new AnnotationPreference();
        ap.setColorPreferenceKey(ANNO_KEY_COLOR);
        ap.setHighlightPreferenceKey(ANNO_KEY_HIGHLIGHT);
        ap.setVerticalRulerPreferenceKey(ANNO_KEY_VERTICAL);
        ap.setOverviewRulerPreferenceKey(ANNO_KEY_OVERVIEW);
        ap.setTextPreferenceKey(ANNO_KEY_TEXT);
        ap.setAnnotationType(ANNO_TYPE);
        _sds.setAnnotationPreference(ap);
 
        _sds.install(EditorsPlugin.getDefault().getPreferenceStore());
 
        _sourceViewer.setDocument(_document, _annotationModel);
 
        _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,
                                                              SWT.FILL,
                                                              true,
                                                              true));
 
        ruler.addDecorator(0, new LineNumberRulerColumn());
 
        Annotation annotation = new Annotation(false);
        annotation.setType(ANNO_TYPE);
        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
        _sds.dispose();
    }
 
    //
    // 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
    {
        setSite(site);
        setInput(input);
    }
 
    public boolean isDirty()
    {
        return false;
    }
 
    public boolean isSaveAsAllowed()
    {
        return false;
    }
 
    public void setFocus()
    {
 
    }
 
}

Back to the top