View Javadoc

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