001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.GridLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.ItemListener;
027: import java.text.DateFormatSymbols;
028: import java.text.DecimalFormat;
029: import java.util.Calendar;
030: import java.util.GregorianCalendar;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Locale;
034: import java.util.NoSuchElementException;
035: import java.util.StringTokenizer;
036: import java.util.Vector;
037:
038: import javax.swing.JCheckBox;
039: import javax.swing.JComboBox;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042:
043: import de.finix.contelligent.client.util.list.NameValueComboBox;
044: import de.finix.contelligent.client.util.list.NameValueListModel;
045:
046: /**
047: *
048: * Panel to enter international dates and times
049: */
050: public class DatePanel extends JPanel implements ActionListener {
051:
052: private JComboBox yearBox, dayBox, hourBox;
053:
054: private NameValueComboBox monthBox;
055:
056: private JCheckBox isEnabledCheckBox;
057:
058: private boolean useEnabledCheckBox = false;
059:
060: private Calendar calendar;
061:
062: private List<ActionListener> actionListeners = new Vector<ActionListener>();
063:
064: private boolean disableTime;
065:
066: private final static Integer[] validYears = new Integer[] {
067: new Integer(1998), new Integer(1999), new Integer(2000),
068: new Integer(2001), new Integer(2002), new Integer(2003),
069: new Integer(2004), new Integer(2005), new Integer(2006),
070: new Integer(2007), new Integer(2008) };
071:
072: private final static Integer[] monthKeys = new Integer[] {
073: new Integer(Calendar.JANUARY),
074: new Integer(Calendar.FEBRUARY),
075: new Integer(Calendar.MARCH), new Integer(Calendar.APRIL),
076: new Integer(Calendar.MAY), new Integer(Calendar.JUNE),
077: new Integer(Calendar.JULY), new Integer(Calendar.AUGUST),
078: new Integer(Calendar.SEPTEMBER),
079: new Integer(Calendar.OCTOBER),
080: new Integer(Calendar.NOVEMBER),
081: new Integer(Calendar.DECEMBER) };
082:
083: private final static Integer[] days = new Integer[31];
084: static {
085: for (int i = 1; i <= 31; i++) {
086: days[i - 1] = new Integer(i);
087: }
088: };
089:
090: private final static String[] europeanHours = new String[] {
091: "0:00", "0:30", "1:00", "1:30", "2:00", "2:30", "3:00",
092: "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:30",
093: "7:00", "7:30", "8:00", "8:30", "9:00", "9:30", "10:00",
094: "10:30", "11:00", "11:30", "12:00", "12:30", "13:00",
095: "13:30", "14:00", "14:30", "15:00", "15:30", "16:00",
096: "16:30", "17:00", "17:30", "18:00", "18:30", "19:00",
097: "19:30", "20:00", "20:30", "21:00", "21:30", "22:00",
098: "22:30", "23:00", "23:30" };
099:
100: /*
101: * private final static String[] americanHours = new String[] { "0:00 AM",
102: * "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00
103: * AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00", "1:00 PM",
104: * "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00
105: * PM", "9:00 PM", "10:00 PM", "11:00 PM", }; private final static Integer[]
106: * hourKeys = new Integer[24]; static { for (int i=0; i <= 23; i++) {
107: * hourKeys[i] = new Integer(i); } };
108: */
109:
110: public DatePanel(String title, Locale locale) {
111: this (title, locale, false, false);
112: }
113:
114: public DatePanel(String title, Locale locale,
115: boolean useEnabledCheckBox, boolean disableTime) {
116: super ();
117: this .useEnabledCheckBox = useEnabledCheckBox;
118: calendar = new GregorianCalendar(locale);
119: init(title, locale, disableTime);
120: }
121:
122: public void setForeground(Color color) {
123: if (yearBox != null)
124: yearBox.setForeground(color);
125: if (monthBox != null)
126: monthBox.setForeground(color);
127: if (dayBox != null)
128: dayBox.setForeground(color);
129: if (hourBox != null)
130: hourBox.setForeground(color);
131: }
132:
133: private void init(String title, Locale locale, boolean disableTime) {
134: this .disableTime = disableTime;
135: GridLayout gl = new GridLayout(1, 6, 10, 10);
136: gl.setHgap(2);
137: gl.setVgap(10);
138: setLayout(gl);
139: DateFormatSymbols symbols = new DateFormatSymbols(locale);
140: String[] months = symbols.getMonths();
141: String[] hours;
142: // if (locale.getCountry().equals("US")) {
143: // hours = americanHours;
144: // } else {
145: hours = europeanHours;
146: // }
147:
148: if (useEnabledCheckBox) {
149: isEnabledCheckBox = new JCheckBox();
150: isEnabledCheckBox.setSelected(true);
151: }
152: yearBox = new JComboBox(validYears);
153: yearBox.setEditable(true); // in case we are later than 2008 ;)
154: yearBox.addActionListener(this );
155: monthBox = new NameValueComboBox(NameValueListModel
156: .buildNameValuePairs(monthKeys, months));
157: monthBox.setSelectedItemByValue(monthKeys[0]);
158: monthBox.addActionListener(this );
159: dayBox = new JComboBox(days);
160: dayBox.addActionListener(this );
161: hourBox = new JComboBox(hours);
162: hourBox.setEditable(true);
163: hourBox.addActionListener(this );
164: JPanel titlePane = new JPanel();
165: if (useEnabledCheckBox)
166: titlePane.add(isEnabledCheckBox, BorderLayout.WEST);
167: if (title != null) {
168: JLabel titleLabel = new JLabel(title);
169: titlePane.add(titleLabel, BorderLayout.WEST);
170: }
171:
172: if (useEnabledCheckBox || title != null)
173: add(titlePane);
174: add(yearBox);
175: add(monthBox);
176: add(dayBox);
177: add(hourBox);
178: if (disableTime) {
179: hourBox.setEnabled(false);
180: }
181: if (useEnabledCheckBox) {
182: isEnabledCheckBox.addItemListener(new ItemListener() {
183: public void itemStateChanged(ItemEvent e) {
184: if (e.getStateChange() == ItemEvent.DESELECTED) {
185: setActive(false);
186: } else {
187: setActive(true);
188: }
189: }
190: });
191: }
192: }
193:
194: public void setEditable(boolean editable) {
195: setActive(editable);
196: }
197:
198: private void setActive(boolean active) {
199: yearBox.setEnabled(active);
200: monthBox.setEnabled(active);
201: dayBox.setEnabled(active);
202: hourBox.setEnabled(active && !disableTime);
203: }
204:
205: public long getMillis() {
206: if (useEnabledCheckBox && !isEnabledCheckBox.isSelected()) {
207: return -1;
208: } else {
209: updateCalendar();
210: return calendar.getTimeInMillis();
211: }
212: }
213:
214: public boolean isActivated() {
215: if (useEnabledCheckBox)
216: return true;
217: return isEnabledCheckBox.isSelected();
218: }
219:
220: public void setMillis(long millis) {
221: if (millis != -1) {
222: calendar.setTimeInMillis(millis);
223: if (useEnabledCheckBox)
224: isEnabledCheckBox.setSelected(true);
225: setActive(true);
226: int year = calendar.get(Calendar.YEAR);
227: int month = calendar.get(Calendar.MONTH);
228: int day = calendar.get(Calendar.DAY_OF_MONTH);
229: int hour = calendar.get(Calendar.HOUR_OF_DAY);
230: int minute = calendar.get(Calendar.MINUTE);
231: int second = calendar.get(Calendar.SECOND);
232:
233: yearBox.setSelectedItem(new Integer(year));
234: monthBox.setSelectedItemByValue(new Integer(month));
235: dayBox.setSelectedItem(new Integer(day));
236: DecimalFormat f = new DecimalFormat("00");
237: String hourString = f.format(hour) + ":" + f.format(minute)
238: + ":" + f.format(second);
239: hourBox.setSelectedItem(hourString);
240: } else {
241: if (useEnabledCheckBox)
242: isEnabledCheckBox.setSelected(false);
243: setActive(false);
244: }
245: }
246:
247: private void updateCalendar() {
248: int year = yearBox.getSelectedItem() == null ? 0
249: : ((Integer) yearBox.getSelectedItem()).intValue();
250: int month = monthBox.getSelectedItem() == null ? 0
251: : ((Integer) monthBox.getSelectedItemValue())
252: .intValue();
253: int day = dayBox.getSelectedItem() == null ? 0
254: : ((Integer) dayBox.getSelectedItem()).intValue();
255: String hourString = (String) hourBox.getSelectedItem();
256: int hour = 0, minute = 0, second = 0;
257: try {
258: StringTokenizer tok = new StringTokenizer(hourString, ":");
259: hour = Integer.parseInt(tok.nextToken());
260: minute = Integer.parseInt(tok.nextToken());
261: second = Integer.parseInt(tok.nextToken());
262: } catch (NoSuchElementException nsee) {
263: // do not care
264: } catch (NumberFormatException nfe) {
265: // do not care
266: }
267: calendar.set(year, month, day, hour, minute, second);
268: }
269:
270: public void addActionListener(ActionListener listener) {
271: actionListeners.add(listener);
272: }
273:
274: public void removeActionListener(ActionListener listener) {
275: actionListeners.remove(listener);
276: }
277:
278: public Iterator getActionListeners() {
279: return actionListeners.iterator();
280: }
281:
282: protected void notifyListeners() {
283: for (Iterator i = getActionListeners(); i.hasNext();) {
284: ((ActionListener) i.next())
285: .actionPerformed(new ActionEvent((Object) this ,
286: ActionEvent.ACTION_PERFORMED,
287: "date changed"));
288: }
289: }
290:
291: public void actionPerformed(ActionEvent e) {
292: notifyListeners();
293: }
294:
295: }
|