001 package de.mospace.swing.text;
002
003 /* de.mospace.swing library
004 * Copyright (C) 2005 Moritz Ringler
005 * $Id: NumericInputVerifier.java 12 2007-07-27 09:48:53Z ringler $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 import java.lang.reflect.Method;
023 import java.lang.reflect.Constructor;
024 import javax.swing.InputVerifier;
025 import javax.swing.JComponent;
026 import javax.swing.text.JTextComponent;
027
028 /** An input verifier that checks if a text component's text can be interpreted
029 * as a particular sub-class of number and if it falls into the allowed
030 * range of numbers.
031 * @deprecated use GeneralInputVerifier instead
032 *
033 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
034 * @author Moritz Ringler
035 */
036 public class NumericInputVerifier extends InputVerifier implements java.io.Serializable{
037 private Class myClass = Double.class;
038 private Method method;
039 private Constructor constructor;
040 private Comparable min = null;
041 private Comparable max = null;
042 private transient Exception ex = null;
043
044 /** Constructs a new NumericInputVerifier for the given
045 * sub-class of Number and the given limits.
046 * @param c the sub-class of number that input will be interpreted as
047 * @param min lower limit. null for none.
048 * @param max upper limit. null for none.
049 * @throws IllegalArgumentException if <code>c</code> does not implement
050 * Number, or if it does not have a static
051 * <code>valueOf(String)</code> method or a single argument string
052 * constructor.
053 * @throws ClassCastException if
054 * <code>min</code> and <code>max</code> are not <code>null</code>
055 * or instances of <code>c</code>.
056 */
057 public NumericInputVerifier(Class c, Comparable min, Comparable max){
058 setNumberClass(c);
059 setUpperLimit(max);
060 setLowerLimit(min);
061 }
062
063 /** Constructs a new NumericInputVerifier for the full range of numbers
064 * in the given sub-class of Number.
065 * @param c the sub-class of number that input will be
066 * interpreted as
067 * @throws IllegalArgumentException if <code>c</code> does not implement
068 * Number, or if it does not have a static
069 * <code>valueOf(String)</code> method or a constructor with
070 * a single String argument
071 */
072 public NumericInputVerifier(Class c){
073 setNumberClass(c);
074 }
075
076 /** Returns the class against which input will be checked.
077 * @see #setNumberClass
078 */
079 public Class getNumberClass(){
080 return myClass;
081 }
082
083 /** Sets the class against which input will be checked. This will
084 * automatically reset the allowed range to the full range of numbers
085 * in this class.
086 * @param c the sub-class of number that input will be interpreted as
087 * @throws IllegalArgumentException if <code>c</code> does not implement
088 * Number, or if it does not have a static
089 * <code>valueOf(String)</code>
090 * method or a single argument string constructor.
091 */
092 public void setNumberClass(Class c){
093 if (Number.class.isAssignableFrom(c) || Comparable.class.isAssignableFrom(c)){
094 myClass = c;
095 } else {
096 throw new IllegalArgumentException(String.valueOf(c) + " is not a " +
097 "comparable subclass of Number");
098 }
099 method = null;
100 constructor = null;
101 try{
102 method = c.getMethod("valueOf",new Class[]{String.class});
103 } catch (NoSuchMethodException exm){
104 try {
105 constructor = c.getConstructor(new Class[]{String.class});
106 } catch (NoSuchMethodException exc){
107 throw new IllegalArgumentException("Caused by " + exc.toString());
108 }
109 }
110 min = max = null;
111 }
112
113 /** Sets the largest allowed number.
114 * @param max upper limit. null for none.
115 * @throws ClassCastException if <code>max</code> is not <code>null</code>
116 * or an instance of <code>getNumberClass</code>.
117 */
118 public void setUpperLimit(Comparable max){
119 if(max == null){
120 this.max = null;
121 } else {
122 if(!(myClass.isInstance(max))){
123 throw new ClassCastException("max (" + max + ") is not an " +
124 "instance of " + myClass);
125 }
126 this.max = max;
127 }
128 }
129
130 /** Returns the largest allowed number.
131 * @return upper limit. null for none.
132 */
133 public Comparable getUpperLimit(){
134 return max;
135 }
136
137 /** Sets the smallest allowed number.
138 * @param min lower limit. null for none.
139 * @throws ClassCastException if <code>min</code> is not <code>null</code>
140 * or an instance of <code>getNumberClass</code>.
141 */
142 public void setLowerLimit(Comparable min){
143 if(min == null){
144 this.min = null;
145 } else {
146 if(!(myClass.isInstance(min))){
147 throw new ClassCastException("min (" + min + ") is not an " +
148 "instance of " + myClass);
149 }
150 this.min = min;
151 }
152 }
153
154 /** Explains why the last call to {@link #verify} returned false. If the
155 * last call to <code>verify</code> returned false, this method will return
156 * <ul>
157 * <li><code>null</code> - if the input was out of range
158 * <li>a NumberFormatException - if the input could not be parsed as a
159 * number of the correct type
160 * <li>or another Exception
161 * </ul>
162 * @return an Exception or <code>null</code>
163 */
164 public Exception explain(){
165 return ex;
166 }
167
168 /** Returns the smallest allowed number.
169 * @return lower limit. null for none.
170 */
171 public Comparable getLowerLimit(){
172 return min;
173 }
174
175 /**
176 * @throws IllegalArgumentException if <code>input</code> is not a
177 * JTextComponent
178 */
179 public boolean verify(JComponent input){
180 if(!(input instanceof JTextComponent)){
181 throw new IllegalArgumentException(input.toString() + " is not a " +
182 "JTextComponent.");
183 }
184 boolean result = false;
185 ex = null;
186 Object[] in = new Object[]{((JTextComponent) input).getText()};
187 try{
188 Object num = (method == null)
189 ? constructor.newInstance(in)
190 : method.invoke(null, in);
191 result =
192 ( min == null || min.compareTo(num) <= 0 ) &&
193 ( max == null || max.compareTo(num) >= 0 );
194 } catch(Exception failed){
195 ex = failed;
196 }
197 return result;
198 }
199 }