1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.updateactionlistener;
20
21 import java.io.IOException;
22
23 import javax.el.ELException;
24 import javax.faces.FacesException;
25 import javax.faces.application.Application;
26 import javax.faces.component.ActionSource;
27 import javax.faces.component.UIComponent;
28 import javax.faces.convert.Converter;
29 import javax.faces.event.ActionListener;
30 import javax.faces.view.facelets.ComponentConfig;
31 import javax.faces.view.facelets.FaceletContext;
32 import javax.faces.view.facelets.FaceletException;
33 import javax.faces.view.facelets.TagAttribute;
34 import javax.faces.view.facelets.TagConfig;
35 import javax.faces.view.facelets.TagHandler;
36 import javax.faces.webapp.UIComponentTag;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41
42
43
44
45
46
47 public class UpdateActionListenerTagHandler extends TagHandler
48 {
49 private static Log logger = LogFactory
50 .getLog(UpdateActionListenerTagHandler.class);
51
52 private TagAttribute converterAttr;
53 private TagAttribute propertyAttr;
54 private TagAttribute valueAttr;
55
56
57
58
59 public UpdateActionListenerTagHandler(ComponentConfig config)
60 {
61 this((TagConfig) config);
62 }
63
64
65
66
67 public UpdateActionListenerTagHandler(TagConfig config)
68 {
69 super(config);
70 valueAttr = getRequiredAttribute("value");
71 propertyAttr = getRequiredAttribute("property");
72 converterAttr = getAttribute("converter");
73 }
74
75
76
77
78 public void apply(FaceletContext ctx, UIComponent parent)
79 throws IOException, FacesException, FaceletException, ELException
80 {
81 logger.debug("Apply called. Component: " + parent);
82 ActionSource actionSource = (ActionSource) parent;
83
84 if (sourceHasProperty(actionSource))
85 return;
86
87 UpdateActionListener al = new UpdateActionListener();
88
89 Application app = ctx.getFacesContext().getApplication();
90 if (converterAttr != null)
91 {
92 Converter converter = app.createConverter(converterAttr
93 .getValue(ctx));
94 al.setConverter(converter);
95 }
96
97 String value = valueAttr.getValue();
98 if (UIComponentTag.isValueReference(value))
99 al.setValueBinding(app.createValueBinding(valueAttr.getValue()));
100 else
101 al.setValue(value);
102
103 al.setPropertyBinding(app.createValueBinding(propertyAttr.getValue()));
104
105 actionSource.addActionListener(al);
106 }
107
108 private boolean sourceHasProperty(ActionSource source)
109 {
110 ActionListener[] listeners = source.getActionListeners();
111 for (int i = 0; i < listeners.length; i++)
112 {
113 ActionListener listener = listeners[i];
114 if (listener instanceof UpdateActionListener == false)
115 continue;
116 UpdateActionListener al = (UpdateActionListener) listener;
117 if (al.getPropertyBinding().getExpressionString().equals(
118 this.propertyAttr.getValue()))
119 {
120 logger.debug("Action listener already has a listener for "
121 + this.propertyAttr.getValue());
122 return true;
123 }
124 }
125 logger.debug("Action listener for property is not present. Property: "
126 + this.propertyAttr.getValue());
127 return false;
128 }
129 }