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