KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DateChooser


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.Container JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.Font JavaDoc;
38 import java.awt.Frame JavaDoc;
39 import java.awt.GridBagConstraints JavaDoc;
40 import java.awt.GridBagLayout JavaDoc;
41 import java.awt.GridLayout JavaDoc;
42 import java.awt.Insets JavaDoc;
43 import java.awt.Point JavaDoc;
44 import java.awt.event.ActionEvent JavaDoc;
45 import java.awt.event.ActionListener JavaDoc;
46 import java.awt.event.KeyEvent JavaDoc;
47 import java.awt.event.KeyListener JavaDoc;
48 import java.awt.event.MouseEvent JavaDoc;
49 import java.awt.event.MouseListener JavaDoc;
50 import java.io.Serializable JavaDoc;
51 import java.text.SimpleDateFormat JavaDoc;
52 import java.util.Calendar JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.util.GregorianCalendar JavaDoc;
55 import java.util.HashMap JavaDoc;
56
57 import javax.swing.BorderFactory JavaDoc;
58 import javax.swing.Icon JavaDoc;
59 import javax.swing.JButton JavaDoc;
60 import javax.swing.JComponent JavaDoc;
61 import javax.swing.JDialog JavaDoc;
62 import javax.swing.JLabel JavaDoc;
63 import javax.swing.JPanel JavaDoc;
64 import javax.swing.JTextField JavaDoc;
65 import javax.swing.SwingConstants JavaDoc;
66
67 import com.genimen.djeneric.language.Messages;
68 import com.genimen.djeneric.util.DjLogger;
69
70 /**
71  * A component that combines a text field and a popup calendar. The user can
72  * select a date from the popup calendar, which appears at the user's request.
73  * If you make the date chooser editable, then the date chooser includes an
74  * editable field into which the user can type a value.
75  *
76  * <strong>Warning:</strong> Serialized objects of this class will not be
77  * compatible with future Swing releases. The current serialization support is
78  * appropriate for short term storage or RMI between applications running the
79  * same version of Swing. As of 1.4, support for long term storage of all
80  * JavaBeans <sup><font size="-2">TM</font></sup> has been added to the
81  * <code>java.beans</code> package. Please see {@link java.beans.XMLEncoder}.
82  *
83  * @beaninfo attribute: isContainer false description: A combination of a text
84  * field and a popup calendar.
85  */

86
87 public class DateChooser extends JComponent JavaDoc implements Serializable JavaDoc, ActionListener JavaDoc, MouseListener JavaDoc, KeyListener JavaDoc
88 {
89
90   private static final long serialVersionUID = 1L;
91   static boolean _onJdk13 = System.getProperty("java.specification.version").startsWith("1.3");
92
93   /*
94    * Flag which determines wether or not the text field is editable
95    *
96    * @see #isEditable
97    * @see #setEditable
98    */

99   private boolean isEditable = false;
100
101   /*
102    * The rate at which the user scrolls through the months on the popup
103    * calendar when the arrow button at the top of the popup calendar are held
104    * down. Units are in months per second.
105    *
106    * @see #getMonthScrollRate
107    * @see #setMonthScrollRate
108    */

109   private int monthScrollRate = 5;
110
111   /*
112    * The date which the user has selected. Return null of the user chose
113    * "NONE".
114    *
115    * @see #getDate
116    * @see #setDate
117    */

118   private Date JavaDoc selectedDate;
119
120   /*
121    * The format in which the selected date will appear in the text field.
122    *
123    * @see #getDateFormat
124    * @see #setDateFormat
125    * @see #getDateFormatPattern
126    * @see #setDateFormatPattern
127    */

128   private SimpleDateFormat JavaDoc returnDateFormatter = new SimpleDateFormat JavaDoc("MMM dd, yyyy");
129
130   /*
131    * Other non-accesored variables
132    */

133   private JTextField JavaDoc componentTextField;
134   private JButton JavaDoc componentButton;
135   private int componentMargin = 0;
136   private JDialog JavaDoc popupCalendar;
137   private JButton JavaDoc todayButton, noneButton;
138   private JButton JavaDoc prevButton, nextButton;
139   private javax.swing.Timer JavaDoc nextMonthTimer, prevMonthTimer;
140   private JLabel JavaDoc monthButton;
141   private SimpleDateFormat JavaDoc monthyearFormatter = new SimpleDateFormat JavaDoc("MMMM yyyy");
142   private JPanel JavaDoc dayHeaderPanel, dayPanel;
143   private JLabel JavaDoc[] dayLabel = new JLabel JavaDoc[42];
144   private Date JavaDoc todaysDate, displayedDate;
145   private GregorianCalendar JavaDoc displayedCalendar, todaysCalendar, selectedCalendar;
146   private int todaysYear, todaysMonth, todaysDay, selectedYear, selectedMonth, selectedDay;
147   private HashMap JavaDoc dayMap = new HashMap JavaDoc();
148
149   ///////////////////
150
// Constructors
151
///////////////////
152

153   /*
154    * Creates a <code> DateChooser </code> with no initial date.
155    */

156   public DateChooser()
157   {
158
159     layoutControl();
160     layoutCalendarWindow();
161   }
162
163   /*
164    * Creates a <code> DateChooser </code> with a text field containing the
165    * specified number of columns.
166    *
167    * @param cols the number of columns in the text field
168    */

169   public DateChooser(int cols)
170   {
171
172     layoutControl();
173     layoutCalendarWindow();
174     componentTextField.setColumns(cols);
175   }
176
177   /*
178    * Creates a <code> DateChooser </code> with a specified initially selected
179    * date.
180    *
181    * @param aDate the date which is initially selected and appears in the text
182    * field
183    */

184   public DateChooser(Date JavaDoc aDate)
185   {
186
187     layoutControl();
188     layoutCalendarWindow();
189
190     selectedDate = aDate;
191     selectedCalendar.setTime(selectedDate);
192     selectedYear = selectedCalendar.get(Calendar.YEAR);
193     selectedMonth = selectedCalendar.get(Calendar.MONTH);
194     selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH);
195     displayedDate = aDate;
196     displayedCalendar.setTime(displayedDate);
197     updateCalendarDisplay();
198     componentTextField.setText(returnDateFormatter.format(selectedDate));
199
200   }
201
202   /*
203    * Creates a <code> DateChooser </code> with a specified initially selected
204    * date and a text field containing the specified number of columns.
205    *
206    * @param aDate the date which is initially selected and appears in the text
207    * field @param cols the number of columns in the text field
208    */

209   public DateChooser(Date JavaDoc aDate, int cols)
210   {
211
212     layoutControl();
213     layoutCalendarWindow();
214
215     selectedDate = aDate;
216     selectedCalendar.setTime(selectedDate);
217     selectedYear = selectedCalendar.get(Calendar.YEAR);
218     selectedMonth = selectedCalendar.get(Calendar.MONTH);
219     selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH);
220     displayedDate = aDate;
221     displayedCalendar.setTime(displayedDate);
222     updateCalendarDisplay();
223     componentTextField.setText(returnDateFormatter.format(selectedDate));
224     componentTextField.setColumns(cols);
225
226   }
227
228   ///////////////////
229
// Public Methods
230
///////////////////
231

232   public Date JavaDoc getDate()
233   {
234     return selectedDate;
235   }
236
237   public SimpleDateFormat JavaDoc getDateFormat()
238   {
239     return returnDateFormatter;
240   }
241
242   public String JavaDoc getDateFormatPattern()
243   {
244     String JavaDoc myPattern = returnDateFormatter.toPattern();
245     return myPattern;
246   }
247
248   public int getMonthScrollRate()
249   {
250     return monthScrollRate;
251   }
252
253   public Font JavaDoc getPopupFont()
254   {
255     Font JavaDoc myFont = monthButton.getFont();
256     return myFont;
257   }
258
259   public Font JavaDoc getTextFont()
260   {
261     Font JavaDoc myFont = componentTextField.getFont();
262     return myFont;
263   }
264
265   public boolean isEditable()
266   {
267     return isEditable;
268   }
269
270   public void setDate(Date JavaDoc myDate)
271   {
272     /*
273      * selectedDate = myDate; selectedCalendar.setTime(selectedDate);
274      * selectedYear = selectedCalendar.get(Calendar.YEAR); selectedMonth =
275      * selectedCalendar.get(Calendar.MONTH); selectedDay =
276      * selectedCalendar.get(Calendar.DAY_OF_MONTH);
277      * componentTextField.setText(returnDateFormatter.format(selectedDate));
278      * displayedCalendar.setTime(selectedDate);
279      */

280     setDateEx(myDate);
281     componentTextField.setText(returnDateFormatter.format(myDate));
282   }
283
284   private void setDateEx(Date JavaDoc myDate)
285   {
286     selectedDate = myDate;
287     selectedCalendar.setTime(selectedDate);
288     selectedYear = selectedCalendar.get(Calendar.YEAR);
289     selectedMonth = selectedCalendar.get(Calendar.MONTH);
290     selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH);
291     displayedCalendar.setTime(selectedDate);
292     updateCalendarDisplay();
293   }
294
295   public void setDateFormat(SimpleDateFormat JavaDoc mySimpleFormat)
296   {
297     returnDateFormatter = mySimpleFormat;
298   }
299
300   public void setDateFormatPattern(String JavaDoc pattern)
301   {
302     returnDateFormatter.applyPattern(pattern);
303   }
304
305   public void setEditable(boolean myEditable)
306   {
307     componentTextField.setEditable(myEditable);
308     isEditable = myEditable;
309   }
310
311   public void setMonthScrollRate(int myMonthScrollRate)
312   {
313     monthScrollRate = myMonthScrollRate;
314   }
315
316   public void setPopupFont(Font JavaDoc myFont)
317   {
318     monthButton.setFont(myFont);
319     todayButton.setFont(myFont);
320     noneButton.setFont(myFont);
321     Component JavaDoc[] dayHeaderLabels = dayHeaderPanel.getComponents();
322     for (int i = 0; i < dayHeaderLabels.length; i++)
323     {
324       dayHeaderLabels[i].setFont(myFont);
325     }
326     Component JavaDoc[] dayLabels = dayPanel.getComponents();
327     for (int i = 0; i < dayLabels.length; i++)
328     {
329       dayLabels[i].setFont(myFont);
330     }
331   }
332
333   public void setTextFont(Font JavaDoc myFont)
334   {
335     componentTextField.setFont(myFont);
336   }
337
338   ///////////////////
339
// Listeners
340
///////////////////
341

342   public void actionPerformed(ActionEvent JavaDoc evt)
343   {
344     Object JavaDoc source = evt.getSource();
345
346     if (source == componentButton)
347     {
348       Point JavaDoc myOrigin = componentButton.getLocationOnScreen();
349       Dimension JavaDoc buttonSize = componentButton.getSize();
350       popupCalendar.pack();
351       Dimension JavaDoc mySize = popupCalendar.getSize();
352       myOrigin.translate((buttonSize.width - mySize.width), (buttonSize.height));
353       popupCalendar.setLocation(myOrigin);
354       popupCalendar.setVisible(true);
355       componentTextField.requestFocus();
356     }
357     if (source == todayButton)
358     {
359       selectedDate = todaysCalendar.getTime();
360       selectedCalendar.setTime(selectedDate);
361       selectedYear = selectedCalendar.get(Calendar.YEAR);
362       selectedMonth = selectedCalendar.get(Calendar.MONTH);
363       selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH);
364       componentTextField.setText(returnDateFormatter.format(selectedDate));
365       popupCalendar.setVisible(false);
366       displayedCalendar.setTime(selectedDate);
367       updateCalendarDisplay();
368     }
369     if (source == noneButton)
370     {
371       componentTextField.setText(null);
372       selectedDate = null;
373       popupCalendar.setVisible(false);
374       selectedYear = 0;
375       selectedMonth = 0;
376       selectedDay = 0;
377       updateCalendarDisplay();
378
379     }
380   }
381
382   public void mousePressed(MouseEvent JavaDoc evt)
383   {
384     Object JavaDoc source = evt.getSource();
385     if (dayMap.containsKey(source))
386     {
387       selectedDate = (Date JavaDoc) dayMap.get(source);
388       selectedCalendar.setTime(selectedDate);
389       selectedYear = selectedCalendar.get(Calendar.YEAR);
390       selectedMonth = selectedCalendar.get(Calendar.MONTH);
391       selectedDay = selectedCalendar.get(Calendar.DAY_OF_MONTH);
392       componentTextField.setText(returnDateFormatter.format(selectedDate));
393       popupCalendar.setVisible(false);
394       updateCalendarDisplay();
395     }
396     else if (source == prevButton)
397     {
398       displayedCalendar.roll(Calendar.MONTH, -1);
399       if (displayedCalendar.get(Calendar.MONTH) == Calendar.DECEMBER)
400       {
401         displayedCalendar.roll(Calendar.YEAR, -1);
402       }
403       updateCalendarDisplay();
404       prevMonthTimer.start();
405     }
406     else if (source == nextButton)
407     {
408       displayedCalendar.roll(Calendar.MONTH, 1);
409       if (displayedCalendar.get(Calendar.MONTH) == Calendar.JANUARY)
410       {
411         displayedCalendar.roll(Calendar.YEAR, 1);
412       }
413       updateCalendarDisplay();
414       nextMonthTimer.start();
415     }
416     else
417     {
418       // Empty
419
}
420   }
421
422   public void mouseReleased(MouseEvent JavaDoc evt)
423   {
424     Object JavaDoc source = evt.getSource();
425     if (source == prevButton)
426     {
427       prevMonthTimer.stop();
428     }
429     else if (source == nextButton)
430     {
431       nextMonthTimer.stop();
432     }
433     else
434     {
435       // Empty
436
}
437   }
438
439   public void mouseEntered(MouseEvent JavaDoc evt)
440   {
441     // Empty
442
}
443
444   public void mouseExited(MouseEvent JavaDoc evt)
445   {
446     Object JavaDoc source = evt.getSource();
447     if (source == prevButton)
448     {
449       prevMonthTimer.stop();
450     }
451     else if (source == nextButton)
452     {
453       nextMonthTimer.stop();
454     }
455     else
456     {
457       // Empty
458
}
459   }
460
461   public void mouseClicked(MouseEvent JavaDoc evt)
462   {
463     // Empty
464
}
465
466   ActionListener JavaDoc monthScrollListener = new ActionListener JavaDoc()
467                                      {
468                                        public void actionPerformed(ActionEvent JavaDoc evt)
469                                        {
470                                          Object JavaDoc source = evt.getSource();
471                                          if (source == prevMonthTimer)
472                                          {
473                                            displayedCalendar.roll(Calendar.MONTH, -1);
474                                            if (displayedCalendar.get(Calendar.MONTH) == Calendar.DECEMBER)
475                                            {
476                                              displayedCalendar.roll(Calendar.YEAR, -1);
477                                            }
478                                            updateCalendarDisplay();
479                                          }
480                                          else if (source == nextMonthTimer)
481                                          {
482                                            displayedCalendar.roll(Calendar.MONTH, 1);
483                                            if (displayedCalendar.get(Calendar.MONTH) == Calendar.JANUARY)
484                                            {
485                                              displayedCalendar.roll(Calendar.YEAR, 1);
486                                            }
487                                            updateCalendarDisplay();
488                                          }
489                                          else
490                                          {
491                                            // Empty
492
}
493                                        }
494                                      };
495
496   ///////////////////
497
// Private Methods
498
///////////////////
499

500   private void layoutControl()
501   {
502     //The visible controls
503
BorderLayout JavaDoc componentLayout = new BorderLayout JavaDoc(componentMargin, 0);
504     setLayout(componentLayout);
505     componentTextField = new JTextField JavaDoc(12);
506     componentTextField.addKeyListener(this);
507     add(componentTextField, BorderLayout.CENTER);
508     componentButton = new JButton JavaDoc();
509     componentButton.setPreferredSize(new Dimension JavaDoc(21, 21));
510     componentButton.setText("...");
511     componentButton.addActionListener(this);
512     componentButton.setFocusPainted(false);
513     add(componentButton, BorderLayout.EAST);
514     setBorder(BorderFactory.createEmptyBorder(componentMargin, componentMargin, componentMargin, componentMargin));
515     addMouseListener(this);
516   }
517
518   private void layoutCalendarWindow()
519   {
520     todaysDate = new Date JavaDoc();
521     todaysCalendar = new GregorianCalendar JavaDoc();
522     todaysCalendar.setTime(todaysDate);
523     todaysYear = todaysCalendar.get(Calendar.YEAR);
524     todaysMonth = todaysCalendar.get(Calendar.MONTH);
525     todaysDay = todaysCalendar.get(Calendar.DAY_OF_MONTH);
526
527     selectedDate = null;
528     selectedCalendar = new GregorianCalendar JavaDoc();
529     selectedYear = 0;
530     selectedMonth = 0;
531     selectedDay = 0;
532
533     displayedDate = new Date JavaDoc();
534     displayedCalendar = new GregorianCalendar JavaDoc();
535     displayedCalendar.setTime(displayedDate);
536
537     popupCalendar = new JDialog JavaDoc((Frame JavaDoc) this.getTopLevelAncestor(), true);
538
539     if (!_onJdk13) popupCalendar.setUndecorated(true);
540
541     JPanel JavaDoc monthPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
542     monthPanel.setBorder(BorderFactory.createEtchedBorder());
543     Icon JavaDoc leftButtonIcon = new ArrowIcon(SwingConstants.LEFT);
544     prevButton = new JButton JavaDoc(leftButtonIcon);
545     prevButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
546     prevButton.setPressedIcon(prevButton.getIcon());
547     prevButton.setFocusPainted(false);
548     prevButton.addMouseListener(this);
549     prevMonthTimer = new javax.swing.Timer JavaDoc((1000 / monthScrollRate), monthScrollListener);
550     prevMonthTimer.setInitialDelay(1000);
551     monthPanel.add(prevButton, BorderLayout.WEST);
552
553     monthButton = new JLabel JavaDoc("SEPTEMBER 8888", JLabel.CENTER);
554     monthButton.setBorder(null);
555     monthButton.addMouseListener(this);
556     monthPanel.add(monthButton, BorderLayout.CENTER);
557
558     Icon JavaDoc rightButtonIcon = new ArrowIcon(SwingConstants.RIGHT);
559     nextButton = new JButton JavaDoc(rightButtonIcon);
560     nextButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
561     nextButton.setPressedIcon(nextButton.getIcon());
562     nextButton.setFocusPainted(false);
563     nextButton.addMouseListener(this);
564     nextMonthTimer = new javax.swing.Timer JavaDoc((1000 / monthScrollRate), monthScrollListener);
565     nextMonthTimer.setInitialDelay(1000);
566     monthPanel.add(nextButton, BorderLayout.EAST);
567
568     dayHeaderPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 7, 2, 2));
569     dayHeaderPanel.setBackground(Color.white);
570     JLabel JavaDoc dayTitleLabel1 = new JLabel JavaDoc("S", JLabel.RIGHT);
571     dayTitleLabel1.setBackground(Color.white);
572     dayTitleLabel1.setForeground(Color.black);
573     dayHeaderPanel.add(dayTitleLabel1);
574     JLabel JavaDoc dayTitleLabel2 = new JLabel JavaDoc("M", JLabel.RIGHT);
575     dayTitleLabel2.setBackground(Color.white);
576     dayTitleLabel2.setForeground(Color.black);
577     dayHeaderPanel.add(dayTitleLabel2);
578     JLabel JavaDoc dayTitleLabel3 = new JLabel JavaDoc("T", JLabel.RIGHT);
579     dayTitleLabel3.setBackground(Color.white);
580     dayTitleLabel3.setForeground(Color.black);
581     dayHeaderPanel.add(dayTitleLabel3);
582     JLabel JavaDoc dayTitleLabel4 = new JLabel JavaDoc("W", JLabel.RIGHT);
583     dayTitleLabel4.setBackground(Color.white);
584     dayTitleLabel4.setForeground(Color.black);
585     dayHeaderPanel.add(dayTitleLabel4);
586     JLabel JavaDoc dayTitleLabel5 = new JLabel JavaDoc("T", JLabel.RIGHT);
587     dayTitleLabel5.setBackground(Color.white);
588     dayTitleLabel5.setForeground(Color.black);
589     dayHeaderPanel.add(dayTitleLabel5);
590     JLabel JavaDoc dayTitleLabel6 = new JLabel JavaDoc("F", JLabel.RIGHT);
591     dayTitleLabel6.setBackground(Color.white);
592     dayTitleLabel6.setForeground(Color.black);
593     dayHeaderPanel.add(dayTitleLabel6);
594     JLabel JavaDoc dayTitleLabel7 = new JLabel JavaDoc("S", JLabel.RIGHT);
595     dayTitleLabel7.setBackground(Color.white);
596     dayTitleLabel7.setForeground(Color.black);
597     dayHeaderPanel.add(dayTitleLabel7);
598
599     dayPanel = new JPanel JavaDoc(new GridLayout JavaDoc(6, 7, 2, 2));
600     dayPanel.setBackground(Color.white);
601     dayPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.black));
602
603     for (int i = 0; i < dayLabel.length; i++)
604     {
605       dayLabel[i] = new JLabel JavaDoc("0", JLabel.RIGHT);
606       dayLabel[i].setOpaque(true);
607       dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
608       dayLabel[i].addMouseListener(this);
609       dayPanel.add(dayLabel[i]);
610     }
611
612     updateCalendarDisplay();
613
614     JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 2, 6, 6));
615     buttonPanel.setBackground(Color.white);
616     buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
617     todayButton = new JButton JavaDoc(Messages.getString("DateChooser.Today"));
618     todayButton.setMargin(new Insets JavaDoc(2, 2, 2, 2));
619     todayButton.setFocusPainted(false);
620     todayButton.addActionListener(this);
621     buttonPanel.add(todayButton);
622
623     noneButton = new JButton JavaDoc(Messages.getString("global.None"));
624     noneButton.setMargin(new Insets JavaDoc(2, 2, 2, 2));
625     noneButton.setFocusPainted(false);
626     noneButton.addActionListener(this);
627     buttonPanel.add(noneButton);
628
629     GridBagLayout JavaDoc bottomPanelGridBag = new GridBagLayout JavaDoc();
630     JPanel JavaDoc bottomPanel = new JPanel JavaDoc(bottomPanelGridBag);
631     bottomPanel.setBackground(Color.white);
632     bottomPanel.setBorder(BorderFactory.createEmptyBorder(0, 15, 0, 15));
633     GridBagConstraints JavaDoc bottomPanelConstraints = new GridBagConstraints JavaDoc();
634     buildConstraints(bottomPanelConstraints, 0, 0, 1, 1, 1, 0);
635     bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
636     bottomPanelConstraints.anchor = GridBagConstraints.CENTER;
637     bottomPanelGridBag.setConstraints(dayHeaderPanel, bottomPanelConstraints);
638     bottomPanel.add(dayHeaderPanel);
639     try
640     {
641       String JavaDoc dateString = componentTextField.getText();
642       if (dateString.trim().length() > 0)
643       {
644         Date JavaDoc dt = returnDateFormatter.parse(dateString);
645         this.setDate(dt);
646       }
647     }
648     catch (java.text.ParseException JavaDoc ex)
649     {
650       DjLogger.log(ex);
651     }
652
653     buildConstraints(bottomPanelConstraints, 0, 1, 1, 1, 1, 0);
654     bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
655     bottomPanelConstraints.anchor = GridBagConstraints.CENTER;
656     bottomPanelGridBag.setConstraints(dayPanel, bottomPanelConstraints);
657     bottomPanel.add(dayPanel);
658
659     buildConstraints(bottomPanelConstraints, 0, 2, 1, 1, 0, 0);
660     bottomPanelConstraints.fill = GridBagConstraints.BOTH;
661     bottomPanelGridBag.setConstraints(buttonPanel, bottomPanelConstraints);
662     bottomPanel.add(buttonPanel);
663
664     GridBagLayout JavaDoc mainPanelGridBag = new GridBagLayout JavaDoc();
665     JPanel JavaDoc mainPanel = new JPanel JavaDoc(mainPanelGridBag);
666     // mainPanel.setBorder(BorderFactory.createRaisedBevelBorder());
667
mainPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
668     GridBagConstraints JavaDoc mainPanelConstraints = new GridBagConstraints JavaDoc();
669     buildConstraints(mainPanelConstraints, 0, 0, 1, 1, 0, 0);
670     mainPanelConstraints.fill = GridBagConstraints.BOTH;
671     mainPanelGridBag.setConstraints(monthPanel, mainPanelConstraints);
672     mainPanel.add(monthPanel);
673
674     buildConstraints(mainPanelConstraints, 0, 1, 1, 1, 1, 0);
675     mainPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
676     mainPanelConstraints.anchor = GridBagConstraints.CENTER;
677     mainPanelGridBag.setConstraints(bottomPanel, mainPanelConstraints);
678     mainPanel.add(bottomPanel);
679
680     Container JavaDoc contentPane = popupCalendar.getContentPane();
681     contentPane.add(mainPanel, BorderLayout.CENTER);
682
683     popupCalendar.pack();
684   }
685
686   private void updateCalendarDisplay()
687   {
688     monthButton.setText(monthyearFormatter.format(displayedCalendar.getTime()));
689
690     displayedCalendar.set(Calendar.DAY_OF_MONTH, 1);
691     int firstDayOfDisplayedMonth = displayedCalendar.get(Calendar.DAY_OF_WEEK);
692     int daysInDisplayedMonth = displayedCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
693     displayedCalendar.roll(Calendar.MONTH, -1);
694     int daysInPrevDisplayedMonth = displayedCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
695     displayedCalendar.set(Calendar.DAY_OF_MONTH, daysInPrevDisplayedMonth);
696
697     dayMap.clear();
698
699     if (firstDayOfDisplayedMonth > 1)
700     {
701       for (int i = (firstDayOfDisplayedMonth - 2); i > (-1); i--)
702       {
703         dayMap.put(dayLabel[i], displayedCalendar.getTime());
704         int thisYear = displayedCalendar.get(Calendar.YEAR);
705         int thisMonth = displayedCalendar.get(Calendar.MONTH);
706         int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH);
707         dayLabel[i].setText(String.valueOf(thisDay));
708         if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay)
709         {
710           dayLabel[i].setBackground(Color.lightGray);
711           dayLabel[i].setForeground(Color.white);
712         }
713         else
714         {
715           dayLabel[i].setBackground(Color.white);
716           dayLabel[i].setForeground(Color.lightGray);
717         }
718         if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay)
719         {
720           dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
721         }
722         else
723         {
724           dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
725         }
726         displayedCalendar.roll(Calendar.DAY_OF_MONTH, -1);
727       }
728     }
729
730     displayedCalendar.roll(Calendar.MONTH, 1);
731     displayedCalendar.set(Calendar.DAY_OF_MONTH, 1);
732
733     for (int i = (firstDayOfDisplayedMonth - 1); i < (firstDayOfDisplayedMonth + daysInDisplayedMonth - 1); i++)
734     {
735       dayMap.put(dayLabel[i], displayedCalendar.getTime());
736       int thisYear = displayedCalendar.get(Calendar.YEAR);
737       int thisMonth = displayedCalendar.get(Calendar.MONTH);
738       int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH);
739       dayLabel[i].setText(String.valueOf(thisDay));
740       dayLabel[i].setForeground(Color.black);
741       if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay)
742       {
743         dayLabel[i].setBackground(Color.lightGray);
744         dayLabel[i].setForeground(Color.white);
745       }
746       else
747       {
748         dayLabel[i].setBackground(Color.white);
749         dayLabel[i].setForeground(Color.black);
750       }
751       if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay)
752       {
753         dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
754       }
755       else
756       {
757         dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
758       }
759       displayedCalendar.roll(Calendar.DAY_OF_MONTH, 1);
760     }
761
762     displayedCalendar.roll(Calendar.MONTH, 1);
763     displayedCalendar.set(Calendar.DAY_OF_MONTH, 1);
764
765     for (int i = (firstDayOfDisplayedMonth + daysInDisplayedMonth - 1); i < dayLabel.length; i++)
766     {
767       dayMap.put(dayLabel[i], displayedCalendar.getTime());
768       int thisYear = displayedCalendar.get(Calendar.YEAR);
769       int thisMonth = displayedCalendar.get(Calendar.MONTH);
770       int thisDay = displayedCalendar.get(Calendar.DAY_OF_MONTH);
771       dayLabel[i].setText(String.valueOf(thisDay));
772       dayLabel[i].setForeground(Color.lightGray);
773       if (thisYear == selectedYear && thisMonth == selectedMonth && thisDay == selectedDay)
774       {
775         dayLabel[i].setBackground(Color.lightGray);
776         dayLabel[i].setForeground(Color.white);
777       }
778       else
779       {
780         dayLabel[i].setBackground(Color.white);
781         dayLabel[i].setForeground(Color.lightGray);
782       }
783       if (thisYear == todaysYear && thisMonth == todaysMonth && thisDay == todaysDay)
784       {
785         dayLabel[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
786       }
787       else
788       {
789         dayLabel[i].setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
790       }
791       displayedCalendar.roll(Calendar.DAY_OF_MONTH, 1);
792     }
793     displayedCalendar.roll(Calendar.MONTH, -1);
794     displayedCalendar.set(Calendar.DAY_OF_MONTH, 1);
795   }
796
797   private void buildConstraints(GridBagConstraints JavaDoc gbc, int gx, int gy, int gw, int gh, int wx, int wy)
798   {
799     gbc.gridx = gx;
800     gbc.gridy = gy;
801     gbc.gridwidth = gw;
802     gbc.gridheight = gh;
803     gbc.weightx = wx;
804     gbc.weighty = wy;
805   }
806
807   public void selectAll()
808   {
809     componentTextField.selectAll();
810   }
811
812   public void clear()
813   {
814     componentTextField.setText("");
815   }
816
817   public void keyPressed(KeyEvent JavaDoc e)
818   {
819   }
820
821   public void keyReleased(KeyEvent JavaDoc e)
822   {
823     try
824     {
825       if (!componentTextField.getText().equals(""))
826       {
827         Date JavaDoc dt = returnDateFormatter.parse(componentTextField.getText());
828         setDateEx(dt);
829       }
830     }
831     catch (java.text.ParseException JavaDoc ex)
832     {
833
834     }
835   }
836
837   public void keyTyped(KeyEvent JavaDoc e)
838   {
839
840   }
841
842   public JTextField JavaDoc getTextField()
843   {
844     return componentTextField;
845   }
846
847 }
Popular Tags