1 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 39 public class ColorCellEditor extends DialogCellEditor { 40 41 44 private static final int DEFAULT_EXTENT = 16; 45 46 49 private static final int GAP = 6; 50 51 54 private Composite composite; 55 56 59 private Label colorLabel; 60 61 64 private Label rgbLabel; 65 66 69 private Image image; 70 71 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 111 public ColorCellEditor(Composite parent) { 112 this(parent, SWT.NONE); 113 } 114 115 124 public ColorCellEditor(Composite parent, int style) { 125 super(parent, style); 126 doSetValue(new RGB(0, 0, 0)); 127 } 128 129 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 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 203 public void dispose() { 204 if (image != null) { 205 image.dispose(); 206 image = null; 207 } 208 super.dispose(); 209 } 210 211 214 protected Object openDialogBox(Control cellEditorWindow) { 215 ColorDialog dialog = new ColorDialog(cellEditorWindow.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 227 protected void updateContents(Object value) { 228 RGB rgb = (RGB) value; 229 if (rgb == null) { 231 rgb = new RGB(0, 0, 0); 232 } 233 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 + ")"); } 246 } 247 | Popular Tags |