org.bibalex.daf.commongui.controls
Class DateChooser

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Dialog
                  extended by javax.swing.JDialog
                      extended by org.bibalex.daf.commongui.controls.DateChooser
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.FocusListener, java.awt.event.ItemListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants

public class DateChooser
extends javax.swing.JDialog
implements java.awt.event.ItemListener, java.awt.event.MouseListener, java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.ActionListener

Custom dialog box to enter dates. The DateChooser class presents a calendar and allows the user to visually select a day, month and year so that it is impossible to enter an invalid date.

See Also:
Serialized Form

Nested Class Summary
private static class DateChooser.FocusablePanel
          Custom panel that can receive the focus.
 
Nested classes/interfaces inherited from class javax.swing.JDialog
javax.swing.JDialog.AccessibleJDialog
 
Nested classes/interfaces inherited from class java.awt.Dialog
java.awt.Dialog.AccessibleAWTDialog
 
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
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.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  java.util.GregorianCalendar calendar
          Auxiliary variable to compute dates.
private  javax.swing.JButton cancel
          "Cancel" button.
private  javax.swing.JLabel day
          Selected day.
private  javax.swing.JLabel[][] days
          Calendar, as a matrix of labels.
private static java.lang.String[] DAYS
          Names of the days of the week.
private static java.awt.Color DAYS_FOREGROUND
          Text color of the days' numbers in the calendar.
private  DateChooser.FocusablePanel daysGrid
          Day selection control.
private static javax.swing.border.Border EMPTY_BORDER
          Empty border, used when the calendar does not have the focus.
private static int FIRST_YEAR
          First year that can be selected.
private static javax.swing.border.Border FOCUSED_BORDER
          Border used to highlight the selected day when the calendar has the focus.
private static int LAST_YEAR
          Last year that can be selected.
private  int lastDay
          Last day of the selected month.
private  javax.swing.JComboBox month
          Month selection control.
private static java.lang.String[] MONTHS
          Names of the months.
private  int offset
          Day of the week (0=Sunday) corresponding to the first day of the selected month.
private  javax.swing.JButton ok
          "Ok" button.
private  boolean okClicked
          true if the "Ok" button was clicked to close the dialog box, false otherwise.
private static java.awt.Color SELECTED_DAY_BACKGROUND
          Text color of the selected day in the calendar.
private static java.awt.Color SELECTED_DAY_FOREGROUND
          Background color of the selected day in the calendar.
private static java.awt.Color WEEK_DAYS_FOREGROUND
          Text color of the days of the weeks, used as column headers in the calendar.
private  javax.swing.JComboBox year
          Year selection control.
 
Fields inherited from class javax.swing.JDialog
accessibleContext, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, EXIT_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DateChooser(java.awt.Dialog owner)
          Constructs a new DateChooser.
DateChooser(java.awt.Dialog owner, java.lang.String title)
          Constructs a new DateChooser with the given title.
DateChooser(java.awt.Frame owner)
          Constructs a new DateChooser.
DateChooser(java.awt.Frame owner, java.lang.String title)
          Constructs a new DateChooser with the given title.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Called when the "Ok" button is pressed.
private  void construct()
          Initializes this DateChooser object.
 void focusGained(java.awt.event.FocusEvent e)
          Called when the calendar gains the focus.
 void focusLost(java.awt.event.FocusEvent e)
          Called when the calendar loses the focus.
private  int getSelectedDay()
          Gets the selected day, as an int.
 void itemStateChanged(java.awt.event.ItemEvent e)
          Called when a new month or year is selected.
 void keyPressed(java.awt.event.KeyEvent e)
          Called when a key is pressed and the calendar has the focus.
 void keyReleased(java.awt.event.KeyEvent e)
          (non-Javadoc)
 void keyTyped(java.awt.event.KeyEvent e)
          (non-Javadoc)
 void mouseClicked(java.awt.event.MouseEvent e)
          Called when the mouse is clicked on a day in the calendar.
 void mouseEntered(java.awt.event.MouseEvent e)
          (non-Javadoc)
 void mouseExited(java.awt.event.MouseEvent e)
          (non-Javadoc)
 void mousePressed(java.awt.event.MouseEvent e)
          (non-Javadoc)
 void mouseReleased(java.awt.event.MouseEvent e)
          (non-Javadoc)
 java.util.Date select()
          Selects new date.
 java.util.Date select(java.util.Date date)
          Selects a date.
private  void setSelected(int newDay)
          Sets the selected day.
private  void setSelected(javax.swing.JLabel newDay)
          Sets the selected day.
private  void update()
          Updates the calendar.
 
Methods inherited from class javax.swing.JDialog
addImpl, createRootPane, dialogInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update
 
Methods inherited from class java.awt.Dialog
addNotify, getTitle, hide, isModal, isResizable, isUndecorated, setModal, setResizable, setTitle, setUndecorated, show
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, finalize, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getMostRecentFocusOwner, getOwnedWindows, getOwner, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindowStateListeners, isActive, isAlwaysOnTop, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setAlwaysOnTop, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationByPlatform, setLocationRelativeTo, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, 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, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MONTHS

private static final java.lang.String[] MONTHS
Names of the months.


DAYS

private static final java.lang.String[] DAYS
Names of the days of the week.


WEEK_DAYS_FOREGROUND

private static final java.awt.Color WEEK_DAYS_FOREGROUND
Text color of the days of the weeks, used as column headers in the calendar.


DAYS_FOREGROUND

private static final java.awt.Color DAYS_FOREGROUND
Text color of the days' numbers in the calendar.


SELECTED_DAY_FOREGROUND

private static final java.awt.Color SELECTED_DAY_FOREGROUND
Background color of the selected day in the calendar.


SELECTED_DAY_BACKGROUND

private static final java.awt.Color SELECTED_DAY_BACKGROUND
Text color of the selected day in the calendar.


EMPTY_BORDER

private static final javax.swing.border.Border EMPTY_BORDER
Empty border, used when the calendar does not have the focus.


FOCUSED_BORDER

private static final javax.swing.border.Border FOCUSED_BORDER
Border used to highlight the selected day when the calendar has the focus.


FIRST_YEAR

private static final int FIRST_YEAR
First year that can be selected.

See Also:
Constant Field Values

LAST_YEAR

private static final int LAST_YEAR
Last year that can be selected.

See Also:
Constant Field Values

calendar

private java.util.GregorianCalendar calendar
Auxiliary variable to compute dates.


days

private javax.swing.JLabel[][] days
Calendar, as a matrix of labels. The first row represents the first week of the month, the second row, the second week, and so on. Each column represents a day of the week, the first is Sunday, and the last is Saturday. The label's text is the number of the corresponding day.


daysGrid

private DateChooser.FocusablePanel daysGrid
Day selection control. It is just a panel that can receive the focus. The actual user interaction is driven by the DateChooser class.


month

private javax.swing.JComboBox month
Month selection control.


year

private javax.swing.JComboBox year
Year selection control.


ok

private javax.swing.JButton ok
"Ok" button.


cancel

private javax.swing.JButton cancel
"Cancel" button.


offset

private int offset
Day of the week (0=Sunday) corresponding to the first day of the selected month. Used to calculate the position, in the calendar (days), corresponding to a given day.


lastDay

private int lastDay
Last day of the selected month.


day

private javax.swing.JLabel day
Selected day.


okClicked

private boolean okClicked
true if the "Ok" button was clicked to close the dialog box, false otherwise.

Constructor Detail

DateChooser

public DateChooser(java.awt.Dialog owner,
                   java.lang.String title)
Constructs a new DateChooser with the given title.

Parameters:
owner - owner dialog
title - dialog title

DateChooser

public DateChooser(java.awt.Dialog owner)
Constructs a new DateChooser.

Parameters:
owner - owner dialog

DateChooser

public DateChooser(java.awt.Frame owner,
                   java.lang.String title)
Constructs a new DateChooser with the given title.

Parameters:
owner - owner frame
title - dialog title

DateChooser

public DateChooser(java.awt.Frame owner)
Constructs a new DateChooser.

Parameters:
owner - owner frame
Method Detail

construct

private void construct()
Initializes this DateChooser object. Creates the controls, registers listeners and initializes the dialog box.


getSelectedDay

private int getSelectedDay()
Gets the selected day, as an int. Parses the text of the selected label in the calendar to get the day.

Returns:
the selected day or -1 if there is no day selected

setSelected

private void setSelected(javax.swing.JLabel newDay)
Sets the selected day. The day is specified as the label control, in the calendar, corresponding to the day to select.

Parameters:
newDay - day to select

setSelected

private void setSelected(int newDay)
Sets the selected day. The day is specified as the number of the day, in the month, to selected. The function compute the corresponding control to select.

Parameters:
newDay - day to select

update

private void update()
Updates the calendar. This function updates the calendar panel to reflect the month and year selected. It keeps the same day of the month that was selected, except if it is beyond the last day of the month. In this case, the last day of the month is selected.


actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Called when the "Ok" button is pressed. Just sets a flag and hides the dialog box.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - The ActionEvent that occured.

focusGained

public void focusGained(java.awt.event.FocusEvent e)
Called when the calendar gains the focus. Just re-sets the selected day so that it is redrawn with the border that indicate focus.

Specified by:
focusGained in interface java.awt.event.FocusListener
Parameters:
e - The FocusEvent that occured.

focusLost

public void focusLost(java.awt.event.FocusEvent e)
Called when the calendar loses the focus. Just re-sets the selected day so that it is redrawn without the border that indicate focus.

Specified by:
focusLost in interface java.awt.event.FocusListener
Parameters:
e - The FocusEvent that occured.

itemStateChanged

public void itemStateChanged(java.awt.event.ItemEvent e)
Called when a new month or year is selected. Updates the calendar to reflect the selection.

Specified by:
itemStateChanged in interface java.awt.event.ItemListener
Parameters:
e - The ItemEvent that occured.

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Called when a key is pressed and the calendar has the focus. Handles the arrow keys so that the user can select a day using the keyboard.

Specified by:
keyPressed in interface java.awt.event.KeyListener
Parameters:
e - The KeyEvent that occured.

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Called when the mouse is clicked on a day in the calendar. Selects the clicked day.

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - The MouseEvent that occured.

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
(non-Javadoc)

Specified by:
keyReleased in interface java.awt.event.KeyListener
See Also:
KeyListener.keyReleased(java.awt.event.KeyEvent)

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
(non-Javadoc)

Specified by:
keyTyped in interface java.awt.event.KeyListener
See Also:
KeyListener.keyTyped(java.awt.event.KeyEvent)

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
(non-Javadoc)

Specified by:
mouseEntered in interface java.awt.event.MouseListener
See Also:
MouseListener.mouseEntered(java.awt.event.MouseEvent)

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
(non-Javadoc)

Specified by:
mouseExited in interface java.awt.event.MouseListener
See Also:
MouseListener.mouseExited(java.awt.event.MouseEvent)

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
(non-Javadoc)

Specified by:
mousePressed in interface java.awt.event.MouseListener
See Also:
MouseListener.mousePressed(java.awt.event.MouseEvent)

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
(non-Javadoc)

Specified by:
mouseReleased in interface java.awt.event.MouseListener
See Also:
MouseListener.mouseReleased(java.awt.event.MouseEvent)

select

public java.util.Date select(java.util.Date date)
Selects a date. Displays the dialog box, with a given date as the selected date, and allows the user select a new date.

Parameters:
date - initial date
Returns:
the new date selected or null if the user press "Cancel" or closes the dialog box

select

public java.util.Date select()
Selects new date. Just calls select(Date) with the system date as the parameter.

Returns:
the same as the function select(Date)