KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > widgets > DateTimePicker


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.client.widgets;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.GridLayout JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComboBox JavaDoc;
34 import javax.swing.JFrame JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.JSpinner JavaDoc;
38 import javax.swing.event.ChangeEvent JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40
41 import org.lucane.client.Plugin;
42 import org.lucane.client.util.Translation;
43
44 /**
45  * @author Jonathan Riboux
46  *
47  * A date/time picker widget.
48  */

49 public class DateTimePicker extends JPanel JavaDoc implements ActionListener JavaDoc, ChangeListener JavaDoc {
50     private Plugin plugin;
51     
52     private JComboBox JavaDoc day;
53     private JComboBox JavaDoc month;
54     private JComboBox JavaDoc year;
55     private JSpinner JavaDoc hour;
56     private JSpinner JavaDoc minute;
57     private JLabel JavaDoc timeSeparator;
58     private JLabel JavaDoc dateTimeSeparator;
59     
60     private boolean showDate;
61     private boolean showTime;
62     
63     private Calendar JavaDoc cal;
64
65     /**
66      * Constructor.
67      * Creates a new DatePicker using the current date.
68      */

69     public DateTimePicker() {
70         this(new Date JavaDoc());
71     }
72
73     /**
74      * Constructor.
75      * Creates a new DatePicker from the date parameter.
76      * @param date the date to select
77      */

78     public DateTimePicker(Date JavaDoc date) {
79         cal = Calendar.getInstance();
80         long t = date.getTime();
81         cal.setTime(new Date JavaDoc((t / 60000) * 60000));
82         showDate=true;
83         showTime=true;
84         initLayout();
85         updateComponents();
86     }
87
88     private void initLayout() {
89         day = new JComboBox JavaDoc();
90         month = new JComboBox JavaDoc();
91         year = new JComboBox JavaDoc();
92         hour = new JSpinner JavaDoc();
93         minute = new JSpinner JavaDoc();
94         timeSeparator = new JLabel JavaDoc(":");
95         dateTimeSeparator = new JLabel JavaDoc("-");
96         GridBagLayout JavaDoc gbl = new GridBagLayout JavaDoc();
97         
98         setLayout(new GridBagLayout JavaDoc());
99         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
100         
101         c.insets=new Insets JavaDoc(2,2,2,2);
102         c.anchor=GridBagConstraints.WEST;
103         c.fill=GridBagConstraints.HORIZONTAL;
104         c.gridy=0;
105         c.gridx=0;
106         c.weightx=0;
107         c.weighty=0;
108         c.fill=GridBagConstraints.BOTH;
109
110         c.gridx=1;
111         c.weightx=1;
112         add(day, c);
113
114         c.gridx=3;
115         c.weightx=1;
116         add(month, c);
117
118         c.gridx=5;
119         c.weightx=1;
120         add(year, c);
121
122         c.gridx=6;
123         c.weightx=0;
124         add(dateTimeSeparator, c);
125
126         c.gridx=7;
127         c.weightx=2;
128         add(hour, c);
129
130         c.gridx=8;
131         c.weightx=0;
132         add(timeSeparator, c);
133
134         c.gridx=9;
135         c.weightx=2;
136         add(minute, c);
137     }
138     
139     private void updateComponents()
140     {
141         int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
142         int currentDay = cal.get(Calendar.DAY_OF_MONTH);
143         day.removeActionListener(this);
144         day.removeAllItems();
145         for(int i=1;i<=maxDays;i++)
146             day.addItem(new Integer JavaDoc(i));
147         day.setSelectedIndex(currentDay-1);
148         day.addActionListener(this);
149
150         int monthIndex = cal.get(Calendar.MONTH);
151         month.removeActionListener(this);
152         month.removeAllItems();
153         for(int i=1;i<=12;i++)
154             month.addItem(Translation.tr("DateTimePicker.month." + i));
155         month.setSelectedIndex(monthIndex);
156         month.addActionListener(this);
157         
158         int currentYear = cal.get(Calendar.YEAR);
159         year.removeActionListener(this);
160         year.removeAllItems();
161         for(int i=-3;i<4;i++)
162             year.addItem(new Integer JavaDoc(currentYear+i));
163         year.setSelectedIndex(3);
164         year.addActionListener(this);
165         
166         int currentHour = cal.get(Calendar.HOUR_OF_DAY);
167         hour.removeChangeListener(this);
168         hour.setValue(new Integer JavaDoc(currentHour));
169         hour.addChangeListener(this);
170         
171         int currentMinute = cal.get(Calendar.MINUTE);
172         minute.removeChangeListener(this);
173         minute.setValue(new Integer JavaDoc(currentMinute));
174         minute.addChangeListener(this);
175         
176     }
177
178     /**
179      * Combo boxes listener. It updates the current date and the combos.
180      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
181      */

182     public void actionPerformed(ActionEvent JavaDoc ae)
183     {
184         if(ae.getSource() == day)
185         {
186             cal.set(Calendar.DAY_OF_MONTH, day.getSelectedIndex()+1);
187             updateComponents();
188         }
189         else if(ae.getSource() == month)
190         {
191             cal.set(Calendar.MONTH, month.getSelectedIndex());
192             updateComponents();
193         }
194         else if(ae.getSource() == year)
195         {
196             cal.set(Calendar.YEAR, ((Integer JavaDoc)year.getSelectedItem()).intValue());
197             updateComponents();
198         }
199     }
200
201     /**
202      * Spin listener. It updates the current date.
203      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
204      */

205     public void stateChanged(ChangeEvent JavaDoc ce) {
206         if(ce.getSource() == hour)
207         {
208             int currentHour = ((Integer JavaDoc)hour.getValue()).intValue();
209             if (currentHour >= 24)
210                 hour.setValue(new Integer JavaDoc(currentHour % 24));
211             if (currentHour < 0) {
212                 hour.setValue(new Integer JavaDoc(currentHour % 24 + 24));
213             }
214             cal.set(Calendar.HOUR_OF_DAY, ((Integer JavaDoc)hour.getValue()).intValue());
215         }
216         else if(ce.getSource() == minute)
217         {
218             int currentMinute = ((Integer JavaDoc)minute.getValue()).intValue();
219             if (currentMinute >= 60)
220                 minute.setValue(new Integer JavaDoc(currentMinute % 60));
221             if (currentMinute < 0)
222                 minute.setValue(new Integer JavaDoc(currentMinute % 60 + 60));
223             cal.set(Calendar.MINUTE, ((Integer JavaDoc)minute.getValue()).intValue());
224         }
225     }
226
227     
228     /**
229      * Sets the current date.
230      * @param date the date to set
231      */

232     public void setDate(Date JavaDoc date) {
233         long t = date.getTime();
234         cal.setTime(new Date JavaDoc((t / 60000) * 60000));
235         updateComponents();
236     }
237     
238     /**
239      * Returns the current date.
240      * @return the current date
241      */

242     public Date JavaDoc getDate() {
243         return cal.getTime();
244     }
245     
246     public boolean isShowDate() {
247         return showDate;
248     }
249     public void setShowDate(boolean showDate) {
250         this.showDate = showDate;
251         updateVisibility();
252     }
253     public boolean isShowTime() {
254         return showTime;
255     }
256     public void setShowTime(boolean showTime) {
257         this.showTime = showTime;
258         updateVisibility();
259     }
260     private void updateVisibility() {
261         day.setVisible(showDate);
262         month.setVisible(showDate);
263         year.setVisible(showDate);
264         dateTimeSeparator.setVisible(showDate && showTime);
265         hour.setVisible(showTime);
266         timeSeparator.setVisible(showTime);
267         minute.setVisible(showTime);
268     }
269
270     /**
271      * For testing purpose
272      */

273     public static void main(String JavaDoc[] args) {
274         JFrame JavaDoc jf = new JFrame JavaDoc();
275
276         final DateTimePicker dp = new DateTimePicker();
277         jf.getContentPane().setLayout(new GridLayout JavaDoc(3,1));
278         jf.getContentPane().add(dp);
279         //dp.setEnabled(false);
280

281         JButton JavaDoc jbg = new JButton JavaDoc("get date");
282         jbg.addActionListener(new ActionListener JavaDoc() {
283             public void actionPerformed(ActionEvent JavaDoc e) {
284                 DialogBox.info(dp.getDate().toString());
285             }
286         });
287         jf.getContentPane().add(jbg);
288
289         JButton JavaDoc jbs = new JButton JavaDoc("set current date");
290         jbs.addActionListener(new ActionListener JavaDoc() {
291             public void actionPerformed(ActionEvent JavaDoc e) {
292                 dp.setDate(new Date JavaDoc());
293             }
294         });
295         jf.getContentPane().add(jbs);
296         jf.setSize(300,300);
297         jf.show();
298     }
299     
300     public void setEnabled(boolean enabled) {
301         super.setEnabled(enabled);
302         Component JavaDoc [] c = getComponents();
303         for (int i=0; i<c.length; i++)
304             c[i].setEnabled(enabled);
305     }
306 }
Popular Tags