1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.core.commands.common.EventManager; 14 import org.eclipse.jface.resource.JFaceResources; 15 import org.eclipse.jface.util.IPropertyChangeListener; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.accessibility.AccessibleAdapter; 19 import org.eclipse.swt.accessibility.AccessibleEvent; 20 import org.eclipse.swt.events.DisposeEvent; 21 import org.eclipse.swt.events.DisposeListener; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.graphics.Color; 25 import org.eclipse.swt.graphics.Font; 26 import org.eclipse.swt.graphics.GC; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.graphics.RGB; 30 import org.eclipse.swt.widgets.Button; 31 import org.eclipse.swt.widgets.ColorDialog; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Display; 35 36 40 public class ColorSelector extends EventManager { 41 47 public static final String PROP_COLORCHANGE = "colorValue"; 49 private Button fButton; 50 51 private Color fColor; 52 53 private RGB fColorValue; 54 55 private Point fExtent; 56 57 private Image fImage; 58 59 66 public ColorSelector(Composite parent) { 67 fButton = new Button(parent, SWT.PUSH); 68 fExtent = computeImageSize(parent); 69 fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y); 70 GC gc = new GC(fImage); 71 gc.setBackground(fButton.getBackground()); 72 gc.fillRectangle(0, 0, fExtent.x, fExtent.y); 73 gc.dispose(); 74 fButton.setImage(fImage); 75 fButton.addSelectionListener(new SelectionAdapter() { 76 public void widgetSelected(SelectionEvent event) { 77 open(); 78 } 79 }); 80 fButton.addDisposeListener(new DisposeListener() { 81 public void widgetDisposed(DisposeEvent event) { 82 if (fImage != null) { 83 fImage.dispose(); 84 fImage = null; 85 } 86 if (fColor != null) { 87 fColor.dispose(); 88 fColor = null; 89 } 90 } 91 }); 92 fButton.getAccessible().addAccessibleListener(new AccessibleAdapter() { 93 98 public void getName(AccessibleEvent e) { 99 e.result = JFaceResources.getString("ColorSelector.Name"); } 101 }); 102 } 103 104 114 public void addListener(IPropertyChangeListener listener) { 115 addListenerObject(listener); 116 } 117 118 125 private Point computeImageSize(Control window) { 126 GC gc = new GC(window); 127 Font f = JFaceResources.getFontRegistry().get( 128 JFaceResources.DIALOG_FONT); 129 gc.setFont(f); 130 int height = gc.getFontMetrics().getHeight(); 131 gc.dispose(); 132 Point p = new Point(height * 3 - 6, height); 133 return p; 134 } 135 136 141 public Button getButton() { 142 return fButton; 143 } 144 145 150 public RGB getColorValue() { 151 return fColorValue; 152 } 153 154 162 public void removeListener(IPropertyChangeListener listener) { 163 removeListenerObject(listener); 164 } 165 166 172 public void setColorValue(RGB rgb) { 173 fColorValue = rgb; 174 updateColorImage(); 175 } 176 177 183 public void setEnabled(boolean state) { 184 getButton().setEnabled(state); 185 } 186 187 191 protected void updateColorImage() { 192 Display display = fButton.getDisplay(); 193 GC gc = new GC(fImage); 194 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); 195 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4); 196 if (fColor != null) { 197 fColor.dispose(); 198 } 199 fColor = new Color(display, fColorValue); 200 gc.setBackground(fColor); 201 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5); 202 gc.dispose(); 203 fButton.setImage(fImage); 204 } 205 206 212 public void open() { 213 ColorDialog colorDialog = new ColorDialog(fButton.getShell()); 214 colorDialog.setRGB(fColorValue); 215 RGB newColor = colorDialog.open(); 216 if (newColor != null) { 217 RGB oldValue = fColorValue; 218 fColorValue = newColor; 219 final Object [] finalListeners = getListeners(); 220 if (finalListeners.length > 0) { 221 PropertyChangeEvent pEvent = new PropertyChangeEvent( 222 this, PROP_COLORCHANGE, oldValue, newColor); 223 for (int i = 0; i < finalListeners.length; ++i) { 224 IPropertyChangeListener listener = (IPropertyChangeListener) finalListeners[i]; 225 listener.propertyChange(pEvent); 226 } 227 } 228 updateColorImage(); 229 } 230 } 231 } 232 | Popular Tags |