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.*;
022: import java.awt.event.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026: import javax.swing.event.*;
027:
028: /**
029: *
030: * @author Michael Bell
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class ClearableDateSelector extends JSpinner implements
035: ChangeListener, Runnable, KeyListener {
036:
037: private Date m_dtTempVal;
038:
039: private String m_sFormat = null;
040:
041: private boolean m_bCleared = false;
042:
043: private boolean m_bIgnoreChanges = false;
044:
045: /**
046: *
047: */
048: public ClearableDateSelector(Date initDate, Date minDate,
049: Date maxDate, String sFormat) {
050: super ();
051: if (minDate != null && initDate.getTime() < minDate.getTime()) {
052: initDate = minDate;
053: }
054:
055: if (maxDate != null && initDate.getTime() > maxDate.getTime()) {
056: initDate = maxDate;
057: }
058:
059: m_sFormat = sFormat;
060:
061: //can't pass null to spinner date model as init date
062: if (initDate == null) {
063: m_bCleared = true;
064: initDate = new Date();
065: }
066:
067: SpinnerModel dateModel = new SpinnerDateModel(initDate,
068: minDate, maxDate, Calendar.YEAR);
069:
070: setModel(dateModel);
071:
072: JSpinner.DateEditor editor = new JSpinner.DateEditor(this ,
073: m_sFormat);
074:
075: editor.getTextField().addKeyListener(this );
076:
077: setEditor(editor);
078:
079: if (m_bCleared == true) {
080: setCleared(true);
081: }
082:
083: this .addChangeListener(this );
084: }
085:
086: public void setCleared(boolean bCleared) {
087: if (bCleared) {
088: this .m_bCleared = true;
089:
090: ((JSpinner.DefaultEditor) getEditor()).getTextField()
091: .setForeground(Color.WHITE);
092: ((JSpinner.DefaultEditor) getEditor()).getTextField()
093: .setSelectedTextColor(Color.WHITE);
094: ((JSpinner.DefaultEditor) getEditor()).getTextField()
095: .setSelectionColor(Color.WHITE);
096:
097: } else {
098: this .m_bCleared = false;
099:
100: ((JSpinner.DefaultEditor) getEditor()).getTextField()
101: .setForeground(Color.BLACK);
102: ((JSpinner.DefaultEditor) getEditor()).getTextField()
103: .setSelectedTextColor(Color.WHITE);
104: ((JSpinner.DefaultEditor) getEditor()).getTextField()
105: .setSelectionColor(Color.BLUE);
106: }
107: SwingUtilities.invokeLater(this );
108: }
109:
110: public boolean isCleared() {
111: return m_bCleared;
112: }
113:
114: /* (non-Javadoc)
115: * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
116: */
117: public void stateChanged(ChangeEvent e) {
118: if (m_bCleared == true && m_bIgnoreChanges == false) {
119: setCleared(false);
120: }
121: }
122:
123: /* (non-Javadoc)
124: * @see java.lang.Runnable#run()
125: */
126: public void run() {
127: this .revalidate();
128: this .repaint();
129:
130: }
131:
132: /* (non-Javadoc)
133: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
134: */
135: public void keyPressed(KeyEvent e) {
136: m_bIgnoreChanges = true;
137: if (e.getKeyCode() == KeyEvent.VK_DELETE) {
138:
139: }
140:
141: }
142:
143: /* (non-Javadoc)
144: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
145: */
146: public void keyReleased(KeyEvent e) {
147: if (e.getKeyCode() == KeyEvent.VK_DELETE) {
148:
149: ((JSpinner.DefaultEditor) getEditor()).getTextField()
150: .setValue(new Date());
151:
152: setCleared(true);
153: }
154:
155: }
156:
157: /* (non-Javadoc)
158: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
159: */
160: public void keyTyped(KeyEvent e) {
161: m_bIgnoreChanges = false;
162: }
163:
164: }
|