1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared_impl.taglib; |
20 | |
|
21 | |
import org.apache.myfaces.shared_impl.el.SimpleActionMethodBinding; |
22 | |
|
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
|
26 | |
import javax.faces.component.*; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import javax.faces.convert.Converter; |
29 | |
import javax.faces.el.MethodBinding; |
30 | |
import javax.faces.el.ValueBinding; |
31 | |
import javax.faces.event.ActionEvent; |
32 | |
import javax.faces.event.ValueChangeEvent; |
33 | |
import javax.faces.webapp.UIComponentTag; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class UIComponentTagUtils |
42 | |
{ |
43 | 0 | private static final Log log = LogFactory.getLog(UIComponentTagUtils.class); |
44 | |
|
45 | 0 | private static final Class[] VALIDATOR_ARGS = {FacesContext.class, |
46 | |
UIComponent.class, |
47 | |
Object.class}; |
48 | 0 | private static final Class[] ACTION_LISTENER_ARGS = {ActionEvent.class}; |
49 | 0 | private static final Class[] VALUE_LISTENER_ARGS = {ValueChangeEvent.class}; |
50 | |
|
51 | 0 | private UIComponentTagUtils() {} |
52 | |
|
53 | |
|
54 | |
public static boolean isValueReference(String v) |
55 | |
{ |
56 | 0 | return UIComponentTag.isValueReference(v); |
57 | |
} |
58 | |
|
59 | |
|
60 | |
public static void setIntegerProperty(FacesContext context, |
61 | |
UIComponent component, |
62 | |
String propName, |
63 | |
String value) |
64 | |
{ |
65 | 0 | if (value != null) |
66 | |
{ |
67 | 0 | if (isValueReference(value)) |
68 | |
{ |
69 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
70 | 0 | component.setValueBinding(propName, vb); |
71 | 0 | } |
72 | |
else |
73 | |
{ |
74 | |
|
75 | 0 | component.getAttributes().put(propName, Integer.valueOf(value)); |
76 | |
} |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
public static void setLongProperty(FacesContext context, |
81 | |
UIComponent component, |
82 | |
String propName, |
83 | |
String value) |
84 | |
{ |
85 | 0 | if (value != null) |
86 | |
{ |
87 | 0 | if (isValueReference(value)) |
88 | |
{ |
89 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
90 | 0 | component.setValueBinding(propName, vb); |
91 | 0 | } |
92 | |
else |
93 | |
{ |
94 | |
|
95 | 0 | component.getAttributes().put(propName, Long.valueOf(value)); |
96 | |
} |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | |
public static void setStringProperty(FacesContext context, |
101 | |
UIComponent component, |
102 | |
String propName, |
103 | |
String value) |
104 | |
{ |
105 | 0 | if (value != null) |
106 | |
{ |
107 | 0 | if (isValueReference(value)) |
108 | |
{ |
109 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
110 | 0 | component.setValueBinding(propName, vb); |
111 | 0 | } |
112 | |
else |
113 | |
{ |
114 | |
|
115 | 0 | component.getAttributes().put(propName, value); |
116 | |
} |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
public static void setBooleanProperty(FacesContext context, |
122 | |
UIComponent component, |
123 | |
String propName, |
124 | |
String value) |
125 | |
{ |
126 | 0 | if (value != null) |
127 | |
{ |
128 | 0 | if (isValueReference(value)) |
129 | |
{ |
130 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
131 | 0 | component.setValueBinding(propName, vb); |
132 | 0 | } |
133 | |
else |
134 | |
{ |
135 | |
|
136 | 0 | component.getAttributes().put(propName, Boolean.valueOf(value)); |
137 | |
} |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
public static void setValueProperty(FacesContext context, |
143 | |
UIComponent component, |
144 | |
String value) |
145 | |
{ |
146 | 0 | if (value != null) |
147 | |
{ |
148 | 0 | if (isValueReference(value)) |
149 | |
{ |
150 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
151 | 0 | component.setValueBinding(org.apache.myfaces.shared_impl.renderkit.JSFAttr.VALUE_ATTR, vb); |
152 | 0 | } |
153 | 0 | else if (component instanceof UICommand) |
154 | |
{ |
155 | 0 | ((UICommand)component).setValue(value); |
156 | |
} |
157 | 0 | else if (component instanceof UIParameter) |
158 | |
{ |
159 | 0 | ((UIParameter)component).setValue(value); |
160 | |
} |
161 | 0 | else if (component instanceof UISelectBoolean) |
162 | |
{ |
163 | 0 | ((UISelectBoolean)component).setValue(Boolean.valueOf(value)); |
164 | |
} |
165 | 0 | else if (component instanceof UIGraphic) |
166 | |
{ |
167 | 0 | ((UIGraphic)component).setValue(value); |
168 | |
} |
169 | |
|
170 | |
|
171 | 0 | else if (component instanceof ValueHolder) |
172 | |
{ |
173 | 0 | ((ValueHolder)component).setValue(value); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value."); |
178 | |
} |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
public static void setConverterProperty(FacesContext context, |
184 | |
UIComponent component, |
185 | |
String value) |
186 | |
{ |
187 | 0 | if (value != null) |
188 | |
{ |
189 | 0 | if (component instanceof ValueHolder) |
190 | |
{ |
191 | 0 | if (isValueReference(value)) |
192 | |
{ |
193 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
194 | 0 | component.setValueBinding(org.apache.myfaces.shared_impl.renderkit.JSFAttr.CONVERTER_ATTR, vb); |
195 | 0 | } |
196 | |
else |
197 | |
{ |
198 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
199 | 0 | Converter converter = facesContext.getApplication().createConverter(value); |
200 | 0 | ((ValueHolder)component).setConverter(converter); |
201 | 0 | } |
202 | |
} |
203 | |
else |
204 | |
{ |
205 | 0 | log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value."); |
206 | |
} |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
public static void setValidatorProperty(FacesContext context, |
212 | |
UIComponent component, |
213 | |
String validator) |
214 | |
{ |
215 | 0 | if (validator != null) |
216 | |
{ |
217 | 0 | if (!(component instanceof EditableValueHolder)) |
218 | |
{ |
219 | 0 | throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder"); |
220 | |
} |
221 | 0 | if (isValueReference(validator)) |
222 | |
{ |
223 | 0 | MethodBinding mb = context.getApplication().createMethodBinding(validator, |
224 | |
VALIDATOR_ARGS); |
225 | 0 | ((EditableValueHolder)component).setValidator(mb); |
226 | 0 | } |
227 | |
else |
228 | |
{ |
229 | 0 | log.error("Component " + component.getClientId(context) + " has invalid validation expression " + validator); |
230 | |
} |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
public static void setValueBinding(FacesContext context, |
235 | |
UIComponent component, |
236 | |
String propName, |
237 | |
String value) |
238 | |
{ |
239 | 0 | if (value != null) |
240 | |
{ |
241 | 0 | if (isValueReference(value)) |
242 | |
{ |
243 | 0 | ValueBinding vb = context.getApplication().createValueBinding(value); |
244 | 0 | component.setValueBinding(propName, vb); |
245 | 0 | } |
246 | |
else |
247 | |
{ |
248 | 0 | throw new IllegalArgumentException("Component " + component.getClientId(context) + " attribute " + propName + " must be a value reference, was " + value); |
249 | |
} |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | |
public static void setActionProperty(FacesContext context, |
254 | |
UIComponent component, |
255 | |
String action) |
256 | |
{ |
257 | 0 | if (action != null) |
258 | |
{ |
259 | 0 | if (!(component instanceof ActionSource)) |
260 | |
{ |
261 | 0 | throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource"); |
262 | |
} |
263 | |
MethodBinding mb; |
264 | 0 | if (isValueReference(action)) |
265 | |
{ |
266 | 0 | mb = context.getApplication().createMethodBinding(action, null); |
267 | |
} |
268 | |
else |
269 | |
{ |
270 | 0 | mb = new SimpleActionMethodBinding(action); |
271 | |
} |
272 | 0 | ((ActionSource)component).setAction(mb); |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | |
public static void setActionListenerProperty(FacesContext context, |
277 | |
UIComponent component, |
278 | |
String actionListener) |
279 | |
{ |
280 | 0 | if (actionListener != null) |
281 | |
{ |
282 | 0 | if (!(component instanceof ActionSource)) |
283 | |
{ |
284 | 0 | throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource"); |
285 | |
} |
286 | 0 | if (isValueReference(actionListener)) |
287 | |
{ |
288 | 0 | MethodBinding mb = context.getApplication().createMethodBinding(actionListener, |
289 | |
ACTION_LISTENER_ARGS); |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | 0 | ((ActionSource)component).setActionListener(mb); |
301 | 0 | } |
302 | |
else |
303 | |
{ |
304 | 0 | log.error("Component " + component.getClientId(context) + " has invalid actionListener value: " + actionListener); |
305 | |
} |
306 | |
} |
307 | 0 | } |
308 | |
|
309 | |
public static void setValueChangedListenerProperty(FacesContext context, |
310 | |
UIComponent component, |
311 | |
String valueChangedListener) |
312 | |
{ |
313 | 0 | if (valueChangedListener != null) |
314 | |
{ |
315 | 0 | if (!(component instanceof EditableValueHolder)) |
316 | |
{ |
317 | 0 | throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder"); |
318 | |
} |
319 | 0 | if (isValueReference(valueChangedListener)) |
320 | |
{ |
321 | 0 | MethodBinding mb = context.getApplication().createMethodBinding(valueChangedListener, |
322 | |
VALUE_LISTENER_ARGS); |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | 0 | ((EditableValueHolder)component).setValueChangeListener(mb); |
333 | 0 | } |
334 | |
else |
335 | |
{ |
336 | 0 | log.error("Component " + component.getClientId(context) + " has invalid valueChangedListener expression " + valueChangedListener); |
337 | |
} |
338 | |
} |
339 | 0 | } |
340 | |
|
341 | |
} |