1 /***************************************************************************
2 * DMC, Dynamical Model Cruncher, simulates and analyzes low-dimensional
4 * Copyright (C) 2002 Alfredo Medio and Giampaolo Gallo
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * For a copy of the GNU General Public, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * Contact: Alfredo Medio Medio@unive.it
22 * adapted by Daniele Pizzoni <auouo@tin.it>
24 ***************************************************************************/
25 package org
.tsho
.dmc2
.ui
.components
;
26 import java
.awt
.Color
;
27 import java
.io
.Serializable
;
29 import javax
.swing
.JTextField
;
30 import java
.awt
.event
.*;
31 import javax
.swing
.event
.DocumentEvent
;
32 import javax
.swing
.event
.DocumentListener
;
33 import javax
.swing
.text
.BadLocationException
;
34 import javax
.swing
.text
.Document
;
36 import org
.jfree
.data
.Range
;
37 import org
.tsho
.dmc2
.ui
.InvalidData
;
41 * @author Giampaolo Gallo
42 * @author Daniele Pizzoni <auouo@tin.it>
44 public final class GetFloat
extends JTextField
implements Serializable
{
45 protected double value
;
47 protected boolean valid
;
48 protected boolean outOfRange
;
49 protected boolean ignoreValid
;
51 protected Range validRange
;
53 protected String name
= "field";
55 public static final String INVALID_FIELD_MESSAGE
= "empty or not valid";
56 public static final String INVALID_RANGE_FIELD_MESSAGE
= "out of range";
64 public GetFloat(int len
) {
69 public GetFloat(String name
, int len
, Range range
) {
77 public GetFloat(String name
, int len
) {
84 public GetFloat(int len
, double initialValue
) {
88 this.getDocument().insertString(
90 new String(Double
.toString(initialValue
)),
93 catch (BadLocationException e
) {}
101 this.getDocument().addDocumentListener(new MyDocumentListener());
103 addFocusListener(new FocusAdapter() {
104 public void focusGained(FocusEvent evt
) {
108 //super.setBorder(BorderFactory.createLineBorder(null));
111 public void syncText() {
113 setText(Double
.toString(value
));
118 public void syncValue() {
119 String s
= getText();
121 value
= Double
.valueOf(s
).doubleValue();
124 catch (NumberFormatException e
) {
130 public void setValue(double value
) {
132 setText(Double
.toString(value
));
135 public double getValue() throws InvalidData
{
136 if (!valid
&& !ignoreValid
) {
138 String message
= name
140 + INVALID_RANGE_FIELD_MESSAGE
142 + validRange
.getLowerBound()
144 + validRange
.getUpperBound()
146 throw new InvalidData(message
);
149 InvalidData e
=new InvalidData(name
+": "+INVALID_FIELD_MESSAGE
);
157 public boolean isValid() {
161 private class MyDocumentListener
implements DocumentListener
{
162 public void insertUpdate(DocumentEvent e
) {
165 public void removeUpdate(DocumentEvent e
) {
168 public void changedUpdate(DocumentEvent e
) {}
170 private void update(DocumentEvent e
) {
171 Document d
= (Document
)e
.getDocument();
174 s
= d
.getText(0, d
.getLength());
176 catch (BadLocationException e1
) {}
178 if (s
!= null && s
.length() > 0) {
180 value
= Double
.valueOf(s
).doubleValue();
183 catch (NumberFormatException e2
) {
190 && validRange
!= null && !validRange
.contains(value
)) {
197 setForeground(valid ? Color
.black
: Color
.red
);
199 if (s
.length() == 0) {
208 public boolean isIgnoreValid() {
215 public void setIgnoreValid(boolean b
) {