com.gsoft.titration.client
Class DateChooser

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--java.awt.Dialog
                          |
                          +--javax.swing.JDialog
                                |
                                +--com.gsoft.titration.client.DateChooser

public class DateChooser
extends JDialog
implements ActionListener, FocusListener, ItemListener, KeyListener, MouseListener

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.


Inner Class Summary
 private static classDateChooser.FocusablePanel
          Custom panel that can receive the focus.

Field Summary
 private GregorianCalendarcalendar
          Auxiliary variable to compute dates.
 private JButtoncancel
          "Cancel" button.
 private JLabelday
          Selected day.
 private final static String[]DAYS
          Names of the days of the week.
 private JLabel[][]days
          Calendar, as a matrix of labels.
 private final static ColorDAYS_FOREGROUND
          Text color of the days' numbers in the calendar.
 private FocusablePaneldaysGrid
          Day selection control.
 private final static BorderEMPTY_BORDER
          Empty border, used when the calendar does not have the focus.
 private final static intFIRST_YEAR
          First year that can be selected.
 private final static BorderFOCUSED_BORDER
          Border used to highlight the selected day when the calendar
has the focus.
 private final static intLAST_YEAR
          Last year that can be selected.
 private intlastDay
          Last day of the selected month.
 private JComboBoxmonth
          Month selection control.
 private final static String[]MONTHS
          Names of the months.
 private intoffset
          Day of the week (0=Sunday) corresponding to the first day of
the selected month.
 private JButtonok
          "Ok" button.
 private booleanokClicked
          true if the "Ok" button was clicked to close the
dialog box, false otherwise.
 private final static ColorSELECTED_DAY_BACKGROUND
          Text color of the selected day in the calendar.
 private final static ColorSELECTED_DAY_FOREGROUND
          Background color of the selected day in the calendar.
 private final static ColorWEEK_DAYS_FOREGROUND
          Text color of the days of the weeks, used as column headers in
the calendar.
 private JComboBoxyear
          Year selection control.

Fields inherited from class javax.swing.JDialog
rootPane, rootPaneCheckingEnabled, accessibleContext

Fields inherited from class java.awt.Component
TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT

Constructor Summary
DateChooser(Dialog owner, String title)
          Constructs a new DateChooser with the given title.
DateChooser(Dialog owner)
          Constructs a new DateChooser.
DateChooser(Frame owner, String title)
          Constructs a new DateChooser with the given title.
DateChooser(Frame owner)
          Constructs a new DateChooser.

Method Summary
 voidactionPerformed(ActionEvent e)
          Called when the "Ok" button is pressed.
 private voidconstruct()
          Initializes this DateChooser object.
 voidfocusGained(FocusEvent e)
          Called when the calendar gains the focus.
 voidfocusLost(FocusEvent e)
          Called when the calendar loses the focus.
 private intgetSelectedDay()
          Gets the selected day, as an int.
 voiditemStateChanged(ItemEvent e)
          Called when a new month or year is selected.
 voidkeyPressed(KeyEvent e)
          Called when a key is pressed and the calendar has the
focus.
 voidkeyReleased(KeyEvent e)
           
 voidkeyTyped(KeyEvent e)
           
 voidmouseClicked(MouseEvent e)
          Called when the mouse is clicked on a day in the
calendar.
 voidmouseEntered(MouseEvent e)
           
 voidmouseExited(MouseEvent e)
           
 voidmousePressed(MouseEvent e)
           
 voidmouseReleased(MouseEvent e)
           
 Dateselect(Date date)
          Selects a date.
 Dateselect()
          Selects new date.
 private voidsetSelected(JLabel newDay)
          Sets the selected day.
 private voidsetSelected(int newDay)
          Sets the selected day.
 private voidupdate()
          Updates the calendar.

Methods inherited from class javax.swing.JDialog
dialogInit, createRootPane, processKeyEvent, processWindowEvent, setDefaultCloseOperation, getDefaultCloseOperation, update, setJMenuBar, getJMenuBar, isRootPaneCheckingEnabled, setRootPaneCheckingEnabled, addImpl, remove, setLayout, getRootPane, setRootPane, getContentPane, setContentPane, getLayeredPane, setLayeredPane, getGlassPane, setGlassPane, setLocationRelativeTo, paramString, getAccessibleContext

Methods inherited from class java.awt.Dialog
addNotify, isModal, setModal, getTitle, setTitle, show, hide, dispose, isResizable, setResizable

Methods inherited from class java.awt.Window
finalize, pack, toFront, toBack, getToolkit, getWarningString, getLocale, getInputContext, setCursor, getOwner, getOwnedWindows, addWindowListener, removeWindowListener, getListeners, processEvent, getFocusOwner, postEvent, isShowing, applyResourceBundle, applyResourceBundle, getGraphicsConfiguration

Methods inherited from class java.awt.Container
getComponentCount, countComponents, getComponent, getComponents, getInsets, insets, add, add, add, add, add, remove, removeAll, getLayout, doLayout, layout, invalidate, validate, validateTree, setFont, getPreferredSize, preferredSize, getMinimumSize, minimumSize, getMaximumSize, getAlignmentX, getAlignmentY, paint, print, paintComponents, printComponents, addContainerListener, removeContainerListener, processContainerEvent, deliverEvent, getComponentAt, locate, getComponentAt, findComponentAt, findComponentAt, removeNotify, isAncestorOf, list, list

Methods inherited from class java.awt.Component
getName, setName, getParent, getPeer, setDropTarget, getDropTarget, getTreeLock, isValid, isDisplayable, isVisible, isEnabled, setEnabled, enable, enable, disable, isDoubleBuffered, enableInputMethods, setVisible, show, getForeground, setForeground, getBackground, setBackground, getFont, setLocale, getColorModel, getLocation, getLocationOnScreen, location, setLocation, move, setLocation, getSize, size, setSize, resize, setSize, resize, getBounds, bounds, setBounds, reshape, setBounds, getX, getY, getWidth, getHeight, getBounds, getSize, getLocation, isOpaque, isLightweight, getGraphics, getFontMetrics, getCursor, paintAll, repaint, repaint, repaint, repaint, printAll, imageUpdate, createImage, createImage, prepareImage, prepareImage, checkImage, checkImage, contains, inside, contains, dispatchEvent, addComponentListener, removeComponentListener, addFocusListener, removeFocusListener, addHierarchyListener, removeHierarchyListener, addHierarchyBoundsListener, removeHierarchyBoundsListener, addKeyListener, removeKeyListener, addMouseListener, removeMouseListener, addMouseMotionListener, removeMouseMotionListener, addInputMethodListener, removeInputMethodListener, getInputMethodRequests, enableEvents, disableEvents, coalesceEvents, processComponentEvent, processFocusEvent, processMouseEvent, processMouseMotionEvent, processInputMethodEvent, processHierarchyEvent, processHierarchyBoundsEvent, handleEvent, mouseDown, mouseDrag, mouseUp, mouseMove, mouseEnter, mouseExit, keyDown, keyUp, action, gotFocus, lostFocus, isFocusTraversable, requestFocus, transferFocus, nextFocus, hasFocus, add, remove, toString, list, list, list, addPropertyChangeListener, removePropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, firePropertyChange, setComponentOrientation, getComponentOrientation

Methods inherited from class java.lang.Object
getClass, hashCode, equals, clone, notify, notifyAll, wait, wait, wait

Field Detail

calendar

private GregorianCalendar calendar
Auxiliary variable to compute dates.

cancel

private JButton cancel
"Cancel" button.

day

private JLabel day
Selected day.

DAYS

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

days

private 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.

DAYS_FOREGROUND

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

daysGrid

private 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.

EMPTY_BORDER

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

FIRST_YEAR

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

FOCUSED_BORDER

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

LAST_YEAR

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

lastDay

private int lastDay
Last day of the selected month.

month

private JComboBox month
Month selection control.

MONTHS

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

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.

ok

private JButton ok
"Ok" button.

okClicked

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

SELECTED_DAY_BACKGROUND

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

SELECTED_DAY_FOREGROUND

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

WEEK_DAYS_FOREGROUND

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

year

private JComboBox year
Year selection control.
Constructor Detail

DateChooser

public DateChooser(Dialog owner, String title)
Constructs a new DateChooser with the given title.
Parameters:
owner owner dialog
title dialog title

DateChooser

public DateChooser(Dialog owner)
Constructs a new DateChooser.
Parameters:
owner owner dialog

DateChooser

public DateChooser(Frame owner, String title)
Constructs a new DateChooser with the given title.
Parameters:
owner owner frame
title dialog title

DateChooser

public DateChooser(Frame owner)
Constructs a new DateChooser.
Parameters:
owner owner frame
Method Detail

actionPerformed

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

construct

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

focusGained

public void focusGained(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.

focusLost

public void focusLost(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.

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

itemStateChanged

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

keyPressed

public void keyPressed(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.

keyReleased

public void keyReleased(KeyEvent e)

keyTyped

public void keyTyped(KeyEvent e)

mouseClicked

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

mouseEntered

public void mouseEntered(MouseEvent e)

mouseExited

public void mouseExited(MouseEvent e)

mousePressed

public void mousePressed(MouseEvent e)

mouseReleased

public void mouseReleased(MouseEvent e)

select

public Date select(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 Date select()
Selects new date. Just calls select(java.util.Date) with the
system date as the parameter.
Returns: the same as the function select(java.util.Date)

setSelected

private void setSelected(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.