View Javadoc

1   /*
2    * $Id: TimeZoneConverter.java,v 1.1 2007/01/05 17:09:51 oeuillot Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.converter;
6   
7   import java.util.HashMap;
8   import java.util.Locale;
9   import java.util.Map;
10  import java.util.TimeZone;
11  
12  import javax.faces.component.UIComponent;
13  import javax.faces.context.FacesContext;
14  import javax.faces.convert.Converter;
15  
16  import org.rcfaces.core.model.AbstractConverter;
17  
18  /**
19   * 
20   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
21   * @version $Revision: 1.1 $ $Date: 2007/01/05 17:09:51 $
22   */
23  public class TimeZoneConverter extends AbstractConverter {
24      private static final String REVISION = "$Revision: 1.1 $";
25  
26      public static final Converter SINGLETON = new TimeZoneConverter();
27  
28      private static final Map timeZonesById;
29      static {
30          String ids[] = TimeZone.getAvailableIDs();
31  
32          timeZonesById = new HashMap(ids.length);
33  
34          Locale defaultLocale = Locale.getDefault();
35          if (Locale.ENGLISH.equals(defaultLocale)) {
36              defaultLocale = null;
37          }
38  
39          for (int i = 0; i < ids.length; i++) {
40              TimeZone l = TimeZone.getTimeZone(ids[i]);
41  
42              timeZonesById.put(l.getID().toLowerCase(), l);
43              timeZonesById.put(l.getDisplayName().toLowerCase(), l);
44  
45              if (defaultLocale != null) {
46                  timeZonesById.put(
47                          l.getDisplayName(defaultLocale).toLowerCase(), l);
48              }
49          }
50  
51          TimeZone defaultTimeZone = TimeZone.getDefault();
52          timeZonesById.put("default", defaultTimeZone);
53      }
54  
55      public Object getAsObject(FacesContext context, UIComponent component,
56              String value) {
57          if (value == null || value.length() < 1) {
58              return null;
59          }
60  
61          return timeZonesById.get(value.toLowerCase());
62      }
63  
64      public String getAsString(FacesContext context, UIComponent component,
65              Object value) {
66          if (value == null) {
67              return null;
68          }
69  
70          TimeZone timeZone = (TimeZone) value;
71  
72          return timeZone.getID();
73      }
74  }