1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.resource.JFaceResources; 14 import org.eclipse.jface.util.IPropertyChangeListener; 15 import org.eclipse.jface.util.PropertyChangeEvent; 16 import org.eclipse.swt.graphics.Font; 17 import org.eclipse.swt.graphics.GC; 18 import org.eclipse.swt.graphics.Point; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 24 27 public class ColorFieldEditor extends FieldEditor { 28 29 32 private ColorSelector colorSelector; 33 34 37 protected ColorFieldEditor() { 38 } 40 41 51 public ColorFieldEditor(String name, String labelText, Composite parent) { 52 super(name, labelText, parent); 53 } 54 55 58 protected void adjustForNumColumns(int numColumns) { 59 ((GridData) colorSelector.getButton().getLayoutData()).horizontalSpan = numColumns - 1; 60 } 61 62 73 protected Point computeImageSize(Control window) { 74 GC gc = new GC(window); 78 Font f = JFaceResources.getFontRegistry().get( 79 JFaceResources.DEFAULT_FONT); 80 gc.setFont(f); 81 int height = gc.getFontMetrics().getHeight(); 82 gc.dispose(); 83 Point p = new Point(height * 3 - 6, height); 84 return p; 85 } 86 87 88 91 protected void doFillIntoGrid(Composite parent, int numColumns) { 92 Control control = getLabelControl(parent); 93 GridData gd = new GridData(); 94 gd.horizontalSpan = numColumns - 1; 95 control.setLayoutData(gd); 96 97 Button colorButton = getChangeControl(parent); 98 colorButton.setLayoutData(new GridData()); 99 100 } 101 102 103 106 protected void doLoad() { 107 if (colorSelector == null) { 108 return; 109 } 110 colorSelector.setColorValue(PreferenceConverter.getColor( 111 getPreferenceStore(), getPreferenceName())); 112 } 113 114 117 protected void doLoadDefault() { 118 if (colorSelector == null) { 119 return; 120 } 121 colorSelector.setColorValue(PreferenceConverter.getDefaultColor( 122 getPreferenceStore(), getPreferenceName())); 123 } 124 125 128 protected void doStore() { 129 PreferenceConverter.setValue(getPreferenceStore(), getPreferenceName(), 130 colorSelector.getColorValue()); 131 } 132 133 138 public ColorSelector getColorSelector() { 139 return colorSelector; 140 } 141 142 149 protected Button getChangeControl(Composite parent) { 150 if (colorSelector == null) { 151 colorSelector = new ColorSelector(parent); 152 colorSelector.addListener(new IPropertyChangeListener() { 153 public void propertyChange(PropertyChangeEvent event) { 155 ColorFieldEditor.this.fireValueChanged(event.getProperty(), 156 event.getOldValue(), event.getNewValue()); 157 setPresentsDefaultValue(false); 158 } 159 }); 160 161 } else { 162 checkParent(colorSelector.getButton(), parent); 163 } 164 return colorSelector.getButton(); 165 } 166 167 170 public int getNumberOfControls() { 171 return 2; 172 } 173 174 180 public void setEnabled(boolean enabled, Composite parent) { 181 super.setEnabled(enabled, parent); 182 getChangeControl(parent).setEnabled(enabled); 183 } 184 185 } 186 | Popular Tags |