KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > DefaultRangeIndicator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.texteditor;
13
14
15 import org.eclipse.jface.text.source.Annotation;
16 import org.eclipse.jface.text.source.IAnnotationPresentation;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.ImageData;
25 import org.eclipse.swt.graphics.PaletteData;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.widgets.Canvas;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Display;
32
33
34 /**
35  * Specialized annotation to indicate a particular range of text lines.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * This class is instantiated automatically by <code>AbstractTextEditor</code>.
39  * </p>
40  */

41 public class DefaultRangeIndicator extends Annotation implements IAnnotationPresentation {
42
43      /** The color palette data of this range indicator */
44     private static PaletteData fgPaletteData;
45     /** The image of this range indicator */
46     private Image fImage;
47
48     /**
49      * Creates a new range indicator.
50      */

51     public DefaultRangeIndicator() {
52     }
53
54     /*
55      * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
56      */

57     public void paint(GC gc, Canvas canvas, Rectangle bounds) {
58
59         Point canvasSize= canvas.getSize();
60
61         int x= 0;
62         int y= bounds.y;
63         int w= canvasSize.x;
64         int h= bounds.height;
65         int b= 1;
66
67         if (y + h > canvasSize.y)
68             h= canvasSize.y - y;
69
70         if (y < 0) {
71             h= h + y;
72             y= 0;
73         }
74
75         if (h <= 0)
76             return;
77
78         Image image = getImage(canvas);
79         gc.drawImage(image, 0, 0, w, h, x, y, w, h);
80
81         gc.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
82         gc.fillRectangle(x, bounds.y, w, b);
83         gc.fillRectangle(x, bounds.y + bounds.height - b, w, b);
84     }
85
86     /*
87      * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
88      */

89     public int getLayer() {
90         return IAnnotationPresentation.DEFAULT_LAYER;
91     }
92
93     /**
94      * Returns the image of this range indicator.
95      *
96      * @param control the control
97      * @return an image
98      */

99     private Image getImage(Control control) {
100         if (fImage == null) {
101                 fImage= createImage(control.getDisplay(), control.getSize());
102
103                 control.addDisposeListener(new DisposeListener() {
104                     public void widgetDisposed(DisposeEvent e) {
105                         if (fImage != null && !fImage.isDisposed()) {
106                             fImage.dispose();
107                             fImage= null;
108                         }
109                     }
110                 });
111         } else {
112             Rectangle imageRectangle= fImage.getBounds();
113             Point controlSize= control.getSize();
114
115             if (imageRectangle.width < controlSize.x || imageRectangle.height < controlSize.y) {
116                 fImage.dispose();
117                 fImage= createImage(control.getDisplay(), controlSize);
118             }
119         }
120
121         return fImage;
122     }
123
124     /**
125      * Creates and returns a new SWT image with the given size on
126      * the given display which is used as this range indicator's image.
127      *
128      * @param display the display on which to create the image
129      * @param size the image size
130      * @return a new image
131      */

132     private static Image createImage(Display display, Point size) {
133
134         int width= size.x;
135         int height= size.y;
136
137         if (fgPaletteData == null)
138             fgPaletteData= createPalette(display);
139
140         ImageData imageData= new ImageData(width, height, 1, fgPaletteData);
141
142         for (int y= 0; y < height; y++)
143             for (int x= 0; x < width; x++)
144                 imageData.setPixel(x, y, (x + y) % 2);
145
146         return new Image(display, imageData);
147     }
148
149     /**
150      * Creates and returns a new color palette data.
151      *
152      * @param display
153      * @return the new color palette data
154      */

155     private static PaletteData createPalette(Display display) {
156         Color c1;
157         Color c2;
158
159         if (false) {
160             // range lighter
161
c1= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
162             c2= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
163         } else {
164             // range darker
165
c1= display.getSystemColor(SWT.COLOR_LIST_SELECTION);
166             c2= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
167         }
168
169         RGB rgbs[]= new RGB[] {
170             new RGB(c1.getRed(), c1.getGreen(), c1.getBlue()),
171             new RGB(c2.getRed(), c2.getGreen(), c2.getBlue())};
172
173         return new PaletteData(rgbs);
174     }
175 }
176
Popular Tags