com.sas.swing.visuals.util
Class JTextFieldValidator

com.sas.swing.visuals.util.JTextFieldValidator
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.FocusListener, java.util.EventListener

public class JTextFieldValidator
implements java.awt.event.FocusListener, java.awt.event.ActionListener

JTextFieldValidator is used as a error-handler/validator on a JTextField. The validation of the text occurs when an actionPerformed event or focusLost occurs on the JTextField. The default error handler used is the JOptionPaneErrorHandler, and the default validator used is the BoundedLong. If the value is valid, the JTextField will remain as is, if not, the JTextField will get set back to its previous value.

Creating a JTextFieldValidator

The following code shows how to use the JTextFieldValidator on a JTextField to validate integer numbers greater than 0:

 JTextField jTextField1 = new JTextField();
 ...
 JTextFieldValidator fieldValidator = new JTextFieldValidator(jTextField1, 0, Integer.MAX_VALUE);
 

This will use the default error handler (JOptionPaneErrorHandler), but and valid instance of com.sas.net.errorhandlers.GenericErrorHandlerInterface will work. Now in order to detect if the value was valid, the user should add a PropertyChangeListener to the validator and listen for a "evalid"e event.

See Also:
JTextField, GenericErrorHandlerInterface,


Field Summary
protected  javax.swing.event.SwingPropertyChangeSupport spcs
           
 
Constructor Summary
JTextFieldValidator(javax.swing.JTextField textField)
          Creates a JTextFieldValidator with a BoundedLong as the validator, with Integer.MIN_VALUE and Integer.MAX_VALUE as its limits.
JTextFieldValidator(javax.swing.JTextField textField, double minValue, double maxValue)
          Creates a JTextFieldValidator with a BoundedDouble as the validator, with minVaue and maxValue as its limits.
JTextFieldValidator(javax.swing.JTextField textField, double minValue, double maxValue, java.lang.String errorMessage)
          Creates a JTextFieldValidator with a BoundedDouble as the validator, with minVaue and maxValue as its limits.
JTextFieldValidator(javax.swing.JTextField textField, int minValue, int maxValue)
          Creates a JTextFieldValidator with a BoundedLong as the validator, with minVaue and maxValue as its limits.
JTextFieldValidator(javax.swing.JTextField textField, int minValue, int maxValue, java.lang.String errorMessage)
          Creates a JTextFieldValidator with a BoundedLong as the validator, with minVaue and maxValue as its limits.
JTextFieldValidator(javax.swing.JTextField textField, ValidationInterface validator)
          Creates a JTextFieldValidator with the ValidationInterface being the validator.
JTextFieldValidator(javax.swing.JTextField textField, ValidationInterface validator, GenericErrorHandlerInterface errorHandler)
          Creates a JTextFieldValidator with the ValidationInterface being the validator and the errorHandler used to handle invalid values entered in the JTextField, as evaluated by the validator.
JTextFieldValidator(javax.swing.JTextField textField, ValidationInterface validator, java.lang.String errorMessage)
          Creates a JTextFieldValidator with the ValidationInterface being the validator.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent ev)
          Handles the actionPerformed event on the JTextField.
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
           
 void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
           
 void firePropertyChange(java.beans.PropertyChangeEvent evt)
          Fire an existing PropertyChangeEvent to any registered listeners.
 void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
          Report a bound property update to any registered listeners.
 void focusGained(java.awt.event.FocusEvent ev)
          Handles the focusGained event on the JTextField.
 void focusLost(java.awt.event.FocusEvent ev)
          Handles the focusLost event on the JTextField.
 GenericErrorHandlerInterface getErrorHandler()
          Returns the error handler used to notify the user of an invalid value entered in the JTextField.
 ValidationInterface getValidator()
          Returns the validator used to evaluate the text entered in the JTextField, notifies the error handler if the value is not valid.
 boolean isValid()
          Returns whether the evaluation of the text was valid or not.
 void removeListenersOnTextField()
          Removes the listeners on teh textField that the validator uses to be notified of when the text changes.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
           
 void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
           
 void setErrorHandler(GenericErrorHandlerInterface errorHandler)
          Sets the error handler used to notify the user of an invalid value entered in the JTextField.
 void setValidator(ValidationInterface validator)
          Sets the validator used to evaluate the text entered in the JTextField, notifies the error handler if the value is not valid.
 

Field Detail

spcs

protected javax.swing.event.SwingPropertyChangeSupport spcs
Constructor Detail

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField)
Creates a JTextFieldValidator with a BoundedLong as the validator, with Integer.MIN_VALUE and Integer.MAX_VALUE as its limits. The JOptionPaneErrorHandler is the default error-handler used. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
See Also:
BoundedLong, JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           double minValue,
                           double maxValue)
Creates a JTextFieldValidator with a BoundedDouble as the validator, with minVaue and maxValue as its limits. The JOptionPaneErrorHandler is the default error-handler used. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
minValue - The minimum value limit used on the validator
maxValue - The maximum value limit used on the validator
See Also:
BoundedDouble, JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           double minValue,
                           double maxValue,
                           java.lang.String errorMessage)
Creates a JTextFieldValidator with a BoundedDouble as the validator, with minVaue and maxValue as its limits. The JOptionPaneErrorHandler is the default error-handler used. The errorMessage is used instead of the default message provided by the validator. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
minValue - The minimum value limit used on the validator
maxValue - The maximum value limit used on the validator
errorMessage - The error message to display in the error-handler, used instead of the default message of the validator
See Also:
BoundedDouble, JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           int minValue,
                           int maxValue)
Creates a JTextFieldValidator with a BoundedLong as the validator, with minVaue and maxValue as its limits. The JOptionPaneErrorHandler is the default error-handler used. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
minValue - The minimum value limit used on the validator
maxValue - The maximum value limit used on the validator
See Also:
BoundedLong, JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           int minValue,
                           int maxValue,
                           java.lang.String errorMessage)
Creates a JTextFieldValidator with a BoundedLong as the validator, with minVaue and maxValue as its limits. The JOptionPaneErrorHandler is the default error-handler used. The errorMessage is used instead of the default message provided by the validator. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
minValue - The minimum value limit used on the validator
maxValue - The maximum value limit used on the validator
errorMessage - The error message to display in the error-handler, used instead of the default message of the validator
See Also:
BoundedLong, JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           ValidationInterface validator)
Creates a JTextFieldValidator with the ValidationInterface being the validator. The JOptionPaneErrorHandler is the default error-handler used. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
validator - The instance of ValidationInterface the is used to validate the text in the JTextField. If the validator returns false, the error-handler will notify the user.
See Also:
JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           ValidationInterface validator,
                           java.lang.String errorMessage)
Creates a JTextFieldValidator with the ValidationInterface being the validator. The JOptionPaneErrorHandler is the default error-handler used. The errorMessage is used instead of the default message provided by the validator. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
validator - The instance of ValidationInterface the is used to validate the text in the JTextField. If the validator returns false, the error-handler will notify the user.
errorMessage - The error message to display in the error-handler, used instead of the default message of the validator
See Also:
JTextField, GenericErrorHandlerInterface, ValidationInterface

JTextFieldValidator

public JTextFieldValidator(javax.swing.JTextField textField,
                           ValidationInterface validator,
                           GenericErrorHandlerInterface errorHandler)
Creates a JTextFieldValidator with the ValidationInterface being the validator and the errorHandler used to handle invalid values entered in the JTextField, as evaluated by the validator. A FocusListener and a ActionListener and added to the JTextField to listen to the updates made to the text.

Parameters:
textField - The JTextField to preform the validation on whenever a actionPerformed or focusLost event occurs on it.
validator - The instance of ValidationInterface the is used to validate the text in the JTextField. If the validator returns false, the error-handler will notify the user.
errorHandler - The error-handler used to handler invalid values in the JTextField
See Also:
JTextField, GenericErrorHandlerInterface, ValidationInterface
Method Detail

removeListenersOnTextField

public void removeListenersOnTextField()
Removes the listeners on teh textField that the validator uses to be notified of when the text changes. Use this method to clean up all listeners and when the validator is to no longer be used, otherwise the validator will no longer work. Convenience method provided to the user to clean up listeners. This method will quite often be called from a customizerClsoing() method in a customizer.


setErrorHandler

public void setErrorHandler(GenericErrorHandlerInterface errorHandler)
Sets the error handler used to notify the user of an invalid value entered in the JTextField.

Parameters:
errorHandler - The instance of the GenericErrorHandlerInterface used to display the error
See Also:
GenericErrorHandlerInterface, ValidationInterface

getErrorHandler

public GenericErrorHandlerInterface getErrorHandler()
Returns the error handler used to notify the user of an invalid value entered in the JTextField.

Returns:
The instance of the GenericErrorHandlerInterface used to display the error
See Also:
GenericErrorHandlerInterface, ValidationInterface

setValidator

public void setValidator(ValidationInterface validator)
Sets the validator used to evaluate the text entered in the JTextField, notifies the error handler if the value is not valid.

Parameters:
validator - The instance of the ValidationInterface used to validate the text
See Also:
GenericErrorHandlerInterface, ValidationInterface

getValidator

public ValidationInterface getValidator()
Returns the validator used to evaluate the text entered in the JTextField, notifies the error handler if the value is not valid.

Returns:
The instance of the ValidationInterface used to validate the text
See Also:
GenericErrorHandlerInterface, ValidationInterface

isValid

public boolean isValid()
Returns whether the evaluation of the text was valid or not.

Returns:
true if the value was valid, false otherwise
See Also:
ValidationInterface

focusGained

public void focusGained(java.awt.event.FocusEvent ev)
Handles the focusGained event on the JTextField.

Specified by:
focusGained in interface java.awt.event.FocusListener
Parameters:
ev - The focusGained event

focusLost

public void focusLost(java.awt.event.FocusEvent ev)
Handles the focusLost event on the JTextField. If the value is valid, a PropertyChangeEvent is fired, else, the text is set back to its previous value and a PropertyChangeEvent is fired

Specified by:
focusLost in interface java.awt.event.FocusListener
Parameters:
ev - The focusLost event, validation is performed unless validation is currently occuring do to an actionPerformed event.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent ev)
Handles the actionPerformed event on the JTextField. If the value is valid, a PropertyChangeEvent is fired, else, the text is set back to its previous value and a PropertyChangeEvent is fired

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
ev - The actionPerformed event, validation is performed unless validation is currently occuring do to a focusLost event.

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)

addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String propertyName,
                                      java.beans.PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(java.lang.String propertyName,
                                         java.beans.PropertyChangeListener listener)

firePropertyChange

public void firePropertyChange(java.lang.String propertyName,
                               java.lang.Object oldValue,
                               java.lang.Object newValue)
Report a bound property update to any registered listeners. No event is fired if old and new are equal and non-null.

Parameters:
propertyName - The programmatic name of the property that was changed.
oldValue - The old value of the property.
newValue - The new value of the property.

firePropertyChange

public void firePropertyChange(java.beans.PropertyChangeEvent evt)
Fire an existing PropertyChangeEvent to any registered listeners. No event is fired if the given event's old and new values are equal and non-null.

Parameters:
evt - The PropertyChangeEvent object.



Copyright © 2009 SAS Institute Inc. All Rights Reserved.