net.wotonomy.ui.swing.util
Class TextInputRangeChecker

java.lang.Object
  extended by net.wotonomy.ui.swing.util.TextInputRangeChecker
All Implemented Interfaces:
java.awt.event.FocusListener, java.util.EventListener

public class TextInputRangeChecker
extends java.lang.Object
implements java.awt.event.FocusListener

This class will actively check the inputs of 2 numbers in seperate text components. The number in the text components represent an upper and lower bound to some range. This class checks to make sure the user inputs values in the lower bound text field that are less than the value of the upper bound and vice versa for the upper bound text field. This class will also check to make sure the bounds fall within a given range if specified. The checks are automatically performed when the focus is lost on either component. If the inputs are correct then no event occurs. If the inputs are not correct, then a dialog message is displayed stating the reason why the bounds are invalid, and the original correct value is restored into the text components.

Version:
$Revision: 904 $
Author:
rglista, $Author: cgruber $

Field Summary
protected static int LOWER
           
protected static int NONE
           
protected static int UPPER
           
 
Constructor Summary
TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent, javax.swing.text.JTextComponent anUpperTextComponent)
          Constructor with some of the settable parameters.
TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent, javax.swing.text.JTextComponent anUpperTextComponent, double aMaxRange)
          Constructor with some of the settable parameters.
TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent, javax.swing.text.JTextComponent anUpperTextComponent, java.lang.String lowerTextName, java.lang.String upperTextName)
          Constructor with some of the settable parameters.
TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent, javax.swing.text.JTextComponent anUpperTextComponent, java.lang.String lowerTextName, java.lang.String upperTextName, double aMaxRange)
          Constructor with all the settable parameters.
 
Method Summary
 void addFocusListener(java.awt.event.FocusListener aListener)
          Adds the listener to the lists of focus listener maintened by this object.
protected  void displayMessage(java.lang.String message, java.awt.Component parent)
          Creates a JOptionPane to display the reason why the bounds failed validation.
protected  void fireFocusEvent(java.awt.event.FocusEvent e)
          Fires a focus lost event if the validation was successfull.
 void focusGained(java.awt.event.FocusEvent e)
          Method used to be notified when one of the text components has gained its focus.
 void focusLost(java.awt.event.FocusEvent e)
          Method used to be notified when one of the text components has lost its focus.
 double getLastValidatedLowerNumber()
          Returns the last valid value of the lower bound.
 double getLastValidatedUpperNumber()
          Returns the last valid value of the upper bound.
protected  double getNumber(javax.swing.text.JTextComponent aComponent)
          Gets the number represented in the text component.
 boolean performCheck()
          Allows the caller to perform the validation of the bounds programatically.
protected  boolean validate(java.lang.Object aComponent)
          Validates the bounds inputed by the user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NONE

protected static final int NONE
See Also:
Constant Field Values

LOWER

protected static final int LOWER
See Also:
Constant Field Values

UPPER

protected static final int UPPER
See Also:
Constant Field Values
Constructor Detail

TextInputRangeChecker

public TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent,
                             javax.swing.text.JTextComponent anUpperTextComponent)
Constructor with some of the settable parameters. No range checking is used.

Parameters:
aLowerTextComponent - A text component for the lower bound.
anUpperTextComponent - A text component for the upper bound.

TextInputRangeChecker

public TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent,
                             javax.swing.text.JTextComponent anUpperTextComponent,
                             java.lang.String lowerTextName,
                             java.lang.String upperTextName)
Constructor with some of the settable parameters. No range checking is used.

Parameters:
aLowerTextComponent - A text component for the lower bound.
anUpperTextComponent - A text component for the upper bound.
lowerTextName - The name of the lower bound, eg - start year.
upperTextName - The name of the upper bound, eg - end year. is used.

TextInputRangeChecker

public TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent,
                             javax.swing.text.JTextComponent anUpperTextComponent,
                             double aMaxRange)
Constructor with some of the settable parameters.

Parameters:
aLowerTextComponent - A text component for the lower bound.
anUpperTextComponent - A text component for the upper bound.
aMaxRange - The range the bounds muist fall between, if 0 then no range is used.

TextInputRangeChecker

public TextInputRangeChecker(javax.swing.text.JTextComponent aLowerTextComponent,
                             javax.swing.text.JTextComponent anUpperTextComponent,
                             java.lang.String lowerTextName,
                             java.lang.String upperTextName,
                             double aMaxRange)
Constructor with all the settable parameters.

Parameters:
aLowerTextComponent - A text component for the lower bound.
anUpperTextComponent - A text component for the upper bound.
lowerTextName - The name of the lower bound, eg - start year.
upperTextName - The name of the upper bound, eg - end year.
aMaxRange - The range the bounds muist fall between, if 0 then no range is used.
Method Detail

performCheck

public boolean performCheck()
Allows the caller to perform the validation of the bounds programatically. The lower bound is compared to the upper bound and range checking is performed. If the lower bound is greater than the upper bound, or the range between the bounds is greater than the max range, then validation fails.

Returns:
TRUE is validation is successfull, FALSE if it fails.

addFocusListener

public void addFocusListener(java.awt.event.FocusListener aListener)
Adds the listener to the lists of focus listener maintened by this object. When one of the 2 text components receives a focus event, this object will fire that focus event to any of its listeners. This is useful when the calling object wants to be notified of the components focus events, but wants to ensure that the validation has occured first.

NOTE: The focus is only fired if the validation was successful. This might have to be changed.

Parameters:
aListener - A Focus Listener to receive Focus Events.

getLastValidatedLowerNumber

public double getLastValidatedLowerNumber()
Returns the last valid value of the lower bound. If this is called while the user is updating the text component but before the focus is lost, the value returned will be the original value before the user started updating the bound.

Returns:
The last valid value of the lower bound.

getLastValidatedUpperNumber

public double getLastValidatedUpperNumber()
Returns the last valid value of the upper bound. If this is called while the user is updating the text component but before the focus is lost, the value returned will be the original value before the user started updating the bound.

Returns:
The last valid value of the upper bound.

focusGained

public void focusGained(java.awt.event.FocusEvent e)
Method used to be notified when one of the text components has gained its focus.

Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent e)
Method used to be notified when one of the text components has lost its focus. Automatic validation occurs here.

Specified by:
focusLost in interface java.awt.event.FocusListener

fireFocusEvent

protected void fireFocusEvent(java.awt.event.FocusEvent e)
Fires a focus lost event if the validation was successfull.


validate

protected boolean validate(java.lang.Object aComponent)
Validates the bounds inputed by the user.

Parameters:
aComponent - The component to use to display a dialog window, if neccessray. If null, then the parent window of the text componets will be used.
Returns:
TRUE if validation was successful, FALSE otherwise.

displayMessage

protected void displayMessage(java.lang.String message,
                              java.awt.Component parent)
Creates a JOptionPane to display the reason why the bounds failed validation.


getNumber

protected double getNumber(javax.swing.text.JTextComponent aComponent)
Gets the number represented in the text component. If the text does not represent a number, then zero is returned.



Copyright © 2006 null. All Rights Reserved.