1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.renderkit.html;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Map;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.behavior.ClientBehavior;
27 import javax.faces.component.behavior.ClientBehaviorContext;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
31
32
33
34
35
36 public class CommonEventUtils
37 {
38 public static long getCommonEventsMarked(UIComponent component)
39 {
40 Long commonEvents = (Long) component.getAttributes().get(CommonEventConstants.COMMON_EVENTS_MARKED);
41
42 if (commonEvents == null)
43 {
44 commonEvents = 0L;
45 }
46 return commonEvents;
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public static boolean renderBehaviorizedAttribute(
118 FacesContext facesContext, ResponseWriter writer,
119 String componentProperty, UIComponent component,
120 String targetClientId, String eventName,
121 Map<String, List<ClientBehavior>> clientBehaviors,
122 String htmlAttrName) throws IOException
123 {
124 return renderBehaviorizedAttribute(facesContext, writer,
125 componentProperty, component, targetClientId, eventName, null,
126 clientBehaviors, htmlAttrName, (String) component
127 .getAttributes().get(componentProperty));
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public static boolean renderBehaviorizedAttribute(
147 FacesContext facesContext, ResponseWriter writer,
148 String componentProperty, UIComponent component, String eventName,
149 Collection<ClientBehaviorContext.Parameter> eventParameters,
150 Map<String, List<ClientBehavior>> clientBehaviors,
151 String htmlAttrName, String attributeValue) throws IOException
152 {
153 return renderBehaviorizedAttribute(facesContext, writer,
154 componentProperty, component,
155 component.getClientId(facesContext), eventName,
156 eventParameters, clientBehaviors, htmlAttrName, attributeValue);
157 }
158
159 public static boolean renderBehaviorizedAttribute(
160 FacesContext facesContext, ResponseWriter writer,
161 String componentProperty, UIComponent component,
162 String targetClientId, String eventName,
163 Collection<ClientBehaviorContext.Parameter> eventParameters,
164 Map<String, List<ClientBehavior>> clientBehaviors,
165 String htmlAttrName, String attributeValue) throws IOException
166 {
167
168 List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors
169 .get(eventName) : null;
170
171 if (cbl == null || cbl.size() == 0)
172 {
173 return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
174 attributeValue);
175 }
176
177 if (cbl.size() > 1 || (cbl.size() == 1 && attributeValue != null))
178 {
179 return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
180 HtmlRendererUtils.buildBehaviorChain(facesContext,
181 component, targetClientId, eventName,
182 eventParameters, clientBehaviors, attributeValue,
183 HtmlRendererUtils.STR_EMPTY));
184 }
185 else
186 {
187
188 return HtmlRendererUtils.renderHTMLStringAttribute(
189 writer,
190 componentProperty,
191 htmlAttrName,
192 cbl.get(0).getScript(
193 ClientBehaviorContext.createClientBehaviorContext(
194 facesContext, component, eventName,
195 targetClientId, eventParameters)));
196 }
197 }
198
199 public static void renderBehaviorizedEventHandlers(
200 FacesContext facesContext, ResponseWriter writer,
201 long commonPropertiesMarked, long commonEventsMarked,
202 UIComponent uiComponent,
203 Map<String, List<ClientBehavior>> clientBehaviors)
204 throws IOException
205 {
206 renderBehaviorizedEventHandlers(facesContext, writer,
207 commonPropertiesMarked, commonEventsMarked, uiComponent,
208 uiComponent.getClientId(facesContext), clientBehaviors);
209 }
210
211 public static void renderBehaviorizedEventHandlers(
212 FacesContext facesContext, ResponseWriter writer,
213 long commonPropertiesMarked, long commonEventsMarked,
214 UIComponent uiComponent, String targetClientId,
215 Map<String, List<ClientBehavior>> clientBehaviors)
216 throws IOException
217 {
218 if ((commonPropertiesMarked & CommonPropertyConstants.ONCLICK_PROP) != 0 ||
219 (commonEventsMarked & CommonEventConstants.CLICK_EVENT) != 0)
220 {
221 renderBehaviorizedAttribute(facesContext, writer, HTML.ONCLICK_ATTR,
222 uiComponent, targetClientId, ClientBehaviorEvents.CLICK,
223 clientBehaviors, HTML.ONCLICK_ATTR);
224 }
225 if ((commonPropertiesMarked & CommonPropertyConstants.ONDBLCLICK_PROP) != 0 ||
226 (commonEventsMarked & CommonEventConstants.DBLCLICK_EVENT) != 0)
227 {
228 renderBehaviorizedAttribute(facesContext, writer, HTML.ONDBLCLICK_ATTR,
229 uiComponent, targetClientId, ClientBehaviorEvents.DBLCLICK,
230 clientBehaviors, HTML.ONDBLCLICK_ATTR);
231 }
232 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEDOWN_PROP) != 0 ||
233 (commonEventsMarked & CommonEventConstants.MOUSEDOWN_EVENT) != 0)
234 {
235 renderBehaviorizedAttribute(facesContext, writer,
236 HTML.ONMOUSEDOWN_ATTR, uiComponent, targetClientId,
237 ClientBehaviorEvents.MOUSEDOWN, clientBehaviors,
238 HTML.ONMOUSEDOWN_ATTR);
239 }
240 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEUP_PROP) != 0 ||
241 (commonEventsMarked & CommonEventConstants.MOUSEUP_EVENT) != 0)
242 {
243 renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEUP_ATTR,
244 uiComponent, targetClientId, ClientBehaviorEvents.MOUSEUP,
245 clientBehaviors, HTML.ONMOUSEUP_ATTR);
246 }
247 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEOVER_PROP) != 0 ||
248 (commonEventsMarked & CommonEventConstants.MOUSEOVER_EVENT) != 0)
249 {
250 renderBehaviorizedAttribute(facesContext, writer,
251 HTML.ONMOUSEOVER_ATTR, uiComponent, targetClientId,
252 ClientBehaviorEvents.MOUSEOVER, clientBehaviors,
253 HTML.ONMOUSEOVER_ATTR);
254 }
255 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEMOVE_PROP) != 0 ||
256 (commonEventsMarked & CommonEventConstants.MOUSEMOVE_EVENT) != 0)
257 {
258 renderBehaviorizedAttribute(facesContext, writer,
259 HTML.ONMOUSEMOVE_ATTR, uiComponent, targetClientId,
260 ClientBehaviorEvents.MOUSEMOVE, clientBehaviors,
261 HTML.ONMOUSEMOVE_ATTR);
262 }
263 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEOUT_PROP) != 0 ||
264 (commonEventsMarked & CommonEventConstants.MOUSEOUT_EVENT) != 0)
265 {
266 renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR,
267 uiComponent, targetClientId, ClientBehaviorEvents.MOUSEOUT,
268 clientBehaviors, HTML.ONMOUSEOUT_ATTR);
269 }
270 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYPRESS_PROP) != 0 ||
271 (commonEventsMarked & CommonEventConstants.KEYPRESS_EVENT) != 0)
272 {
273 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYPRESS_ATTR,
274 uiComponent, targetClientId, ClientBehaviorEvents.KEYPRESS,
275 clientBehaviors, HTML.ONKEYPRESS_ATTR);
276 }
277 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYDOWN_PROP) != 0 ||
278 (commonEventsMarked & CommonEventConstants.KEYDOWN_EVENT) != 0)
279 {
280 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYDOWN_ATTR,
281 uiComponent, targetClientId, ClientBehaviorEvents.KEYDOWN,
282 clientBehaviors, HTML.ONKEYDOWN_ATTR);
283 }
284 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYUP_PROP) != 0 ||
285 (commonEventsMarked & CommonEventConstants.KEYUP_EVENT) != 0)
286 {
287 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYUP_ATTR,
288 uiComponent, targetClientId, ClientBehaviorEvents.KEYUP,
289 clientBehaviors, HTML.ONKEYUP_ATTR);
290 }
291 }
292
293 public static void renderBehaviorizedEventHandlersWithoutOnclick(
294 FacesContext facesContext, ResponseWriter writer,
295 long commonPropertiesMarked, long commonEventsMarked,
296 UIComponent uiComponent,
297 Map<String, List<ClientBehavior>> clientBehaviors)
298 throws IOException
299 {
300 renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer,
301 commonPropertiesMarked, commonEventsMarked, uiComponent,
302 uiComponent.getClientId(facesContext), clientBehaviors);
303 }
304
305
306
307
308
309
310
311
312
313 public static void renderBehaviorizedEventHandlersWithoutOnclick(
314 FacesContext facesContext, ResponseWriter writer,
315 long commonPropertiesMarked, long commonEventsMarked,
316 UIComponent uiComponent, String targetClientId,
317 Map<String, List<ClientBehavior>> clientBehaviors)
318 throws IOException
319 {
320 if ((commonPropertiesMarked & CommonPropertyConstants.ONDBLCLICK_PROP) != 0 ||
321 (commonEventsMarked & CommonEventConstants.DBLCLICK_EVENT) != 0)
322 {
323 renderBehaviorizedAttribute(facesContext, writer, HTML.ONDBLCLICK_ATTR,
324 uiComponent, targetClientId, ClientBehaviorEvents.DBLCLICK,
325 clientBehaviors, HTML.ONDBLCLICK_ATTR);
326 }
327 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEDOWN_PROP) != 0 ||
328 (commonEventsMarked & CommonEventConstants.MOUSEDOWN_EVENT) != 0)
329 {
330 renderBehaviorizedAttribute(facesContext, writer,
331 HTML.ONMOUSEDOWN_ATTR, uiComponent, targetClientId,
332 ClientBehaviorEvents.MOUSEDOWN, clientBehaviors,
333 HTML.ONMOUSEDOWN_ATTR);
334 }
335 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEUP_PROP) != 0 ||
336 (commonEventsMarked & CommonEventConstants.MOUSEUP_EVENT) != 0)
337 {
338 renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEUP_ATTR,
339 uiComponent, targetClientId, ClientBehaviorEvents.MOUSEUP,
340 clientBehaviors, HTML.ONMOUSEUP_ATTR);
341 }
342 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEOVER_PROP) != 0 ||
343 (commonEventsMarked & CommonEventConstants.MOUSEOVER_EVENT) != 0)
344 {
345 renderBehaviorizedAttribute(facesContext, writer,
346 HTML.ONMOUSEOVER_ATTR, uiComponent, targetClientId,
347 ClientBehaviorEvents.MOUSEOVER, clientBehaviors,
348 HTML.ONMOUSEOVER_ATTR);
349 }
350 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEMOVE_PROP) != 0 ||
351 (commonEventsMarked & CommonEventConstants.MOUSEMOVE_EVENT) != 0)
352 {
353 renderBehaviorizedAttribute(facesContext, writer,
354 HTML.ONMOUSEMOVE_ATTR, uiComponent, targetClientId,
355 ClientBehaviorEvents.MOUSEMOVE, clientBehaviors,
356 HTML.ONMOUSEMOVE_ATTR);
357 }
358 if ((commonPropertiesMarked & CommonPropertyConstants.ONMOUSEOUT_PROP) != 0 ||
359 (commonEventsMarked & CommonEventConstants.MOUSEOUT_EVENT) != 0)
360 {
361 renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR,
362 uiComponent, targetClientId, ClientBehaviorEvents.MOUSEOUT,
363 clientBehaviors, HTML.ONMOUSEOUT_ATTR);
364 }
365 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYPRESS_PROP) != 0 ||
366 (commonEventsMarked & CommonEventConstants.KEYPRESS_EVENT) != 0)
367 {
368 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYPRESS_ATTR,
369 uiComponent, targetClientId, ClientBehaviorEvents.KEYPRESS,
370 clientBehaviors, HTML.ONKEYPRESS_ATTR);
371 }
372 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYDOWN_PROP) != 0 ||
373 (commonEventsMarked & CommonEventConstants.KEYDOWN_EVENT) != 0)
374 {
375 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYDOWN_ATTR,
376 uiComponent, targetClientId, ClientBehaviorEvents.KEYDOWN,
377 clientBehaviors, HTML.ONKEYDOWN_ATTR);
378 }
379 if ((commonPropertiesMarked & CommonPropertyConstants.ONKEYUP_PROP) != 0 ||
380 (commonEventsMarked & CommonEventConstants.KEYUP_EVENT) != 0)
381 {
382 renderBehaviorizedAttribute(facesContext, writer, HTML.ONKEYUP_ATTR,
383 uiComponent, targetClientId, ClientBehaviorEvents.KEYUP,
384 clientBehaviors, HTML.ONKEYUP_ATTR);
385 }
386 }
387
388
389
390
391
392
393
394
395
396 public static void renderBehaviorizedFieldEventHandlers(
397 FacesContext facesContext, ResponseWriter writer,
398 long commonPropertiesMarked, long commonEventsMarked,
399 UIComponent uiComponent, String targetClientId,
400 Map<String, List<ClientBehavior>> clientBehaviors)
401 throws IOException
402 {
403 if ((commonPropertiesMarked & CommonPropertyConstants.ONFOCUS_PROP) != 0 ||
404 (commonEventsMarked & CommonEventConstants.FOCUS_EVENT) != 0)
405 {
406 renderBehaviorizedAttribute(facesContext, writer, HTML.ONFOCUS_ATTR,
407 uiComponent, targetClientId, ClientBehaviorEvents.FOCUS, clientBehaviors,
408 HTML.ONFOCUS_ATTR);
409 }
410 if ((commonPropertiesMarked & CommonPropertyConstants.ONBLUR_PROP) != 0 ||
411 (commonEventsMarked & CommonEventConstants.BLUR_EVENT) != 0)
412 {
413 renderBehaviorizedAttribute(facesContext, writer, HTML.ONBLUR_ATTR,
414 uiComponent, targetClientId, ClientBehaviorEvents.BLUR, clientBehaviors,
415 HTML.ONBLUR_ATTR);
416 }
417 if ((commonPropertiesMarked & CommonPropertyConstants.ONCHANGE_PROP) != 0 ||
418 (commonEventsMarked & CommonEventConstants.CHANGE_EVENT) != 0)
419 {
420 renderBehaviorizedAttribute(facesContext, writer, HTML.ONCHANGE_ATTR,
421 uiComponent, targetClientId, ClientBehaviorEvents.CHANGE, clientBehaviors,
422 HTML.ONCHANGE_ATTR);
423 }
424 if ((commonPropertiesMarked & CommonPropertyConstants.ONSELECT_PROP) != 0 ||
425 (commonEventsMarked & CommonEventConstants.SELECT_EVENT) != 0)
426 {
427 renderBehaviorizedAttribute(facesContext, writer, HTML.ONSELECT_ATTR,
428 uiComponent, targetClientId, ClientBehaviorEvents.SELECT, clientBehaviors,
429 HTML.ONSELECT_ATTR);
430 }
431 }
432
433 public static void renderBehaviorizedFieldEventHandlersWithoutOnfocus(
434 FacesContext facesContext, ResponseWriter writer,
435 long commonPropertiesMarked, long commonEventsMarked,
436 UIComponent uiComponent, String targetClientId,
437 Map<String, List<ClientBehavior>> clientBehaviors)
438 throws IOException
439 {
440 if ((commonPropertiesMarked & CommonPropertyConstants.ONBLUR_PROP) != 0 ||
441 (commonEventsMarked & CommonEventConstants.BLUR_EVENT) != 0)
442 {
443 renderBehaviorizedAttribute(facesContext, writer, HTML.ONBLUR_ATTR,
444 uiComponent, targetClientId, ClientBehaviorEvents.BLUR, clientBehaviors,
445 HTML.ONBLUR_ATTR);
446 }
447 if ((commonPropertiesMarked & CommonPropertyConstants.ONCHANGE_PROP) != 0 ||
448 (commonEventsMarked & CommonEventConstants.CHANGE_EVENT) != 0)
449 {
450 renderBehaviorizedAttribute(facesContext, writer, HTML.ONCHANGE_ATTR,
451 uiComponent, targetClientId, ClientBehaviorEvents.CHANGE, clientBehaviors,
452 HTML.ONCHANGE_ATTR);
453 }
454 if ((commonPropertiesMarked & CommonPropertyConstants.ONSELECT_PROP) != 0 ||
455 (commonEventsMarked & CommonEventConstants.SELECT_EVENT) != 0)
456 {
457 renderBehaviorizedAttribute(facesContext, writer, HTML.ONSELECT_ATTR,
458 uiComponent, targetClientId, ClientBehaviorEvents.SELECT, clientBehaviors,
459 HTML.ONSELECT_ATTR);
460 }
461 }
462
463 public static void renderBehaviorizedFieldEventHandlersWithoutOnchange(
464 FacesContext facesContext, ResponseWriter writer,
465 long commonPropertiesMarked, long commonEventsMarked,
466 UIComponent uiComponent,
467 Map<String, List<ClientBehavior>> clientBehaviors)
468 throws IOException
469 {
470 renderBehaviorizedFieldEventHandlersWithoutOnchange(
471 facesContext, writer, commonPropertiesMarked, commonEventsMarked,
472 uiComponent, uiComponent.getClientId(facesContext), clientBehaviors);
473 }
474
475 public static void renderBehaviorizedFieldEventHandlersWithoutOnchange(
476 FacesContext facesContext, ResponseWriter writer,
477 long commonPropertiesMarked, long commonEventsMarked,
478 UIComponent uiComponent, String targetClientId,
479 Map<String, List<ClientBehavior>> clientBehaviors)
480 throws IOException
481 {
482 if ((commonPropertiesMarked & CommonPropertyConstants.ONFOCUS_PROP) != 0 ||
483 (commonEventsMarked & CommonEventConstants.FOCUS_EVENT) != 0)
484 {
485 renderBehaviorizedAttribute(facesContext, writer, HTML.ONFOCUS_ATTR,
486 uiComponent, targetClientId, ClientBehaviorEvents.FOCUS, clientBehaviors,
487 HTML.ONFOCUS_ATTR);
488 }
489 if ((commonPropertiesMarked & CommonPropertyConstants.ONBLUR_PROP) != 0 ||
490 (commonEventsMarked & CommonEventConstants.BLUR_EVENT) != 0)
491 {
492 renderBehaviorizedAttribute(facesContext, writer, HTML.ONBLUR_ATTR,
493 uiComponent, targetClientId, ClientBehaviorEvents.BLUR, clientBehaviors,
494 HTML.ONBLUR_ATTR);
495 }
496 if ((commonPropertiesMarked & CommonPropertyConstants.ONSELECT_PROP) != 0 ||
497 (commonEventsMarked & CommonEventConstants.SELECT_EVENT) != 0)
498 {
499 renderBehaviorizedAttribute(facesContext, writer, HTML.ONSELECT_ATTR,
500 uiComponent, targetClientId, ClientBehaviorEvents.SELECT, clientBehaviors,
501 HTML.ONSELECT_ATTR);
502 }
503 }
504
505 public static void renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
506 FacesContext facesContext, ResponseWriter writer,
507 long commonPropertiesMarked, long commonEventsMarked,
508 UIComponent uiComponent,
509 Map<String, List<ClientBehavior>> clientBehaviors)
510 throws IOException
511 {
512 renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
513 facesContext, writer,
514 commonPropertiesMarked, commonEventsMarked,
515 uiComponent, uiComponent.getClientId(facesContext),
516 clientBehaviors);
517 }
518
519 public static void renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
520 FacesContext facesContext, ResponseWriter writer,
521 long commonPropertiesMarked, long commonEventsMarked,
522 UIComponent uiComponent, String targetClientId,
523 Map<String, List<ClientBehavior>> clientBehaviors)
524 throws IOException
525 {
526 if ((commonPropertiesMarked & CommonPropertyConstants.ONFOCUS_PROP) != 0 ||
527 (commonEventsMarked & CommonEventConstants.FOCUS_EVENT) != 0)
528 {
529 renderBehaviorizedAttribute(facesContext, writer, HTML.ONFOCUS_ATTR,
530 uiComponent, targetClientId, ClientBehaviorEvents.FOCUS, clientBehaviors,
531 HTML.ONFOCUS_ATTR);
532 }
533 if ((commonPropertiesMarked & CommonPropertyConstants.ONBLUR_PROP) != 0 ||
534 (commonEventsMarked & CommonEventConstants.BLUR_EVENT) != 0)
535 {
536 renderBehaviorizedAttribute(facesContext, writer, HTML.ONBLUR_ATTR,
537 uiComponent, targetClientId, ClientBehaviorEvents.BLUR, clientBehaviors,
538 HTML.ONBLUR_ATTR);
539 }
540 }
541 }