View Javadoc

1   /*
2    * $Id: CalendarModeConverter.java,v 1.18 2011/06/16 09:29:40 jbmeslin Exp $
3    */
4   package org.rcfaces.core.internal.converter;
5   
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import javax.faces.component.UIComponent;
11  import javax.faces.context.FacesContext;
12  import javax.faces.convert.Converter;
13  
14  import org.rcfaces.core.component.capability.ICalendarModeCapability;
15  import org.rcfaces.core.model.AbstractConverter;
16  
17  /**
18   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
19   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:40 $
20   */
21  public class CalendarModeConverter extends AbstractConverter {
22      private static final String REVISION = "$Revision: 1.18 $";
23  
24      public static final Converter SINGLETON = new CalendarModeConverter();
25  
26      private static final Integer DEFAULT_CALENDAR_MODE = new Integer(
27              ICalendarModeCapability.DEFAULT_CALENDAR_MODE);
28  
29      private static Map CALENDAR_MODES = new HashMap(5);
30      static {
31          CALENDAR_MODES.put(ICalendarModeCapability.DATE_CALENDAR_MODE_NAME,
32                  new Integer(ICalendarModeCapability.DATE_CALENDAR_MODE));
33          CALENDAR_MODES.put(ICalendarModeCapability.PERIOD_CALENDAR_MODE_NAME,
34                  new Integer(ICalendarModeCapability.PERIOD_CALENDAR_MODE));
35          CALENDAR_MODES.put(ICalendarModeCapability.DEFAULT_CALENDAR_MODE_NAME,
36                  new Integer(ICalendarModeCapability.DEFAULT_CALENDAR_MODE));
37      }
38  
39      /*
40       * (non-Javadoc)
41       * 
42       * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
43       *      javax.faces.component.UIComponent, java.lang.String)
44       */
45      public Object getAsObject(FacesContext context, UIComponent component,
46              String value) {
47  
48          if (value == null || value.length() < 1 || "default".equals(value)) {
49              return DEFAULT_CALENDAR_MODE;
50          }
51  
52          value = value.toLowerCase();
53  
54          Integer i = (Integer) CALENDAR_MODES.get(value);
55          if (i != null) {
56              return i;
57          }
58  
59          throw new IllegalArgumentException("Keyword '" + value
60                  + "' is not supported for a calendar-mode type !");
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
67       *      javax.faces.component.UIComponent, java.lang.Object)
68       */
69      public String getAsString(FacesContext context, UIComponent component,
70              Object value) {
71  
72          if (value == null) {
73              return (String) CALENDAR_MODES.get(DEFAULT_CALENDAR_MODE);
74          }
75  
76          if ((value instanceof Integer) == false) {
77              throw new IllegalArgumentException("Value must be an Integer !");
78          }
79  
80          for (Iterator it = CALENDAR_MODES.entrySet().iterator(); it.hasNext();) {
81              Map.Entry entry = (Map.Entry) it.next();
82  
83              if (value.equals(entry.getValue())) {
84                  return (String) entry.getKey();
85              }
86          }
87  
88          throw new IllegalArgumentException("Value '" + value
89                  + "' is not supported for a calendar-mode type !");
90      }
91  
92      public static final String getName(int hiddenMode) {
93          switch (hiddenMode) {
94          case ICalendarModeCapability.DATE_CALENDAR_MODE:
95              return ICalendarModeCapability.DATE_CALENDAR_MODE_NAME;
96  
97          case ICalendarModeCapability.PERIOD_CALENDAR_MODE:
98              return ICalendarModeCapability.PERIOD_CALENDAR_MODE_NAME;
99          }
100 
101         return null;
102     }
103 }