Mp3dings 
20100427

de.mospace.swing.text
Class GeneralInputVerifier

java.lang.Object
  extended by javax.swing.InputVerifier
      extended by de.mospace.swing.text.GeneralInputVerifier
All Implemented Interfaces:
Serializable

public class GeneralInputVerifier
extends InputVerifier
implements Serializable

An input verifier that checks if a text component's text can be interpreted as a value of a particular class and optionally if it falls into an allowed range of values of that class.

Here is some sample code

static GeneralInputVerifier giv;
static JTextComponent text = new javax.swing.JTextField();

private static void test(String s){
    text.setText(s);
    System.out.println(s + ", " + giv.verify(text) + ", " + giv.explain());
}

public static void main(String[] argv) throws Exception{
    System.out.println("Double, > -1, < 10");
    giv = new GeneralInputVerifier(Double.class, new Double(-1), new Double(10));
    test("-2");
    test("-1");
    test("5");
    test("10");
    test("11");
    test("x12");

    System.out.println();
    System.out.println("Date, yyyy-MM-dd, > 2007-02-15" );
    java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
    giv = new GeneralInputVerifier(java.util.Date.class,
    java.text.DateFormat.class.getMethod("parse", new Class[]{String.class}),
    format);
    giv.setLowerLimit(format.parse("2007-02-15"));
    test("2007-08-15");
    test("200a-08-15");
    test("2005-08-15");
}
The output is
Double, > -1, < 10
-2, false, null
-1, true, null
5, true, null
10, true, null
11, false, null
x12, false, java.lang.NumberFormatException: For input string: "x12"

Date, yyyy-MM-dd, > 2007-02-15
2007-08-15, true, null
200a-08-15, false, java.text.ParseException: Unparseable date: "200a-08-15"
2005-08-15, false, null

Version:
$Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
Author:
Moritz Ringler
See Also:
Serialized Form

Constructor Summary
GeneralInputVerifier(Class c)
          Constructs a new GeneralInputVerifier for the full range of values in the specified class.
GeneralInputVerifier(Class c, Comparable min, Comparable max)
          Constructs a new GeneralInputVerifier for the given class and the given limits.
GeneralInputVerifier(Class c, Method m)
          Creates a new GeneralInputVerifier for the specified class that uses the specified static method to construct a value from a string.
GeneralInputVerifier(Class c, Method m, Object o)
          Creates a new GeneralInputVerifier for the specified class that uses the specified method and object to construct a value from a string.
 
Method Summary
 Throwable explain()
          Explains why the last call to verify returned false.
 Class getInputClass()
          Returns the class against which input will be checked.
 Comparable getLowerLimit()
          Returns the smallest allowed value.
 Comparable getUpperLimit()
          Returns the largest allowed value.
 void setInputClass(Class c)
          Sets the class against which input will be checked.
 void setInputClass(Class c, Method m)
          Sets the class against which input will be checked and the static method used to construct values of this class from a string.
 void setInputClass(Class c, Method m, Object o)
          Sets the class against which input will be checked and the method and object used to construct values of this class from a string.
 void setLowerLimit(Comparable min)
          Sets the smallest allowed value.
 void setUpperLimit(Comparable max)
          Sets the largest allowed value.
 boolean verify(JComponent c)
          Verifies the current text of a JTextComponent.
 boolean verify(String text)
          Verifies the specified text.
 
Methods inherited from class javax.swing.InputVerifier
shouldYieldFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GeneralInputVerifier

public GeneralInputVerifier(Class c,
                            Comparable min,
                            Comparable max)
Constructs a new GeneralInputVerifier for the given class and the given limits.

Parameters:
c - the class that input will be interpreted as
min - lower limit. null for none.
max - upper limit. null for none.
Throws:
IllegalArgumentException - if c does not have a public static valueOf(String) method or a public constructor with a single string argument.
ClassCastException - if min and max are neither null nor instances of c.

GeneralInputVerifier

public GeneralInputVerifier(Class c)
Constructs a new GeneralInputVerifier for the full range of values in the specified class.

Parameters:
c - the class that input will be interpreted as
Throws:
IllegalArgumentException - if c does not have a public static valueOf(String) method or a public constructor with a single String argument

GeneralInputVerifier

public GeneralInputVerifier(Class c,
                            Method m)
Creates a new GeneralInputVerifier for the specified class that uses the specified static method to construct a value from a string.

Parameters:
c - the class that input will be interpreted as
m - a public static method that accepts a single string argument and whose return type can be assigned to c
Throws:
IllegalArgumentException - if method is not a public static method that accepts a single string argument and has a return type of c

GeneralInputVerifier

public GeneralInputVerifier(Class c,
                            Method m,
                            Object o)
Creates a new GeneralInputVerifier for the specified class that uses the specified method and object to construct a value from a string. This will automatically reset the allowed range to the full range of values in this class.

Parameters:
c - the class that input will be interpreted as
m - a method that accepts a single string argument and whose return type can be assigned to c
o - the object to invoke the method on, or null if m is static
Throws:
IllegalArgumentException - if method is not a public method of o that accepts a single string argument and has a return type of c
Method Detail

getInputClass

public Class getInputClass()
Returns the class against which input will be checked.

See Also:
setInputClass(java.lang.Class)

setInputClass

public void setInputClass(Class c)
Sets the class against which input will be checked. This will automatically reset the allowed range to the full range of values in this class.

Parameters:
c - the class that input will be interpreted as
Throws:
IllegalArgumentException - if c does not have a public static valueOf(String) method that returns a value of c or a public constructor with a single string argument.

setInputClass

public void setInputClass(Class c,
                          Method m)
Sets the class against which input will be checked and the static method used to construct values of this class from a string. This will reset the allowed range to the full range of values in this class.

Parameters:
c - the class that input will be interpreted as
m - a static method that accepts a single string argument and whose return type can be assigned to c
Throws:
IllegalArgumentException - if method is not a public static method that accepts a single string argument and has a return type of c

setInputClass

public void setInputClass(Class c,
                          Method m,
                          Object o)
Sets the class against which input will be checked and the method and object used to construct values of this class from a string. This will automatically reset the allowed range to the full range of values in this class.

Parameters:
c - the class that input will be interpreted as
m - a method that accepts a single string argument and whose return type can be assigned to c
o - the object to invoke the method on, or null if m is static
Throws:
IllegalArgumentException - if method is not a public method of o that accepts a single string argument and has a return type of c

setUpperLimit

public void setUpperLimit(Comparable max)
Sets the largest allowed value.

Parameters:
max - upper limit. null for none.
Throws:
ClassCastException - if max is neither null nor an instance of getInputClass.

getUpperLimit

public Comparable getUpperLimit()
Returns the largest allowed value.

Returns:
upper limit. null for none.

setLowerLimit

public void setLowerLimit(Comparable min)
Sets the smallest allowed value.

Parameters:
min - lower limit. null for none.
Throws:
ClassCastException - if min is neither null nor an instance of getInputClass.

explain

public Throwable explain()
Explains why the last call to verify returned false. This method will return any exception thrown within the verify methods.

Returns:
a throwable or null

getLowerLimit

public Comparable getLowerLimit()
Returns the smallest allowed value.

Returns:
lower limit. null for none.

verify

public boolean verify(JComponent c)
Verifies the current text of a JTextComponent.

Specified by:
verify in class InputVerifier
Parameters:
c - a swing text component
Returns:
whether the current text of c is legal input
Throws:
ClassCastException - if c is not a JTextComponent

verify

public boolean verify(String text)
Verifies the specified text.

Parameters:
text - the input to verify
Returns:
whether text is legal input.

Mp3dings 
20100427