1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.DisposeEvent; 15 import org.eclipse.swt.events.DisposeListener; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Scale; 22 23 29 public class ScaleFieldEditor extends FieldEditor { 30 31 34 private int incrementValue; 35 36 39 private int maxValue; 40 41 44 private int minValue; 45 46 49 private int oldValue; 50 51 54 private int pageIncrementValue; 55 56 59 protected Scale scale; 60 61 71 public ScaleFieldEditor(String name, String labelText, Composite parent) { 72 super(name, labelText, parent); 73 setDefaultValues(); 74 } 75 76 94 public ScaleFieldEditor(String name, String labelText, Composite parent, 95 int min, int max, int increment, int pageIncrement) { 96 super(name, labelText, parent); 97 setValues(min, max, increment, pageIncrement); 98 } 99 100 105 protected void adjustForNumColumns(int numColumns) { 106 ((GridData) scale.getLayoutData()).horizontalSpan = numColumns - 1; 107 } 108 109 115 protected void doFillIntoGrid(Composite parent, int numColumns) { 116 Control control = getLabelControl(parent); 117 GridData gd = new GridData(); 118 control.setLayoutData(gd); 119 120 scale = getScaleControl(parent); 121 gd = new GridData(GridData.FILL_HORIZONTAL); 122 gd.verticalAlignment = GridData.FILL; 123 gd.horizontalSpan = numColumns - 1; 124 gd.grabExcessHorizontalSpace = true; 125 scale.setLayoutData(gd); 126 updateScale(); 127 } 128 129 134 protected void doLoad() { 135 if (scale != null) { 136 int value = getPreferenceStore().getInt(getPreferenceName()); 137 scale.setSelection(value); 138 oldValue = value; 139 } 140 } 141 142 147 protected void doLoadDefault() { 148 if (scale != null) { 149 int value = getPreferenceStore().getDefaultInt(getPreferenceName()); 150 scale.setSelection(value); 151 } 152 valueChanged(); 153 } 154 155 160 protected void doStore() { 161 getPreferenceStore() 162 .setValue(getPreferenceName(), scale.getSelection()); 163 } 164 165 171 public int getIncrement() { 172 return incrementValue; 173 } 174 175 181 public int getMaximum() { 182 return maxValue; 183 } 184 185 191 public int getMinimum() { 192 return minValue; 193 } 194 195 200 public int getNumberOfControls() { 201 return 2; 202 } 203 204 210 public int getPageIncrement() { 211 return pageIncrementValue; 212 } 213 214 220 public Scale getScaleControl() { 221 return scale; 222 } 223 224 232 private Scale getScaleControl(Composite parent) { 233 if (scale == null) { 234 scale = new Scale(parent, SWT.HORIZONTAL); 235 scale.setFont(parent.getFont()); 236 scale.addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent event) { 238 valueChanged(); 239 } 240 }); 241 scale.addDisposeListener(new DisposeListener() { 242 public void widgetDisposed(DisposeEvent event) { 243 scale = null; 244 } 245 }); 246 } else { 247 checkParent(scale, parent); 248 } 249 return scale; 250 } 251 252 261 private void setDefaultValues() { 262 setValues(0, 10, 1, 1); 263 } 264 265 270 public void setFocus() { 271 if (scale != null && !scale.isDisposed()) { 272 scale.setFocus(); 273 } 274 } 275 276 284 public void setIncrement(int increment) { 285 this.incrementValue = increment; 286 updateScale(); 287 } 288 289 297 public void setMaximum(int max) { 298 this.maxValue = max; 299 updateScale(); 300 } 301 302 310 public void setMinimum(int min) { 311 this.minValue = min; 312 updateScale(); 313 } 314 315 323 public void setPageIncrement(int pageIncrement) { 324 this.pageIncrementValue = pageIncrement; 325 updateScale(); 326 } 327 328 340 private void setValues(int min, int max, int increment, int pageIncrement) { 341 this.incrementValue = increment; 342 this.maxValue = max; 343 this.minValue = min; 344 this.pageIncrementValue = pageIncrement; 345 updateScale(); 346 } 347 348 351 private void updateScale() { 352 if (scale != null && !scale.isDisposed()) { 353 scale.setMinimum(getMinimum()); 354 scale.setMaximum(getMaximum()); 355 scale.setIncrement(getIncrement()); 356 scale.setPageIncrement(getPageIncrement()); 357 } 358 } 359 360 369 protected void valueChanged() { 370 setPresentsDefaultValue(false); 371 372 int newValue = scale.getSelection(); 373 if (newValue != oldValue) { 374 fireStateChanged(IS_VALID, false, true); 375 fireValueChanged(VALUE, new Integer (oldValue), 376 new Integer (newValue)); 377 oldValue = newValue; 378 } 379 } 380 } 381 | Popular Tags |