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