fixed traj. plot bug when manual bounds
[iDMC.git] / src / java / org / tsho / dmc2 / ui / components / GetFloat.java
blob749ba8e0aa7b0f27033bdc72b02de27ab1486e9b
1 /***************************************************************************
2 * DMC, Dynamical Model Cruncher, simulates and analyzes low-dimensional
3 * dynamical systems
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
18 * USA
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;
40 /**
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";
58 public GetFloat() {
59 super();
60 init();
64 public GetFloat(int len) {
65 super(len);
66 init();
69 public GetFloat(String name, int len, Range range) {
70 super(len);
72 this.name = name;
73 validRange = range;
74 init();
77 public GetFloat(String name, int len) {
78 super(len);
80 this.name = name;
81 init();
84 public GetFloat(int len, double initialValue) {
85 super(len);
86 init();
87 try {
88 this.getDocument().insertString(
90 new String(Double.toString(initialValue)),
91 null);
93 catch (BadLocationException e) {}
96 private void init() {
97 valid = false;
98 ignoreValid = false;
99 outOfRange = false;
101 this.getDocument().addDocumentListener(new MyDocumentListener());
103 addFocusListener(new FocusAdapter() {
104 public void focusGained(FocusEvent evt) {
105 selectAll();
108 //super.setBorder(BorderFactory.createLineBorder(null));
111 public void syncText() {
112 if (valid)
113 setText(Double.toString(value));
114 else
115 setText("");
118 public void syncValue() {
119 String s = getText();
120 try {
121 value = Double.valueOf(s).doubleValue();
122 valid = true;
124 catch (NumberFormatException e) {
125 value = 0.0;
126 valid = false;
130 public void setValue(double value) {
131 this.value = value;
132 setText(Double.toString(value));
135 public double getValue() throws InvalidData {
136 if (!valid && !ignoreValid) {
137 if (outOfRange) {
138 String message = name
139 + ": "
140 + INVALID_RANGE_FIELD_MESSAGE
141 + " ("
142 + validRange.getLowerBound()
143 + " < "
144 + validRange.getUpperBound()
145 + ")";
146 throw new InvalidData(message);
148 else {
149 InvalidData e=new InvalidData(name+": "+INVALID_FIELD_MESSAGE);
150 throw e;
154 return value;
157 public boolean isValid() {
158 return valid;
161 private class MyDocumentListener implements DocumentListener {
162 public void insertUpdate(DocumentEvent e) {
163 update(e);
165 public void removeUpdate(DocumentEvent e) {
166 update(e);
168 public void changedUpdate(DocumentEvent e) {}
170 private void update(DocumentEvent e) {
171 Document d = (Document)e.getDocument();
172 String s = null;
173 try {
174 s = d.getText(0, d.getLength());
176 catch (BadLocationException e1) {}
178 if (s != null && s.length() > 0) {
179 try {
180 value = Double.valueOf(s).doubleValue();
181 valid = true;
183 catch (NumberFormatException e2) {
184 value = 0.0;
185 valid = false;
186 outOfRange = false;
189 if (valid != false
190 && validRange != null && !validRange.contains(value)) {
192 value = 0.0;
193 valid = false;
194 outOfRange = true;
197 setForeground(valid ? Color.black : Color.red);
199 if (s.length() == 0) {
200 value = 0.0;
201 valid = false;
206 * @return
208 public boolean isIgnoreValid() {
209 return ignoreValid;
213 * @param b
215 public void setIgnoreValid(boolean b) {
216 ignoreValid = b;