View Javadoc

1   /*
2    * $Id: KeyFlagsConverter.java,v 1.18 2011/06/16 09:29:40 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.converter;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   import java.util.StringTokenizer;
10  
11  import javax.faces.FacesException;
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: jbmeslin $)
21   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:40 $
22   */
23  public class KeyFlagsConverter extends AbstractConverter {
24      private static final String REVISION = "$Revision: 1.18 $";
25  
26      public static final Converter SINGLETON = new KeyFlagsConverter();
27  
28      private static final Integer NO_FLAGS = new Integer(0);
29  
30      public static final int SHIFT_FLAG = 0x01;
31  
32      public static final int CONTROL_FLAG = 0x02;
33  
34      public static final int ALT_FLAG = 0x04;
35  
36      public static final int META_FLAG = 0x08;
37  
38      private static final Map FLAGS = new HashMap(8);
39      static {
40          FLAGS.put("SHIFT", new Integer(SHIFT_FLAG));
41          FLAGS.put("CONTROL", new Integer(CONTROL_FLAG));
42          FLAGS.put("CTRL", new Integer(CONTROL_FLAG));
43          FLAGS.put("ALT", new Integer(ALT_FLAG));
44          FLAGS.put("META", new Integer(META_FLAG));
45      }
46  
47      public Object getAsObject(FacesContext context, UIComponent component,
48              String value) {
49          if (value == null) {
50              return NO_FLAGS;
51          }
52  
53          StringTokenizer st = new StringTokenizer(value, ",; ");
54  
55          int mask = 0;
56  
57          for (; st.hasMoreTokens();) {
58              String token = st.nextToken().toUpperCase();
59  
60              Integer flags = (Integer) FLAGS.get(token);
61              if (flags == null) {
62                  continue;
63              }
64  
65              mask |= flags.intValue();
66          }
67  
68          if (mask == 0) {
69              return NO_FLAGS;
70          }
71  
72          return new Integer(mask);
73      }
74  
75      public String getAsString(FacesContext context, UIComponent component,
76              Object value) {
77          throw new FacesException("Not implemented !");
78      }
79  
80      public static Integer convertUpperCase(String key) {
81          return (Integer) FLAGS.get(key);
82      }
83  }