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