1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.internal; |
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.el.ValueBinding; |
28 | |
|
29 | |
import org.seasar.framework.beans.BeanDesc; |
30 | |
import org.seasar.framework.beans.PropertyDesc; |
31 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
32 | |
import org.seasar.framework.util.AssertionUtil; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class ComponentAttributesMap implements Map, Serializable { |
42 | |
|
43 | |
private static final long serialVersionUID = 1L; |
44 | |
|
45 | 705 | private UIComponent component = null; |
46 | |
|
47 | 705 | private Map attributes = null; |
48 | |
|
49 | 705 | private BeanDesc beanDesc = null; |
50 | |
|
51 | |
private static final int INITIAL_CAPACITY = 64; |
52 | |
|
53 | 662 | public ComponentAttributesMap(UIComponent component) { |
54 | 662 | this.component = component; |
55 | 662 | this.attributes = new HashMap(INITIAL_CAPACITY); |
56 | 662 | setupPropertyDesc(); |
57 | 662 | } |
58 | |
|
59 | 43 | public ComponentAttributesMap(UIComponent component, Map attributes) { |
60 | 43 | this.component = component; |
61 | 43 | this.attributes = new HashMap(attributes); |
62 | 43 | setupPropertyDesc(); |
63 | 43 | } |
64 | |
|
65 | |
public int size() { |
66 | 87 | return attributes.size(); |
67 | |
} |
68 | |
|
69 | |
public void clear() { |
70 | 0 | attributes.clear(); |
71 | 0 | } |
72 | |
|
73 | |
public boolean isEmpty() { |
74 | 0 | return attributes.isEmpty(); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
public boolean containsKey(Object key) { |
79 | 3 | if (beanDesc.hasPropertyDesc((String) key)) { |
80 | 1 | return false; |
81 | |
} else { |
82 | 2 | return attributes.containsKey(key); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
public boolean containsValue(Object value) { |
87 | 0 | return attributes.containsValue(value); |
88 | |
} |
89 | |
|
90 | |
public Collection values() { |
91 | 0 | return attributes.values(); |
92 | |
} |
93 | |
|
94 | |
public void putAll(Map map) { |
95 | 1 | for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) { |
96 | 1 | Map.Entry entry = (Map.Entry) itr.next(); |
97 | 1 | put(entry.getKey(), entry.getValue()); |
98 | |
} |
99 | 1 | } |
100 | |
|
101 | |
public Set entrySet() { |
102 | 651 | return attributes.entrySet(); |
103 | |
} |
104 | |
|
105 | |
public Set keySet() { |
106 | 0 | return attributes.keySet(); |
107 | |
} |
108 | |
|
109 | |
public Object get(Object key) { |
110 | 1625 | String k = (String) key; |
111 | 1625 | PropertyDesc propertyDesc = null; |
112 | 1625 | if (beanDesc.hasPropertyDesc(k)) { |
113 | 1398 | propertyDesc = beanDesc.getPropertyDesc(k); |
114 | |
} |
115 | 1625 | if (propertyDesc != null && propertyDesc.isReadable()) { |
116 | 1398 | Object value = getComponentProperty(propertyDesc); |
117 | 1398 | if (value != null) { |
118 | 495 | return value; |
119 | |
} |
120 | 903 | ValueBinding vb = component.getValueBinding((String) key); |
121 | 903 | return (vb != null) ? vb |
122 | |
.getValue(FacesContext.getCurrentInstance()) : null; |
123 | |
} else { |
124 | 227 | return attributes.get((String) key); |
125 | |
} |
126 | |
} |
127 | |
|
128 | |
public Object remove(Object key) { |
129 | 19 | String k = (String) key; |
130 | 19 | if (beanDesc.hasPropertyDesc(k)) { |
131 | 1 | throw new IllegalArgumentException( |
132 | |
"can't remove component property"); |
133 | |
} |
134 | 18 | return attributes.remove(key); |
135 | |
} |
136 | |
|
137 | |
public Object put(Object key, Object value) { |
138 | 285 | AssertionUtil.assertNotNull("key", key); |
139 | 284 | AssertionUtil.assertNotNull("value", value); |
140 | 283 | assertKeyIsString(key); |
141 | 282 | String k = (String) key; |
142 | 282 | PropertyDesc propertyDesc = null; |
143 | 282 | if (beanDesc.hasPropertyDesc(k)) { |
144 | 57 | propertyDesc = beanDesc.getPropertyDesc(k); |
145 | |
} |
146 | 282 | Object returnValue = null; |
147 | 282 | if (propertyDesc != null && propertyDesc.isWritable()) { |
148 | 56 | if (propertyDesc.isReadable()) { |
149 | 56 | returnValue = getComponentProperty(propertyDesc); |
150 | |
} |
151 | 56 | setComponentProperty(propertyDesc, value); |
152 | |
} else { |
153 | 226 | returnValue = attributes.put(key, value); |
154 | |
} |
155 | 282 | return returnValue; |
156 | |
} |
157 | |
|
158 | |
private void setupPropertyDesc() { |
159 | 705 | Class clazz = component.getClass(); |
160 | 705 | beanDesc = BeanDescFactory.getBeanDesc(clazz); |
161 | 705 | } |
162 | |
|
163 | |
private void setComponentProperty(PropertyDesc propertyDesc, Object value) { |
164 | 56 | propertyDesc.setValue(component, value); |
165 | 56 | } |
166 | |
|
167 | |
private Object getComponentProperty(PropertyDesc propertyDesc) { |
168 | 1454 | return propertyDesc.getValue(component); |
169 | |
} |
170 | |
|
171 | |
public Map getAttributesActual() { |
172 | 43 | return attributes; |
173 | |
} |
174 | |
|
175 | |
private static void assertKeyIsString(Object key) { |
176 | 283 | if (!(key instanceof String)) { |
177 | 1 | throw new ClassCastException("key must be a String"); |
178 | |
} |
179 | 282 | } |
180 | |
|
181 | |
} |