1 16 package javax.faces.convert; 17 18 import javax.faces.component.UIComponent; 19 import javax.faces.component.StateHolder; 20 import javax.faces.context.FacesContext; 21 import java.text.DateFormat ; 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Locale ; 25 import java.util.TimeZone ; 26 27 54 public class DateTimeConverter 55 implements Converter, StateHolder 56 { 57 private static final String CONVERSION_MESSAGE_ID = "javax.faces.convert.DateTimeConverter.CONVERSION"; 58 59 public static final String CONVERTER_ID = "javax.faces.DateTime"; 61 private static final String TYPE_DATE = "date"; 62 private static final String TYPE_TIME = "time"; 63 private static final String TYPE_BOTH = "both"; 64 private static final String STYLE_DEFAULT = "default"; 65 private static final String STYLE_MEDIUM = "medium"; 66 private static final String STYLE_SHORT = "short"; 67 private static final String STYLE_LONG = "long"; 68 private static final String STYLE_FULL = "full"; 69 70 private String _dateStyle; 71 private Locale _locale; 72 private String _pattern; 73 private String _timeStyle; 74 private TimeZone _timeZone; 75 private String _type; 76 private boolean _transient; 77 78 public DateTimeConverter() 80 { 81 } 82 83 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) 85 { 86 if (facesContext == null) throw new NullPointerException ("facesContext"); 87 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 88 89 if (value != null) 90 { 91 value = value.trim(); 92 if (value.length() > 0) 93 { 94 DateFormat format = getDateFormat(); 95 format.setLenient(true); 96 TimeZone tz = getTimeZone(); 97 if( tz != null ) 98 format.setTimeZone( tz ); 99 try 100 { 101 return format.parse(value); 102 } 103 catch (ParseException e) 104 { 105 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, 106 CONVERSION_MESSAGE_ID, 107 new Object []{value,uiComponent.getId()}), e); 108 } 109 } 110 } 111 return null; 112 } 113 114 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) 115 { 116 if (facesContext == null) throw new NullPointerException ("facesContext"); 117 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 118 119 if (value == null) 120 { 121 return ""; 122 } 123 if (value instanceof String ) 124 { 125 return (String )value; 126 } 127 128 DateFormat format = getDateFormat(); 129 if (_timeZone != null) 130 { 131 format.setTimeZone(_timeZone); 132 } 133 try 134 { 135 return format.format(value); 136 } 137 catch (Exception e) 138 { 139 throw new ConverterException("Cannot convert value '" + value + "'"); 140 } 141 } 142 143 private DateFormat getDateFormat() 144 { 145 String type = getType(); 146 DateFormat format = null; 147 if (_pattern != null) 148 { 149 format = new SimpleDateFormat (_pattern, getLocale()); 150 } 151 else if (type.equals(TYPE_DATE)) 152 { 153 format = DateFormat.getDateInstance(calcStyle(getDateStyle()), getLocale()); 154 } 155 else if (type.equals(TYPE_TIME)) 156 { 157 format = DateFormat.getTimeInstance(calcStyle(getTimeStyle()), getLocale()); 158 } 159 else if (type.equals(TYPE_BOTH)) 160 { 161 format = DateFormat.getDateTimeInstance(calcStyle(getDateStyle()), 162 calcStyle(getTimeStyle()), 163 getLocale()); 164 } 165 else 166 { 167 throw new ConverterException("invalid type '" + _type + "'"); 168 } 169 return format; 170 } 171 172 private int calcStyle(String name) 173 { 174 if (name.equals(STYLE_DEFAULT)) 175 { 176 return DateFormat.DEFAULT; 177 } 178 if (name.equals(STYLE_MEDIUM)) 179 { 180 return DateFormat.MEDIUM; 181 } 182 if (name.equals(STYLE_SHORT)) 183 { 184 return DateFormat.SHORT; 185 } 186 if (name.equals(STYLE_LONG)) 187 { 188 return DateFormat.LONG; 189 } 190 if (name.equals(STYLE_FULL)) 191 { 192 return DateFormat.FULL; 193 } 194 195 throw new ConverterException("invalid style '" + name + "'"); 196 } 197 198 public void restoreState(FacesContext facesContext, Object state) 200 { 201 Object [] values = (Object [])state; 202 _dateStyle = (String )values[0]; 203 _locale = (Locale )values[1]; 204 _pattern = (String )values[2]; 205 _timeStyle = (String )values[3]; 206 _timeZone = (TimeZone )values[4]; 207 _type = (String )values[5]; 208 } 209 210 public Object saveState(FacesContext facesContext) 211 { 212 Object [] values = new Object [6]; 213 values[0] = _dateStyle; 214 values[1] = _locale; 215 values[2] = _pattern; 216 values[3] = _timeStyle; 217 values[4] = _timeZone; 218 values[5] = _type; 219 return values; 220 } 221 222 public String getDateStyle() 224 { 225 return _dateStyle != null ? _dateStyle : STYLE_DEFAULT; 226 } 227 228 public void setDateStyle(String dateStyle) 229 { 230 _dateStyle = dateStyle; 232 } 233 234 public Locale getLocale() 235 { 236 if (_locale != null) return _locale; 237 FacesContext context = FacesContext.getCurrentInstance(); 238 return context.getViewRoot().getLocale(); 239 } 240 241 public void setLocale(Locale locale) 242 { 243 _locale = locale; 244 } 245 246 public String getPattern() 247 { 248 return _pattern; 249 } 250 251 public void setPattern(String pattern) 252 { 253 _pattern = pattern; 254 } 255 256 public String getTimeStyle() 257 { 258 return _timeStyle != null ? _timeStyle : STYLE_DEFAULT; 259 } 260 261 public void setTimeStyle(String timeStyle) 262 { 263 _timeStyle = timeStyle; 265 } 266 267 public TimeZone getTimeZone() 268 { 269 return _timeZone; 270 } 271 272 public void setTimeZone(TimeZone timeZone) 273 { 274 _timeZone = timeZone; 275 } 276 277 public boolean isTransient() 278 { 279 return _transient; 280 } 281 282 public void setTransient(boolean aTransient) 283 { 284 _transient = aTransient; 285 } 286 287 public String getType() 288 { 289 return _type != null ? _type : TYPE_DATE; 290 } 291 292 public void setType(String type) 293 { 294 _type = type; 296 } 297 } 298 | Popular Tags |