Project

General

Profile

Download (3.15 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor;
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
public class EditorAnnotation extends Annotation implements IAnnotationPresentation {
18
	private static final Logger logger = Logger
19
			.getLogger(EditorAnnotation.class);
20
    private IMarker marker;
21
    private String text;
22
    private int line;
23
    private Position position;
24

    
25
    // error identifiers, images and colors
26
    public static String ERROR_TYPE = "editor.error.type";
27
    public static Image ERROR_IMAGE = ImageResources.getImage(ImageResources.ERROR_ANNOTATION_ICON);
28
    public static final RGB ERROR_RGB = new RGB(255, 0, 0);    
29
    
30
    public static String WARNING_TYPE = "editor.warning.type";
31
    public static Image WARNING_IMAGE = ImageResources.getImage(ImageResources.WARNING_ANNOTATION_ICON);
32
    public static final RGB WARNING_RGB = new RGB(244, 200, 45); 
33
    
34
    public EditorAnnotation(IMarker marker) {
35
        this.marker = marker;
36
    }
37

    
38
    public EditorAnnotation(int line, String text) {
39
    	this(ERROR_TYPE, line, text);
40
    }
41

    
42
    public EditorAnnotation(String type, int line, String text) {
43
    	super(type, false, text);
44
        this.marker = null;
45
        this.line = line;
46
        this.text = text;
47
    }
48
    
49
    public IMarker getMarker() {
50
        return marker;
51
    }
52

    
53
    public int getLine() {
54
        return line;
55
    }
56

    
57
    public String getText() {
58
    	if (ERROR_TYPE.equals(getType())) {
59
    		return "Error: " + text;
60
    	}
61
    	if (WARNING_TYPE.equals(getType())) {
62
    		return "Warning: " + text;
63
    	}
64
    	return super.getText();
65
    }
66

    
67
	/**
68
	 * @return
69
	 */
70
	private Image getImage() {
71
    	if (ERROR_TYPE.equals(getType())) {
72
    		return ERROR_IMAGE;
73
    	}
74
    	if (WARNING_TYPE.equals(getType())) {
75
    		return WARNING_IMAGE;
76
    	}
77
		logger.warn("No image for type " + getType());
78
		return null;
79
	}
80
    
81
    public int getLayer() {
82
        return 3;
83
    }
84

    
85

    
86
    public Position getPosition() {
87
        return position;
88
    }
89

    
90
    public void setPosition(Position position) {
91
        this.position = position;
92
    }
93

    
94
	
95
	public void paint(GC gc, Canvas canvas, Rectangle bounds) {
96
		Point canvasSize= canvas.getSize();
97

    
98
		int x= 0;
99
		int y= bounds.y;
100
		int w= canvasSize.x;
101
		int h= bounds.height;
102

    
103
		if (y + h > canvasSize.y)
104
			h= canvasSize.y - y;
105

    
106
		if (y < 0) {
107
			h= h + y;
108
			y= 0;
109
		}
110

    
111
		if (h <= 0)
112
			return;
113

    
114
		Image image = getImage();
115
		
116
		if (image == null) {
117
			return;
118
		}
119
		
120
		Rectangle r = image.getBounds();
121
		
122
		int destX = x + w - r.width;
123
		int destY = y + h - r.height;
124
		
125
		gc.drawImage(image, 0, 0, r.width, r.height, destX, destY, r.width, r.height);
126
	}
127
}
(4-4/26)