View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components;
21  
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  
26  import java.util.Calendar;
27  import java.util.Date;
28  import java.util.GregorianCalendar;
29  
30  import javax.swing.JDialog;
31  
32  
33  /**
34   * This is a container for SimpleDateSelector and SimpleTimeSelector components,
35   * combining the functionality of both. The components consists of six
36   * user-editable fields for year, month, day, hour, minute and seconds. All
37   * fields are validating allowing user to select only valid date and time
38   * values. These are the obtained using getDate and setDate methods. 
39   * <p>
40   * When a new date is set the component notifies PropertyChangeListeners through
41   * property <code>date</code>.
42   * </p>
43   *
44   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
45   * @version $id$
46   */
47  public class DateTimeSelector extends javax.swing.JPanel
48  {
49  	
50  	private static final long serialVersionUID = 1L;
51  	private SimpleDateSelector dateChooser;
52  	private SimpleTimeSelector timeChooser;
53  	private Calendar calendar = new GregorianCalendar();
54  
55  	/**
56  	 * Constructs a new DateTimeSelector.
57  	 */
58  	public DateTimeSelector()
59  	{
60  		super(new GridBagLayout());
61  		createComponents();
62  	}
63  
64  	/**
65  	 * Internal helper routine creates all the components. 
66  	 */
67  	protected void createComponents()
68  	{
69  		GridBagConstraints c = new GridBagConstraints();
70  		c.weightx = 1.0;
71  		c.weighty = 0.0;
72  		c.fill = GridBagConstraints.HORIZONTAL;
73  		c.gridx = 0;
74  		c.insets = new Insets(4, 4, 4, 4);
75  
76  		c.gridy = 0;
77  
78  		dateChooser = new SimpleDateSelector();
79  		add(dateChooser, c);
80  
81  		timeChooser = new SimpleTimeSelector();
82  		c.gridy = 1;
83  		add(timeChooser, c);
84  	}
85  
86  	/**
87  	 * Returns the date displayed by this component. The result is
88  	 * java.util.Date object. Only the following fields are set: SECOND,
89  	 * MINUTE, HOUR_OF_DAY, DAY_OF_MONTH, MONTH, YEAR. For more information on
90  	 * these fields see the java.util.Calendar. 
91  	 *
92  	 * @return java.util.Date
93  	 */
94  	public Date getDate()
95  	{
96  		calendar.set(Calendar.SECOND, timeChooser.getSeconds());
97  		calendar.set(Calendar.MINUTE, timeChooser.getMinute());
98  		calendar.set(Calendar.HOUR_OF_DAY, timeChooser.getHour());
99  		calendar.set(Calendar.YEAR, dateChooser.getYear());
100 		calendar.set(Calendar.MONTH, dateChooser.getMonth());
101 		calendar.set(Calendar.DAY_OF_MONTH, dateChooser.getDay());
102 
103 		return calendar.getTime();
104 	}
105 
106 	/**
107 	 * Sets the date to display in this selector. Value is set through the
108 	 * java.util.Date object. Only the following fields are considerer:
109 	 * SECOND, MINUTE, HOUR_OF_DAY, DAY_OF_MONTH, MONTH, YEAR. For more
110 	 * information on these fields see the java.util.Calendar class. 
111 	 *
112 	 * @param date java.util.Date
113 	 */
114 	public void setDate(Date date)
115 	{
116 		dateChooser.setDate(date);
117 		timeChooser.setTime(date);
118 	}
119 
120 	/**
121 	 * Sets the enabled state of this component and the components contained in
122 	 * it. 
123 	 *
124 	 * @param how boolean
125 	 */
126 	public void setEnabled(boolean how)
127 	{
128 		super.setEnabled(how);
129 		dateChooser.setEnabled(how);
130 		timeChooser.setEnabled(how);
131 	}
132 	
133 	/**
134 	 * Run test applet.
135 	 *
136 	 * @param args command line arguments
137 	 */
138 	public static void main(String[] args)
139 	{
140 		JDialog d = new JDialog();
141 		DateTimeSelector dtc = new DateTimeSelector();
142 
143 		GridBagConstraints c = new GridBagConstraints();
144 
145 		c.fill = GridBagConstraints.BOTH;
146 
147 		d.getContentPane().setLayout(new GridBagLayout());
148 		d.getContentPane().add(dtc, c);
149 
150 		d.setModal(true);
151 		d.setVisible(true);
152 		System.out.println(dtc.getDate().toString());
153 	}
154 }
155 
156 /* __oOo__ */