001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.jface.viewers;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.custom.TableTree;
014: import org.eclipse.swt.graphics.Color;
015: import org.eclipse.swt.graphics.FontMetrics;
016: import org.eclipse.swt.graphics.GC;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.swt.graphics.ImageData;
019: import org.eclipse.swt.graphics.PaletteData;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.graphics.RGB;
022: import org.eclipse.swt.graphics.Rectangle;
023: import org.eclipse.swt.widgets.ColorDialog;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Layout;
028: import org.eclipse.swt.widgets.Table;
029: import org.eclipse.swt.widgets.Tree;
030:
031: /**
032: * A cell editor that manages a color field.
033: * The cell editor's value is the color (an SWT <code>RBG</code>).
034: * <p>
035: * This class may be instantiated; it is not intended to be subclassed.
036: * </p>
037: */
038: public class ColorCellEditor extends DialogCellEditor {
039:
040: /**
041: * The default extent in pixels.
042: */
043: private static final int DEFAULT_EXTENT = 16;
044:
045: /**
046: * Gap between between image and text in pixels.
047: */
048: private static final int GAP = 6;
049:
050: /**
051: * The composite widget containing the color and RGB label widgets
052: */
053: private Composite composite;
054:
055: /**
056: * The label widget showing the current color.
057: */
058: private Label colorLabel;
059:
060: /**
061: * The label widget showing the RGB values.
062: */
063: private Label rgbLabel;
064:
065: /**
066: * The image.
067: */
068: private Image image;
069:
070: /**
071: * Internal class for laying out this cell editor.
072: */
073: private class ColorCellLayout extends Layout {
074: public Point computeSize(Composite editor, int wHint,
075: int hHint, boolean force) {
076: if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
077: return new Point(wHint, hHint);
078: }
079: Point colorSize = colorLabel.computeSize(SWT.DEFAULT,
080: SWT.DEFAULT, force);
081: Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT,
082: SWT.DEFAULT, force);
083: return new Point(colorSize.x + GAP + rgbSize.x, Math.max(
084: colorSize.y, rgbSize.y));
085: }
086:
087: public void layout(Composite editor, boolean force) {
088: Rectangle bounds = editor.getClientArea();
089: Point colorSize = colorLabel.computeSize(SWT.DEFAULT,
090: SWT.DEFAULT, force);
091: Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT,
092: SWT.DEFAULT, force);
093: int ty = (bounds.height - rgbSize.y) / 2;
094: if (ty < 0) {
095: ty = 0;
096: }
097: colorLabel.setBounds(-1, 0, colorSize.x, colorSize.y);
098: rgbLabel.setBounds(colorSize.x + GAP - 1, ty, bounds.width
099: - colorSize.x - GAP, bounds.height);
100: }
101: }
102:
103: /**
104: * Creates a new color cell editor parented under the given control.
105: * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
106: * validator.
107: *
108: * @param parent the parent control
109: */
110: public ColorCellEditor(Composite parent) {
111: this (parent, SWT.NONE);
112: }
113:
114: /**
115: * Creates a new color cell editor parented under the given control.
116: * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
117: * validator.
118: *
119: * @param parent the parent control
120: * @param style the style bits
121: * @since 2.1
122: */
123: public ColorCellEditor(Composite parent, int style) {
124: super (parent, style);
125: doSetValue(new RGB(0, 0, 0));
126: }
127:
128: /**
129: * Creates and returns the color image data for the given control
130: * and RGB value. The image's size is either the control's item extent
131: * or the cell editor's default extent, which is 16 pixels square.
132: *
133: * @param w the control
134: * @param color the color
135: */
136: private ImageData createColorImage(Control w, RGB color) {
137:
138: GC gc = new GC(w);
139: FontMetrics fm = gc.getFontMetrics();
140: int size = fm.getAscent();
141: gc.dispose();
142:
143: int indent = 6;
144: int extent = DEFAULT_EXTENT;
145: if (w instanceof Table) {
146: extent = ((Table) w).getItemHeight() - 1;
147: } else if (w instanceof Tree) {
148: extent = ((Tree) w).getItemHeight() - 1;
149: } else if (w instanceof TableTree) {
150: extent = ((TableTree) w).getItemHeight() - 1;
151: }
152:
153: if (size > extent) {
154: size = extent;
155: }
156:
157: int width = indent + size;
158: int height = extent;
159:
160: int xoffset = indent;
161: int yoffset = (height - size) / 2;
162:
163: RGB black = new RGB(0, 0, 0);
164: PaletteData dataPalette = new PaletteData(new RGB[] { black,
165: black, color });
166: ImageData data = new ImageData(width, height, 4, dataPalette);
167: data.transparentPixel = 0;
168:
169: int end = size - 1;
170: for (int y = 0; y < size; y++) {
171: for (int x = 0; x < size; x++) {
172: if (x == 0 || y == 0 || x == end || y == end) {
173: data.setPixel(x + xoffset, y + yoffset, 1);
174: } else {
175: data.setPixel(x + xoffset, y + yoffset, 2);
176: }
177: }
178: }
179:
180: return data;
181: }
182:
183: /* (non-Javadoc)
184: * Method declared on DialogCellEditor.
185: */
186: protected Control createContents(Composite cell) {
187: Color bg = cell.getBackground();
188: composite = new Composite(cell, getStyle());
189: composite.setBackground(bg);
190: composite.setLayout(new ColorCellLayout());
191: colorLabel = new Label(composite, SWT.LEFT);
192: colorLabel.setBackground(bg);
193: rgbLabel = new Label(composite, SWT.LEFT);
194: rgbLabel.setBackground(bg);
195: rgbLabel.setFont(cell.getFont());
196: return composite;
197: }
198:
199: /* (non-Javadoc)
200: * Method declared on CellEditor.
201: */
202: public void dispose() {
203: if (image != null) {
204: image.dispose();
205: image = null;
206: }
207: super .dispose();
208: }
209:
210: /* (non-Javadoc)
211: * Method declared on DialogCellEditor.
212: */
213: protected Object openDialogBox(Control cellEditorWindow) {
214: ColorDialog dialog = new ColorDialog(cellEditorWindow
215: .getShell());
216: Object value = getValue();
217: if (value != null) {
218: dialog.setRGB((RGB) value);
219: }
220: value = dialog.open();
221: return dialog.getRGB();
222: }
223:
224: /* (non-Javadoc)
225: * Method declared on DialogCellEditor.
226: */
227: protected void updateContents(Object value) {
228: RGB rgb = (RGB) value;
229: // XXX: We don't have a value the first time this method is called".
230: if (rgb == null) {
231: rgb = new RGB(0, 0, 0);
232: }
233: // XXX: Workaround for 1FMQ0P3: SWT:ALL - TableItem.setImage doesn't work if using the identical image."
234: if (image != null) {
235: image.dispose();
236: }
237:
238: ImageData id = createColorImage(colorLabel.getParent()
239: .getParent(), rgb);
240: ImageData mask = id.getTransparencyMask();
241: image = new Image(colorLabel.getDisplay(), id, mask);
242: colorLabel.setImage(image);
243:
244: rgbLabel
245: .setText("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
246: }
247: }
|