001: /*******************************************************************************
002: * Copyright (c) 2000, 2004 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Common Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/cpl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.presentation.sidewinder;
011:
012: import org.eclipse.jface.resource.JFaceResources;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.events.PaintEvent;
015: import org.eclipse.swt.events.PaintListener;
016: import org.eclipse.swt.graphics.*;
017: import org.eclipse.swt.widgets.Canvas;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.ui.presentations.IPresentablePart;
020:
021: public class PartItem extends Canvas implements PaintListener {
022:
023: private String text;
024: private Image image;
025: private boolean selected;
026: private boolean focus = false;
027: private IPresentablePart part;
028:
029: private static final int VERT_SPACING = 5;
030: private static final int HORIZ_SPACING = 3;
031:
032: int[] SIMPLE_TOP_LEFT_CORNER = new int[] { 0, 4, 4, 0 };
033: int[] SIMPLE_TOP_RIGHT_CORNER = new int[] { 0, 0, 0, 0 };
034:
035: //static final int[] SIMPLE_TOP_LEFT_CORNER = new int[] {0,2, 1,1, 2,0};
036: //static final int[] SIMPLE_TOP_RIGHT_CORNER = new int[] {-2,0, -1,1, 0,2};
037:
038: static final int[] SIMPLE_BOTTOM_LEFT_CORNER = new int[] { 0, -2,
039: 1, -1, 2, 0 };
040: static final int[] SIMPLE_BOTTOM_RIGHT_CORNER = new int[] { -2, 0,
041: -1, -1, 0, -2 };
042: private boolean showImage;
043: private boolean showText;
044:
045: public PartItem(Composite parent, IPresentablePart part) {
046: super (parent, SWT.NONE);
047: addPaintListener(this );
048: this .part = part;
049: }
050:
051: public void paintControl(PaintEvent e) {
052: Rectangle titleRect = getClientArea();
053: int x = titleRect.x + VERT_SPACING;
054: int y = titleRect.y + HORIZ_SPACING;
055: GC gc = e.gc;
056: setBackground(getParent().getBackground());
057: fill(gc, titleRect.x, titleRect.y, titleRect.width - 1,
058: titleRect.height);
059:
060: Image image = getImage();
061: if (image != null && showImage) {
062: Rectangle imageBounds = image.getBounds();
063: int imageX = x;
064: int imageHeight = imageBounds.height;
065: int imageY = (titleRect.height - imageHeight) / 2;
066: int imageWidth = imageBounds.width * imageHeight
067: / imageBounds.height;
068: gc.drawImage(image, imageBounds.x, imageBounds.y,
069: imageBounds.width, imageBounds.height, imageX,
070: imageY, imageWidth, imageHeight);
071: x += imageWidth + VERT_SPACING;
072: }
073:
074: int textWidth = titleRect.width - 1;
075: if (textWidth > 0 && text != null && showText) {
076: Font gcFont = gc.getFont();
077: gc.setFont(getFont());
078: Point extent = gc.textExtent(text, SWT.DRAW_TRANSPARENT
079: | SWT.DRAW_MNEMONIC);
080: int textY = titleRect.y + (titleRect.height - extent.y) / 2;
081:
082: if (selected)
083: gc.setForeground(e.display
084: .getSystemColor(SWT.COLOR_WHITE));
085: else
086: gc.setForeground(e.display
087: .getSystemColor(SWT.COLOR_BLACK));
088: gc.setFont(JFaceResources.getDefaultFont());
089: gc.drawText(text, x, textY, SWT.DRAW_TRANSPARENT
090: | SWT.DRAW_MNEMONIC);
091: }
092:
093: }
094:
095: public Point computeSize(int wHint, int hHint) {
096: int width = VERT_SPACING;
097: int height = HORIZ_SPACING;
098: GC gc = new GC(this );
099: if (image != null && showImage) {
100: Rectangle imageBounds = image.getBounds();
101: height = imageBounds.height + HORIZ_SPACING;
102: width += imageBounds.width + VERT_SPACING;
103: }
104:
105: if (text != null && showText) {
106: Point extent = gc.textExtent(text, SWT.DRAW_TRANSPARENT
107: | SWT.DRAW_MNEMONIC);
108: width += extent.x + VERT_SPACING;
109: height = Math.max(height, extent.y) + HORIZ_SPACING;
110: }
111:
112: if (wHint != SWT.DEFAULT)
113: width = wHint;
114: if (hHint != SWT.DEFAULT)
115: height = hHint;
116: gc.dispose();
117: return new Point(width, height);
118: }
119:
120: private void fill(GC gc, int x, int y, int width, int height) {
121: int[] left = SIMPLE_TOP_LEFT_CORNER;
122: int[] right = SIMPLE_TOP_RIGHT_CORNER;
123: int[] shape = new int[left.length + right.length + 4];
124: int index = 0;
125: shape[index++] = x;
126: shape[index++] = y + height + 1;
127: for (int i = 0; i < left.length / 2; i++) {
128: shape[index++] = x + left[2 * i];
129: shape[index++] = y + left[2 * i + 1];
130: }
131: for (int i = 0; i < right.length / 2; i++) {
132: shape[index++] = x + width + right[2 * i];
133: shape[index++] = y + right[2 * i + 1];
134: }
135: shape[index++] = x + width;
136: shape[index++] = y + height + 1;
137:
138: // Fill in background
139: Region clipping = new Region();
140: gc.getClipping(clipping);
141: Region region = new Region();
142: region.add(shape);
143: region.intersect(clipping);
144: gc.setClipping(region);
145:
146: gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_GRAY));
147:
148: Color fg = null;
149: if (part.isDirty())
150: fg = getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
151: else if (!selected)
152: fg = getDisplay().getSystemColor(SWT.COLOR_WHITE);
153: //else if(focus)
154: //fg = getDisplay().getSystemColor(SWT.COLOR_BLUE);
155: else
156: fg = getDisplay().getSystemColor(SWT.COLOR_BLUE);
157:
158: gc.setForeground(fg);
159:
160: gc.fillGradientRectangle(x, y, x + width, y + height, true);
161: //gc.fillRectangle(x, y, x + width, y + height);
162:
163: region.dispose();
164: clipping.dispose();
165:
166: gc.setClipping((Rectangle) null);
167: gc.setForeground(getDisplay().getSystemColor(
168: SWT.COLOR_DARK_GRAY));
169: gc.drawPolyline(shape);
170:
171: // Fill in parent background for non-rectangular shape
172: Region r = new Region();
173: r.add(new Rectangle(x, y, width + 1, height + 1));
174: r.subtract(shape);
175: gc.setBackground(getParent().getBackground());
176: //fillRegion(gc, r);
177: r.dispose();
178: }
179:
180: static void fillRegion(GC gc, Region region) {
181: // NOTE: region passed in to this function will be modified
182: Region clipping = new Region();
183: gc.getClipping(clipping);
184: region.intersect(clipping);
185: gc.setClipping(region);
186: gc.fillRectangle(region.getBounds());
187: gc.setClipping(clipping);
188: clipping.dispose();
189: }
190:
191: public Point computeSize(int wHint, int hHint, boolean changed) {
192: return computeSize(wHint, hHint);
193: }
194:
195: public void setText(String text) {
196: this .text = text;
197: redraw();
198: }
199:
200: public String getText() {
201: return this .text;
202: }
203:
204: public void setImage(Image image) {
205: this .image = image;
206: }
207:
208: public Image getImage() {
209: return this .image;
210: }
211:
212: public void setSelected(boolean selected) {
213: this .selected = selected;
214: redraw();
215: }
216:
217: public boolean getSelected() {
218: return this .selected;
219: }
220:
221: public boolean isFocus() {
222: return focus;
223: }
224:
225: public void setFocus(boolean focus) {
226: this .focus = focus;
227: }
228:
229: public void setShowImage(boolean showImage) {
230: this .showImage = showImage;
231: }
232:
233: public void setShowText(boolean showText) {
234: this.showText = showText;
235: }
236: }
|