Monday, July 6, 2009

If you are using Annotations to do some text decorations and highlighting in your TextEditor or StatusTextEditor sub classes, you have to make sure to add the AnnotationPainter as a TextPresentationListener to the SourceViewer, in addition to adding it as a Painter. This is required when you want to use text styling strategies (ITextStyleStrategy) instead of the IDrawingStrategy.

The easiest way to highlight text in your color using the AnnotationPainter is to use its inbuilt addHighlightAnnotationType method.


public class AnnotatedDocumentEditor extends StatusTextEditor{

public static final String ID = "edu.pitt.dbmi.odie.ui.editors.AnnotatedDocumentEditor";
private AnnotationPainter annotationPainter;

public AnnotatedDocumentEditor() {
super();
setDocumentProvider(new AnnotatedDocumentProvider());
}

@Override
protected ISourceViewer createSourceViewer(Composite parent,
IVerticalRuler ruler, int styles) {
SourceViewer sv = (SourceViewer) super.createSourceViewer(parent, ruler, styles);
initAnnotationPainter(sv);

sv.addPainter(annotationPainter);
sv.addTextPresentationListener(annotationPainter);
return sv;
}

private void initAnnotationPainter(SourceViewer sv) {
IAnnotationAccess annotationAccess = new IAnnotationAccess() {
public Object getType(Annotation annotation) {
return annotation.getType();
}
public boolean isMultiLine(Annotation annotation) {
return true;
}
public boolean isTemporary(Annotation annotation) {
return true;
}
};

annotationPainter = new AnnotationPainter(sv,annotationAccess);

annotationPainter.addHighlightAnnotationType("DUMMY1");
annotationPainter.addHighlightAnnotationType("DUMMY2");
annotationPainter.addHighlightAnnotationType("DUMMY3");

Display display = Display.getDefault();

annotationPainter.setAnnotationTypeColor("DUMMY1", new Color(display, new RGB(125,189,0)));
annotationPainter.setAnnotationTypeColor("DUMMY2", new Color(display, new RGB(228,142,52)));
annotationPainter.setAnnotationTypeColor("DUMMY3", new Color(display, new RGB(244,229,61)));
}

}

Please note that the above code is not complete. You need to create the AnnotationModel for each document using your implementation of the AbstractDocumentProvider

See the following links for good tutorials on using AnnotationPainter and extending TextEditors in Eclipse:

http://eclipse.dzone.com/articles/beyond-rich-text-tricks-using-
http://www.realsolve.co.uk/site/tech/jface-text.php

1 comments:

Titus Barik said...

Okay, so how exactly is the text itself highlighted? How do you specify the text range?

Post a Comment