Fixed a problem with updating views and editors after an import has run; Correcting...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / EditorAnnotation.java
1 package eu.etaxonomy.taxeditor.editor.name;
2
3 import org.apache.log4j.Logger;
4 import org.eclipse.core.resources.IMarker;
5 import org.eclipse.jface.text.Position;
6 import org.eclipse.jface.text.source.Annotation;
7 import org.eclipse.jface.text.source.IAnnotationPresentation;
8 import org.eclipse.swt.graphics.GC;
9 import org.eclipse.swt.graphics.Image;
10 import org.eclipse.swt.graphics.Point;
11 import org.eclipse.swt.graphics.RGB;
12 import org.eclipse.swt.graphics.Rectangle;
13 import org.eclipse.swt.widgets.Canvas;
14
15 import eu.etaxonomy.taxeditor.model.ImageResources;
16
17 /**
18 * <p>EditorAnnotation class.</p>
19 *
20 * @author n.hoffmann
21 * @version $Id: $
22 */
23 public class EditorAnnotation extends Annotation implements IAnnotationPresentation {
24 private static final Logger logger = Logger
25 .getLogger(EditorAnnotation.class);
26 private IMarker marker;
27 private String text;
28 private int line;
29 private Position position;
30
31 // error identifiers, images and colors
32 /** Constant <code>ERROR_TYPE="editor.error.type"</code> */
33 public static String ERROR_TYPE = "editor.error.type";
34 /** Constant <code>ERROR_IMAGE</code> */
35 public static Image ERROR_IMAGE = ImageResources.getImage(ImageResources.ERROR_ANNOTATION_ICON);
36 /** Constant <code>ERROR_RGB</code> */
37 public static final RGB ERROR_RGB = new RGB(255, 0, 0);
38
39 /** Constant <code>WARNING_TYPE="editor.warning.type"</code> */
40 public static String WARNING_TYPE = "editor.warning.type";
41 /** Constant <code>WARNING_IMAGE</code> */
42 public static Image WARNING_IMAGE = ImageResources.getImage(ImageResources.WARNING_ANNOTATION_ICON);
43 /** Constant <code>WARNING_RGB</code> */
44 public static final RGB WARNING_RGB = new RGB(244, 200, 45);
45
46 /**
47 * <p>Constructor for EditorAnnotation.</p>
48 *
49 * @param marker a org.eclipse.core.resources.IMarker object.
50 */
51 public EditorAnnotation(IMarker marker) {
52 this.marker = marker;
53 }
54
55 /**
56 * <p>Constructor for EditorAnnotation.</p>
57 *
58 * @param line a int.
59 * @param text a {@link java.lang.String} object.
60 */
61 public EditorAnnotation(int line, String text) {
62 this(ERROR_TYPE, line, text);
63 }
64
65 /**
66 * <p>Constructor for EditorAnnotation.</p>
67 *
68 * @param type a {@link java.lang.String} object.
69 * @param line a int.
70 * @param text a {@link java.lang.String} object.
71 */
72 public EditorAnnotation(String type, int line, String text) {
73 super(type, false, text);
74 this.marker = null;
75 this.line = line;
76 this.text = text;
77 }
78
79 /**
80 * <p>Getter for the field <code>marker</code>.</p>
81 *
82 * @return a org.eclipse.core.resources.IMarker object.
83 */
84 public IMarker getMarker() {
85 return marker;
86 }
87
88 /**
89 * <p>Getter for the field <code>line</code>.</p>
90 *
91 * @return a int.
92 */
93 public int getLine() {
94 return line;
95 }
96
97 /**
98 * <p>Getter for the field <code>text</code>.</p>
99 *
100 * @return a {@link java.lang.String} object.
101 */
102 public String getText() {
103 if (ERROR_TYPE.equals(getType())) {
104 return "Error: " + text;
105 }
106 if (WARNING_TYPE.equals(getType())) {
107 return "Warning: " + text;
108 }
109 return super.getText();
110 }
111
112 /**
113 * @return
114 */
115 private Image getImage() {
116 if (ERROR_TYPE.equals(getType())) {
117 return ERROR_IMAGE;
118 }
119 if (WARNING_TYPE.equals(getType())) {
120 return WARNING_IMAGE;
121 }
122 logger.warn("No image for type " + getType());
123 return null;
124 }
125
126 /**
127 * <p>getLayer</p>
128 *
129 * @return a int.
130 */
131 public int getLayer() {
132 return 3;
133 }
134
135
136 /**
137 * <p>Getter for the field <code>position</code>.</p>
138 *
139 * @return a {@link org.eclipse.jface.text.Position} object.
140 */
141 public Position getPosition() {
142 return position;
143 }
144
145 /**
146 * <p>Setter for the field <code>position</code>.</p>
147 *
148 * @param position a {@link org.eclipse.jface.text.Position} object.
149 */
150 public void setPosition(Position position) {
151 this.position = position;
152 }
153
154
155 /** {@inheritDoc} */
156 public void paint(GC gc, Canvas canvas, Rectangle bounds) {
157 Point canvasSize= canvas.getSize();
158
159 int x= 0;
160 int y= bounds.y;
161 int w= canvasSize.x;
162 int h= bounds.height;
163
164 if (y + h > canvasSize.y)
165 h= canvasSize.y - y;
166
167 if (y < 0) {
168 h= h + y;
169 y= 0;
170 }
171
172 if (h <= 0)
173 return;
174
175 Image image = getImage();
176
177 if (image == null) {
178 return;
179 }
180
181 Rectangle r = image.getBounds();
182
183 int destX = x + w - r.width;
184 int destY = y + h - r.height;
185
186 gc.drawImage(image, 0, 0, r.width, r.height, destX, destY, r.width, r.height);
187 }
188 }