Debian Codegraph @

src/dr/app/gui/components/RealNumberField.java

raw symbols SCIP
1 /*
2 * RealNumberField.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.components;
27
28 import javax.swing.*;
29 import javax.swing.event.*;
30 import javax.swing.text.AttributeSet;
31 import javax.swing.text.BadLocationException;
32 import javax.swing.text.Document;
33 import javax.swing.text.PlainDocument;
34 import java.awt.*;
35 import java.awt.event.FocusEvent;
36 import java.awt.event.FocusListener;
37
38 public class RealNumberField extends JTextField implements FocusListener, DocumentListener {
39
40 public static String NaN = "NaN";
41 public static String POSITIVE_INFINITY = "+INF";
42 public static String NEGATIVE_INFINITY = "-INF";
43 public static String MAX_VALUE = "MAX";
44 public static String MIN_VALUE = "MIN";
45
46 protected static char MINUS = '-';
47 protected static char PERIOD = '.';
48 protected EventListenerList changeListeners = new EventListenerList();
49 protected double min;
50 protected double max;
51 protected final boolean includeMin;
52 protected final boolean includeMax;
53 protected boolean range_check = false;
54 protected boolean range_checked = false;
55 protected String label; // make sensible error message
56
57 private boolean isValueValid = true;
58
59 protected boolean allowEmpty = false;
60
61 public RealNumberField() { // no FocusListener
62 super();
63 setLabel("Value");
64 includeMin = true;
65 includeMax = true;
66 }
67
68 public RealNumberField(double min, double max) {
69 this(min, max, "Value");
70 this.addFocusListener(this);
71 }
72
73 public RealNumberField(double min, double max, String label) { // no FocusListener
74 this(min, true, max, true, label);
75 }
76
77 public RealNumberField(double min, boolean includeMin, double max, boolean includeMax, String label) { // no FocusListener
78 super();
79 this.min = min;
80 this.max = max;
81 this.includeMin = includeMin;
82 this.includeMax = includeMax;
83 setLabel(label);
84 range_check = true;
85 }
86
87 public void setAllowEmpty(boolean allowEmpty) {
88 this.allowEmpty = allowEmpty;
89 }
90
91 public void focusGained(FocusEvent evt) {
92 }
93
94 public void focusLost(FocusEvent evt) {
95 validateField();
96 }
97
98 public void validateField() {
99 if (range_check && !range_checked) {
100 range_checked = true;
101 isValueValid = isValueValid();
102 if (!isValueValid) {
103 displayErrorMessage();
104 // regain focus for this component
105 this.requestFocus();
106 }
107 }
108
109 }
110
111 public boolean isValueValid() {
112 if (getText().trim().equals("") && allowEmpty) {
113 return true;
114 }
115 if (range_check) {
116 try {
117 double value = getValue();
118 if (value < min || value > max) {
119 return false;
120 }
121 if (!includeMin && value == min) {
122 return false;
123 }
124 if (!includeMax && value == max) {
125 return false;
126 }
127 } catch (NumberFormatException e) {
128 return false;
129 }
130 }
131 return true;
132 }
133
134 public void setText(Double value) {
135 if (value == null && allowEmpty) {
136 setText("");
137 return;
138 }
139 if (value.isNaN()) {
140 setText(NaN);
141 } else if (value == Double.POSITIVE_INFINITY) {
142 setText(POSITIVE_INFINITY);
143 } else if (value == Double.NEGATIVE_INFINITY) {
144 setText(NEGATIVE_INFINITY);
145 } else if (value == Double.MAX_VALUE) {
146 setText(MAX_VALUE);
147 } else if (value == Double.MIN_VALUE) {
148 setText(MIN_VALUE);
149 } else {
150 setText(Double.toString(value));
151 }
152 }
153
154 public void setText(Integer obj) {
155 setText(obj.toString()); // where used?
156 }
157
158 public void setText(Long obj) {
159 setText(obj.toString()); // where used?
160 }
161
162 public String getErrorMessage() {
163 String message = "";
164 if (min == Double.MIN_VALUE) {
165 message = " greater than 0";
166 } else if (!Double.isInfinite(min) && min != -Double.MAX_VALUE) {
167 message = " greater than " + min;
168 }
169 if (max == -Double.MIN_VALUE) {
170 message = " less than 0";
171 } else if (!Double.isInfinite(max) && max != Double.MAX_VALUE) {
172 if (message.length() > 0) {
173 message += " and";
174 }
175 message = " less than " + max;
176 }
177
178 return label + " must be" + message;
179 }
180
181 private void displayErrorMessage() {
182 JOptionPane.showMessageDialog(null,
183 getErrorMessage(), "Invalid value", JOptionPane.ERROR_MESSAGE);
184 }
185
186 public void setRange(double min, double max) {
187 this.min = min;
188 this.max = max;
189 range_check = true;
190 }
191
192 public void setValue(double value) {
193 if (range_check) {
194 if (value < min || value > max) {
195 displayErrorMessage();
196 return;
197 }
198 if (!includeMin && value == min) {
199 displayErrorMessage();
200 return;
201 }
202 if (!includeMax && value == max) {
203 displayErrorMessage();
204 return;
205 }
206 }
207 setText(value);
208 }
209
210 public void setLabel(String label) {
211 this.label = label;
212 }
213
214 public Double getValue() {
215 try {
216 if (allowEmpty && getText().trim().equals("")) {
217 return null;
218 } else if (getText().equals(POSITIVE_INFINITY)) {
219 return Double.POSITIVE_INFINITY;
220 } else if (getText().equals(NEGATIVE_INFINITY)) {
221 return Double.NEGATIVE_INFINITY;
222 } else if (getText().equals(MAX_VALUE)) {
223 return Double.MAX_VALUE;
224 } else if (getText().equals(MIN_VALUE)) {
225 return Double.MIN_VALUE;
226 } else if (getText().equals(NaN)) {
227 return Double.NaN;
228 } else {
229 // System.out.println("=" + getText() + "=");
230 return new Double(getText());
231 }
232 } catch (NumberFormatException e) {
233 JOptionPane.showMessageDialog(null, "Unable to parse number correctly",
234 "Number Format Exception",
235 JOptionPane.ERROR_MESSAGE);
236 isValueValid = false;
237 return null;
238 }
239 }
240
241 protected Document createDefaultModel() {
242 Document doc = new RealNumberField.RealNumberFieldDocument();
243 doc.addDocumentListener(this);
244 return doc;
245 }
246
247 public void insertUpdate(DocumentEvent e) {
248 fireChanged();
249 }
250
251 public void removeUpdate(DocumentEvent e) {
252 fireChanged();
253 }
254
255 public void changedUpdate(DocumentEvent e) {
256 fireChanged();
257 }
258
259 static char[] numberSet = {
260 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
261 };
262
263 class RealNumberFieldDocument extends PlainDocument {
264 public void insertString(int offs, String str, AttributeSet a)
265 throws BadLocationException {
266
267 if (str == "" || str == null) return;
268 if (str.equals("+INF") || str.equals("-INF") || str.equals("NaN")
269 || str.equals("MAX_VALUE") || str.equals("MIN_VALUE")) {
270 super.insertString(offs, str, a);
271 return;
272 }
273
274 str = str.trim();
275
276 int length = getLength();
277 String buf = getText(0, offs) + str + getText(offs, length - offs);
278 buf = buf.trim().toUpperCase();
279 char[] array = buf.toCharArray();
280
281 if (array.length > 0) {
282 if (array[0] != MINUS && !member(array[0], numberSet) &&
283 array[0] != PERIOD) {
284 Toolkit.getDefaultToolkit().beep();
285 return;
286 }
287 }
288
289 boolean period_found = (array.length > 0 && array[0] == PERIOD);
290 boolean exponent_found = false;
291 int exponent_index = -1;
292 boolean exponent_sign_found = false;
293
294 for (int i = 1; i < array.length; i++) {
295 if (!member(array[i], numberSet)) {
296 if (!period_found && array[i] == PERIOD) {
297 period_found = true;
298 } else if (!exponent_found && array[i] == 'E') {
299 exponent_found = true;
300 exponent_index = i;
301 } else if (exponent_found && i == (exponent_index + 1) && !exponent_sign_found && array[i] == '-') {
302 exponent_sign_found = true;
303 } else {
304 Toolkit.getDefaultToolkit().beep();
305 return;
306 }
307 }
308 }
309 super.insertString(offs, str, a);
310 }
311 }
312
313 static boolean member(char item, char[] array) {
314 for (char anArray : array) {
315 if (anArray == item) {
316 return true;
317 }
318 }
319 return false;
320 }
321 //------------------------------------------------------------------------
322 // Event Methods
323 //------------------------------------------------------------------------
324
325 public void addChangeListener(ChangeListener x) {
326 changeListeners.add(ChangeListener.class, x);
327 }
328
329 public void removeChangeListener(ChangeListener x) {
330 changeListeners.remove(ChangeListener.class, x);
331 }
332
333 protected void fireChanged() {
334 range_checked = false;
335 isValueValid = true;
336
337 ChangeEvent c = new ChangeEvent(this);
338 Object[] listeners = changeListeners.getListenerList();
339 for (int i = listeners.length - 2; i >= 0; i -= 2) {
340 if (listeners[i] == ChangeListener.class) {
341 ChangeListener cl = (ChangeListener) listeners[i + 1];
342 cl.stateChanged(c);
343 }
344 }
345 }
346 }