4
* Copyright (c) 2009 JAM Development Team
6
* This package is distributed under the Lesser Gnu Public Licence (LGPL)
10
package figtree.ui.components;
13
import javax.swing.event.*;
14
import javax.swing.text.AttributeSet;
15
import javax.swing.text.BadLocationException;
16
import javax.swing.text.Document;
17
import javax.swing.text.PlainDocument;
19
import java.awt.event.FocusEvent;
20
import java.awt.event.FocusListener;
23
public class RealNumberField extends JTextField
24
implements FocusListener, DocumentListener {
26
protected static char MINUS = '-';
27
protected static char PERIOD = '.';
28
protected EventListenerList changeListeners = new EventListenerList();
31
protected boolean range_check = false;
32
protected boolean range_checked = false;
34
public RealNumberField() {
38
public RealNumberField(double min, double max) {
43
this.addFocusListener(this);
46
public void focusGained(FocusEvent evt) {
49
public void focusLost(FocusEvent evt) {
50
if (range_check && !range_checked) {
53
double value = (Double.valueOf(getText())).doubleValue();
54
if (value < min || value > max) {
58
} catch (NumberFormatException e) {
65
public void setText(Double obj) {
66
setText(obj.toString());
69
public void setText(Integer obj) {
70
setText(obj.toString());
73
public void setText(Long obj) {
74
setText(obj.toString());
77
protected void errorMsg() {
78
JOptionPane.showMessageDialog(this,
79
"Illegal entry\nValue must be between " + min + " and " +
80
max + " inclusive", "Error", JOptionPane.ERROR_MESSAGE);
83
public void setRange(double min, double max) {
89
public void setValue(double value) {
91
if (value < min || value > max) {
96
setText(Double.toString(value));
99
public Double getValue() {
101
return new Double(getText());
102
} catch (NumberFormatException e) {
107
public Double getValue(double def) {
109
return new Double(getText());
110
} catch (NumberFormatException e) {
111
return new Double(def);
115
protected Document createDefaultModel() {
116
Document doc = new RealNumberFieldDocument();
117
doc.addDocumentListener(this);
121
public void insertUpdate(DocumentEvent e) {
122
range_checked = false;
126
public void removeUpdate(DocumentEvent e) {
127
range_checked = false;
131
public void changedUpdate(DocumentEvent e) {
132
range_checked = false;
136
static char[] numberSet = {
137
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
140
class RealNumberFieldDocument extends PlainDocument {
141
public void insertString(int offs, String str, AttributeSet a)
142
throws BadLocationException {
144
if (str == null) return;
147
int length = getLength();
148
String buf = getText(0, offs) + str + getText(offs, length - offs);
149
buf = buf.trim().toUpperCase();
150
char[] array = buf.toCharArray();
152
if (array.length > 0) {
153
if (array[0] != MINUS && !member(array[0], numberSet) &&
154
array[0] != PERIOD) {
155
Toolkit.getDefaultToolkit().beep();
160
boolean period_found = (array.length > 0 && array[0] == PERIOD);
161
boolean exponent_found = false;
162
int exponent_index = -1;
163
boolean exponent_sign_found = false;
165
for (int i = 1; i < array.length; i++) {
166
if (!member(array[i], numberSet)) {
167
if (!period_found && array[i] == PERIOD) {
169
} else if (!exponent_found && array[i] == 'E') {
170
exponent_found = true;
172
} else if (exponent_found && i == (exponent_index + 1) && !exponent_sign_found && array[i] == '-') {
173
exponent_sign_found = true;
175
Toolkit.getDefaultToolkit().beep();
180
super.insertString(offs, str, a);
184
static boolean member(char item, char[] array) {
185
for (int i = 0; i < array.length; i++)
186
if (array[i] == item) return true;
189
//------------------------------------------------------------------------
191
//------------------------------------------------------------------------
193
public void addChangeListener(ChangeListener x) {
194
changeListeners.add(ChangeListener.class, x);
197
public void removeChangeListener(ChangeListener x) {
198
changeListeners.remove(ChangeListener.class, x);
201
protected void fireChanged() {
202
ChangeEvent c = new ChangeEvent(this);
203
Object[] listeners = changeListeners.getListenerList();
204
for (int i = listeners.length - 2; i >= 0; i -= 2) {
205
if (listeners[i] == ChangeListener.class) {
206
ChangeListener cl = (ChangeListener) listeners[i + 1];