001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.swing;
020:
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.LayoutManager;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.KeyEvent;
030: import java.awt.event.KeyListener;
031: import java.text.ParseException;
032: import java.text.SimpleDateFormat;
033: import java.util.ArrayList;
034: import java.util.Calendar;
035: import java.util.Date;
036: import java.util.Iterator;
037:
038: import javax.swing.BorderFactory;
039: import javax.swing.JButton;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042: import javax.swing.JSpinner;
043: import javax.swing.SpinnerDateModel;
044: import javax.swing.SpinnerModel;
045: import javax.swing.SwingUtilities;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048:
049: /**
050: *
051: * @author Matthew Large
052: * @version $Revision: 1.1 $
053: *
054: */
055: public class DateSelector extends JPanel implements LayoutManager,
056: ChangeListener, ActionListener, Runnable, KeyListener {
057:
058: public static final String CLEAR_ACTION = "CLEAR";
059: private static final String XSD_DATE_FORMAT = "yyyy-MM-dd G'T'HH:mm:ss";
060: private SimpleDateFormat m_df = new java.text.SimpleDateFormat(
061: XSD_DATE_FORMAT);
062:
063: private JSpinner m_dateSpinner = null;
064: private JSpinner m_timeSpinner = null;
065:
066: private JLabel m_dateLabel = null;
067: private JLabel m_timeLabel = null;
068:
069: private Date m_maxDate = null;
070: private Date m_minDate = null;
071: private Date m_initDate = new Date();
072:
073: private boolean m_bShowTime = false;
074: private boolean m_bShowDate = true;
075: private boolean m_bShowClearButton = true;
076:
077: private String m_sDateFormat = "yyyy/MM/dd";
078: private String m_sTimeFormat = "HH:mm";
079:
080: private ArrayList m_changeListeners = new ArrayList();
081:
082: private JButton m_clearButton = null;
083:
084: private boolean m_bCleared = false;
085:
086: /**
087: *
088: */
089: public DateSelector() {
090: super ();
091: this .setup();
092: }
093:
094: public boolean isCleared() {
095: return this .m_bCleared;
096: }
097:
098: public void setInitalDate(Date initDate) {
099: this .m_initDate = initDate;
100: this .setup();
101: }
102:
103: public void setMaxDate(Date maxDate) {
104: this .m_maxDate = maxDate;
105: this .setup();
106: }
107:
108: public void setMinDate(Date minDate) {
109: this .m_minDate = minDate;
110: this .setup();
111: }
112:
113: public void setShowTime(boolean bShowTime) {
114: this .m_bShowTime = bShowTime;
115: this .setup();
116: }
117:
118: public boolean isShowingDate() {
119: return this .m_bShowDate;
120: }
121:
122: public boolean isShowingTime() {
123: return this .m_bShowTime;
124: }
125:
126: public boolean isShowingClearButton() {
127: return this .m_bShowClearButton;
128: }
129:
130: public void setShowDate(boolean bShowDate) {
131: this .m_bShowDate = bShowDate;
132: this .setup();
133: }
134:
135: public void setDateFormat(String sDateFormat) {
136: this .m_sDateFormat = sDateFormat;
137: this .setup();
138: }
139:
140: public void setShowClearButton(boolean bShowClearButton) {
141: this .m_bShowClearButton = bShowClearButton;
142: this .setup();
143: }
144:
145: public void addChangeListener(ChangeListener changeListener) {
146: this .m_changeListeners.add(changeListener);
147: }
148:
149: public void setTimeFormat(String sTimeFormat) {
150: this .m_sTimeFormat = sTimeFormat;
151: this .setup();
152: }
153:
154: private void setup() {
155:
156: if (this .m_dateSpinner != null) {
157: this .m_dateSpinner.getModel().removeChangeListener(this );
158: }
159: if (this .m_timeSpinner != null) {
160: this .m_timeSpinner.getModel().removeChangeListener(this );
161: }
162:
163: this .setLayout(this );
164: this .removeAll();
165:
166: if (this .m_bShowDate) {
167:
168: this .m_dateLabel = new JLabel("Date");
169: this .m_dateLabel.setFont(this .getFont());
170: this .add(this .m_dateLabel);
171:
172: this .m_dateSpinner = this .getDateSpinner();
173: this .add(m_dateSpinner);
174:
175: this .m_dateSpinner.getModel().addChangeListener(this );
176: ((JSpinner.DateEditor) this .m_dateSpinner.getEditor())
177: .getTextField().addKeyListener(this );
178: }
179:
180: if (this .m_bShowTime) {
181:
182: this .m_timeLabel = new JLabel("Time");
183: this .m_timeLabel.setFont(this .getFont());
184: this .add(this .m_timeLabel);
185:
186: this .m_timeSpinner = this .getTimeSpinner();
187: this .add(m_timeSpinner);
188:
189: this .m_timeSpinner.getModel().addChangeListener(this );
190: ((JSpinner.DateEditor) this .m_timeSpinner.getEditor())
191: .getTextField().addKeyListener(this );
192: }
193:
194: if (this .m_bShowClearButton) {
195: this .m_clearButton = new JButton("Clear");
196: this .m_clearButton.setFont(this .getFont());
197: this .m_clearButton.setActionCommand(CLEAR_ACTION);
198: this .m_clearButton.addActionListener(this );
199: this .add(m_clearButton);
200: }
201: this .setCleared(this .m_bCleared);
202: }
203:
204: public Date getValue() {
205: if (this .m_bCleared) {
206: return null;
207: } else {
208: StringBuffer sBuff = new StringBuffer();
209: if (this .m_bShowDate) {
210: sBuff.append(this .getDateValue());
211: } else {
212: SimpleDateFormat dtDF = new SimpleDateFormat(
213: "yyyy-MM-dd G");
214: sBuff.append(dtDF.format(new Date()));
215: }
216:
217: if (this .m_bShowTime) {
218: sBuff.append("T").append(this .getTimeValue()).append(
219: ":00");
220: } else {
221: sBuff.append("T00:00:00");
222: }
223:
224: Date dtRetn = null;
225:
226: try {
227: dtRetn = this .m_df.parse(sBuff.toString());
228: } catch (ParseException e) {
229: e.printStackTrace();
230: }
231:
232: return dtRetn;
233: }
234: }
235:
236: private String getDateValue() {
237: SimpleDateFormat dtDF = new SimpleDateFormat("yyyy-MM-dd G");
238: String sValue = dtDF
239: .format(((Date) ((JSpinner) this .m_dateSpinner)
240: .getValue()));
241: return sValue.trim();
242: }
243:
244: private String getTimeValue() {
245: SimpleDateFormat dtDF = new SimpleDateFormat("HH:mm:ss");
246: String sValue = dtDF
247: .format(((Date) ((JSpinner) this .m_timeSpinner)
248: .getValue()));
249: return sValue.trim();
250: }
251:
252: private JSpinner getDateSpinner() {
253: JSpinner spinner = null;
254:
255: if (this .m_minDate != null
256: && this .m_initDate.getTime() < this .m_minDate.getTime()) {
257: this .m_initDate = this .m_minDate;
258: }
259:
260: if (this .m_maxDate != null
261: && this .m_initDate.getTime() > this .m_maxDate.getTime()) {
262: this .m_initDate = this .m_maxDate;
263: }
264:
265: SpinnerModel dateModel = new SpinnerDateModel(this .m_initDate,
266: this .m_minDate, this .m_maxDate, Calendar.YEAR);
267:
268: spinner = new JSpinner(dateModel);
269: spinner.setBorder(BorderFactory.createEtchedBorder());
270:
271: spinner.setEditor(new JSpinner.DateEditor(spinner,
272: this .m_sDateFormat));
273:
274: return spinner;
275: }
276:
277: private JSpinner getTimeSpinner() {
278: JSpinner spinner = null;
279:
280: SpinnerModel dateModel = dateModel = new SpinnerDateModel(
281: this .m_initDate, this .m_minDate, this .m_maxDate,
282: Calendar.YEAR);
283:
284: spinner = new JSpinner(dateModel);
285: spinner.setBorder(BorderFactory.createEtchedBorder());
286:
287: spinner.setEditor(new JSpinner.DateEditor(spinner,
288: this .m_sTimeFormat));
289:
290: return spinner;
291: }
292:
293: /* (non-Javadoc)
294: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
295: */
296: public void layoutContainer(Container arg0) {
297: int nXpos = 0;
298:
299: if (this .m_bShowDate) {
300: this .m_dateLabel.setSize(25, 20);
301: this .m_dateLabel.setLocation(nXpos, 0);
302: nXpos = nXpos + this .m_dateLabel.getSize().width + 10;
303:
304: this .m_dateSpinner.setSize(100, 20);
305: this .m_dateSpinner.setLocation(nXpos, 0);
306: nXpos = nXpos + this .m_dateSpinner.getSize().width + 10;
307: }
308:
309: if (this .m_bShowTime) {
310: this .m_timeLabel.setSize(25, 20);
311: this .m_timeLabel.setLocation(nXpos, 0);
312: nXpos = nXpos + this .m_timeLabel.getSize().width + 10;
313:
314: this .m_timeSpinner.setSize(50, 20);
315: this .m_timeSpinner.setLocation(nXpos, 0);
316: nXpos = nXpos + this .m_timeSpinner.getSize().width + 10;
317: }
318:
319: if (this .m_bShowClearButton) {
320: this .m_clearButton.setSize(70, 20);
321: this .m_clearButton.setLocation(nXpos, 0);
322: }
323: }
324:
325: /* (non-Javadoc)
326: * @see java.awt.Component#getPreferredSize()
327: */
328: public Dimension getPreferredSize() {
329: int nHeight = 25;
330: int nWidth = 0;
331:
332: if (this .m_bShowTime) {
333: nWidth = nWidth + 135;
334: }
335:
336: nWidth = nWidth + this .m_clearButton.getSize().width;
337:
338: nWidth = nWidth + 215;
339:
340: return new Dimension(nWidth, nHeight);
341: }
342:
343: /**
344: * @param arg0
345: */
346: private DateSelector(boolean arg0) {
347: super (arg0);
348: }
349:
350: /**
351: * @param arg0
352: */
353: private DateSelector(LayoutManager arg0) {
354: super (arg0);
355: }
356:
357: /**
358: * @param arg0
359: * @param arg1
360: */
361: private DateSelector(LayoutManager arg0, boolean arg1) {
362: super (arg0, arg1);
363: }
364:
365: /* (non-Javadoc)
366: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
367: */
368: public void removeLayoutComponent(Component arg0) {
369: }
370:
371: /* (non-Javadoc)
372: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
373: */
374: public void addLayoutComponent(String arg0, Component arg1) {
375: }
376:
377: /* (non-Javadoc)
378: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
379: */
380: public Dimension minimumLayoutSize(Container arg0) {
381: return this .getPreferredSize();
382: }
383:
384: /* (non-Javadoc)
385: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
386: */
387: public Dimension preferredLayoutSize(Container arg0) {
388: return this .getPreferredSize();
389: }
390:
391: /* (non-Javadoc)
392: * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
393: */
394: public void stateChanged(ChangeEvent ce) {
395: if (ce != null) {
396: this .setCleared(false);
397: }
398: ChangeEvent ce2 = new ChangeEvent(this );
399: Iterator itor = this .m_changeListeners.iterator();
400: while (itor.hasNext()) {
401: ChangeListener listener = (ChangeListener) itor.next();
402: listener.stateChanged(ce2);
403: }
404: }
405:
406: /* (non-Javadoc)
407: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
408: */
409: public void keyReleased(KeyEvent ke) {
410: if (ke != null) {
411: this .setCleared(false);
412: }
413: ChangeEvent ce2 = new ChangeEvent(this );
414: Iterator itor = this .m_changeListeners.iterator();
415: while (itor.hasNext()) {
416: ChangeListener listener = (ChangeListener) itor.next();
417: listener.stateChanged(ce2);
418: }
419: }
420:
421: public void setCleared(boolean bCleared) {
422: if (bCleared) {
423: this .m_bCleared = true;
424:
425: if (this .m_dateSpinner != null) {
426: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
427: .getEditor()).getTextField().setForeground(
428: Color.WHITE);
429: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
430: .getEditor()).getTextField()
431: .setSelectedTextColor(Color.WHITE);
432: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
433: .getEditor()).getTextField().setSelectionColor(
434: Color.WHITE);
435: }
436:
437: if (this .m_timeSpinner != null) {
438: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
439: .getEditor()).getTextField().setForeground(
440: Color.WHITE);
441: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
442: .getEditor()).getTextField()
443: .setSelectedTextColor(Color.WHITE);
444: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
445: .getEditor()).getTextField().setSelectionColor(
446: Color.WHITE);
447: }
448: SwingUtilities.invokeLater(this );
449: this .stateChanged(null);
450: } else {
451: this .m_bCleared = false;
452:
453: if (this .m_dateSpinner != null) {
454: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
455: .getEditor()).getTextField().setForeground(
456: Color.BLACK);
457: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
458: .getEditor()).getTextField()
459: .setSelectedTextColor(Color.WHITE);
460: ((JSpinner.DefaultEditor) ((JSpinner) this .m_dateSpinner)
461: .getEditor()).getTextField().setSelectionColor(
462: Color.BLUE);
463: }
464:
465: if (this .m_timeSpinner != null) {
466: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
467: .getEditor()).getTextField().setForeground(
468: Color.BLACK);
469: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
470: .getEditor()).getTextField()
471: .setSelectedTextColor(Color.WHITE);
472: ((JSpinner.DefaultEditor) ((JSpinner) this .m_timeSpinner)
473: .getEditor()).getTextField().setSelectionColor(
474: Color.BLUE);
475: }
476: SwingUtilities.invokeLater(this );
477: }
478: }
479:
480: /* (non-Javadoc)
481: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
482: */
483: public void actionPerformed(ActionEvent ae) {
484: if (ae.getActionCommand().equals(CLEAR_ACTION)) {
485: this .setCleared(true);
486: }
487: }
488:
489: /* (non-Javadoc)
490: * @see java.lang.Runnable#run()
491: */
492: public void run() {
493: this .revalidate();
494: this .repaint();
495: }
496:
497: /* (non-Javadoc)
498: * @see java.awt.Component#setEnabled(boolean)
499: */
500: public void setEnabled(boolean bEnabled) {
501: super .setEnabled(bEnabled);
502: if (this .m_dateSpinner != null) {
503: this .m_dateSpinner.setEnabled(bEnabled);
504: }
505: if (this .m_timeSpinner != null) {
506: this .m_timeSpinner.setEnabled(bEnabled);
507: }
508: if (this .m_clearButton != null) {
509: this .m_clearButton.setEnabled(bEnabled);
510: }
511: }
512:
513: /* (non-Javadoc)
514: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
515: */
516: public void keyPressed(KeyEvent arg0) {
517: }
518:
519: /* (non-Javadoc)
520: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
521: */
522: public void keyTyped(KeyEvent arg0) {
523: }
524:
525: /* (non-Javadoc)
526: * @see java.awt.Component#setFont(java.awt.Font)
527: */
528: public void setFont(Font arg0) {
529: super.setFont(arg0);
530: if (this.m_dateLabel != null) {
531: this.m_dateLabel.setFont(arg0);
532: }
533: if (this.m_timeLabel != null) {
534: this.m_timeLabel.setFont(arg0);
535: }
536: }
537:
538: }
|