KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > relatedevents > RelatedEventDate


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing.relatedevents;
20
21 import java.awt.*;
22 import java.text.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.him.metadata.range.swing.*;
29 import org.openharmonise.swing.datefield.*;
30 import org.openharmonise.vfs.metadata.*;
31 import org.openharmonise.vfs.metadata.range.*;
32 import org.openharmonise.vfs.metadata.value.*;
33
34
35 /**
36  *
37  * @author Michael Bell
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class RelatedEventDate extends AbstractRangeDisplay implements DateFieldListener, RangeDisplay, LayoutManager {
42     private static final String JavaDoc XSD_DATE_FORMAT = "yyyy-MM-dd G";
43     private SimpleDateFormat df =
44         new java.text.SimpleDateFormat JavaDoc(XSD_DATE_FORMAT);
45
46     private String JavaDoc m_sDateFormat = "yyyy/MM/dd G";
47     private JDateField m_dateField = null;
48     
49     /**
50      * @param propInstance
51      */

52     public RelatedEventDate(PropertyInstance propInstance) {
53         super(propInstance);
54         setup();
55     }
56
57     /**
58      *
59      */

60     private void setup() {
61         this.setLayout(this);
62         
63         DateRange range =
64                     (DateRange) this.getPropertyInstance().getDefinition().getRange();
65
66         PropertyInstance propInst = getPropertyInstance();
67         
68         List JavaDoc vals = propInst.getValues();
69         
70         Date dt = null;
71         
72         if(vals.size() > 0) {
73             String JavaDoc sDate = ((DateValue) vals.get(0)).getValue();
74             
75             try {
76                 dt = df.parse(sDate);
77             } catch (ParseException e) {
78                 e.printStackTrace();
79             }
80         } else {
81         }
82         
83         this.m_dateField = new JDateField(XSD_DATE_FORMAT);
84         if(dt!=null) {
85             this.m_dateField.setDate(dt);
86         }
87         this.m_dateField.setMinimumDate(range.getMinimum());
88         this.m_dateField.setMaximumDate(range.getMaximum());
89         this.m_dateField.addDateFieldListener(this);
90         
91         this.add(m_dateField);
92     }
93
94     /* (non-Javadoc)
95      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
96      */

97     public void layoutContainer(Container arg0) {
98         this.m_dateField.setSize(this.m_dateField.getPreferredSize());
99         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 JavaDoc vals = propInst.getValues();
110         
111         if(vals != null && vals.size() > 0) {
112             try {
113                 Date dt = df.parse(((DateValue)vals.get(0)).getValue());
114                 Date now = new Date();
115                 
116                 if(dt.after(now)) {
117                     bIsValid = false;
118                 }
119             } catch (ParseException e) {
120                 e.printStackTrace();
121             }
122         }
123         
124         if(bIsValid == true) {
125             Property prop = getPropertyInstance().getDefinition();
126         
127             DateRange range = (DateRange)prop.getRange();
128
129             bIsValid = range.validate(propInst.getValues()).isValid();
130         }
131         
132         if(bIsValid == false) {
133             m_dateField.setForeground(Color.RED);
134             
135         } else if(m_dateField.hasValue() == false) {
136             m_dateField.setForeground(Color.BLACK);
137         }
138         
139         return bIsValid;
140     }
141
142     /* (non-Javadoc)
143      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
144      */

145     public JPanel getPanel() {
146         
147         return this;
148     }
149
150     /* (non-Javadoc)
151      * @see java.awt.Component#getPreferredSize()
152      */

153     public Dimension getPreferredSize() {
154         return m_dateField.getPreferredSize();
155     }
156     
157     protected String JavaDoc getValue() {
158         SimpleDateFormat dtDF = new SimpleDateFormat( "yyyy-MM-dd G" );
159         String JavaDoc sValue = "";
160         try {
161             sValue = dtDF.format( this.m_dateField.getDate() );
162         } catch(Exception JavaDoc e) {
163             e.printStackTrace();
164         }
165         return sValue.trim();
166     }
167
168     /* (non-Javadoc)
169      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
170      */

171     public void removeLayoutComponent(Component arg0) {
172     }
173
174     /* (non-Javadoc)
175      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
176      */

177     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
178     }
179
180     /* (non-Javadoc)
181      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
182      */

183     public Dimension minimumLayoutSize(Container arg0) {
184         return this.getPreferredSize();
185     }
186
187     /* (non-Javadoc)
188      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
189      */

190     public Dimension preferredLayoutSize(Container arg0) {
191         return this.getPreferredSize();
192     }
193
194     /* (non-Javadoc)
195      * @see com.simulacramedia.swing.datefield.DateFieldListener#validationFailed(com.simulacramedia.swing.datefield.JDateField, int)
196      */

197     public void validationFailed(JDateField source, int nReason) {
198         m_dateField.setForeground(Color.RED);
199     }
200
201     /* (non-Javadoc)
202      * @see com.simulacramedia.swing.datefield.DateFieldListener#valueChanged(com.simulacramedia.swing.datefield.JDateField)
203      */

204     public void valueChanged(JDateField source) {
205         DateValue val = (DateValue)getPropertyInstance().getNewValueInstance();
206         val.setValue(getValue());
207         this.getPropertyInstance().setValue(val);
208         firePropertyChange(RelatedEventValueDisplay.PROPERTY_METADATA_CHANGE, null, null);
209         m_dateField.setForeground(Color.BLACK);
210     }
211
212 }
213
Popular Tags