KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > properties > AWTColorCellEditor


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 02.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.properties;
9
10 import org.eclipse.jface.viewers.DialogCellEditor;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.custom.TableTree;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.FontMetrics;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.ImageData;
18 import org.eclipse.swt.graphics.PaletteData;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.graphics.RGB;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.ColorDialog;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Layout;
27 import org.eclipse.swt.widgets.Table;
28 import org.eclipse.swt.widgets.Tree;
29
30 import com.nightlabs.editor2d.util.J2DUtil;
31
32
33 public class AWTColorCellEditor
34 extends DialogCellEditor
35 {
36   /*
37      * The default extent in pixels.
38      */

39     private static final int DEFAULT_EXTENT= 16;
40     
41     /**
42      * Gap between between image and text in pixels.
43      */

44     private static final int GAP = 6;
45
46     /**
47      * The composite widget containing the color and RGB label widgets
48      */

49     private Composite composite;
50
51     /**
52      * The label widget showing the current color.
53      */

54     private Label colorLabel;
55
56     /**
57      * The label widget showing the RGB values.
58      */

59     private Label rgbLabel;
60
61     /**
62      * The image.
63      */

64     private Image image;
65     
66     /**
67      * Internal class for laying out this cell editor.
68      */

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

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

119 public AWTColorCellEditor(Composite parent, int style) {
120     super(parent, style);
121 // doSetValue(new java.awt.Color(0,0,0));
122
}
123
124 /**
125  * Creates and returns the color image data for the given control
126  * and RGB value. The image's size is either the control's item extent
127  * or the cell editor's default extent, which is 16 pixels square.
128  *
129  * @param w the control
130  * @param color the color
131  */

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

178 protected Control createContents(Composite cell)
179 {
180   // TODO: remove colorLabel, replaced by AWTColorLabelProvider
181
Color bg = cell.getBackground();
182     composite = new Composite(cell, getStyle());
183     composite.setBackground(bg);
184     composite.setLayout(new ColorCellLayout());
185     colorLabel = new Label(composite, SWT.LEFT);
186     colorLabel.setBackground(bg);
187     rgbLabel = new Label(composite, SWT.LEFT);
188     rgbLabel.setBackground(bg);
189     rgbLabel.setFont(cell.getFont());
190     return composite;
191 }
192
193 /* (non-Javadoc)
194  * Method declared on CellEditor.
195  */

196 public void dispose() {
197     if (image != null) {
198         image.dispose();
199         image = null;
200     }
201     super.dispose();
202 }
203
204 /* (non-Javadoc)
205  * Method declared on DialogCellEditor.
206  */

207 protected Object JavaDoc openDialogBox(Control cellEditorWindow) {
208     ColorDialog dialog = new ColorDialog(cellEditorWindow.getShell());
209     Object JavaDoc value = getValue();
210     if (value != null) {
211       if (value instanceof String JavaDoc) {
212         value = new java.awt.Color JavaDoc(0,0,0);
213       }
214       if (value instanceof java.awt.Color JavaDoc) {
215         value = J2DUtil.toRGB((java.awt.Color JavaDoc)value);
216       }
217       dialog.setRGB((RGB) value);
218     }
219     value = dialog.open();
220     RGB rgb = dialog.getRGB();
221     java.awt.Color JavaDoc awtColor = new java.awt.Color JavaDoc(rgb.red, rgb.green, rgb.blue);
222     return awtColor;
223 }
224
225     /* (non-Javadoc)
226      * Method declared on DialogCellEditor.
227      */

228     protected void updateContents(Object JavaDoc value)
229     {
230       if (value != null) {
231           System.out.println("value = instanceof "+value.getClass());
232       }
233
234       if (value instanceof String JavaDoc) {
235         value = new java.awt.Color JavaDoc(0,0,0);
236       }
237       
238     // RGB rgb = (RGB) value;
239
if (value == null) {
240         value = new java.awt.Color JavaDoc(0,0,0);
241       }
242           
243       RGB rgb = J2DUtil.toRGB((java.awt.Color JavaDoc)value);
244         // XXX: We don't have a value the first time this method is called".
245
if (rgb == null) {
246             rgb = new RGB(0, 0, 0);
247         }
248         // XXX: Workaround for 1FMQ0P3: SWT:ALL - TableItem.setImage doesn't work if using the identical image."
249
if (image != null)
250             image.dispose();
251     
252         ImageData id = createColorImage(colorLabel.getParent().getParent(), rgb);
253         ImageData mask = id.getTransparencyMask();
254         image = new Image(colorLabel.getDisplay(), id, mask);
255         colorLabel.setImage(image);
256     
257         rgbLabel.setText("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
258
}
259 }
260
261
Popular Tags