KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ColorCellEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jface.viewers;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.TableTree;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.FontMetrics;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.ImageData;
20 import org.eclipse.swt.graphics.PaletteData;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.widgets.ColorDialog;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Layout;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.Tree;
31
32 /**
33  * A cell editor that manages a color field.
34  * The cell editor's value is the color (an SWT <code>RBG</code>).
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  */

39 public class ColorCellEditor extends DialogCellEditor {
40
41     /**
42      * The default extent in pixels.
43      */

44     private static final int DEFAULT_EXTENT = 16;
45
46     /**
47      * Gap between between image and text in pixels.
48      */

49     private static final int GAP = 6;
50
51     /**
52      * The composite widget containing the color and RGB label widgets
53      */

54     private Composite composite;
55
56     /**
57      * The label widget showing the current color.
58      */

59     private Label colorLabel;
60
61     /**
62      * The label widget showing the RGB values.
63      */

64     private Label rgbLabel;
65
66     /**
67      * The image.
68      */

69     private Image image;
70
71     /**
72      * Internal class for laying out this cell editor.
73      */

74     private class ColorCellLayout extends Layout {
75         public Point computeSize(Composite editor, int wHint, int hHint,
76                 boolean force) {
77             if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
78                 return new Point(wHint, hHint);
79             }
80             Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
81                     force);
82             Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
83                     force);
84             return new Point(colorSize.x + GAP + rgbSize.x, Math.max(
85                     colorSize.y, rgbSize.y));
86         }
87
88         public void layout(Composite editor, boolean force) {
89             Rectangle bounds = editor.getClientArea();
90             Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
91                     force);
92             Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
93                     force);
94             int ty = (bounds.height - rgbSize.y) / 2;
95             if (ty < 0) {
96                 ty = 0;
97             }
98             colorLabel.setBounds(-1, 0, colorSize.x, colorSize.y);
99             rgbLabel.setBounds(colorSize.x + GAP - 1, ty, bounds.width
100                     - colorSize.x - GAP, bounds.height);
101         }
102     }
103
104     /**
105      * Creates a new color cell editor parented under the given control.
106      * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
107      * validator.
108      *
109      * @param parent the parent control
110      */

111     public ColorCellEditor(Composite parent) {
112         this(parent, SWT.NONE);
113     }
114
115     /**
116      * Creates a new color cell editor parented under the given control.
117      * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
118      * validator.
119      *
120      * @param parent the parent control
121      * @param style the style bits
122      * @since 2.1
123      */

124     public ColorCellEditor(Composite parent, int style) {
125         super(parent, style);
126         doSetValue(new RGB(0, 0, 0));
127     }
128
129     /**
130      * Creates and returns the color image data for the given control
131      * and RGB value. The image's size is either the control's item extent
132      * or the cell editor's default extent, which is 16 pixels square.
133      *
134      * @param w the control
135      * @param color the color
136      */

137     private ImageData createColorImage(Control w, RGB color) {
138
139         GC gc = new GC(w);
140         FontMetrics fm = gc.getFontMetrics();
141         int size = fm.getAscent();
142         gc.dispose();
143
144         int indent = 6;
145         int extent = DEFAULT_EXTENT;
146         if (w instanceof Table) {
147             extent = ((Table) w).getItemHeight() - 1;
148         } else if (w instanceof Tree) {
149             extent = ((Tree) w).getItemHeight() - 1;
150         } else if (w instanceof TableTree) {
151             extent = ((TableTree) w).getItemHeight() - 1;
152         }
153
154         if (size > extent) {
155             size = extent;
156         }
157
158         int width = indent + size;
159         int height = extent;
160
161         int xoffset = indent;
162         int yoffset = (height - size) / 2;
163
164         RGB black = new RGB(0, 0, 0);
165         PaletteData dataPalette = new PaletteData(new RGB[] { black, black,
166                 color });
167         ImageData data = new ImageData(width, height, 4, dataPalette);
168         data.transparentPixel = 0;
169
170         int end = size - 1;
171         for (int y = 0; y < size; y++) {
172             for (int x = 0; x < size; x++) {
173                 if (x == 0 || y == 0 || x == end || y == end) {
174                     data.setPixel(x + xoffset, y + yoffset, 1);
175                 } else {
176                     data.setPixel(x + xoffset, y + yoffset, 2);
177                 }
178             }
179         }
180
181         return data;
182     }
183
184     /* (non-Javadoc)
185      * Method declared on DialogCellEditor.
186      */

187     protected Control createContents(Composite cell) {
188         Color bg = cell.getBackground();
189         composite = new Composite(cell, getStyle());
190         composite.setBackground(bg);
191         composite.setLayout(new ColorCellLayout());
192         colorLabel = new Label(composite, SWT.LEFT);
193         colorLabel.setBackground(bg);
194         rgbLabel = new Label(composite, SWT.LEFT);
195         rgbLabel.setBackground(bg);
196         rgbLabel.setFont(cell.getFont());
197         return composite;
198     }
199
200     /* (non-Javadoc)
201      * Method declared on CellEditor.
202      */

203     public void dispose() {
204         if (image != null) {
205             image.dispose();
206             image = null;
207         }
208         super.dispose();
209     }
210
211     /* (non-Javadoc)
212      * Method declared on DialogCellEditor.
213      */

214     protected Object JavaDoc openDialogBox(Control cellEditorWindow) {
215         ColorDialog dialog = new ColorDialog(cellEditorWindow.getShell());
216         Object JavaDoc 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 JavaDoc 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().getParent(), rgb);
239         ImageData mask = id.getTransparencyMask();
240         image = new Image(colorLabel.getDisplay(), id, mask);
241         colorLabel.setImage(image);
242
243         rgbLabel
244                 .setText("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
245
}
246 }
247
Popular Tags