001: package net.refractions.udig.style.sld.editor.internal;
002:
003: /*******************************************************************************
004: * Copyright (c) 2000, 2004 IBM Corporation and others.
005: * All rights reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * IBM Corporation - initial API and implementation
012: *******************************************************************************/
013:
014: import org.eclipse.jface.viewers.CellEditor;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.graphics.RGB;
017: import org.eclipse.swt.widgets.ColorDialog;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Display;
021: import org.eclipse.swt.widgets.Shell;
022:
023: /**
024: * A cell editor that manages a color field.
025: * The cell editor's value is the color (an SWT <code>RBG</code>).
026: * <p>
027: * This class may be instantiated; it is not intended to be subclassed.
028: * </p>
029: */
030: public class ColourCellEditor extends CellEditor {
031:
032: Object value;
033:
034: /**
035: * Internal class for laying out this cell editor.
036: */
037: // private class ColorCellLayout extends Layout {
038: // public Point computeSize(Composite editor, int wHint, int hHint,
039: // boolean force) {
040: // if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
041: // return new Point(wHint, hHint);
042: // Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
043: // force);
044: // Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
045: // force);
046: // return new Point(colorSize.x + GAP + rgbSize.x, Math.max(
047: // colorSize.y, rgbSize.y));
048: // }
049: //
050: // public void layout(Composite editor, boolean force) {
051: // Rectangle bounds = editor.getClientArea();
052: // Point colorSize = colorLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
053: // force);
054: // Point rgbSize = rgbLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT,
055: // force);
056: // int ty = (bounds.height - rgbSize.y) / 2;
057: // if (ty < 0)
058: // ty = 0;
059: // colorLabel.setBounds(-1, 0, colorSize.x, colorSize.y);
060: // rgbLabel.setBounds(colorSize.x + GAP - 1, ty, bounds.width
061: // - colorSize.x - GAP, bounds.height);
062: // }
063: // }
064: //
065: /**
066: * Creates a new color cell editor parented under the given control.
067: * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
068: * validator.
069: *
070: * @param parent the parent control
071: */
072: public ColourCellEditor(Composite parent) {
073: this (parent, SWT.NONE);
074: }
075:
076: /**
077: * Creates a new color cell editor parented under the given control.
078: * The cell editor value is black (<code>RGB(0,0,0)</code>) initially, and has no
079: * validator.
080: *
081: * @param parent the parent control
082: * @param style the style bits
083: * @since 2.1
084: */
085: public ColourCellEditor(Composite parent, int style) {
086: super (parent, style);
087: doSetValue(new RGB(0, 0, 0));
088: }
089:
090: //
091: // /**
092: // * Creates and returns the color image data for the given control
093: // * and RGB value. The image's size is either the control's item extent
094: // * or the cell editor's default extent, which is 16 pixels square.
095: // *
096: // * @param w the control
097: // * @param color the color
098: // */
099: // private ImageData createColorImage(Control w, RGB color) {
100: //
101: // GC gc = new GC(w);
102: // FontMetrics fm = gc.getFontMetrics();
103: // int size = fm.getAscent();
104: // gc.dispose();
105: //
106: // int indent = 6;
107: // int extent = DEFAULT_EXTENT;
108: // if (w instanceof Table)
109: // extent = ((Table) w).getItemHeight() - 1;
110: // else if (w instanceof Tree)
111: // extent = ((Tree) w).getItemHeight() - 1;
112: // else if (w instanceof TableTree)
113: // extent = ((TableTree) w).getItemHeight() - 1;
114: //
115: // if (size > extent)
116: // size = extent;
117: //
118: // int width = indent + size;
119: // int height = extent;
120: //
121: // int xoffset = indent;
122: // int yoffset = (height - size) / 2;
123: //
124: // RGB black = new RGB(0, 0, 0);
125: // PaletteData dataPalette = new PaletteData(new RGB[] { black, black,
126: // color });
127: // ImageData data = new ImageData(width, height, 4, dataPalette);
128: // data.transparentPixel = 0;
129: //
130: // int end = size - 1;
131: // for (int y = 0; y < size; y++) {
132: // for (int x = 0; x < size; x++) {
133: // if (x == 0 || y == 0 || x == end || y == end)
134: // data.setPixel(x + xoffset, y + yoffset, 1);
135: // else
136: // data.setPixel(x + xoffset, y + yoffset, 2);
137: // }
138: // }
139: //
140: // return data;
141: // }
142:
143: /* (non-Javadoc)
144: * Method declared on CellEditor.
145: */
146: public void dispose() {
147: super .dispose();
148: }
149:
150: /* (non-Javadoc)
151: * Method declared on DialogCellEditor.
152: */
153: protected Object openDialogBox(Shell shell) {
154: ColorDialog dialog = new ColorDialog(shell);
155: //Object value = getValue();
156: if (value != null)
157: dialog.setRGB((RGB) value);
158: value = dialog.open();
159: return dialog.getRGB();
160: }
161:
162: /* (non-Javadoc)
163: * Method declared on DialogCellEditor.
164: */
165: // protected void updateContents(Object value) {
166: // RGB rgb = (RGB) value;
167: // // XXX: We don't have a value the first time this method is called".
168: // if (rgb == null) {
169: // rgb = new RGB(0, 0, 0);
170: // }
171: // // XXX: Workaround for 1FMQ0P3: SWT:ALL - TableItem.setImage doesn't work if using the identical image."
172: // if (image != null)
173: // image.dispose();
174: //
175: // ImageData id = createColorImage(colorLabel.getParent().getParent(), rgb);
176: // ImageData mask = id.getTransparencyMask();
177: // image = new Image(colorLabel.getDisplay(), id, mask);
178: // colorLabel.setImage(image);
179: //
180: // rgbLabel
181: // .setText("(" + rgb.red + "," + rgb.green + "," + rgb.blue + ")");//$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
182: // }
183: @Override
184: protected Control createControl(Composite cell) {
185: //we won't actually create a control, but we'll spawn a dialog
186:
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: return null;
198: }
199:
200: @Override
201: protected Object doGetValue() {
202: value = openDialogBox(Display.getCurrent().getActiveShell());
203: return value;
204: }
205:
206: @Override
207: protected void doSetFocus() {
208: }
209:
210: @Override
211: protected void doSetValue(Object value) {
212: this.value = value;
213: }
214: }
|