1 package net.mograsim.plugin.util;
3 import org.eclipse.jface.preference.IntegerFieldEditor;
4 import org.eclipse.jface.preference.StringFieldEditor;
5 import org.eclipse.swt.widgets.Composite;
6 import org.eclipse.swt.widgets.Text;
9 * A field editor for an double type preference.<br>
10 * Adapted from {@link IntegerFieldEditor}.
12 public class DoubleFieldEditor extends StringFieldEditor
14 private double minValidValue = 0;
16 private double maxValidValue = Double.MAX_VALUE;
18 private static final int DEFAULT_TEXT_LIMIT = 10;
21 * Creates a new double field editor
23 protected DoubleFieldEditor()
28 * Creates an double field editor.
30 * @param name the name of the preference this field editor works on
31 * @param labelText the label text of the field editor
32 * @param parent the parent of the field editor's control
34 public DoubleFieldEditor(String name, String labelText, Composite parent)
36 this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
40 * Creates an double field editor.
42 * @param name the name of the preference this field editor works on
43 * @param labelText the label text of the field editor
44 * @param parent the parent of the field editor's control
45 * @param textLimit the maximum number of characters in the text.
47 public DoubleFieldEditor(String name, String labelText, Composite parent, int textLimit)
49 init(name, labelText);
50 setTextLimit(textLimit);
51 setEmptyStringAllowed(false);
52 setErrorMessage("Value must be a double");
53 createControl(parent);
57 * Sets the range of valid values for this field.
59 * @param min the minimum allowed value (inclusive)
60 * @param max the maximum allowed value (inclusive)
62 public void setValidRange(int min, int max)
66 setErrorMessage("Value must be a double between " + min + " and " + max);
70 protected boolean checkState()
72 Text text = getTextControl();
79 String numberString = text.getText();
82 double number = Double.parseDouble(numberString);
83 if (number >= minValidValue && number <= maxValidValue)
93 catch (@SuppressWarnings("unused") NumberFormatException e1)
102 protected void doLoad()
104 Text text = getTextControl();
107 double value = getPreferenceStore().getDouble(getPreferenceName());
108 String valueAsString = String.valueOf(value);
109 text.setText(valueAsString);
110 oldValue = valueAsString;
116 protected void doLoadDefault()
118 Text text = getTextControl();
121 double value = getPreferenceStore().getDefaultDouble(getPreferenceName());
122 text.setText(String.valueOf(value));
128 protected void doStore()
130 Text text = getTextControl();
133 Double d = Double.valueOf(text.getText());
134 getPreferenceStore().setValue(getPreferenceName(), d.intValue());
139 * Returns this field editor's current value as a double.
142 * @exception NumberFormatException if the <code>String</code> does not contain a parsable double
144 public double getDoubleValue() throws NumberFormatException
146 return Double.valueOf(getStringValue()).doubleValue();