001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.client.widgets;
021:
022: import java.awt.Component;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.GridLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.util.Calendar;
030: import java.util.Date;
031:
032: import javax.swing.JButton;
033: import javax.swing.JComboBox;
034: import javax.swing.JFrame;
035: import javax.swing.JLabel;
036: import javax.swing.JPanel;
037: import javax.swing.JSpinner;
038: import javax.swing.event.ChangeEvent;
039: import javax.swing.event.ChangeListener;
040:
041: import org.lucane.client.Plugin;
042: import org.lucane.client.util.Translation;
043:
044: /**
045: * @author Jonathan Riboux
046: *
047: * A date/time picker widget.
048: */
049: public class DateTimePicker extends JPanel implements ActionListener,
050: ChangeListener {
051: private Plugin plugin;
052:
053: private JComboBox day;
054: private JComboBox month;
055: private JComboBox year;
056: private JSpinner hour;
057: private JSpinner minute;
058: private JLabel timeSeparator;
059: private JLabel dateTimeSeparator;
060:
061: private boolean showDate;
062: private boolean showTime;
063:
064: private Calendar cal;
065:
066: /**
067: * Constructor.
068: * Creates a new DatePicker using the current date.
069: */
070: public DateTimePicker() {
071: this (new Date());
072: }
073:
074: /**
075: * Constructor.
076: * Creates a new DatePicker from the date parameter.
077: * @param date the date to select
078: */
079: public DateTimePicker(Date date) {
080: cal = Calendar.getInstance();
081: long t = date.getTime();
082: cal.setTime(new Date((t / 60000) * 60000));
083: showDate = true;
084: showTime = true;
085: initLayout();
086: updateComponents();
087: }
088:
089: private void initLayout() {
090: day = new JComboBox();
091: month = new JComboBox();
092: year = new JComboBox();
093: hour = new JSpinner();
094: minute = new JSpinner();
095: timeSeparator = new JLabel(":");
096: dateTimeSeparator = new JLabel("-");
097: GridBagLayout gbl = new GridBagLayout();
098:
099: setLayout(new GridBagLayout());
100: GridBagConstraints c = new GridBagConstraints();
101:
102: c.insets = new Insets(2, 2, 2, 2);
103: c.anchor = GridBagConstraints.WEST;
104: c.fill = GridBagConstraints.HORIZONTAL;
105: c.gridy = 0;
106: c.gridx = 0;
107: c.weightx = 0;
108: c.weighty = 0;
109: c.fill = GridBagConstraints.BOTH;
110:
111: c.gridx = 1;
112: c.weightx = 1;
113: add(day, c);
114:
115: c.gridx = 3;
116: c.weightx = 1;
117: add(month, c);
118:
119: c.gridx = 5;
120: c.weightx = 1;
121: add(year, c);
122:
123: c.gridx = 6;
124: c.weightx = 0;
125: add(dateTimeSeparator, c);
126:
127: c.gridx = 7;
128: c.weightx = 2;
129: add(hour, c);
130:
131: c.gridx = 8;
132: c.weightx = 0;
133: add(timeSeparator, c);
134:
135: c.gridx = 9;
136: c.weightx = 2;
137: add(minute, c);
138: }
139:
140: private void updateComponents() {
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(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(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(currentHour));
169: hour.addChangeListener(this );
170:
171: int currentMinute = cal.get(Calendar.MINUTE);
172: minute.removeChangeListener(this );
173: minute.setValue(new Integer(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 ae) {
183: if (ae.getSource() == day) {
184: cal.set(Calendar.DAY_OF_MONTH, day.getSelectedIndex() + 1);
185: updateComponents();
186: } else if (ae.getSource() == month) {
187: cal.set(Calendar.MONTH, month.getSelectedIndex());
188: updateComponents();
189: } else if (ae.getSource() == year) {
190: cal.set(Calendar.YEAR, ((Integer) year.getSelectedItem())
191: .intValue());
192: updateComponents();
193: }
194: }
195:
196: /**
197: * Spin listener. It updates the current date.
198: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
199: */
200: public void stateChanged(ChangeEvent ce) {
201: if (ce.getSource() == hour) {
202: int currentHour = ((Integer) hour.getValue()).intValue();
203: if (currentHour >= 24)
204: hour.setValue(new Integer(currentHour % 24));
205: if (currentHour < 0) {
206: hour.setValue(new Integer(currentHour % 24 + 24));
207: }
208: cal.set(Calendar.HOUR_OF_DAY, ((Integer) hour.getValue())
209: .intValue());
210: } else if (ce.getSource() == minute) {
211: int currentMinute = ((Integer) minute.getValue())
212: .intValue();
213: if (currentMinute >= 60)
214: minute.setValue(new Integer(currentMinute % 60));
215: if (currentMinute < 0)
216: minute.setValue(new Integer(currentMinute % 60 + 60));
217: cal.set(Calendar.MINUTE, ((Integer) minute.getValue())
218: .intValue());
219: }
220: }
221:
222: /**
223: * Sets the current date.
224: * @param date the date to set
225: */
226: public void setDate(Date date) {
227: long t = date.getTime();
228: cal.setTime(new Date((t / 60000) * 60000));
229: updateComponents();
230: }
231:
232: /**
233: * Returns the current date.
234: * @return the current date
235: */
236: public Date getDate() {
237: return cal.getTime();
238: }
239:
240: public boolean isShowDate() {
241: return showDate;
242: }
243:
244: public void setShowDate(boolean showDate) {
245: this .showDate = showDate;
246: updateVisibility();
247: }
248:
249: public boolean isShowTime() {
250: return showTime;
251: }
252:
253: public void setShowTime(boolean showTime) {
254: this .showTime = showTime;
255: updateVisibility();
256: }
257:
258: private void updateVisibility() {
259: day.setVisible(showDate);
260: month.setVisible(showDate);
261: year.setVisible(showDate);
262: dateTimeSeparator.setVisible(showDate && showTime);
263: hour.setVisible(showTime);
264: timeSeparator.setVisible(showTime);
265: minute.setVisible(showTime);
266: }
267:
268: /**
269: * For testing purpose
270: */
271: public static void main(String[] args) {
272: JFrame jf = new JFrame();
273:
274: final DateTimePicker dp = new DateTimePicker();
275: jf.getContentPane().setLayout(new GridLayout(3, 1));
276: jf.getContentPane().add(dp);
277: //dp.setEnabled(false);
278:
279: JButton jbg = new JButton("get date");
280: jbg.addActionListener(new ActionListener() {
281: public void actionPerformed(ActionEvent e) {
282: DialogBox.info(dp.getDate().toString());
283: }
284: });
285: jf.getContentPane().add(jbg);
286:
287: JButton jbs = new JButton("set current date");
288: jbs.addActionListener(new ActionListener() {
289: public void actionPerformed(ActionEvent e) {
290: dp.setDate(new Date());
291: }
292: });
293: jf.getContentPane().add(jbs);
294: jf.setSize(300, 300);
295: jf.show();
296: }
297:
298: public void setEnabled(boolean enabled) {
299: super .setEnabled(enabled);
300: Component[] c = getComponents();
301: for (int i = 0; i < c.length; i++)
302: c[i].setEnabled(enabled);
303: }
304: }
|