001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.jface.text.source.Annotation;
013: import org.eclipse.jface.text.source.IAnnotationPresentation;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.events.DisposeEvent;
017: import org.eclipse.swt.events.DisposeListener;
018: import org.eclipse.swt.graphics.Color;
019: import org.eclipse.swt.graphics.GC;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.graphics.ImageData;
022: import org.eclipse.swt.graphics.PaletteData;
023: import org.eclipse.swt.graphics.Point;
024: import org.eclipse.swt.graphics.RGB;
025: import org.eclipse.swt.graphics.Rectangle;
026: import org.eclipse.swt.widgets.Canvas;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Display;
029:
030: /**
031: * Specialized annotation to indicate a particular range of text lines.
032: * <p>
033: * This class may be instantiated; it is not intended to be subclassed.
034: * This class is instantiated automatically by <code>AbstractTextEditor</code>.
035: * </p>
036: */
037: public class DefaultRangeIndicator extends Annotation implements
038: IAnnotationPresentation {
039:
040: /** The color palette data of this range indicator */
041: private static PaletteData fgPaletteData;
042: /** The image of this range indicator */
043: private Image fImage;
044:
045: /**
046: * Creates a new range indicator.
047: */
048: public DefaultRangeIndicator() {
049: }
050:
051: /*
052: * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
053: */
054: public void paint(GC gc, Canvas canvas, Rectangle bounds) {
055:
056: Point canvasSize = canvas.getSize();
057:
058: int x = 0;
059: int y = bounds.y;
060: int w = canvasSize.x;
061: int h = bounds.height;
062: int b = 1;
063:
064: if (y + h > canvasSize.y)
065: h = canvasSize.y - y;
066:
067: if (y < 0) {
068: h = h + y;
069: y = 0;
070: }
071:
072: if (h <= 0)
073: return;
074:
075: Image image = getImage(canvas);
076: gc.drawImage(image, 0, 0, w, h, x, y, w, h);
077:
078: gc.setBackground(canvas.getDisplay().getSystemColor(
079: SWT.COLOR_LIST_SELECTION));
080: gc.fillRectangle(x, bounds.y, w, b);
081: gc.fillRectangle(x, bounds.y + bounds.height - b, w, b);
082: }
083:
084: /*
085: * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
086: */
087: public int getLayer() {
088: return IAnnotationPresentation.DEFAULT_LAYER;
089: }
090:
091: /**
092: * Returns the image of this range indicator.
093: *
094: * @param control the control
095: * @return an image
096: */
097: private Image getImage(Control control) {
098: if (fImage == null) {
099: fImage = createImage(control.getDisplay(), control
100: .getSize());
101:
102: control.addDisposeListener(new DisposeListener() {
103: public void widgetDisposed(DisposeEvent e) {
104: if (fImage != null && !fImage.isDisposed()) {
105: fImage.dispose();
106: fImage = null;
107: }
108: }
109: });
110: } else {
111: Rectangle imageRectangle = fImage.getBounds();
112: Point controlSize = control.getSize();
113:
114: if (imageRectangle.width < controlSize.x
115: || 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,
141: fgPaletteData);
142:
143: for (int y = 0; y < height; y++)
144: for (int x = 0; x < width; x++)
145: imageData.setPixel(x, y, (x + y) % 2);
146:
147: return new Image(display, imageData);
148: }
149:
150: /**
151: * Creates and returns a new color palette data.
152: *
153: * @param display
154: * @return the new color palette data
155: */
156: private static PaletteData createPalette(Display display) {
157: Color c1;
158: Color c2;
159:
160: if (false) {
161: // range lighter
162: c1 = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
163: c2 = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
164: } else {
165: // range darker
166: c1 = display.getSystemColor(SWT.COLOR_LIST_SELECTION);
167: c2 = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
168: }
169:
170: RGB rgbs[] = new RGB[] {
171: new RGB(c1.getRed(), c1.getGreen(), c1.getBlue()),
172: new RGB(c2.getRed(), c2.getGreen(), c2.getBlue()) };
173:
174: return new PaletteData(rgbs);
175: }
176: }
|