1 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 39 private static final int DEFAULT_EXTENT= 16; 40 41 44 private static final int GAP = 6; 45 46 49 private Composite composite; 50 51 54 private Label colorLabel; 55 56 59 private Label rgbLabel; 60 61 64 private Image image; 65 66 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 106 public AWTColorCellEditor(Composite parent) { 107 this(parent, SWT.NONE); 108 } 109 110 119 public AWTColorCellEditor(Composite parent, int style) { 120 super(parent, style); 121 } 123 124 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 178 protected Control createContents(Composite cell) 179 { 180 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 196 public void dispose() { 197 if (image != null) { 198 image.dispose(); 199 image = null; 200 } 201 super.dispose(); 202 } 203 204 207 protected Object openDialogBox(Control cellEditorWindow) { 208 ColorDialog dialog = new ColorDialog(cellEditorWindow.getShell()); 209 Object value = getValue(); 210 if (value != null) { 211 if (value instanceof String ) { 212 value = new java.awt.Color (0,0,0); 213 } 214 if (value instanceof java.awt.Color ) { 215 value = J2DUtil.toRGB((java.awt.Color )value); 216 } 217 dialog.setRGB((RGB) value); 218 } 219 value = dialog.open(); 220 RGB rgb = dialog.getRGB(); 221 java.awt.Color awtColor = new java.awt.Color (rgb.red, rgb.green, rgb.blue); 222 return awtColor; 223 } 224 225 228 protected void updateContents(Object value) 229 { 230 if (value != null) { 231 System.out.println("value = instanceof "+value.getClass()); 232 } 233 234 if (value instanceof String ) { 235 value = new java.awt.Color (0,0,0); 236 } 237 238 if (value == null) { 240 value = new java.awt.Color (0,0,0); 241 } 242 243 RGB rgb = J2DUtil.toRGB((java.awt.Color )value); 244 if (rgb == null) { 246 rgb = new RGB(0, 0, 0); 247 } 248 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 + ")"); } 259 } 260 261 | Popular Tags |