Source code: jmmv/ui/DateEntry.java
1 /*
2 * DiskCat - Disk Cataloguer
3 * Copyright (C) 2002 Julio Merino <slink@unixbsd.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21 package jmmv.ui;
22
23 import java.awt.event.ItemEvent;
24 import java.awt.event.ItemListener;
25 import java.awt.event.KeyEvent;
26 import java.awt.event.KeyListener;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.util.Calendar;
30 import java.util.ResourceBundle;
31 import java.util.Date;
32 import javax.swing.JComboBox;
33 import javax.swing.JPanel;
34 import javax.swing.JTextField;
35 import javax.swing.event.ChangeEvent;
36 import javax.swing.event.ChangeListener;
37 import javax.swing.event.EventListenerList;
38
39 /**
40 * This class implements a JPanel that can be used to read dates from
41 * a Swing application. The panel is formed by a JTextField to enter
42 * the day, a JComboBox to select the month and another JTextField to
43 * enter the year.
44 *
45 * Data is managed throught the java.util.Date class, or derivates,
46 * so you can also control java.sql.Date objects.
47 *
48 * @author Julio Merino
49 */
50 public final class DateEntry extends JPanel implements ItemListener, KeyListener {
51 // Object data
52 private static ResourceBundle rc = null;
53
54 // Event control variables
55 private EventListenerList listenerList = new EventListenerList();
56 private ChangeEvent changeEvent = null;
57 private boolean ignoreEvents;
58
59 // Form variables
60 private JTextField mDay;
61 private JComboBox mMonth;
62 private JTextField mYear;
63
64 public DateEntry() {
65 GridBagLayout gbl;
66 GridBagConstraints gbc;
67
68 if (rc == null)
69 rc = ResourceBundle.getBundle("jmmvBundle");
70
71 ignoreEvents = false;
72
73 // Initialize form fields
74 mDay = new JTextField();
75 mMonth = new JComboBox();
76 mYear = new JTextField();
77
78 mMonth.addItem(rc.getString("DATEENTRY_UNKNOWN"));
79 mMonth.addItem(rc.getString("DATEENTRY_JANUARY"));
80 mMonth.addItem(rc.getString("DATEENTRY_FEBRUARY"));
81 mMonth.addItem(rc.getString("DATEENTRY_MARCH"));
82 mMonth.addItem(rc.getString("DATEENTRY_APRIL"));
83 mMonth.addItem(rc.getString("DATEENTRY_MAY"));
84 mMonth.addItem(rc.getString("DATEENTRY_JUNE"));
85 mMonth.addItem(rc.getString("DATEENTRY_JULY"));
86 mMonth.addItem(rc.getString("DATEENTRY_AUGUST"));
87 mMonth.addItem(rc.getString("DATEENTRY_SEPTEMBER"));
88 mMonth.addItem(rc.getString("DATEENTRY_OCTOBER"));
89 mMonth.addItem(rc.getString("DATEENTRY_NOVEMBER"));
90 mMonth.addItem(rc.getString("DATEENTRY_DECEMBER"));
91
92 gbl = new GridBagLayout();
93 setLayout(gbl);
94 gbc = new GridBagConstraints();
95 gbc.fill = GridBagConstraints.BOTH;
96
97 gbc.weightx = 0.3;
98 gbl.setConstraints(mDay, gbc);
99 add(mDay);
100 gbc.weightx = 0.4;
101 gbl.setConstraints(mMonth, gbc);
102 add(mMonth);
103 gbc.weightx = 0.3;
104 gbl.setConstraints(mYear, gbc);
105 add(mYear);
106
107 mDay.addKeyListener(this);
108 mMonth.addItemListener(this);
109 mYear.addKeyListener(this);
110 }
111
112 /**
113 * Checks if the date is set properly. If any of the fields is
114 * empty, this function will return false; otherwise, true. It
115 * does not check for a valid date.
116 */
117 public boolean isSet() {
118 if (mDay.getText().equals("")) return false;
119 if (mMonth.getSelectedIndex() <= 0) return false;
120 if (mYear.getText().equals("")) return false;
121 return true;
122 }
123
124 /**
125 * Returns a java.util.Date object with the representation of the
126 * written date.
127 *
128 * @return A java.util.Date object. CAN BE NULL if the date is
129 * unset, so be sure to check it.
130 */
131 public Date getDate() {
132 if (!isSet())
133 return null;
134
135 Calendar cal = Calendar.getInstance();
136 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(mDay.getText()));
137 cal.set(Calendar.MONTH, mMonth.getSelectedIndex() - 1);
138 cal.set(Calendar.YEAR, Integer.parseInt(mYear.getText()));
139 return (Date) cal.getTime();
140 }
141
142 /**
143 * Sets the date to be shown, trought a java.util.Date object.
144 * A null pointer will clear the form.
145 */
146 public void setDate(Date d) {
147 if (d == null)
148 clear();
149 else {
150 Calendar cal = Calendar.getInstance();
151
152 cal.setTime(d);
153 ignoreEvents = true;
154 mDay.setText(new Integer(cal.get(Calendar.DAY_OF_MONTH)).toString());
155 mMonth.setSelectedIndex(cal.get(Calendar.MONTH) + 1);
156 mYear.setText(new Integer(cal.get(Calendar.YEAR)).toString());
157 ignoreEvents = false;
158 }
159 }
160
161 /**
162 * Clears the form.
163 */
164 public void clear() {
165 ignoreEvents = true;
166 mDay.setText("");
167 mMonth.setSelectedIndex(0);
168 mYear.setText("");
169 ignoreEvents = false;
170 }
171
172 /**
173 * This action manager is used to enter updating mode when a field
174 * in the dialog, that allows item selection, is changed.
175 */
176 public void itemStateChanged(ItemEvent evt) {
177 if (ignoreEvents) return;
178 fireStateChanged();
179 }
180
181 /**
182 * This action manager is used to enter updating mode when a field
183 * in the dialog, that allows text change, is edited.
184 */
185 public void keyTyped(KeyEvent evt) {
186 if (ignoreEvents) return;
187 fireStateChanged();
188 }
189
190 /** Unused */
191 public void keyPressed(KeyEvent evt) {}
192
193 /** Unused */
194 public void keyReleased(KeyEvent evt) {}
195
196 public void addChangeListener(ChangeListener l) {
197 listenerList.add(ChangeListener.class, l);
198 }
199
200 public void removeChangeListener(ChangeListener l) {
201 listenerList.remove(ChangeListener.class, l);
202 }
203
204 private void fireStateChanged() {
205 Object[] listeners = listenerList.getListenerList();
206
207 for (int i = listeners.length-2; i>=0; i-=2) {
208 if (listeners[i] == ChangeListener.class) {
209 if (changeEvent == null)
210 changeEvent = new ChangeEvent(this);
211 ((ChangeListener) listeners[i+1]).stateChanged(changeEvent);
212 }
213 }
214 }
215 }