1
2
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.IHorizontalTextPositionCapability;
16 import org.rcfaces.core.model.AbstractConverter;
17
18
19
20
21
22 public class HorizontalTextPositionConverter extends AbstractConverter {
23 private static final String REVISION = "$Revision: 1.18 $";
24
25 public static final Converter SINGLETON = new HorizontalTextPositionConverter();
26
27 protected static final String RIGHT_POSITION_NAME = "right";
28
29 protected static final String CENTER_POSITION_NAME = "center";
30
31 protected static final String LEFT_POSITION_NAME = "left";
32
33 private static final String DEFAULT_POSITION_NAME = "default";
34
35 private static final Integer DEFAULT_POSITION = new Integer(
36 IHorizontalTextPositionCapability.DEFAULT_POSITION);
37
38 protected static Map HORIZONTAL_TEXT_POSITIONS = new HashMap(5);
39 static {
40 Integer i = new Integer(IHorizontalTextPositionCapability.LEFT_POSITION);
41 HORIZONTAL_TEXT_POSITIONS.put(LEFT_POSITION_NAME, i);
42
43 i = new Integer(IHorizontalTextPositionCapability.RIGHT_POSITION);
44 HORIZONTAL_TEXT_POSITIONS.put(RIGHT_POSITION_NAME, i);
45
46 i = new Integer(IHorizontalTextPositionCapability.CENTER_POSITION);
47 HORIZONTAL_TEXT_POSITIONS.put(CENTER_POSITION_NAME, i);
48 }
49
50 protected Map getTextPositions() {
51 return HORIZONTAL_TEXT_POSITIONS;
52 }
53
54 protected Integer getDefaultPosition() {
55 return DEFAULT_POSITION;
56 }
57
58 public Object getAsObject(FacesContext context, UIComponent component,
59 String value) {
60
61 if (value == null || value.length() < 1
62 || "default".equalsIgnoreCase(value)) {
63 return getDefaultPosition();
64 }
65
66 value = value.toLowerCase();
67
68 Integer i = (Integer) getTextPositions().get(value);
69 if (i != null) {
70 return i;
71 }
72
73 throw new IllegalArgumentException("Keyword '" + value
74 + "' is not supported for a text-position type !");
75 }
76
77 public String getAsString(FacesContext context, UIComponent component,
78 Object value) {
79
80 if (value == null) {
81 return (String) getTextPositions().get(getDefaultPosition());
82 }
83
84 if ((value instanceof Integer) == false) {
85 throw new IllegalArgumentException("Value must be an Integer !");
86 }
87
88 for (Iterator it = getTextPositions().entrySet().iterator(); it
89 .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 text-position type !");
99 }
100
101 }