com.fedmest.cw.keyboard.validators
Class DateValidator

java.lang.Object
  extended by com.fedmest.cw.keyboard.validators.DateValidator
All Implemented Interfaces:
KeyboardInputValidator<java.util.Date>

public final class DateValidator
extends java.lang.Object
implements KeyboardInputValidator<java.util.Date>

This class parses a string from user input. Create this class by specifying either a format string following the conventions of the SimpleDateFormat class or by passing an actual instance of a DateFormat into the constructor. You can then use your validator instance with the KeyboardUtils class to get a date from the user. If the user canceled input on a cancelable KeyboardUtils, the class will return null instead of a Date.

Version:
1.0
Author:
Federico Mestrone

Constructor Summary
DateValidator(java.text.DateFormat format)
          Creates a new instance of this validator with a DateFormat instance detailing how the date should be parsed from the typed string.
DateValidator(java.lang.String formatString)
          Creates a new instance of this validator with a string that represent the accepted format of the date that will be parsed following the conventions of the SimpleDateFormat class.
 
Method Summary
 java.util.Date cancelValue()
          Allows you to specify the return value to offer when the user cancels input on a cancelable instance of KeyboardUtils .
 java.util.Date checkInput(java.lang.String txt)
          Takes the input string, checks its validity and converts it to the data type specified by the type argument.
 void resetErrorMessage()
          This method resets the error message that will be displayed to users on validation failure to the default text ("Invalid date - retry").
 void resetMax()
          This method resets the latest date that will be accepted by this validator to the latest possible date value.
 void resetMin()
          This method resets the earliest date that will be accepted by this validator to the earliest possible date value.
 void resetRange()
          This method resets the earliest and latest dates that will be accepted by this validator to the earliest and latest possible date values respectively.
 void resetRangeMessage()
          This method resets to the default text the error message that will be displayed to users when the value is out of range ("Value out of range - retry").
 void setErrorMessage(java.lang.String errorMessage)
          Sets the error message that will be displayed to the user if validation fails because the input string was not a proper date value.
 void setMax(java.util.Date max)
          This method sets the latest date that will be accepted by this validator.
 void setMin(java.util.Date min)
          This method sets the earliest date that will be accepted by this validator.
 void setRange(java.util.Date min, java.util.Date max)
          This method sets the earliest and latest dates that will be accepted by this validator.
 void setRangeMessage(java.lang.String rangeMessage)
          This method sets the error message that will be displayed to the user if validation fails due to a value being outside of the range specified with setMax(Date), setMin(Date) or setRange(Date, Date).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateValidator

public DateValidator(java.lang.String formatString)
Creates a new instance of this validator with a string that represent the accepted format of the date that will be parsed following the conventions of the SimpleDateFormat class.

Parameters:
formatString - the format specification of the date to parse

DateValidator

public DateValidator(java.text.DateFormat format)
Creates a new instance of this validator with a DateFormat instance detailing how the date should be parsed from the typed string.

Parameters:
format - an instance of the DateFormat class used for date parsing
Method Detail

resetErrorMessage

public void resetErrorMessage()
This method resets the error message that will be displayed to users on validation failure to the default text ("Invalid date - retry").

See Also:
setErrorMessage(String)

setErrorMessage

public void setErrorMessage(java.lang.String errorMessage)
Sets the error message that will be displayed to the user if validation fails because the input string was not a proper date value. Basically this happens when DateFormat.parse(String), invoked internally by this validator, raises a ParseException.

Parameters:
errorMessage - the new error messages to display to users when validation fails for any reason.
See Also:
resetErrorMessage()

setRangeMessage

public void setRangeMessage(java.lang.String rangeMessage)
This method sets the error message that will be displayed to the user if validation fails due to a value being outside of the range specified with setMax(Date), setMin(Date) or setRange(Date, Date).

Parameters:
rangeMessage - the new error message to display to users

resetRangeMessage

public void resetRangeMessage()
This method resets to the default text the error message that will be displayed to users when the value is out of range ("Value out of range - retry").

See Also:
setRangeMessage(String)

setMax

public void setMax(java.util.Date max)
This method sets the latest date that will be accepted by this validator. The value is inclusive. The validator will fail if the input value is greater than the value of the max parameter.

Parameters:
max - the latest date allowed in parsing (inclusive)

resetMax

public void resetMax()
This method resets the latest date that will be accepted by this validator to the latest possible date value.

See Also:
setMax(Date)

setMin

public void setMin(java.util.Date min)
This method sets the earliest date that will be accepted by this validator. The value is inclusive. The validator will fail if the input value is less than the value of the min parameter.

Parameters:
min - the earliest date allowed in parsing (inclusive)

resetMin

public void resetMin()
This method resets the earliest date that will be accepted by this validator to the earliest possible date value.

See Also:
setMin(Date)

setRange

public void setRange(java.util.Date min,
                     java.util.Date max)
This method sets the earliest and latest dates that will be accepted by this validator. The values are inclusive. The validator will fail if the input value is not within the range specified by the min and max parameters. Invoking this method is equivalent to invoking setMin(Date) and setMax(Date).

Parameters:
min - the earliest date allowed in parsing (inclusive)
max - the latest date allowed in parsing (inclusive)
See Also:
setMin(Date), setMax(Date)

resetRange

public void resetRange()
This method resets the earliest and latest dates that will be accepted by this validator to the earliest and latest possible date values respectively.

See Also:
setMin(Date), setMax(Date), resetMin(), resetMax()

checkInput

public java.util.Date checkInput(java.lang.String txt)
                          throws KeyboardInputException
Description copied from interface: KeyboardInputValidator
Takes the input string, checks its validity and converts it to the data type specified by the type argument. If validation fails, a KeyboardInputException is raised and the user will have to type their input again. If a null reference is passed into the method, the method may throw a NullPointerException, so programmers do not need to worry about checking for null pointers.

Specified by:
checkInput in interface KeyboardInputValidator<java.util.Date>
Parameters:
txt - the input text. If the string is null, NullPointerException will be thrown.
Returns:
the result of converting the typed string into the data type expected
Throws:
KeyboardInputException - if validation against the input string failed

cancelValue

public java.util.Date cancelValue()
Description copied from interface: KeyboardInputValidator
Allows you to specify the return value to offer when the user cancels input on a cancelable instance of KeyboardUtils . Though null should be returned in most cases, this method is useful when - for example - you want to return a primitive type from your validator and can't use null as a return value without causing a bit of inconvenienve in your application code (explicit unboxing and checks for null values).

Specified by:
cancelValue in interface KeyboardInputValidator<java.util.Date>
Returns:
the value to return when user cancels input


Copyright © 2008 Federico Mestrone. All Rights Reserved.