1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.events.DisposeEvent; 16 import org.eclipse.swt.events.DisposeListener; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.layout.GridData; 21 import org.eclipse.swt.layout.GridLayout; 22 import org.eclipse.swt.widgets.Button; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Group; 26 27 31 public class RadioGroupFieldEditor extends FieldEditor { 32 33 36 private String [][] labelsAndValues; 37 38 41 private int numColumns; 42 43 46 private int indent = HORIZONTAL_GAP; 47 48 51 private String value; 52 53 57 private Composite radioBox; 58 59 63 private Button[] radioButtons; 64 65 68 private boolean useGroup; 69 70 73 protected RadioGroupFieldEditor() { 74 } 75 76 101 public RadioGroupFieldEditor(String name, String labelText, int numColumns, 102 String [][] labelAndValues, Composite parent) { 103 this(name, labelText, numColumns, labelAndValues, parent, false); 104 } 105 106 130 public RadioGroupFieldEditor(String name, String labelText, int numColumns, 131 String [][] labelAndValues, Composite parent, boolean useGroup) { 132 init(name, labelText); 133 Assert.isTrue(checkArray(labelAndValues)); 134 this.labelsAndValues = labelAndValues; 135 this.numColumns = numColumns; 136 this.useGroup = useGroup; 137 createControl(parent); 138 } 139 140 143 protected void adjustForNumColumns(int numColumns) { 144 Control control = getLabelControl(); 145 if (control != null) { 146 ((GridData) control.getLayoutData()).horizontalSpan = numColumns; 147 } 148 ((GridData) radioBox.getLayoutData()).horizontalSpan = numColumns; 149 } 150 151 158 private boolean checkArray(String [][] table) { 159 if (table == null) { 160 return false; 161 } 162 for (int i = 0; i < table.length; i++) { 163 String [] array = table[i]; 164 if (array == null || array.length != 2) { 165 return false; 166 } 167 } 168 return true; 169 } 170 171 174 protected void doFillIntoGrid(Composite parent, int numColumns) { 175 if (useGroup) { 176 Control control = getRadioBoxControl(parent); 177 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 178 control.setLayoutData(gd); 179 } else { 180 Control control = getLabelControl(parent); 181 GridData gd = new GridData(); 182 gd.horizontalSpan = numColumns; 183 control.setLayoutData(gd); 184 control = getRadioBoxControl(parent); 185 gd = new GridData(); 186 gd.horizontalSpan = numColumns; 187 gd.horizontalIndent = indent; 188 control.setLayoutData(gd); 189 } 190 191 } 192 193 196 protected void doLoad() { 197 updateValue(getPreferenceStore().getString(getPreferenceName())); 198 } 199 200 203 protected void doLoadDefault() { 204 updateValue(getPreferenceStore().getDefaultString(getPreferenceName())); 205 } 206 207 210 protected void doStore() { 211 if (value == null) { 212 getPreferenceStore().setToDefault(getPreferenceName()); 213 return; 214 } 215 216 getPreferenceStore().setValue(getPreferenceName(), value); 217 } 218 219 222 public int getNumberOfControls() { 223 return 1; 224 } 225 226 231 public Composite getRadioBoxControl(Composite parent) { 232 if (radioBox == null) { 233 234 Font font = parent.getFont(); 235 236 if (useGroup) { 237 Group group = new Group(parent, SWT.NONE); 238 group.setFont(font); 239 String text = getLabelText(); 240 if (text != null) { 241 group.setText(text); 242 } 243 radioBox = group; 244 GridLayout layout = new GridLayout(); 245 layout.horizontalSpacing = HORIZONTAL_GAP; 246 layout.numColumns = numColumns; 247 radioBox.setLayout(layout); 248 } else { 249 radioBox = new Composite(parent, SWT.NONE); 250 GridLayout layout = new GridLayout(); 251 layout.marginWidth = 0; 252 layout.marginHeight = 0; 253 layout.horizontalSpacing = HORIZONTAL_GAP; 254 layout.numColumns = numColumns; 255 radioBox.setLayout(layout); 256 radioBox.setFont(font); 257 } 258 259 radioButtons = new Button[labelsAndValues.length]; 260 for (int i = 0; i < labelsAndValues.length; i++) { 261 Button radio = new Button(radioBox, SWT.RADIO | SWT.LEFT); 262 radioButtons[i] = radio; 263 String [] labelAndValue = labelsAndValues[i]; 264 radio.setText(labelAndValue[0]); 265 radio.setData(labelAndValue[1]); 266 radio.setFont(font); 267 radio.addSelectionListener(new SelectionAdapter() { 268 public void widgetSelected(SelectionEvent event) { 269 String oldValue = value; 270 value = (String ) event.widget.getData(); 271 setPresentsDefaultValue(false); 272 fireValueChanged(VALUE, oldValue, value); 273 } 274 }); 275 } 276 radioBox.addDisposeListener(new DisposeListener() { 277 public void widgetDisposed(DisposeEvent event) { 278 radioBox = null; 279 radioButtons = null; 280 } 281 }); 282 } else { 283 checkParent(radioBox, parent); 284 } 285 return radioBox; 286 } 287 288 293 public void setIndent(int indent) { 294 if (indent < 0) { 295 this.indent = 0; 296 } else { 297 this.indent = indent; 298 } 299 } 300 301 306 private void updateValue(String selectedValue) { 307 this.value = selectedValue; 308 if (radioButtons == null) { 309 return; 310 } 311 312 if (this.value != null) { 313 boolean found = false; 314 for (int i = 0; i < radioButtons.length; i++) { 315 Button radio = radioButtons[i]; 316 boolean selection = false; 317 if (((String ) radio.getData()).equals(this.value)) { 318 selection = true; 319 found = true; 320 } 321 radio.setSelection(selection); 322 } 323 if (found) { 324 return; 325 } 326 } 327 328 if (radioButtons.length > 0) { 331 radioButtons[0].setSelection(true); 332 this.value = (String ) radioButtons[0].getData(); 333 } 334 return; 335 } 336 337 340 public void setEnabled(boolean enabled, Composite parent) { 341 if (!useGroup) { 342 super.setEnabled(enabled, parent); 343 } 344 for (int i = 0; i < radioButtons.length; i++) { 345 radioButtons[i].setEnabled(enabled); 346 } 347 348 } 349 } 350 | Popular Tags |