Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/theotherbell/ui/DateField.java


1   /***********************************************************
2   
3    DateField.java
4    Copyright (C) 2003 Brenda Bell
5   
6    ***********************************************************/
7   
8   /***********************************************************
9   
10   This file is part of JavaDatePicker.
11  
12   JavaDatePicker is free software; you can redistribute it
13   and/or modify it under the terms of the GNU Lesser
14   General Public License as published by the Free Software
15   Foundation; either version 2 of the License, or (at your
16   option) any later version.
17  
18   JavaDatePicker is distributed in the hope that it will
19   be useful, but WITHOUT ANY WARRANTY; without even the
20   implied warranty of MERCHANTABILITY or FITNESS FOR A
21   PARTICULAR PURPOSE.  See the GNU Lesser General Public
22   License for more details.
23  
24   You should have received a copy of the GNU Lesser
25   General Public License along with JavaDatePicker; if
26   not, write to the Free Software Foundation, Inc., 59
27   Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  
29   ***********************************************************/
30  
31  package com.theotherbell.ui;
32  
33  import java.awt.*;
34  import java.awt.event.*;
35  import java.text.DateFormat;
36  import java.text.ParseException;
37  import java.util.Date;
38  import java.util.GregorianCalendar;
39  
40  import javax.swing.*;
41  
42  import org.netbeans.lib.awtextra.AbsoluteLayout;
43  import org.netbeans.lib.awtextra.AbsoluteConstraints;
44  
45  /**
46   * GUI component that lets the user enter a date using a drop-down DatePicker.
47   * Usage is illustrated in the sample code below:
48   * <PRE>
49   *  JDialog dlg = new JDialog(new Frame(), true);
50   *  DateField df = new DateField();
51   *  dlg.getContentPane().add(df);
52   *  dlg.pack();
53   *  dlg.show();
54   *  if (null != df.getDate())
55   *    System.out.println(df.getDate().toString());
56   *  dlg.dispose();
57   *  System.exit(0);
58   * </PRE>
59   * @author B. Bell
60   * @version 1.1a
61   *
62   */
63  public final class DateField extends JPanel {
64  
65      /**
66       * Displays the currently selected date.
67       */
68      private final JTextField dateText = new JTextField();
69  
70      /**
71       * When clicked, displays the DatePicker immediately under the text field.
72       */
73      private final JButton dropdownButton = new JButton();
74  
75      /**
76       * The current DatePicker instance.
77       */
78      private com.theotherbell.ui.DatePicker dp;
79  
80      /**
81       * The DatePicker's container.
82       */
83      private JDialog dlg;
84  
85      /**
86       * Listener that will catch the selected date when the DatePicker is hidden.
87       */
88      final class Listener extends ComponentAdapter {
89  
90          /**
91           * Event handler that catches the selected date when the DatePicker is
92           * hidden.
93           * @param evt
94           */
95          public void componentHidden(final ComponentEvent evt) {
96              final Date dt = ((com.theotherbell.ui.DatePicker) evt.getSource()).getDate();
97              if (null != dt)
98                  dateText.setText(dateToString(dt));
99              dlg.dispose();
100         }
101 
102     }
103 
104     /**
105      * Default constructor; the initially selected date is "empty".
106      */
107     public DateField() {
108         super();
109         init();
110     }
111 
112     /**
113      * Alternate constructor that initializes the currently selected date
114      * to the specified date.
115      * @param initialDate
116      */
117     public DateField(final Date initialDate) {
118         super();
119         init();
120         dateText.setText(dateToString(initialDate));
121     }
122 
123     /**
124      * Returns the currently selected date or null if not set.
125      * @return date
126      */
127     public Date getDate() {
128         return stringToDate(dateText.getText());
129     }
130 
131     /**
132      * Initializes the panel components.
133      */
134     private void init() {
135         setLayout(new AbsoluteLayout());
136 
137         dateText.setText("");
138         dateText.setEditable(false);
139         dateText.setBackground(new Color(255, 255, 255));
140         add(dateText, new AbsoluteConstraints(10, 10, 141, 20));
141 
142         dropdownButton.setText("...");
143         dropdownButton.setMargin(new Insets(2, 2, 2, 2));
144         dropdownButton.addActionListener(new ActionListener() {
145             public void actionPerformed(final ActionEvent evt) {
146                 onButtonClick(evt);
147             }
148         });
149         add(dropdownButton, new AbsoluteConstraints(151, 10, 20, 21));
150     }
151 
152     /**
153      * Event handler that displays the DatePicker when the button is
154      * clicked.
155      * @param evt
156      */
157     private void onButtonClick(final java.awt.event.ActionEvent evt) {
158         if ("".equals(dateText.getText()))
159             dp = new com.theotherbell.ui.DatePicker();
160         else
161             dp = new com.theotherbell.ui.DatePicker(stringToDate(dateText.getText()));
162         dp.setHideOnSelect(true);
163         dp.addComponentListener(new Listener());
164 
165         final Point p = dateText.getLocationOnScreen();
166         p.setLocation(p.getX(), p.getY() - 1 + dateText.getSize().getHeight());
167 
168         dlg = new JDialog(new JFrame(), true);
169         dlg.setLocation(p);
170         dlg.setResizable(false);
171         dlg.setUndecorated(true);
172         dlg.getContentPane().add(dp);
173         dlg.pack();
174         dlg.show();
175     }
176 
177     /**
178      * Returns a short string representation for the specified date (January 1, 2003).
179      * @param dt
180      * @return short string
181      */
182     private static String dateToString(final Date dt) {
183         if (null != dt)
184             return DateFormat.getDateInstance(DateFormat.LONG).format(dt);
185         return null;
186     }
187 
188     /**
189      * Constructs a Date object from a short date (January 1, 2003).
190      * @param s
191      * @return date
192      */
193     private static Date stringToDate(final String s) {
194         try {
195             return DateFormat.getDateInstance(DateFormat.LONG).parse(s);
196         } catch (ParseException e) {
197             return null;
198         }
199     }
200 
201 }