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.relatedevents;
020:
021: import java.awt.*;
022: import java.text.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.him.metadata.range.swing.*;
029: import org.openharmonise.swing.datefield.*;
030: import org.openharmonise.vfs.metadata.*;
031: import org.openharmonise.vfs.metadata.range.*;
032: import org.openharmonise.vfs.metadata.value.*;
033:
034: /**
035: *
036: * @author Michael Bell
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class RelatedEventDate extends AbstractRangeDisplay implements
041: DateFieldListener, RangeDisplay, LayoutManager {
042: private static final String XSD_DATE_FORMAT = "yyyy-MM-dd G";
043: private SimpleDateFormat df = new java.text.SimpleDateFormat(
044: XSD_DATE_FORMAT);
045:
046: private String m_sDateFormat = "yyyy/MM/dd G";
047: private JDateField m_dateField = null;
048:
049: /**
050: * @param propInstance
051: */
052: public RelatedEventDate(PropertyInstance propInstance) {
053: super (propInstance);
054: setup();
055: }
056:
057: /**
058: *
059: */
060: private void setup() {
061: this .setLayout(this );
062:
063: DateRange range = (DateRange) this .getPropertyInstance()
064: .getDefinition().getRange();
065:
066: PropertyInstance propInst = getPropertyInstance();
067:
068: List vals = propInst.getValues();
069:
070: Date dt = null;
071:
072: if (vals.size() > 0) {
073: String sDate = ((DateValue) vals.get(0)).getValue();
074:
075: try {
076: dt = df.parse(sDate);
077: } catch (ParseException e) {
078: e.printStackTrace();
079: }
080: } else {
081: }
082:
083: this .m_dateField = new JDateField(XSD_DATE_FORMAT);
084: if (dt != null) {
085: this .m_dateField.setDate(dt);
086: }
087: this .m_dateField.setMinimumDate(range.getMinimum());
088: this .m_dateField.setMaximumDate(range.getMaximum());
089: this .m_dateField.addDateFieldListener(this );
090:
091: this .add(m_dateField);
092: }
093:
094: /* (non-Javadoc)
095: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
096: */
097: public void layoutContainer(Container arg0) {
098: this .m_dateField.setSize(this .m_dateField.getPreferredSize());
099: this .m_dateField.setLocation(0, 0);
100: }
101:
102: /* (non-Javadoc)
103: * @see java.awt.Component#isValid()
104: */
105: public boolean isMetadataValid() {
106: boolean bIsValid = true;
107: PropertyInstance propInst = getPropertyInstance();
108:
109: List vals = propInst.getValues();
110:
111: if (vals != null && vals.size() > 0) {
112: try {
113: Date dt = df
114: .parse(((DateValue) vals.get(0)).getValue());
115: Date now = new Date();
116:
117: if (dt.after(now)) {
118: bIsValid = false;
119: }
120: } catch (ParseException e) {
121: e.printStackTrace();
122: }
123: }
124:
125: if (bIsValid == true) {
126: Property prop = getPropertyInstance().getDefinition();
127:
128: DateRange range = (DateRange) prop.getRange();
129:
130: bIsValid = range.validate(propInst.getValues()).isValid();
131: }
132:
133: if (bIsValid == false) {
134: m_dateField.setForeground(Color.RED);
135:
136: } else if (m_dateField.hasValue() == false) {
137: m_dateField.setForeground(Color.BLACK);
138: }
139:
140: return bIsValid;
141: }
142:
143: /* (non-Javadoc)
144: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
145: */
146: public JPanel getPanel() {
147:
148: return this ;
149: }
150:
151: /* (non-Javadoc)
152: * @see java.awt.Component#getPreferredSize()
153: */
154: public Dimension getPreferredSize() {
155: return m_dateField.getPreferredSize();
156: }
157:
158: protected String getValue() {
159: SimpleDateFormat dtDF = new SimpleDateFormat("yyyy-MM-dd G");
160: String sValue = "";
161: try {
162: sValue = dtDF.format(this .m_dateField.getDate());
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: return sValue.trim();
167: }
168:
169: /* (non-Javadoc)
170: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
171: */
172: public void removeLayoutComponent(Component arg0) {
173: }
174:
175: /* (non-Javadoc)
176: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
177: */
178: public void addLayoutComponent(String arg0, Component arg1) {
179: }
180:
181: /* (non-Javadoc)
182: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
183: */
184: public Dimension minimumLayoutSize(Container arg0) {
185: return this .getPreferredSize();
186: }
187:
188: /* (non-Javadoc)
189: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
190: */
191: public Dimension preferredLayoutSize(Container arg0) {
192: return this .getPreferredSize();
193: }
194:
195: /* (non-Javadoc)
196: * @see com.simulacramedia.swing.datefield.DateFieldListener#validationFailed(com.simulacramedia.swing.datefield.JDateField, int)
197: */
198: public void validationFailed(JDateField source, int nReason) {
199: m_dateField.setForeground(Color.RED);
200: }
201:
202: /* (non-Javadoc)
203: * @see com.simulacramedia.swing.datefield.DateFieldListener#valueChanged(com.simulacramedia.swing.datefield.JDateField)
204: */
205: public void valueChanged(JDateField source) {
206: DateValue val = (DateValue) getPropertyInstance()
207: .getNewValueInstance();
208: val.setValue(getValue());
209: this.getPropertyInstance().setValue(val);
210: firePropertyChange(
211: RelatedEventValueDisplay.PROPERTY_METADATA_CHANGE,
212: null, null);
213: m_dateField.setForeground(Color.BLACK);
214: }
215:
216: }
|