p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.prototype2 / src / eu / etaxonomy / taxeditor / prototype2 / view / NameViewer.java
1 package eu.etaxonomy.taxeditor.prototype2.view;
2
3 import java.util.Iterator;
4
5 import org.eclipse.jface.text.Document;
6 import org.eclipse.jface.text.Position;
7 import org.eclipse.jface.text.source.Annotation;
8 import org.eclipse.jface.text.source.AnnotationModel;
9 import org.eclipse.jface.text.source.AnnotationPainter;
10 import org.eclipse.jface.text.source.IAnnotationAccess;
11 import org.eclipse.jface.text.source.SourceViewer;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Display;
16
17 import eu.etaxonomy.taxeditor.prototype2.view.nameviewersupport.AnnotationMarkerAccess;
18 import eu.etaxonomy.taxeditor.prototype2.view.nameviewersupport.ErrorAnnotation;
19 import eu.etaxonomy.taxeditor.prototype2.view.nameviewersupport.WarningAnnotation;
20
21 /**
22 * SourceViewer implementation called by NameEditorView.
23 *
24 * @author p.ciardelli
25 *
26 */
27 public class NameViewer extends SourceViewer {
28
29 public NameViewer(Composite parent) {
30 super(parent, null, SWT.WRAP | SWT.MULTI | SWT.RESIZE);
31
32 // Set name viewer's text to name cache
33 this.setDocument(new Document(""), new AnnotationModel());
34
35 // Annotations section
36 IAnnotationAccess fAnnotationAccess = new AnnotationMarkerAccess();
37
38 // To paint the annotations
39 AnnotationPainter annotationPainter = new AnnotationPainter(this, fAnnotationAccess);
40
41 // Add ability to paint red squigglies
42 annotationPainter.addAnnotationType(ErrorAnnotation.ERROR_TYPE);
43 annotationPainter.setAnnotationTypeColor(ErrorAnnotation.ERROR_TYPE,
44 new Color(Display.getDefault(), ErrorAnnotation.ERROR_RGB));
45
46 // Add ability to paint yellow squigglies
47 annotationPainter.addAnnotationType(WarningAnnotation.WARNING_TYPE);
48 annotationPainter.setAnnotationTypeColor(WarningAnnotation.WARNING_TYPE,
49 new Color(Display.getDefault(), WarningAnnotation.WARNING_RGB));
50
51 this.addPainter(annotationPainter);
52 }
53
54 public void setShowError(boolean hasProblem) {
55
56 String text = this.getTextWidget().getText();
57
58 Iterator<Annotation> annotations = this.getAnnotationModel().getAnnotationIterator();
59 while (annotations.hasNext()) {
60 Annotation annotation = annotations.next();
61 if (annotation.getType().equals(ErrorAnnotation.ERROR_TYPE))
62 this.getAnnotationModel().removeAnnotation(annotation);
63 }
64
65 // Failed attempt to get multi-line squigglies drawn
66 if (hasProblem && text.length() > 0) {
67 for (int i = 1; i <= this.getTextWidget().getLineCount(); i++) {
68 this.getAnnotationModel().addAnnotation(
69 new ErrorAnnotation(i, "Could not parse name."),
70 new Position(0, text.length()));
71 }
72 }
73 }
74 }