KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > datehandling > DateValuePanel


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.datehandling;
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.gui.*;
31 import org.openharmonise.vfs.metadata.*;
32 import org.openharmonise.vfs.metadata.range.*;
33
34
35 /**
36  * Component to display date property values in the metadata window.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class DateValuePanel
43     extends AbstractRangeDisplay
44     implements RangeDisplay, LayoutManager, DateFieldListener {
45
46     /**
47      * Date format string.
48      */

49     private static final String JavaDoc XSD_DATE_FORMAT = "yyyy-MM-dd G";
50     
51     /**
52      * Date formatter.
53      */

54     private SimpleDateFormat df =
55         new java.text.SimpleDateFormat JavaDoc(XSD_DATE_FORMAT);
56
57     /**
58      * Height of component.
59      */

60     protected int m_nHeight = 10;
61     
62     /**
63      * Date field component.
64      */

65     private JDateField m_dateField = null;
66
67     /**
68      * Previous string value.
69      */

70     private String JavaDoc m_sPreviousText = "";
71
72     /**
73      * Warnings label.
74      */

75     protected WarningsLabel m_warnings = null;
76
77     /**
78      * Error label
79      */

80     protected JLabel m_errorLabel = null;
81
82     /**
83      * String value.
84      */

85     private String JavaDoc m_sValue = "";
86     
87     /**
88      * Parent range display component.
89      */

90     private DateRangeDisplay m_display = null;
91     
92     /**
93      *
94      */

95     private boolean m_bNoValueAllowed = true;
96     
97     /**
98      * @param propInstance
99      */

100     public DateValuePanel(DateRangeDisplay display, PropertyInstance propInstance, String JavaDoc sValue, 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 =
122             (DateRange) this.getPropertyInstance().getDefinition().getRange();
123
124         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
125         Iterator itor = domains.iterator();
126         
127
128         String JavaDoc sWarning = "";
129
130         this.m_errorLabel = new JLabel();
131         this.m_errorLabel.setIcon(
132             IconManager.getInstance().getIcon("16-message-confirm.gif"));
133         this.add(this.m_errorLabel);
134
135         if (range.getMinimum() != null && range.getMaximum() != null) {
136             sWarning =
137                 "Please enter a date (ccyy/MM/dd) between {"
138                     + this.df.format(range.getMinimum())
139                     + "} and {"
140                     + this.df.format(range.getMaximum())
141                     + "}.";
142         } else if (range.getMaximum() != null) {
143             sWarning =
144                 "Please enter a date (ccyy/MM/dd) before {"
145                     + this.df.format(range.getMaximum())
146                     + "}.";
147         } else if (range.getMinimum() != null) {
148             sWarning =
149                 "Please enter a date (ccyy/MM/dd) after {"
150                     + this.df.format(range.getMinimum())
151                     + "}.";
152         } else {
153             sWarning = "Please enter a date (ccyy/MM/dd).";
154         }
155
156         m_warnings = new WarningsLabel(sWarning);
157         this.add(m_warnings);
158         
159         this.m_dateField = new JDateField("yyyy/MM/dd G");
160         this.m_dateField.setBorder( BorderFactory.createEtchedBorder() );
161         this.m_dateField.addDateFieldListener(this);
162         this.m_dateField.setMinimumDate(range.getMinimum());
163         this.m_dateField.setMaximumDate(range.getMaximum());
164         if(this.m_sValue!=null && !this.m_sValue.equals("")) {
165             Date dtValue = null;
166             try {
167                 dtValue = df.parse(this.m_sValue);
168             } catch (Exception JavaDoc e) {
169                 //NO-OP
170
}
171             if(dtValue!=null) {
172                 this.m_dateField.setDate(dtValue);
173             }
174         }
175         this.add(this.m_dateField);
176         
177     }
178
179     /* (non-Javadoc)
180      * @see java.awt.Component#getPreferredSize()
181      */

182     public Dimension getPreferredSize() {
183         this.layoutContainer(null);
184         int nWidth = this.m_warnings.getSize().width + this.m_warnings.getLocation().x;
185         if(this.m_dateField.getSize().width>nWidth) {
186             nWidth =this.m_dateField.getSize().width;
187         }
188         nWidth = nWidth + 60;
189         int nHeight = 10;
190
191         nHeight = nHeight + this.m_dateField.getSize().height;
192         return new Dimension(nWidth, nHeight);
193     }
194
195     /* (non-Javadoc)
196      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
197      */

198     public void removeLayoutComponent(Component arg0) {
199         // NO-OP
200
}
201
202     /* (non-Javadoc)
203      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
204      */

205     public void layoutContainer(Container arg0) {
206         int nWidth = this.getParent().getWidth() - 18;
207         
208         int nXpos = 20;
209         
210         this.m_dateField.setSize(this.m_dateField.getPreferredSize());
211         this.m_dateField.setLocation(nXpos, 0);
212         nXpos = nXpos + this.m_dateField.getSize().width + 10;
213
214         this.m_errorLabel.setLocation(0,0);
215         this.m_errorLabel.setSize(15, 15);
216         this.repaint();
217     }
218
219     /* (non-Javadoc)
220      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
221      */

222     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
223         // NO-OP
224
}
225
226     /* (non-Javadoc)
227      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
228      */

229     public Dimension minimumLayoutSize(Container arg0) {
230         return this.getPreferredSize();
231     }
232
233     /* (non-Javadoc)
234      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
235      */

236     public Dimension preferredLayoutSize(Container arg0) {
237         return this.getPreferredSize();
238     }
239
240     protected String JavaDoc getValue() {
241         Date dtValue = this.m_dateField.getDate();
242         String JavaDoc sValue = "";
243         try {
244             if(dtValue != null) {
245                 sValue = this.df.format(dtValue);
246             }
247         } catch(Exception JavaDoc e) {
248             e.printStackTrace();
249         }
250              
251         return sValue;
252     }
253
254     /* (non-Javadoc)
255      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
256      */

257     public boolean isMetadataValid() {
258         if(!this.m_dateField.hasValue()) {
259             return true;
260         } else {
261             PropertyInstance propInst = getPropertyInstance();
262             Property prop = getPropertyInstance().getDefinition();
263         
264             DateRange range = (DateRange)prop.getRange();
265         
266             return range.validate(propInst.getValues()).isValid();
267         }
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(
275                         IconManager.getInstance().getIcon("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(
284             IconManager.getInstance().getIcon("16-message-confirm.gif"));
285         this.m_display.valueChanged(this.getValue().trim());
286         super.validateTab();
287     }
288
289 }
290
Popular Tags