KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > _ComponentAttributesMap


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.component;
17
18 import java.beans.BeanInfo JavaDoc;
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.faces.FacesException;
31 import javax.faces.context.FacesContext;
32 import javax.faces.el.ValueBinding;
33
34 /**
35  * @author Manfred Geiler (latest modification by $Author: matzew $)
36  * @version $Revision: 1.8 $ $Date: 2005/01/08 19:59:33 $
37  * $Log: _ComponentAttributesMap.java,v $
38  * Revision 1.8 2005/01/08 19:59:33 matzew
39  * closed MYFACES-74. Thanks to Heath Borders-Wing
40  *
41  * Revision 1.7 2004/07/01 22:00:50 mwessendorf
42  * ASF switch
43  *
44  * Revision 1.6 2004/04/06 06:48:23 manolito
45  * IndexedPropertyDescriptor issue
46  *
47  */

48 class _ComponentAttributesMap
49         implements Map JavaDoc, Serializable JavaDoc
50 {
51     private static final Object JavaDoc[] EMPTY_ARGS = new Object JavaDoc[0];
52
53     private UIComponent _component;
54     private Map JavaDoc _attributes = null; //We delegate instead of derive from HashMap, so that we can later optimize Serialization
55
private transient Map JavaDoc _propertyDescriptorMap = null;
56
57     _ComponentAttributesMap(UIComponent component)
58     {
59         _component = component;
60         _attributes = new HashMap JavaDoc();
61     }
62
63     _ComponentAttributesMap(UIComponent component, Map JavaDoc attributes)
64     {
65         _component = component;
66         _attributes = attributes;
67     }
68
69     public int size()
70     {
71         return _attributes.size();
72     }
73
74     public void clear()
75     {
76         _attributes.clear();
77     }
78
79     public boolean isEmpty()
80     {
81         return _attributes.isEmpty();
82     }
83
84     public boolean containsKey(Object JavaDoc key)
85     {
86         checkKey(key);
87         if (getPropertyDescriptor((String JavaDoc)key) == null)
88         {
89             return _attributes.containsKey(key);
90         }
91         else
92         {
93             return false;
94         }
95     }
96
97     /**
98      * @param value null is allowed
99      */

100     public boolean containsValue(Object JavaDoc value)
101     {
102         return _attributes.containsValue(value);
103     }
104
105     public Collection JavaDoc values()
106     {
107         return _attributes.values();
108     }
109
110     public void putAll(Map JavaDoc t)
111     {
112         for (Iterator JavaDoc it = t.entrySet().iterator(); it.hasNext(); )
113         {
114             Map.Entry JavaDoc entry = (Entry)it.next();
115             put(entry.getKey(), entry.getValue());
116         }
117     }
118
119     public Set JavaDoc entrySet()
120     {
121         return _attributes.entrySet();
122     }
123
124     public Set JavaDoc keySet()
125     {
126         return _attributes.keySet();
127     }
128
129     public Object JavaDoc get(Object JavaDoc key)
130     {
131         checkKey(key);
132         PropertyDescriptor JavaDoc propertyDescriptor = getPropertyDescriptor((String JavaDoc)key);
133         if (propertyDescriptor != null)
134         {
135             Object JavaDoc value = getComponentProperty(propertyDescriptor);
136             if (value != null)
137             {
138                 return value;
139             }
140             
141             ValueBinding vb = _component.getValueBinding((String JavaDoc) key);
142             return vb != null ? vb.getValue(FacesContext.getCurrentInstance()) : null;
143         }
144         else
145         {
146             return _attributes.get(key);
147         }
148     }
149
150     public Object JavaDoc remove(Object JavaDoc key)
151     {
152         checkKey(key);
153         PropertyDescriptor JavaDoc propertyDescriptor = getPropertyDescriptor((String JavaDoc)key);
154         if (propertyDescriptor != null)
155         {
156             throw new IllegalArgumentException JavaDoc("Cannot remove component property attribute");
157         }
158         return _attributes.remove(key);
159     }
160
161     /**
162      * @param key String, null is not allowed
163      * @param value null is allowed
164      */

165     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
166     {
167         checkKey(key);
168         PropertyDescriptor JavaDoc propertyDescriptor = getPropertyDescriptor((String JavaDoc)key);
169         if (propertyDescriptor != null)
170         {
171             if (propertyDescriptor.getReadMethod() != null)
172             {
173                 Object JavaDoc oldValue = getComponentProperty(propertyDescriptor);
174                 setComponentProperty(propertyDescriptor, value);
175                 return oldValue;
176             }
177             else
178             {
179                 setComponentProperty(propertyDescriptor, value);
180                 return null;
181             }
182         }
183         else
184         {
185             return _attributes.put(key, value);
186         }
187     }
188
189
190     private PropertyDescriptor JavaDoc getPropertyDescriptor(String JavaDoc key)
191     {
192         if (_propertyDescriptorMap == null)
193         {
194             BeanInfo JavaDoc beanInfo;
195             try
196             {
197                 beanInfo = Introspector.getBeanInfo(_component.getClass());
198             }
199             catch (IntrospectionException JavaDoc e)
200             {
201                 throw new FacesException(e);
202             }
203             PropertyDescriptor JavaDoc[] propertyDescriptors = beanInfo.getPropertyDescriptors();
204             _propertyDescriptorMap = new HashMap JavaDoc();
205             for (int i = 0; i < propertyDescriptors.length; i++)
206             {
207                 PropertyDescriptor JavaDoc propertyDescriptor = propertyDescriptors[i];
208                 if (propertyDescriptor.getReadMethod() != null)
209                 {
210                     _propertyDescriptorMap.put(propertyDescriptor.getName(),
211                                                propertyDescriptor);
212                 }
213             }
214         }
215         return (PropertyDescriptor JavaDoc)_propertyDescriptorMap.get(key);
216     }
217
218
219     private Object JavaDoc getComponentProperty(PropertyDescriptor JavaDoc propertyDescriptor)
220     {
221         Method JavaDoc readMethod = propertyDescriptor.getReadMethod();
222         if (readMethod == null)
223         {
224             throw new IllegalArgumentException JavaDoc("Component property " + propertyDescriptor.getName() + " is not readable");
225         }
226         try
227         {
228             return readMethod.invoke(_component, EMPTY_ARGS);
229         }
230         catch (Exception JavaDoc e)
231         {
232             FacesContext facesContext = FacesContext.getCurrentInstance();
233             throw new FacesException("Could not get property " + propertyDescriptor.getName() + " of component " + _component.getClientId(facesContext), e);
234         }
235     }
236
237
238     private void setComponentProperty(PropertyDescriptor JavaDoc propertyDescriptor, Object JavaDoc value)
239     {
240         Method JavaDoc writeMethod = propertyDescriptor.getWriteMethod();
241         if (writeMethod == null)
242         {
243             throw new IllegalArgumentException JavaDoc("Component property " + propertyDescriptor.getName() + " is not writable");
244         }
245         try
246         {
247             writeMethod.invoke(_component, new Object JavaDoc[] {value});
248         }
249         catch (Exception JavaDoc e)
250         {
251             FacesContext facesContext = FacesContext.getCurrentInstance();
252             throw new FacesException("Could not set property " + propertyDescriptor.getName() + " of component " + _component.getClientId(facesContext), e);
253         }
254     }
255
256
257     private void checkKey(Object JavaDoc key)
258     {
259         if (key == null) throw new NullPointerException JavaDoc("key");
260         if (!(key instanceof String JavaDoc)) throw new ClassCastException JavaDoc("key is not a String");
261     }
262
263     Map JavaDoc getUnderlyingMap()
264     {
265         return _attributes;
266     }
267 }
Popular Tags