uk.co.keang.date
Class AbstractDateSelector<T>

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by uk.co.keang.date.AbstractDateSelector<T>
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible
Direct Known Subclasses:
DateSelector, IntervalSelector

public abstract class AbstractDateSelector<T>
extends javax.swing.JPanel

An abstract Date Selection GUI.
Displays a calendar style view of the days in a month. The month and year can be easily changed via drop down controls and/or by month increment buttons and the day selected by clicking on a day button.

If todays date is visible ie the panel is showing the todays month and year then the text on the button with todays day is coloured RED.

As well as being able to select a date using the mouse the following key-strokes are recognised:
LEFT: move back 1 day
RIGHT: move forward one day
UP: move back 1 week
DOWN: move forward 1 week
PAGE_UP: move back 1 month
PAGE_DOWN: move forward 1 month
HOME: move back 1 year
END: move forward 1 year
ESC: cancel any selected date and hide the panel
ENTER: retain the selected date and hide the panel

Listeners can be added to receive notification of a change of selected date and/or the selection of a day button. Factory methods are provided to:
1. Create a dialog to show the selector optionally with OK/Cancel buttons
2. Attach a popup-on-hover feature to a component to popup a date selector if the mouse hovers over the component.

The chronology of the initial date is maintained throughout the life of this object.

Version:
1.1
Author:
A.G.Docherty
See Also:
Serialized Form

Nested Class Summary
static class AbstractDateSelector.DayButton
          JToggleButton that knows what day it represents
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected static java.lang.String ACTION_NAME_DAY_DEC
           
protected static java.lang.String ACTION_NAME_DAY_INC
           
protected static java.lang.String ACTION_NAME_ENTER
           
protected static java.lang.String ACTION_NAME_ESC
           
protected static java.lang.String ACTION_NAME_MONTH_DEC
           
protected static java.lang.String ACTION_NAME_MONTH_INC
           
protected static java.lang.String ACTION_NAME_WEEK_DEC
           
protected static java.lang.String ACTION_NAME_WEEK_INC
           
protected static java.lang.String ACTION_NAME_YEAR_DEC
           
protected static java.lang.String ACTION_NAME_YEAR_INC
           
protected  org.joda.time.MutableDateTime baseDate
           
protected  org.joda.time.Chronology chronology
           
protected  org.joda.time.MutableDateTime movingDate
           
protected  java.awt.Window window
           
 
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 java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
AbstractDateSelector(org.joda.time.DateTime base, org.joda.time.DateTime end, boolean includeOkCancel)
          Construct the panel for the default locale
AbstractDateSelector(org.joda.time.DateTime base, org.joda.time.DateTime end, boolean includeOkCancel, java.util.Locale loc)
          Construct the panel
 
Method Summary
 void addDateChangedListener(javax.swing.event.ChangeListener l)
          Adds an date change listener.
 void addDayActionListener(java.awt.event.ActionListener l)
          Adds an action listener to each of the day buttons.
(package private) static javax.swing.JDialog createDialog(java.awt.Component owner, java.lang.String title, AbstractDateSelector panel)
          Displays a modal dialog allowing the selection of a date.
protected  void fireDateChangeListener()
          fires the date change event to all listeners
protected  void fireDayBtnListener(java.awt.event.ActionEvent e)
          fires the day selected event to all listeners
abstract  T getSelectedDate()
          Gets the selected date
(package private) static java.awt.Window getWindowForComponent(java.awt.Component parentComponent)
          Gets the window owner of this component
protected  void initKeyActions()
          init the key strokes that have meaning for this panel
 boolean isShowDayNames()
          Gets the Show Day Names display state
 boolean isShowOkCancelPanel()
          Gets the Show OK Cancel Panel
 boolean isShowSurroundingDays()
          Gets the Show Surrounding Days display state
 boolean isShowWeekNums()
          Gets the Show Week Numbers state
 void removeDateChangedListener(javax.swing.event.ChangeListener l)
          Removes the date change listener
 void removeDayActionListener(java.awt.event.ActionListener l)
          Removes the action listener from each of the day buttons.
(package private) static void removePopupOnHoverFromComponent(java.awt.Component owner)
          Removes the Popup on hover functionality from this component
 void setCancelBtnAction(javax.swing.Action a)
          Sets the action for the Cancel button
 void setFont(java.awt.Font f)
          Sets the Font for all the components.
 void setOkBtnAction(javax.swing.Action a)
          Sets the action for the OK button
(package private) static void setPopupOnHoverForComponent(java.awt.Component owner, java.util.concurrent.Callable callBack)
          Adds a pop up listener to this component.
abstract  void setSelectedDate(T date)
          Sets the selected date.
 void setShowDayNames(boolean show)
          Sets the Show Day Names display state
 void setShowOkCancelPanel(boolean show)
          Sets the Show Ok Cancel Panel state
 void setShowSurroundingDays(boolean show)
          Sets the Show Surrounding Days display state
 void setShowWeekNums(boolean show)
          Sets the Show Week Numbers state
protected  void updateOnSettingSelectedDate()
           
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
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, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, 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, addInputMethodListener, 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, getInputMethodRequests, 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, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, 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

ACTION_NAME_ENTER

protected static final java.lang.String ACTION_NAME_ENTER
See Also:
Constant Field Values

ACTION_NAME_ESC

protected static final java.lang.String ACTION_NAME_ESC
See Also:
Constant Field Values

ACTION_NAME_YEAR_INC

protected static final java.lang.String ACTION_NAME_YEAR_INC
See Also:
Constant Field Values

ACTION_NAME_YEAR_DEC

protected static final java.lang.String ACTION_NAME_YEAR_DEC
See Also:
Constant Field Values

ACTION_NAME_MONTH_INC

protected static final java.lang.String ACTION_NAME_MONTH_INC
See Also:
Constant Field Values

ACTION_NAME_MONTH_DEC

protected static final java.lang.String ACTION_NAME_MONTH_DEC
See Also:
Constant Field Values

ACTION_NAME_WEEK_INC

protected static final java.lang.String ACTION_NAME_WEEK_INC
See Also:
Constant Field Values

ACTION_NAME_WEEK_DEC

protected static final java.lang.String ACTION_NAME_WEEK_DEC
See Also:
Constant Field Values

ACTION_NAME_DAY_INC

protected static final java.lang.String ACTION_NAME_DAY_INC
See Also:
Constant Field Values

ACTION_NAME_DAY_DEC

protected static final java.lang.String ACTION_NAME_DAY_DEC
See Also:
Constant Field Values

window

protected java.awt.Window window

baseDate

protected org.joda.time.MutableDateTime baseDate

movingDate

protected org.joda.time.MutableDateTime movingDate

chronology

protected org.joda.time.Chronology chronology
Constructor Detail

AbstractDateSelector

public AbstractDateSelector(org.joda.time.DateTime base,
                            org.joda.time.DateTime end,
                            boolean includeOkCancel)
Construct the panel for the default locale

Parameters:
base - - the base date
end - - the end date, this may be before or after the base date
includeOkCancel - - true to include a panel with OK and Cancel buttons

AbstractDateSelector

public AbstractDateSelector(org.joda.time.DateTime base,
                            org.joda.time.DateTime end,
                            boolean includeOkCancel,
                            java.util.Locale loc)
Construct the panel

Parameters:
base - - the base date
end - - the end date, this may be before or after the base date
includeOkCancel - - true to include a panel with OK and Cancel buttons
loc - - the locale to use or null for the default locale
Method Detail

setPopupOnHoverForComponent

static void setPopupOnHoverForComponent(java.awt.Component owner,
                                        java.util.concurrent.Callable callBack)
Adds a pop up listener to this component. When the mouse hovers over the component a DateTimeSelector popup will display. The panel will close when: 1. a day button is selected 2. the mouse moves out of the panel 3. the ESCAPE key is pressed 4. the ENTER key is pressed

The given listener will be called only if the panel closes due to a day being selected or the ENTER key is pressed

Parameters:
owner - - the component to add the hover popup listener to
callBack - - a callable object that will be called when a day button is selected

removePopupOnHoverFromComponent

static void removePopupOnHoverFromComponent(java.awt.Component owner)
Removes the Popup on hover functionality from this component

Parameters:
owner - - the component

createDialog

static javax.swing.JDialog createDialog(java.awt.Component owner,
                                        java.lang.String title,
                                        AbstractDateSelector panel)
Displays a modal dialog allowing the selection of a date. This method is provided to allow a Calendar pane to be constructed, configured and then displayed.

Parameters:
owner - - the owning component or null
title - - The title to display in the dialog frame or null for no title
panel - - the panel
Returns:
the selected date or null if cancel pressed

getWindowForComponent

static java.awt.Window getWindowForComponent(java.awt.Component parentComponent)
                                      throws java.awt.HeadlessException
Gets the window owner of this component

Parameters:
parentComponent - - the component
Returns:
The window containing this component
Throws:
java.awt.HeadlessException - if there is no window owner

initKeyActions

protected void initKeyActions()
init the key strokes that have meaning for this panel


getSelectedDate

public abstract T getSelectedDate()
Gets the selected date

Returns:
the selected date or null if cancel was pressed

setSelectedDate

public abstract void setSelectedDate(T date)
Sets the selected date. Once the values have been set the overriding method should call updateOnSettingSelectedDate() to update the display.

Parameters:
date - - the selected date

updateOnSettingSelectedDate

protected void updateOnSettingSelectedDate()

isShowWeekNums

public boolean isShowWeekNums()
Gets the Show Week Numbers state

Returns:
true if the panel will display week numbers

setShowWeekNums

public void setShowWeekNums(boolean show)
Sets the Show Week Numbers state

Parameters:
show - - true to display week numbers, false to hide them

isShowDayNames

public boolean isShowDayNames()
Gets the Show Day Names display state

Returns:
true if the panel will display Day Names

setShowDayNames

public void setShowDayNames(boolean show)
Sets the Show Day Names display state

Parameters:
show - - true to display Day Names, false to hide them

isShowSurroundingDays

public boolean isShowSurroundingDays()
Gets the Show Surrounding Days display state

Returns:
true if the panel will display the surrounding days from neighbouring months

setShowSurroundingDays

public void setShowSurroundingDays(boolean show)
Sets the Show Surrounding Days display state

Parameters:
show - - true to display Surrounding Days, false to hide them

isShowOkCancelPanel

public boolean isShowOkCancelPanel()
Gets the Show OK Cancel Panel

Returns:
true if the panel will display OK & Cancel Panel

setShowOkCancelPanel

public void setShowOkCancelPanel(boolean show)
Sets the Show Ok Cancel Panel state

Parameters:
show - - true to display OK Cancel Panel

addDayActionListener

public void addDayActionListener(java.awt.event.ActionListener l)
Adds an action listener to each of the day buttons. This listener will only be called when a DayButton is pressed. Changes to the month or year will not cause this event to be fired.

Parameters:
l - - the listener to add

removeDayActionListener

public void removeDayActionListener(java.awt.event.ActionListener l)
Removes the action listener from each of the day buttons.

Parameters:
l - - the listener to remove

addDateChangedListener

public void addDateChangedListener(javax.swing.event.ChangeListener l)
Adds an date change listener. This listener will be called whenever the date changes. This can be through selecting a new day button, changing the month or the changing the year.

Parameters:
l - - the listener to add

removeDateChangedListener

public void removeDateChangedListener(javax.swing.event.ChangeListener l)
Removes the date change listener

Parameters:
l - - the listener to remove

fireDateChangeListener

protected void fireDateChangeListener()
fires the date change event to all listeners


fireDayBtnListener

protected void fireDayBtnListener(java.awt.event.ActionEvent e)
fires the day selected event to all listeners

Parameters:
e - - the event to fire

setFont

public void setFont(java.awt.Font f)
Sets the Font for all the components. After changing the font size you may need to call pack() to resize the dialog

Overrides:
setFont in class javax.swing.JComponent
Parameters:
f - - the new font

setOkBtnAction

public void setOkBtnAction(javax.swing.Action a)
Sets the action for the OK button

Parameters:
a - - the action to use

setCancelBtnAction

public void setCancelBtnAction(javax.swing.Action a)
Sets the action for the Cancel button

Parameters:
a - - the action to use