edu.wisc.ssec.mcidasv.data.dateChooser
Class JTextFieldDateEditor

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JTextField
                      extended by javax.swing.JFormattedTextField
                          extended by edu.wisc.ssec.mcidasv.data.dateChooser.JTextFieldDateEditor
All Implemented Interfaces:
IDateEditor, ActionListener, FocusListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CaretListener, Scrollable, SwingConstants

public class JTextFieldDateEditor
extends JFormattedTextField
implements IDateEditor, CaretListener, FocusListener, ActionListener

JTextFieldDateEditor is the default editor used by JDateChooser. It is a formatted text field, that colores valid dates green/black and invalid dates red. The date format patten and mask can be set manually. If not set, the MEDIUM pattern of a SimpleDateFormat with regards to the actual locale is used.

Version:
$LastChangedRevision: 97 $, $LastChangedDate: 2006-05-24 17:30:41 +0200 (Mi, 24 Mai 2006) $
Author:
Kai Toedter
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JFormattedTextField
JFormattedTextField.AbstractFormatter, JFormattedTextField.AbstractFormatterFactory
 
Nested classes/interfaces inherited from class javax.swing.JTextField
JTextField.AccessibleJTextField
 
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
private  Calendar calendar
           
protected  Color darkGreen
           
protected  Date date
           
protected  SimpleDateFormat dateFormatter
           
protected  String datePattern
           
protected  DateUtil dateUtil
           
private  int hours
           
private  boolean ignoreDatePatternChange
           
private  boolean isMaskVisible
           
protected  MaskFormatter maskFormatter
           
protected  String maskPattern
           
private  int millis
           
private  int minutes
           
protected  char placeholder
           
private  int seconds
           
private static long serialVersionUID
           
 
Fields inherited from class javax.swing.JFormattedTextField
COMMIT, COMMIT_OR_REVERT, PERSIST, REVERT
 
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.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
JTextFieldDateEditor()
           
JTextFieldDateEditor(boolean showMask, String datePattern, String maskPattern, char placeholder)
           
JTextFieldDateEditor(String datePattern, String maskPattern, char placeholder)
           
 
Method Summary
 void actionPerformed(ActionEvent e)
          Validates the typed date and sets it (only if it is valid).
 void caretUpdate(CaretEvent event)
          After any user input, the value of the textfield is proofed.
private  void checkText()
           
 String createMaskFromDatePattern(String datePattern)
          Creates a mask from a date pattern.
 void focusGained(FocusEvent e)
           
 void focusLost(FocusEvent focusEvent)
           
 Date getDate()
          Returns the date.
 String getDateFormatString()
          Returns tha date format string.
 Date getMaxSelectableDate()
          Gets the minimum selectable date.
 Date getMinSelectableDate()
          Gets the maximum selectable date.
 Dimension getPreferredSize()
          Returns the preferred size.
 JComponent getUiComponent()
          Returns the UI component, e.g. the actual JTextField implementing the editor.
 boolean isMaskVisible()
          Returns true, if the mask is visible.
static void main(String[] s)
          Creates a JFrame with a JCalendar inside and can be used for testing.
 void setDate(Date date)
          Sets the date.
protected  void setDate(Date date, boolean firePropertyChange)
          Sets the date.
 void setDateFormatString(String dateFormatString)
          Sets the date format string, e.g.
 void setEnabled(boolean b)
          Enables and disabled the compoment.
 void setLocale(Locale locale)
          Sets the locale.
 void setMaskVisible(boolean isMaskVisible)
          Sets the mask visible.
 void setMaxSelectableDate(Date max)
          Sets the maximum selectable date.
 void setMinSelectableDate(Date min)
          Sets the minimum selectable date.
 void setSelectableDateRange(Date min, Date max)
          Sets a valid date range for selectable dates.
 
Methods inherited from class javax.swing.JFormattedTextField
commitEdit, getActions, getFocusLostBehavior, getFormatter, getFormatterFactory, getUIClassID, getValue, invalidEdit, isEditValid, processFocusEvent, processInputMethodEvent, setDocument, setFocusLostBehavior, setFormatter, setFormatterFactory, setValue
 
Methods inherited from class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getScrollOffset, 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, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, 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, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, 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, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, 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
 
Methods inherited from interface edu.wisc.ssec.mcidasv.data.dateChooser.IDateEditor
addPropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, removePropertyChangeListener
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

date

protected Date date

dateFormatter

protected SimpleDateFormat dateFormatter

maskFormatter

protected MaskFormatter maskFormatter

datePattern

protected String datePattern

maskPattern

protected String maskPattern

placeholder

protected char placeholder

darkGreen

protected Color darkGreen

dateUtil

protected DateUtil dateUtil

isMaskVisible

private boolean isMaskVisible

ignoreDatePatternChange

private boolean ignoreDatePatternChange

hours

private int hours

minutes

private int minutes

seconds

private int seconds

millis

private int millis

calendar

private Calendar calendar
Constructor Detail

JTextFieldDateEditor

public JTextFieldDateEditor()

JTextFieldDateEditor

public JTextFieldDateEditor(String datePattern,
                            String maskPattern,
                            char placeholder)

JTextFieldDateEditor

public JTextFieldDateEditor(boolean showMask,
                            String datePattern,
                            String maskPattern,
                            char placeholder)
Method Detail

getDate

public Date getDate()
Description copied from interface: IDateEditor
Returns the date.

Specified by:
getDate in interface IDateEditor
Returns:
the date

setDate

public void setDate(Date date)
Description copied from interface: IDateEditor
Sets the date. This should be implemented as a bound property, firing the "date" property.

Specified by:
setDate in interface IDateEditor
Parameters:
date - the date to set

setDate

protected void setDate(Date date,
                       boolean firePropertyChange)
Sets the date.

Parameters:
date - the date
firePropertyChange - true, if the date property should be fired.

setDateFormatString

public void setDateFormatString(String dateFormatString)
Description copied from interface: IDateEditor
Sets the date format string, e.g. "MM/dd/yy". If the date format string is null or invalid, the date format string will be set to the MEDIUM Simple date format of the current locale.

Specified by:
setDateFormatString in interface IDateEditor
Parameters:
dateFormatString - the date format string

getDateFormatString

public String getDateFormatString()
Description copied from interface: IDateEditor
Returns tha date format string.

Specified by:
getDateFormatString in interface IDateEditor
Returns:
the date format string

getUiComponent

public JComponent getUiComponent()
Description copied from interface: IDateEditor
Returns the UI component, e.g. the actual JTextField implementing the editor.

Specified by:
getUiComponent in interface IDateEditor
Returns:
the UI component

caretUpdate

public void caretUpdate(CaretEvent event)
After any user input, the value of the textfield is proofed. Depending on being a valid date, the value is colored green or red.

Specified by:
caretUpdate in interface CaretListener
Parameters:
event - the caret event

focusLost

public void focusLost(FocusEvent focusEvent)
Specified by:
focusLost in interface FocusListener

checkText

private void checkText()

focusGained

public void focusGained(FocusEvent e)
Specified by:
focusGained in interface FocusListener

setLocale

public void setLocale(Locale locale)
Description copied from interface: IDateEditor
Sets the locale. Usually this should have impact on the current date format string.

Specified by:
setLocale in interface IDateEditor
Overrides:
setLocale in class Component
Parameters:
locale - the locale to set

createMaskFromDatePattern

public String createMaskFromDatePattern(String datePattern)
Creates a mask from a date pattern. This is a very simple (and incomplete) implementation thet works only with numbers. A date pattern of "MM/dd/yy" will result in the mask "##/##/##". Probably you want to override this method if it does not fit your needs.

Parameters:
datePattern - the date pattern
Returns:
the mask

isMaskVisible

public boolean isMaskVisible()
Returns true, if the mask is visible.

Returns:
true, if the mask is visible

setMaskVisible

public void setMaskVisible(boolean isMaskVisible)
Sets the mask visible.

Parameters:
isMaskVisible - true, if the mask should be visible

getPreferredSize

public Dimension getPreferredSize()
Returns the preferred size. If a date pattern is set, it is the size the date pattern would take.

Overrides:
getPreferredSize in class JTextField

actionPerformed

public void actionPerformed(ActionEvent e)
Validates the typed date and sets it (only if it is valid).

Specified by:
actionPerformed in interface ActionListener

setEnabled

public void setEnabled(boolean b)
Enables and disabled the compoment. It also fixes the background bug 4991597 and sets the background explicitely to a TextField.inactiveBackground.

Specified by:
setEnabled in interface IDateEditor
Overrides:
setEnabled in class JComponent
Parameters:
b - true, if the UI component should be enabled.

getMaxSelectableDate

public Date getMaxSelectableDate()
Description copied from interface: IDateEditor
Gets the minimum selectable date.

Specified by:
getMaxSelectableDate in interface IDateEditor
Returns:
the minimum selectable date

getMinSelectableDate

public Date getMinSelectableDate()
Description copied from interface: IDateEditor
Gets the maximum selectable date.

Specified by:
getMinSelectableDate in interface IDateEditor
Returns:
the maximum selectable date

setMaxSelectableDate

public void setMaxSelectableDate(Date max)
Description copied from interface: IDateEditor
Sets the maximum selectable date.

Specified by:
setMaxSelectableDate in interface IDateEditor
Parameters:
max - maximum selectable date

setMinSelectableDate

public void setMinSelectableDate(Date min)
Description copied from interface: IDateEditor
Sets the minimum selectable date.

Specified by:
setMinSelectableDate in interface IDateEditor
Parameters:
min - minimum selectable date

setSelectableDateRange

public void setSelectableDateRange(Date min,
                                   Date max)
Description copied from interface: IDateEditor
Sets a valid date range for selectable dates. If max is before min, the default range with no limitation is set.

Specified by:
setSelectableDateRange in interface IDateEditor
Parameters:
min - the minimum selectable date or null (then the minimum date should be set to 01\01\0001)
max - the maximum selectable date or null (then the maximum date should be set to 01\01\9999)

main

public static void main(String[] s)
Creates a JFrame with a JCalendar inside and can be used for testing.

Parameters:
s - The command line arguments