MainPreferencePage: Added fields for line widths
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / util / DoubleFieldEditor.java
1 package net.mograsim.plugin.util;
2
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;
7
8 /**
9  * A field editor for an double type preference.<br>
10  * Adapted from {@link IntegerFieldEditor}.
11  */
12 public class DoubleFieldEditor extends StringFieldEditor
13 {
14         private double minValidValue = 0;
15
16         private double maxValidValue = Double.MAX_VALUE;
17
18         private static final int DEFAULT_TEXT_LIMIT = 10;
19
20         /**
21          * Creates a new double field editor
22          */
23         protected DoubleFieldEditor()
24         {
25         }
26
27         /**
28          * Creates an double field editor.
29          *
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
33          */
34         public DoubleFieldEditor(String name, String labelText, Composite parent)
35         {
36                 this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
37         }
38
39         /**
40          * Creates an double field editor.
41          *
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.
46          */
47         public DoubleFieldEditor(String name, String labelText, Composite parent, int textLimit)
48         {
49                 init(name, labelText);
50                 setTextLimit(textLimit);
51                 setEmptyStringAllowed(false);
52                 setErrorMessage("Value must be a double");
53                 createControl(parent);
54         }
55
56         /**
57          * Sets the range of valid values for this field.
58          *
59          * @param min the minimum allowed value (inclusive)
60          * @param max the maximum allowed value (inclusive)
61          */
62         public void setValidRange(int min, int max)
63         {
64                 minValidValue = min;
65                 maxValidValue = max;
66                 setErrorMessage("Value must be a double between " + min + " and " + max);
67         }
68
69         @Override
70         protected boolean checkState()
71         {
72                 Text text = getTextControl();
73
74                 if (text == null)
75                 {
76                         return false;
77                 }
78
79                 String numberString = text.getText();
80                 try
81                 {
82                         double number = Double.parseDouble(numberString);
83                         if (number >= minValidValue && number <= maxValidValue)
84                         {
85                                 clearErrorMessage();
86                                 return true;
87                         }
88
89                         showErrorMessage();
90                         return false;
91
92                 }
93                 catch (@SuppressWarnings("unused") NumberFormatException e1)
94                 {
95                         showErrorMessage();
96                 }
97
98                 return false;
99         }
100
101         @Override
102         protected void doLoad()
103         {
104                 Text text = getTextControl();
105                 if (text != null)
106                 {
107                         double value = getPreferenceStore().getDouble(getPreferenceName());
108                         String valueAsString = String.valueOf(value);
109                         text.setText(valueAsString);
110                         oldValue = valueAsString;
111                 }
112
113         }
114
115         @Override
116         protected void doLoadDefault()
117         {
118                 Text text = getTextControl();
119                 if (text != null)
120                 {
121                         double value = getPreferenceStore().getDefaultDouble(getPreferenceName());
122                         text.setText(String.valueOf(value));
123                 }
124                 valueChanged();
125         }
126
127         @Override
128         protected void doStore()
129         {
130                 Text text = getTextControl();
131                 if (text != null)
132                 {
133                         Double d = Double.valueOf(text.getText());
134                         getPreferenceStore().setValue(getPreferenceName(), d.intValue());
135                 }
136         }
137
138         /**
139          * Returns this field editor's current value as a double.
140          *
141          * @return the value
142          * @exception NumberFormatException if the <code>String</code> does not contain a parsable double
143          */
144         public double getDoubleValue() throws NumberFormatException
145         {
146                 return Double.valueOf(getStringValue()).doubleValue();
147         }
148 }