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.him.metadata.range.swing.datetimehandling;
020:
021: import java.awt.*;
022: import java.text.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026:
027: import org.openharmonise.him.metadata.range.swing.*;
028: import org.openharmonise.swing.datefield.*;
029: import org.openharmonise.vfs.gui.*;
030: import org.openharmonise.vfs.metadata.*;
031: import org.openharmonise.vfs.metadata.range.*;
032:
033: /**
034: * @author Matthew Large
035: *
036: */
037: public class DateTimeValuePanel extends AbstractRangeDisplay implements
038: RangeDisplay, LayoutManager, DateFieldListener {
039:
040: private static final String XSD_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G";
041: private SimpleDateFormat df = new java.text.SimpleDateFormat(
042: XSD_DATE_FORMAT);
043:
044: protected int m_nHeight = 10;
045:
046: private String m_sPreviousText = "";
047:
048: protected WarningsLabel m_warnings = null;
049:
050: protected JLabel m_errorLabel = null;
051:
052: private String m_sValue = "";
053:
054: private DateTimeRangeDisplay m_display = null;
055:
056: private JDateField m_dateField = null;
057:
058: /**
059: * @param propInstance
060: */
061: public DateTimeValuePanel(DateTimeRangeDisplay display,
062: PropertyInstance propInstance, String sValue) {
063: super (propInstance);
064: this .m_display = display;
065: this .m_sValue = sValue;
066: this .setup();
067: }
068:
069: /* (non-Javadoc)
070: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
071: */
072: public JPanel getPanel() {
073: return null;
074: }
075:
076: public void setup() {
077: this .setLayout(this );
078: DateTimeRange range = (DateTimeRange) this
079: .getPropertyInstance().getDefinition().getRange();
080:
081: String sWarning = "";
082:
083: this .m_errorLabel = new JLabel();
084: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
085: "16-message-confirm.gif"));
086: this .add(this .m_errorLabel);
087:
088: if (range.getMinimum() != null && range.getMaximum() != null) {
089: sWarning = "Please enter a date (ccyy/MM/dd HH:mm) between {"
090: + this .df.format(range.getMinimum())
091: + "} and {"
092: + this .df.format(range.getMaximum()) + "}.";
093: } else if (range.getMaximum() != null) {
094: sWarning = "Please enter a date (ccyy/MM/dd HH:mm) before {"
095: + this .df.format(range.getMaximum()) + "}.";
096: } else if (range.getMinimum() != null) {
097: sWarning = "Please enter a date (ccyy/MM/dd HH:mm) after {"
098: + this .df.format(range.getMinimum()) + "}.";
099: } else {
100: sWarning = "Please enter a date (ccyy/MM/dd HH:mm).";
101: }
102:
103: m_warnings = new WarningsLabel(sWarning);
104: this .add(m_warnings);
105:
106: this .m_dateField = new JDateField("yyyy/MM/dd HH:mm G");
107: this .m_dateField.setBorder(BorderFactory.createEtchedBorder());
108: this .m_dateField.addDateFieldListener(this );
109: this .m_dateField.setMinimumDate(range.getMinimum());
110: this .m_dateField.setMaximumDate(range.getMaximum());
111: if (this .m_sValue != null && !this .m_sValue.equals("")) {
112: Date dtValue = null;
113: try {
114: dtValue = df.parse(this .m_sValue);
115: } catch (Exception e) {
116: //NO-OP
117: }
118: if (dtValue != null) {
119: this .m_dateField.setDate(dtValue);
120: }
121: }
122: this .add(this .m_dateField);
123: }
124:
125: /* (non-Javadoc)
126: * @see java.awt.Component#getPreferredSize()
127: */
128: public Dimension getPreferredSize() {
129: this .layoutContainer(null);
130: int nWidth = this .m_warnings.getSize().width
131: + this .m_warnings.getLocation().x;
132: if (this .m_dateField.getSize().width > nWidth) {
133: nWidth = this .m_dateField.getSize().width;
134: }
135: nWidth = nWidth + 60;
136:
137: return new Dimension(nWidth, m_nHeight);
138: }
139:
140: /* (non-Javadoc)
141: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
142: */
143: public void removeLayoutComponent(Component arg0) {
144: // NO-OP
145: }
146:
147: /* (non-Javadoc)
148: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
149: */
150: public void layoutContainer(Container arg0) {
151: int nWidth = this .getParent().getWidth() - 18;
152:
153: m_warnings.setSize(m_warnings.getPreferredSize());
154: m_warnings.setLocation(20, 0);
155:
156: m_nHeight = m_warnings.getPreferredSize().height + 10;
157:
158: int nXpos = 20;
159:
160: this .m_dateField.setSize(this .m_dateField.getPreferredSize());
161: this .m_dateField.setLocation(nXpos, m_nHeight);
162: nXpos = nXpos + this .m_dateField.getSize().width + 10;
163: m_nHeight = m_nHeight + m_dateField.getPreferredSize().height
164: + 10;
165: this .m_errorLabel.setLocation(0, 0);
166: this .m_errorLabel.setSize(15, 15);
167: this .repaint();
168: }
169:
170: /* (non-Javadoc)
171: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
172: */
173: public void addLayoutComponent(String arg0, Component arg1) {
174: // NO-OP
175: }
176:
177: /* (non-Javadoc)
178: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
179: */
180: public Dimension minimumLayoutSize(Container arg0) {
181: return this .getPreferredSize();
182: }
183:
184: /* (non-Javadoc)
185: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
186: */
187: public Dimension preferredLayoutSize(Container arg0) {
188: return this .getPreferredSize();
189: }
190:
191: protected String getValue() {
192: Date dtValue = this .m_dateField.getDate();
193: String sValue = "";
194: try {
195: sValue = this .df.format(dtValue);
196: } catch (Exception e) {
197: //NO-OP
198: }
199:
200: return sValue;
201: }
202:
203: /* (non-Javadoc)
204: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
205: */
206: public boolean isMetadataValid() {
207: return !this .m_errorLabel.getIcon().toString().equals(
208: IconManager.getInstance().getIcon(
209: "16-message-error.gif").toString());
210: }
211:
212: private JSpinner getDateSpinner(DateTimeRange range, String sValue) {
213: JSpinner spinner = null;
214: Date dtMax = range.getMaximum();
215: Date dtMin = range.getMinimum();
216:
217: Date dtNow = null;
218:
219: if (sValue != null && sValue.length() > 0) {
220: try {
221: dtNow = this .df.parse(sValue);
222: } catch (ParseException e) {
223: e.printStackTrace();
224: dtNow = new Date();
225: }
226: } else {
227: dtNow = new Date();
228: }
229:
230: SpinnerModel dateModel = dateModel = new SpinnerDateModel(
231: dtNow, dtMin, dtMax, Calendar.YEAR);
232:
233: spinner = new JSpinner(dateModel);
234:
235: spinner
236: .setEditor(new JSpinner.DateEditor(spinner,
237: "yyyy/MM/dd"));
238:
239: return spinner;
240: }
241:
242: private JSpinner getTimeSpinner(DateTimeRange range, String sValue) {
243: JSpinner spinner = null;
244: Date dtMax = range.getMaximum();
245: Date dtMin = range.getMinimum();
246:
247: Date dtNow = null;
248:
249: if (sValue != null && sValue.length() > 0) {
250: try {
251: dtNow = this .df.parse(sValue);
252: } catch (ParseException e) {
253: e.printStackTrace();
254: dtNow = new Date();
255: }
256: } else {
257: dtNow = new Date();
258: }
259:
260: SpinnerModel dateModel = dateModel = new SpinnerDateModel(
261: dtNow, dtMin, dtMax, Calendar.YEAR);
262:
263: spinner = new JSpinner(dateModel);
264:
265: spinner.setEditor(new JSpinner.DateEditor(spinner, "HH:mm"));
266:
267: return spinner;
268: }
269:
270: /* (non-Javadoc)
271: * @see com.simulacramedia.swing.datefield.DateFieldListener#validationFailed(com.simulacramedia.swing.datefield.JDateField, int)
272: */
273: public void validationFailed(JDateField source, int nReason) {
274: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
275: "16-message-error.gif"));
276: super .validateTab();
277: }
278:
279: /* (non-Javadoc)
280: * @see com.simulacramedia.swing.datefield.DateFieldListener#valueChanged(com.simulacramedia.swing.datefield.JDateField)
281: */
282: public void valueChanged(JDateField source) {
283: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
284: "16-message-confirm.gif"));
285: this.m_display.valueChanged(this.getValue().trim());
286: super.validateTab();
287: }
288:
289: }
|