KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > propertyeditor > DateEditor


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.util.propertyeditor;
23
24 import java.beans.PropertyEditorSupport JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import org.jboss.util.NestedRuntimeException;
33
34 /**
35  * A property editor for {@link java.util.Date}.
36  *
37  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
38  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
39  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
40  * @author <a HREF="mailto:adrian.brock@jboss.org">Adrian Brock</a>
41  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
42  * @version <tt>$Revision: 1958 $</tt>
43  */

44 public class DateEditor extends PropertyEditorSupport JavaDoc
45 {
46    /** The formats to use when parsing the string date */
47    private static DateFormat JavaDoc[] formats;
48    static
49    {
50       initialize();
51    }
52
53    /**
54     * Setup the parsing formats. Offered as a separate static method to allow
55     * testing of locale changes, since SimpleDateFormat will use the default
56     * locale upon construction. Should not be normally used!
57     */

58    public static void initialize()
59    {
60       PrivilegedAction JavaDoc action = new PrivilegedAction JavaDoc()
61       {
62          public Object JavaDoc run()
63          {
64             String JavaDoc defaultFormat = System.getProperty("org.jboss.util.propertyeditor.DateEditor.format",
65                   "MMM d, yyyy");
66             formats = new DateFormat JavaDoc[]
67             {
68                new SimpleDateFormat JavaDoc(defaultFormat),
69                // Tue Jan 04 00:00:00 PST 2005
70
new SimpleDateFormat JavaDoc("EEE MMM d HH:mm:ss z yyyy"),
71                // Wed, 4 Jul 2001 12:08:56 -0700
72
new SimpleDateFormat JavaDoc("EEE, d MMM yyyy HH:mm:ss Z")
73             };
74             return null;
75          }
76       };
77       AccessController.doPrivileged(action);
78    }
79
80    /** Keep the text version of the date */
81    private String JavaDoc text;
82    
83    /**
84     * Sets directly the java.util.Date value
85     *
86     * @param value a java.util.Date
87     */

88    public void setValue(Object JavaDoc value)
89    {
90       if (value instanceof Date JavaDoc)
91       {
92          text = null;
93          super.setValue(value);
94       }
95       else
96       {
97          throw new IllegalArgumentException JavaDoc("setValue() expected java.util.Date value, got "
98                + value.getClass().getName());
99       }
100    }
101    
102    /**
103     * Parse the text into a java.util.Date by trying
104     * one by one the registered DateFormat(s).
105     *
106     * @param text the string date
107     */

108    public void setAsText(String JavaDoc text)
109    {
110       ParseException JavaDoc pe = null;
111       
112       for (int i = 0; i < formats.length; i++)
113       {
114          try
115          {
116             // try to parse the date
117
DateFormat JavaDoc df = formats[i];
118             Date JavaDoc date = df.parse(text);
119             
120             // store the date in both forms
121
this.text = text;
122             super.setValue(date);
123             
124             // done
125
return;
126          }
127          catch (ParseException JavaDoc e)
128          {
129             // remember the last seen exception
130
pe = e;
131          }
132       }
133       // couldn't parse
134
throw new NestedRuntimeException(pe);
135    }
136
137    /**
138     * Returns either the cached string date, or the stored
139     * java.util.Date instance formated to string using the
140     * last of the registered DateFormat(s)
141     *
142     * @return date as string
143     */

144    public String JavaDoc getAsText()
145    {
146       if (text == null)
147       {
148          DateFormat JavaDoc df = formats[formats.length - 1];
149          Date JavaDoc date = (Date JavaDoc)getValue();
150          text = df.format(date);
151       }
152       return text;
153    }
154    
155 }
Popular Tags