KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > taglib > core > ConvertDateTimeTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.taglib.core;
17
18 import java.util.Locale JavaDoc;
19 import java.util.TimeZone JavaDoc;
20 import javax.faces.context.FacesContext;
21 import javax.faces.convert.Converter;
22 import javax.faces.convert.DateTimeConverter;
23 import javax.faces.el.ValueBinding;
24 import javax.faces.webapp.ConverterTag;
25 import javax.faces.webapp.UIComponentTag;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28
29 /**
30  * @author Manfred Geiler (latest modification by $Author: bdudney $)
31  * @version $Revision: 1.7 $ $Date: 2004/11/22 16:13:08 $
32  * $Log: ConvertDateTimeTag.java,v $
33  * Revision 1.7 2004/11/22 16:13:08 bdudney
34  * added code to make sure the converterId is set in WebLogic8.1sp3.
35  *
36  * Revision 1.6 2004/10/13 11:51:00 matze
37  * renamed packages to org.apache
38  *
39  * Revision 1.5 2004/07/01 22:05:03 mwessendorf
40  * ASF switch
41  *
42  * Revision 1.4 2004/06/27 19:26:18 mwessendorf
43  * added default value, needed by jsf-spec
44  *
45  * Revision 1.3 2004/04/16 15:35:59 manolito
46  * Log
47  *
48  */

49 public class ConvertDateTimeTag
50         extends ConverterTag
51 {
52     private String JavaDoc _dateStyle = "default"; // the default value as required by the spec (default in this case)
53
private String JavaDoc _locale = null;
54     private String JavaDoc _pattern = null;
55     private String JavaDoc _timeStyle = "default"; // the default value as required by the spec (default in this case)
56
private String JavaDoc _timeZone = null;
57     private String JavaDoc _type = null;
58
59     public ConvertDateTimeTag()
60     {
61       super();
62         //setConverterId(DateTimeConverter.CONVERTER_ID);
63
}
64
65     public void setDateStyle(String JavaDoc dateStyle)
66     {
67         _dateStyle = dateStyle;
68     }
69
70     public void setLocale(String JavaDoc locale)
71     {
72         _locale = locale;
73     }
74
75     public void setPattern(String JavaDoc pattern)
76     {
77         _pattern = pattern;
78     }
79
80     public void setTimeStyle(String JavaDoc timeStyle)
81     {
82         _timeStyle = timeStyle;
83     }
84
85     public void setTimeZone(String JavaDoc timeZone)
86     {
87         _timeZone = timeZone;
88     }
89
90     public void setType(String JavaDoc type)
91     {
92         _type = type;
93     }
94
95     public void setPageContext(PageContext JavaDoc context)
96     {
97         super.setPageContext(context);
98         setConverterId(DateTimeConverter.CONVERTER_ID);
99     }
100
101     protected Converter createConverter() throws JspException JavaDoc
102     {
103         DateTimeConverter converter = (DateTimeConverter)super.createConverter();
104
105         FacesContext facesContext = FacesContext.getCurrentInstance();
106         setConverterDateStyle(facesContext, converter, _dateStyle);
107         setConverterLocale(facesContext, converter, _locale);
108         setConverterPattern(facesContext, converter, _pattern);
109         setConverterTimeStyle(facesContext, converter, _timeStyle);
110         setConverterTimeZone(facesContext, converter, _timeZone);
111         setConverterType(facesContext, converter, _type);
112
113         return converter;
114     }
115     
116     
117     private static void setConverterDateStyle(FacesContext facesContext,
118                                               DateTimeConverter converter,
119                                               String JavaDoc value)
120     {
121         if (value == null) return;
122         if (UIComponentTag.isValueReference(value))
123         {
124             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
125             converter.setDateStyle((String JavaDoc)vb.getValue(facesContext));
126         }
127         else
128         {
129             converter.setDateStyle(value);
130         }
131     }
132
133     private static void setConverterLocale(FacesContext facesContext,
134                                               DateTimeConverter converter,
135                                               String JavaDoc value)
136     {
137         if (value == null) return;
138         if (UIComponentTag.isValueReference(value))
139         {
140             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
141             converter.setLocale((Locale JavaDoc)vb.getValue(facesContext));
142         }
143         else
144         {
145             throw new IllegalArgumentException JavaDoc("Attribute locale of type String not supported");
146         }
147     }
148
149     private static void setConverterPattern(FacesContext facesContext,
150                                               DateTimeConverter converter,
151                                               String JavaDoc value)
152     {
153         if (value == null) return;
154         if (UIComponentTag.isValueReference(value))
155         {
156             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
157             converter.setPattern((String JavaDoc)vb.getValue(facesContext));
158         }
159         else
160         {
161             converter.setPattern(value);
162         }
163     }
164
165     private static void setConverterTimeStyle(FacesContext facesContext,
166                                               DateTimeConverter converter,
167                                               String JavaDoc value)
168     {
169         if (value == null) return;
170         if (UIComponentTag.isValueReference(value))
171         {
172             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
173             converter.setTimeStyle((String JavaDoc)vb.getValue(facesContext));
174         }
175         else
176         {
177             converter.setTimeStyle(value);
178         }
179     }
180
181     private static void setConverterTimeZone(FacesContext facesContext,
182                                               DateTimeConverter converter,
183                                               String JavaDoc value)
184     {
185         if (value == null) return;
186         if (UIComponentTag.isValueReference(value))
187         {
188             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
189             converter.setTimeZone((TimeZone JavaDoc)vb.getValue(facesContext));
190         }
191         else
192         {
193             converter.setTimeZone(TimeZone.getTimeZone(value));
194         }
195     }
196
197     private static void setConverterType(FacesContext facesContext,
198                                               DateTimeConverter converter,
199                                               String JavaDoc value)
200     {
201         if (value == null) return;
202         if (UIComponentTag.isValueReference(value))
203         {
204             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
205             converter.setType((String JavaDoc)vb.getValue(facesContext));
206         }
207         else
208         {
209             converter.setType(value);
210         }
211     }
212
213 }
214
Popular Tags