Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / container / EditorAnnotation.java
1 package eu.etaxonomy.taxeditor.editor.name.container;
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.cdm.strategy.parser.ParserProblem;
16 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
17 import eu.etaxonomy.taxeditor.model.ImageResources;
18
19 /**
20 * <p>EditorAnnotation class.</p>
21 *
22 * @author n.hoffmann
23 * @version $Id: $
24 */
25 public class EditorAnnotation extends Annotation implements IAnnotationPresentation {
26 private static final Logger logger = Logger
27 .getLogger(EditorAnnotation.class);
28 private final IMarker marker;
29 private String text;
30 private int line;
31 private Position position;
32
33 public enum EditorAnnotationType{
34 WARNING(ImageResources.WARNING_ANNOTATION_ICON, 244, 200, 45),
35 ERROR(ImageResources.ERROR_ANNOTATION_ICON, 255, 0, 0);
36
37 public Image image;
38 public RGB color;
39
40 EditorAnnotationType(String imageResource, int r, int g, int b){
41 image = ImageResources.getImage(imageResource);
42 color = new RGB(r, g, b);
43 }
44
45 /**
46 * @param problem
47 * @return
48 */
49 public static EditorAnnotationType getTypeByProblem(
50 ParserProblem problem) {
51 if (problem.isWarning()) {
52 return WARNING;
53 }else if (problem.isError()) {
54 return ERROR;
55 }
56 return null;
57 }
58
59 }
60
61 // // error identifiers, images and colors
62 // /** Constant <code>ERROR_TYPE="editor.error.type"</code> */
63 // public static String ERROR_TYPE = "editor.error.type";
64 // /** Constant <code>ERROR_IMAGE</code> */
65 // public static Image ERROR_IMAGE = ImageResources.getImage(ImageResources.ERROR_ANNOTATION_ICON);
66 // /** Constant <code>ERROR_RGB</code> */
67 // public static final RGB ERROR_RGB = new RGB(255, 0, 0);
68 //
69 // /** Constant <code>WARNING_TYPE="editor.warning.type"</code> */
70 // public static String WARNING_TYPE = "editor.warning.type";
71 // /** Constant <code>WARNING_IMAGE</code> */
72 // public static Image WARNING_IMAGE = ImageResources.getImage(ImageResources.WARNING_ANNOTATION_ICON);
73 // /** Constant <code>WARNING_RGB</code> */
74 // public static final RGB WARNING_RGB = new RGB(244, 200, 45);
75
76
77 public EditorAnnotation(ParserProblem problem){
78 this(EditorAnnotationType.getTypeByProblem(problem), 0, problem.getMessage());
79 }
80
81
82 /**
83 * <p>Constructor for EditorAnnotation.</p>
84 *
85 * @param marker a org.eclipse.core.resources.IMarker object.
86 */
87 public EditorAnnotation(IMarker marker) {
88 this.marker = marker;
89 }
90
91 /**
92 * <p>Constructor for EditorAnnotation.</p>
93 *
94 * @param line a int.
95 * @param text a {@link java.lang.String} object.
96 */
97 public EditorAnnotation(int line, String text) {
98 this(EditorAnnotationType.ERROR, line, text);
99 }
100
101 /**
102 * <p>Constructor for EditorAnnotation.</p>
103 *
104 * @param type a {@link java.lang.String} object.
105 * @param line a int.
106 * @param text a {@link java.lang.String} object.
107 */
108 public EditorAnnotation(EditorAnnotationType type, int line, String text) {
109 super(type.name(), false, text);
110 this.marker = null;
111 this.line = line;
112 this.text = text;
113 }
114
115 /**
116 * <p>Getter for the field <code>marker</code>.</p>
117 *
118 * @return a org.eclipse.core.resources.IMarker object.
119 */
120 public IMarker getMarker() {
121 return marker;
122 }
123
124 /**
125 * <p>Getter for the field <code>line</code>.</p>
126 *
127 * @return a int.
128 */
129 public int getLine() {
130 return line;
131 }
132
133 /**
134 * <p>Getter for the field <code>text</code>.</p>
135 *
136 * @return a {@link java.lang.String} object.
137 */
138 @Override
139 public String getText() {
140 if (EditorAnnotationType.ERROR.name().equals(getType())) {
141 return Messages.EditorAnnotation_ERROR + text;
142 }
143 if (EditorAnnotationType.WARNING.name().equals(getType())) {
144 return Messages.EditorAnnotation_WARNING + text;
145 }
146 return super.getText();
147 }
148
149 /**
150 * @return
151 */
152 private Image getImage() {
153 return EditorAnnotationType.valueOf(getType()).image;
154 }
155
156 /**
157 * <p>getLayer</p>
158 *
159 * @return a int.
160 */
161 public int getLayer() {
162 return 3;
163 }
164
165
166 /**
167 * <p>Getter for the field <code>position</code>.</p>
168 *
169 * @return a {@link org.eclipse.jface.text.Position} object.
170 */
171 public Position getPosition() {
172 return position;
173 }
174
175 /**
176 * <p>Setter for the field <code>position</code>.</p>
177 *
178 * @param position a {@link org.eclipse.jface.text.Position} object.
179 */
180 public void setPosition(Position position) {
181 this.position = position;
182 }
183
184
185 /** {@inheritDoc} */
186 public void paint(GC gc, Canvas canvas, Rectangle bounds) {
187 Point canvasSize= canvas.getSize();
188
189 int x= 0;
190 int y= bounds.y;
191 int w= canvasSize.x;
192 int h= bounds.height;
193
194 if (y + h > canvasSize.y)
195 h= canvasSize.y - y;
196
197 if (y < 0) {
198 h= h + y;
199 y= 0;
200 }
201
202 if (h <= 0)
203 return;
204
205 Image image = getImage();
206
207 if (image == null) {
208 return;
209 }
210
211 Rectangle r = image.getBounds();
212
213 int destX = x + w - r.width;
214 int destY = y + h - r.height;
215
216 gc.drawImage(image, 0, 0, r.width, r.height, destX, destY, r.width, r.height);
217 }
218 }