KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > taglib > UIComponentTagUtils


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 org.apache.myfaces.taglib;
17
18 import org.apache.myfaces.el.SimpleActionMethodBinding;
19 import org.apache.myfaces.renderkit.JSFAttr;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.component.*;
25 import javax.faces.context.FacesContext;
26 import javax.faces.convert.Converter;
27 import javax.faces.el.MethodBinding;
28 import javax.faces.el.ValueBinding;
29 import javax.faces.event.ActionEvent;
30 import javax.faces.event.ValueChangeEvent;
31 import javax.faces.webapp.UIComponentTag;
32
33 /**
34  * @author Manfred Geiler (latest modification by $Author: matze $)
35  * @version $Revision: 1.7 $ $Date: 2004/10/13 11:51:01 $
36  * $Log: UIComponentTagUtils.java,v $
37  * Revision 1.7 2004/10/13 11:51:01 matze
38  * renamed packages to org.apache
39  *
40  * Revision 1.6 2004/08/06 22:41:00 o_rossmueller
41  * fix #995085: set value of UIGraphic
42  *
43  * Revision 1.5 2004/07/01 22:01:21 mwessendorf
44  * ASF switch
45  *
46  * Revision 1.4 2004/04/23 13:57:54 manolito
47  * bug #940740
48  *
49  * Revision 1.3 2004/04/16 15:13:33 manolito
50  * validator attribute support and MethodBinding invoke exception handling fixed
51  *
52  * Revision 1.2 2004/03/30 12:16:08 manolito
53  * header comments
54  *
55  */

56 public class UIComponentTagUtils
57 {
58     private static final Log log = LogFactory.getLog(UIComponentTagUtils.class);
59
60     private static final Class JavaDoc[] VALIDATOR_ARGS = {FacesContext.class,
61                                                    UIComponent.class,
62                                                    Object JavaDoc.class};
63     private static final Class JavaDoc[] ACTION_LISTENER_ARGS = {ActionEvent.class};
64     private static final Class JavaDoc[] VALUE_LISTENER_ARGS = {ValueChangeEvent.class};
65
66     private UIComponentTagUtils() {} //util class, no instantiation allowed
67

68
69     public static boolean isValueReference(String JavaDoc v)
70     {
71         return UIComponentTag.isValueReference(v);
72     }
73
74
75     public static void setIntegerProperty(FacesContext context,
76                                           UIComponent component,
77                                           String JavaDoc propName,
78                                           String JavaDoc value)
79     {
80         if (value != null)
81         {
82             if (isValueReference(value))
83             {
84                 ValueBinding vb = context.getApplication().createValueBinding(value);
85                 component.setValueBinding(propName, vb);
86             }
87             else
88             {
89                 //FIXME: should use converter maybe?
90
component.getAttributes().put(propName, Integer.valueOf(value));
91             }
92         }
93     }
94
95     public static void setStringProperty(FacesContext context,
96                                      UIComponent component,
97                                      String JavaDoc propName,
98                                      String JavaDoc value)
99     {
100         if (value != null)
101         {
102             if (isValueReference(value))
103             {
104                 ValueBinding vb = context.getApplication().createValueBinding(value);
105                 component.setValueBinding(propName, vb);
106             }
107             else
108             {
109                 //TODO: Warning if component has no such property (with reflection)
110
component.getAttributes().put(propName, value);
111             }
112         }
113     }
114
115
116     public static void setBooleanProperty(FacesContext context,
117                                       UIComponent component,
118                                       String JavaDoc propName,
119                                       String JavaDoc value)
120     {
121         if (value != null)
122         {
123             if (isValueReference(value))
124             {
125                 ValueBinding vb = context.getApplication().createValueBinding(value);
126                 component.setValueBinding(propName, vb);
127             }
128             else
129             {
130                 //TODO: More sophisticated way to convert boolean value (yes/no, 1/0, on/off, etc.)
131
component.getAttributes().put(propName, Boolean.valueOf(value));
132             }
133         }
134     }
135
136
137     public static void setValueProperty(FacesContext context,
138                                         UIComponent component,
139                                         String JavaDoc value)
140     {
141         if (value != null)
142         {
143             if (isValueReference(value))
144             {
145                 ValueBinding vb = context.getApplication().createValueBinding(value);
146                 component.setValueBinding(JSFAttr.VALUE_ATTR, vb);
147             }
148             else if (component instanceof UICommand)
149             {
150                 ((UICommand)component).setValue(value);
151             }
152             else if (component instanceof UIParameter)
153             {
154                 ((UIParameter)component).setValue(value);
155             }
156             else if (component instanceof UISelectBoolean)
157             {
158                 ((UISelectBoolean)component).setValue(Boolean.valueOf(value));
159             }
160             else if (component instanceof UIGraphic)
161             {
162                 ((UIGraphic)component).setValue(value);
163             }
164             //Since many input components are ValueHolders the special components
165
//must come first, ValueHolder is the last resort.
166
else if (component instanceof ValueHolder)
167             {
168                 ((ValueHolder)component).setValue(value);
169             }
170             else
171             {
172                 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value.");
173             }
174         }
175     }
176
177
178     public static void setConverterProperty(FacesContext context,
179                                         UIComponent component,
180                                         String JavaDoc value)
181     {
182         if (value != null)
183         {
184             if (component instanceof ValueHolder)
185             {
186                 if (isValueReference(value))
187                 {
188                     ValueBinding vb = context.getApplication().createValueBinding(value);
189                     component.setValueBinding(JSFAttr.CONVERTER_ATTR, vb);
190                 }
191                 else
192                 {
193                     FacesContext facesContext = FacesContext.getCurrentInstance();
194                     Converter converter = facesContext.getApplication().createConverter(value);
195                     ((ValueHolder)component).setConverter(converter);
196                 }
197             }
198             else
199             {
200                 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value.");
201             }
202         }
203     }
204
205
206     public static void setValidatorProperty(FacesContext context,
207                                             UIComponent component,
208                                             String JavaDoc validator)
209     {
210         if (validator != null)
211         {
212             if (!(component instanceof EditableValueHolder))
213             {
214                 throw new IllegalArgumentException JavaDoc("Component " + component.getClientId(context) + " is no EditableValueHolder");
215             }
216             if (isValueReference(validator))
217             {
218                 MethodBinding mb = context.getApplication().createMethodBinding(validator,
219                                                                                 VALIDATOR_ARGS);
220                 ((EditableValueHolder)component).setValidator(mb);
221             }
222             else
223             {
224                 log.error("Invalid expression " + validator);
225             }
226         }
227     }
228
229     public static void setValueBinding(FacesContext context,
230                                        UIComponent component,
231                                        String JavaDoc propName,
232                                        String JavaDoc value)
233     {
234         if (value != null)
235         {
236             if (isValueReference(value))
237             {
238                 ValueBinding vb = context.getApplication().createValueBinding(value);
239                 component.setValueBinding(propName, vb);
240             }
241             else
242             {
243                 throw new IllegalArgumentException JavaDoc("Attribute " + propName + " must be a value reference");
244             }
245         }
246     }
247
248     public static void setActionProperty(FacesContext context,
249                                          UIComponent component,
250                                          String JavaDoc action)
251     {
252         if (action != null)
253         {
254             if (!(component instanceof UICommand))
255             {
256                 throw new IllegalArgumentException JavaDoc("Component " + component.getClientId(context) + " is no UICommand");
257             }
258             MethodBinding mb;
259             if (isValueReference(action))
260             {
261                 mb = context.getApplication().createMethodBinding(action, null);
262             }
263             else
264             {
265                 mb = new SimpleActionMethodBinding(action);
266             }
267             ((UICommand)component).setAction(mb);
268         }
269     }
270
271     public static void setActionListenerProperty(FacesContext context,
272                                                  UIComponent component,
273                                                  String JavaDoc actionListener)
274     {
275         if (actionListener != null)
276         {
277             if (!(component instanceof ActionSource))
278             {
279                 throw new IllegalArgumentException JavaDoc("Component " + component.getClientId(context) + " is no ActionSource");
280             }
281             if (isValueReference(actionListener))
282             {
283                 MethodBinding mb = context.getApplication().createMethodBinding(actionListener,
284                                                                                 ACTION_LISTENER_ARGS);
285                 ((ActionSource)component).setActionListener(mb);
286             }
287             else
288             {
289                 log.error("Invalid expression " + actionListener);
290             }
291         }
292     }
293
294     public static void setValueChangedListenerProperty(FacesContext context,
295                                                        UIComponent component,
296                                                        String JavaDoc valueChangedListener)
297     {
298         if (valueChangedListener != null)
299         {
300             if (!(component instanceof EditableValueHolder))
301             {
302                 throw new IllegalArgumentException JavaDoc("Component " + component.getClientId(context) + " is no EditableValueHolder");
303             }
304             if (isValueReference(valueChangedListener))
305             {
306                 MethodBinding mb = context.getApplication().createMethodBinding(valueChangedListener,
307                                                                                 VALUE_LISTENER_ARGS);
308                 ((EditableValueHolder)component).setValueChangeListener(mb);
309             }
310             else
311             {
312                 log.error("Invalid expression " + valueChangedListener);
313             }
314         }
315     }
316
317 }
318
Popular Tags