1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.texteditor;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationPresentation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
/**
* Specialized annotation to indicate a particular range of text lines.
* <p>
* This class may be instantiated; it is not intended to be subclassed.
* This class is instantiated automatically by <code>AbstractTextEditor</code>.
* </p>
*/
public class DefaultRangeIndicator extends Annotation implements IAnnotationPresentation {
/** The color palette data of this range indicator */
private static PaletteData fgPaletteData;
/** The image of this range indicator */
private Image fImage;
/**
* Creates a new range indicator.
*/
public DefaultRangeIndicator() {
}
/*
* @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
*/
public void paint(GC gc, Canvas canvas, Rectangle bounds) {
Point canvasSize= canvas.getSize();
int x= 0;
int y= bounds.y;
int w= canvasSize.x;
int h= bounds.height;
int b= 1;
if (y + h > canvasSize.y)
h= canvasSize.y - y;
if (y < 0) {
h= h + y;
y= 0;
}
if (h <= 0)
return;
Image image = getImage(canvas);
gc.drawImage(image, 0, 0, w, h, x, y, w, h);
gc.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.fillRectangle(x, bounds.y, w, b);
gc.fillRectangle(x, bounds.y + bounds.height - b, w, b);
}
/*
* @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
*/
public int getLayer() {
return IAnnotationPresentation.DEFAULT_LAYER;
}
/**
* Returns the image of this range indicator.
*
* @param control the control
* @return an image
*/
private Image getImage(Control control) {
if (fImage == null) {
fImage= createImage(control.getDisplay(), control.getSize());
control.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if (fImage != null && !fImage.isDisposed()) {
fImage.dispose();
fImage= null;
}
}
});
} else {
Rectangle imageRectangle= fImage.getBounds();
Point controlSize= control.getSize();
if (imageRectangle.width < controlSize.x || imageRectangle.height < controlSize.y) {
fImage.dispose();
fImage= createImage(control.getDisplay(), controlSize);
}
}
return fImage;
}
/**
* Creates and returns a new SWT image with the given size on
* the given display which is used as this range indicator's image.
*
* @param display the display on which to create the image
* @param size the image size
* @return a new image
*/
private static Image createImage(Display display, Point size) {
int width= size.x;
int height= size.y;
if (fgPaletteData == null)
fgPaletteData= createPalette(display);
ImageData imageData= new ImageData(width, height, 1, fgPaletteData);
for (int y= 0; y < height; y++)
for (int x= 0; x < width; x++)
imageData.setPixel(x, y, (x + y) % 2);
return new Image(display, imageData);
}
/**
* Creates and returns a new color palette data.
*
* @param display
* @return the new color palette data
*/
private static PaletteData createPalette(Display display) {
Color c1;
Color c2;
if (false) {
// range lighter
c1= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
c2= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
} else {
// range darker
c1= display.getSystemColor(SWT.COLOR_LIST_SELECTION);
c2= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
}
RGB rgbs[]= new RGB[] {
new RGB(c1.getRed(), c1.getGreen(), c1.getBlue()),
new RGB(c2.getRed(), c2.getGreen(), c2.getBlue())};
return new PaletteData(rgbs);
}
}
|