net.sourceforge.doyen
Class DataField

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.text.JTextComponent
                  extended byjavax.swing.JTextField
                      extended bynet.sourceforge.doyen.DataField
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Scrollable, Serializable, SwingConstants
Direct Known Subclasses:
DateTimeField

public class DataField
extends JTextField

A class that implements basic functionality for a text field that places constraints on its input. DataField validates its input when it loses or gains focus, generates an action event, or when messaged with the validateInput() method. The no-op method checkInput() must be overridden by subclassers to perform the actual data validation.

Invalid input is flagged by repainting the contents of the field in red. If a key is typed into a field so highlighted, the text reverts back to black (non-flagged). Validation is not performed whenever the contents of the field change, as the necessary parsing is an expensive operation.

Author:
Mark Lindner
See Also:
Serialized Form

Nested Class Summary
private  class DataField._DocumentListener
          document listener
private static class DataField.ChangeSupport
           
 
Nested classes inherited from class javax.swing.JTextField
JTextField.AccessibleJTextField
 
Nested classes inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
private  boolean adjusting
           
private  DataField.ChangeSupport csupport
           
private  DataField._DocumentListener documentListener
           
private  boolean inputRequired
           
protected  boolean invalid
          A state flag for representing validation state.
 
Fields inherited from class javax.swing.JTextField
notifyAction
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DataField()
          Construct a new DataField.
DataField(int width)
          Construct a new DataField with the specified width.
 
Method Summary
private  void _fireChange()
          Delay-fire a change event, but only if the current DocumentEvent is not the result of a call to setText().
private  void _init()
          initialization
 void addChangeListener(ChangeListener listener)
          Add a ChangeListener to this component's list of listeners.
protected  boolean checkInput()
          Determine if the given input is valid for this field.
 boolean isInputRequired()
          Determine if input is required in this field.
protected  void paintInvalid(boolean invalid)
          Paint the necessary decorations for the field to denote invalid (or valid) input.
 void removeChangeListener(ChangeListener listener)
          Add a ChangeListener to this component's list of listeners.
 void setDocument(Document doc)
           
 void setEditable(boolean flag)
          Set the editable state of this field.
 void setInputRequired(boolean flag)
          Specify whether an input is required in this field.
 void setText(String text)
          Set the text to be displayed by this field.
 boolean validateInput()
          Validate the input in this field.
 
Methods inherited from class javax.swing.JTextField
addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setFont, setHorizontalAlignment, setScrollOffset
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

csupport

private DataField.ChangeSupport csupport

documentListener

private DataField._DocumentListener documentListener

inputRequired

private boolean inputRequired

invalid

protected boolean invalid
A state flag for representing validation state.


adjusting

private boolean adjusting
Constructor Detail

DataField

public DataField()
Construct a new DataField.


DataField

public DataField(int width)
Construct a new DataField with the specified width.

Method Detail

_init

private void _init()
initialization


setDocument

public void setDocument(Document doc)

addChangeListener

public void addChangeListener(ChangeListener listener)
Add a ChangeListener to this component's list of listeners. ChangeEvents are fired when this text field's document model changes.

Parameters:
listener - The listener to add.

removeChangeListener

public void removeChangeListener(ChangeListener listener)
Add a ChangeListener to this component's list of listeners. ChangeEvents are fired when this text field's document model changes.

Parameters:
listener - The listener to add.

_fireChange

private void _fireChange()
Delay-fire a change event, but only if the current DocumentEvent is not the result of a call to setText().


setText

public final void setText(String text)
Set the text to be displayed by this field. A ChangeEvent will not be fired when the data in the field is modified via this call.

Parameters:
text - The text to set.

paintInvalid

protected void paintInvalid(boolean invalid)
Paint the necessary decorations for the field to denote invalid (or valid) input. The default implementation sets the text color to red if the input is invalid and black otherwise. This method may be overridden by subclassers who wish to customize the method of visual feedback.

Parameters:
invalid - A flag specifying whether the input in the field is currently valid or invalid.

setEditable

public void setEditable(boolean flag)
Set the editable state of this field.

Parameters:
flag - A flag specifying whether this field should be editable. Non-editable fields are made transparent.

setInputRequired

public void setInputRequired(boolean flag)
Specify whether an input is required in this field. If no input is required, the validateInput() method will return true if the field is left empty; otherwise it will return false.

Parameters:
flag - The flag.
See Also:
validateInput(), isInputRequired()

isInputRequired

public boolean isInputRequired()
Determine if input is required in this field.

Returns:
true if input is required in this field, and false otherwise.

validateInput

public final boolean validateInput()
Validate the input in this field.

Returns:
true if the field contains valid input or if the field contains no input and input is not required, and false otherwise.

checkInput

protected boolean checkInput()
Determine if the given input is valid for this field. The default implementation returns true.

Returns:
true if the input is valid, and false otherwise.