1 16 package org.apache.myfaces.taglib.core; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 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 ; 27 import javax.servlet.jsp.PageContext ; 28 29 49 public class ConvertDateTimeTag 50 extends ConverterTag 51 { 52 private String _dateStyle = "default"; private String _locale = null; 54 private String _pattern = null; 55 private String _timeStyle = "default"; private String _timeZone = null; 57 private String _type = null; 58 59 public ConvertDateTimeTag() 60 { 61 super(); 62 } 64 65 public void setDateStyle(String dateStyle) 66 { 67 _dateStyle = dateStyle; 68 } 69 70 public void setLocale(String locale) 71 { 72 _locale = locale; 73 } 74 75 public void setPattern(String pattern) 76 { 77 _pattern = pattern; 78 } 79 80 public void setTimeStyle(String timeStyle) 81 { 82 _timeStyle = timeStyle; 83 } 84 85 public void setTimeZone(String timeZone) 86 { 87 _timeZone = timeZone; 88 } 89 90 public void setType(String type) 91 { 92 _type = type; 93 } 94 95 public void setPageContext(PageContext context) 96 { 97 super.setPageContext(context); 98 setConverterId(DateTimeConverter.CONVERTER_ID); 99 } 100 101 protected Converter createConverter() throws JspException 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 value) 120 { 121 if (value == null) return; 122 if (UIComponentTag.isValueReference(value)) 123 { 124 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 125 converter.setDateStyle((String )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 value) 136 { 137 if (value == null) return; 138 if (UIComponentTag.isValueReference(value)) 139 { 140 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 141 converter.setLocale((Locale )vb.getValue(facesContext)); 142 } 143 else 144 { 145 throw new IllegalArgumentException ("Attribute locale of type String not supported"); 146 } 147 } 148 149 private static void setConverterPattern(FacesContext facesContext, 150 DateTimeConverter converter, 151 String value) 152 { 153 if (value == null) return; 154 if (UIComponentTag.isValueReference(value)) 155 { 156 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 157 converter.setPattern((String )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 value) 168 { 169 if (value == null) return; 170 if (UIComponentTag.isValueReference(value)) 171 { 172 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 173 converter.setTimeStyle((String )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 value) 184 { 185 if (value == null) return; 186 if (UIComponentTag.isValueReference(value)) 187 { 188 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 189 converter.setTimeZone((TimeZone )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 value) 200 { 201 if (value == null) return; 202 if (UIComponentTag.isValueReference(value)) 203 { 204 ValueBinding vb = facesContext.getApplication().createValueBinding(value); 205 converter.setType((String )vb.getValue(facesContext)); 206 } 207 else 208 { 209 converter.setType(value); 210 } 211 } 212 213 } 214 | Popular Tags |