View Javadoc

1   /*
2    * $Id: ClientFullStateConverter.java,v 1.1 2008/01/29 10:59:56 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.IClientFullStateCapability;
16  import org.rcfaces.core.component.capability.IHorizontalTextPositionCapability;
17  
18  /**
19   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
20   * @version $Revision: 1.1 $ $Date: 2008/01/29 10:59:56 $
21   */
22  public class ClientFullStateConverter implements Converter {
23      private static final String REVISION = "$Revision: 1.1 $";
24  
25      public static final Converter SINGLETON = new ClientFullStateConverter();
26  
27      private static final String NONE_CLIENT_FULL_STATE_NAME = "none";
28  
29      private static final String ONEWAY_CLIENT_FULL_STATE_NAME = "oneway";
30  
31      private static final String TWOWAYS_CLIENT_FULL_STATE_NAME = "twoways";
32  
33      private static final String FALSE_CLIENT_FULL_STATE_NAME = "false";
34  
35      private static final String TRUE_CLIENT_FULL_STATE_NAME = "true";
36  
37      private static final Integer DEFAULT_CLIENT_FULL_STATE = new Integer(
38              IHorizontalTextPositionCapability.DEFAULT_POSITION);
39  
40      private static Map FULL_STATES = new HashMap(8);
41      static {
42          Integer i = new Integer(
43                  IClientFullStateCapability.NONE_CLIENT_FULL_STATE);
44          FULL_STATES.put(NONE_CLIENT_FULL_STATE_NAME, i);
45  
46          i = new Integer(IClientFullStateCapability.ONEWAY_CLIENT_FULL_STATE);
47          FULL_STATES.put(ONEWAY_CLIENT_FULL_STATE_NAME, i);
48  
49          i = new Integer(IClientFullStateCapability.TWOWAYS_CLIENT_FULL_STATE);
50          FULL_STATES.put(TWOWAYS_CLIENT_FULL_STATE_NAME, i);
51  
52          i = new Integer(IClientFullStateCapability.FALSE_CLIENT_FULL_STATE);
53          FULL_STATES.put(FALSE_CLIENT_FULL_STATE_NAME, i);
54  
55          i = new Integer(IClientFullStateCapability.TRUE_CLIENT_FULL_STATE);
56          FULL_STATES.put(TRUE_CLIENT_FULL_STATE_NAME, i);
57      }
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_CLIENT_FULL_STATE;
65          }
66  
67          value = value.toLowerCase();
68  
69          Integer i = (Integer) FULL_STATES.get(value);
70          if (i != null) {
71              return i;
72          }
73  
74          throw new IllegalArgumentException("Keyword '" + value
75                  + "' is not supported for a full-state type !");
76      }
77  
78      public String getAsString(FacesContext context, UIComponent component,
79              Object value) {
80  
81          if (value == null) {
82              return (String) FULL_STATES.get(DEFAULT_CLIENT_FULL_STATE);
83          }
84  
85          if ((value instanceof Integer) == false) {
86              throw new IllegalArgumentException("Value must be an Integer !");
87          }
88  
89          for (Iterator it = FULL_STATES.entrySet().iterator(); it.hasNext();) {
90              Map.Entry entry = (Map.Entry) it.next();
91  
92              if (value.equals(entry.getValue())) {
93                  return (String) entry.getKey();
94              }
95          }
96  
97          throw new IllegalArgumentException("Value '" + value
98                  + "' is not supported for a full-state type !");
99      }
100 
101 }