Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * WholeNumberField.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
39 public class WholeNumberField extends JTextField
40 implements FocusListener, DocumentListener {
41
42 protected static char MINUS_CHAR = '-';
43 protected EventListenerList changeListeners = new EventListenerList();
44 protected long min;
45 protected long max;
46 protected boolean range_check = false;
47 protected boolean range_checked = false;
48
49 public WholeNumberField() {
50 this(Integer.MIN_VALUE, Integer.MAX_VALUE);
51 }
52
53 public WholeNumberField(int min, int max) {
54 super();
55 this.min = min;
56 this.max = max;
57 range_check = true;
58 this.addFocusListener(this);
59 }
60
61 public WholeNumberField(long min, long max) {
62 super();
63 this.min = min;
64 this.max = max;
65 range_check = true;
66 this.addFocusListener(this);
67 }
68
69 public void focusGained(FocusEvent evt) {
70 }
71
72 public void focusLost(FocusEvent evt) {
73 if (range_check && !range_checked) {
74 range_checked = true;
75 try {
76 long value = Long.valueOf(getText());
77 if (value < min || value > max) {
78 errorMsg();
79 }
80 } catch (NumberFormatException e) {
81 errorMsg();
82 }
83 }
84 }
85
86 public void setText(Integer obj) {
87 setText(obj.toString());
88 }
89
90 protected void errorMsg() {
91 JOptionPane.showMessageDialog(this,
92 "Illegal entry\nValue must be between " + min + " and " +
93 max + " inclusive", "Error", JOptionPane.ERROR_MESSAGE);
94 }
95
96 public void setValue(int value) {
97 if (range_check) {
98 if (value < min || value > max) {
99 errorMsg();
100 return;
101 }
102 }
103 setText(Integer.toString(value));
104 }
105
106 public void setValue(long value) {
107 if (range_check) {
108 if (value < min || value > max) {
109 errorMsg();
110 return;
111 }
112 }
113 setText(Long.toString(value));
114 }
115
116 public Integer getValue() {
117 try {
118 return new Integer(getText());
119 } catch (NumberFormatException e) {
120 return null;
121 }
122 }
123
124 public Long getLongValue() {
125 try {
126 return new Long(getText());
127 } catch (NumberFormatException e) {
128 return null;
129 }
130 }
131
132 public Integer getValue(int default_value) {
133 Integer value = getValue();
134 if (value == null)
135 return default_value;
136 else
137 return value;
138 }
139
140 public Long getValue(long default_value) {
141 Long value = getLongValue();
142 if (value == null)
143 return default_value;
144 else
145 return value;
146 }
147
148 protected Document createDefaultModel() {
149 Document doc = new WholeNumberFieldDocument();
150 doc.addDocumentListener(this);
151 return doc;
152 }
153
154 public void insertUpdate(DocumentEvent e) {
155 range_checked = false;
156 fireChanged();
157 }
158
159 public void removeUpdate(DocumentEvent e) {
160 range_checked = false;
161 fireChanged();
162 }
163
164 public void changedUpdate(DocumentEvent e) {
165 range_checked = false;
166 fireChanged();
167 }
168
169 static char[] numberSet = {
170 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
171 };
172
173 class WholeNumberFieldDocument extends PlainDocument {
174 public void insertString(int offs, String str, AttributeSet a)
175 throws BadLocationException {
176
177 if (str == null) return;
178 str = str.trim();
179
180 String buf = getText(0, offs) + str;
181 char[] array = buf.toCharArray();
182
183 if (array.length > 0) {
184 if (array[0] != MINUS_CHAR && !member(array[0], numberSet)) {
185 Toolkit.getDefaultToolkit().beep();
186 return;
187 }
188 }
189
190 for (int i = 1; i < array.length; i++) {
191 if (!member(array[i], numberSet)) {
192 Toolkit.getDefaultToolkit().beep();
193 return;
194 }
195 }
196 super.insertString(offs, str, a);
197 }
198 }
199
200 static boolean member(char item, char[] array) {
201 for (int i = 0; i < array.length; i++)
202 if (array[i] == item) return true;
203 return false;
204 }
205 //------------------------------------------------------------------------
206 // Event Methods
207 //------------------------------------------------------------------------
208
209 public void addChangeListener(ChangeListener x) {
210 changeListeners.add(ChangeListener.class, x);
211 }
212
213 public void removeChangeListener(ChangeListener x) {
214 changeListeners.remove(ChangeListener.class, x);
215 }
216
217 protected void fireChanged() {
218 ChangeEvent c = new ChangeEvent(this);
219 Object[] listeners = changeListeners.getListenerList();
220 for (int i = listeners.length - 2; i >= 0; i -= 2) {
221 if (listeners[i] == ChangeListener.class) {
222 ChangeListener cl = (ChangeListener) listeners[i + 1];
223 cl.stateChanged(c);
224 }
225 }
226 }
227 }