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.webapp.UIComponentTag;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.sun.facelets.FaceletContext;
36 import com.sun.facelets.FaceletException;
37 import com.sun.facelets.tag.TagAttribute;
38 import com.sun.facelets.tag.TagConfig;
39 import com.sun.facelets.tag.TagHandler;
40 import com.sun.facelets.tag.jsf.ComponentConfig;
41
42
43
44
45
46
47
48 public class UpdateActionListenerTagHandler extends TagHandler
49 {
50 private static Log logger = LogFactory
51 .getLog(UpdateActionListenerTagHandler.class);
52
53 private TagAttribute converterAttr;
54 private TagAttribute propertyAttr;
55 private TagAttribute valueAttr;
56
57
58
59
60 public UpdateActionListenerTagHandler(ComponentConfig config)
61 {
62 this((TagConfig) config);
63 }
64
65
66
67
68 public UpdateActionListenerTagHandler(TagConfig config)
69 {
70 super(config);
71 valueAttr = getRequiredAttribute("value");
72 propertyAttr = getRequiredAttribute("property");
73 converterAttr = getAttribute("converter");
74 }
75
76
77
78
79 public void apply(FaceletContext ctx, UIComponent parent)
80 throws IOException, FacesException, FaceletException, ELException
81 {
82 logger.debug("Apply called. Component: " + parent);
83 ActionSource actionSource = (ActionSource) parent;
84
85 if (sourceHasProperty(actionSource))
86 return;
87
88 UpdateActionListener al = new UpdateActionListener();
89
90 Application app = ctx.getFacesContext().getApplication();
91 if (converterAttr != null)
92 {
93 Converter converter = app.createConverter(converterAttr
94 .getValue(ctx));
95 al.setConverter(converter);
96 }
97
98 String value = valueAttr.getValue();
99 if (UIComponentTag.isValueReference(value))
100 al.setValueBinding(app.createValueBinding(valueAttr.getValue()));
101 else
102 al.setValue(value);
103
104 al.setPropertyBinding(app.createValueBinding(propertyAttr.getValue()));
105
106 actionSource.addActionListener(al);
107 }
108
109 private boolean sourceHasProperty(ActionSource source)
110 {
111 ActionListener[] listeners = source.getActionListeners();
112 for (int i = 0; i < listeners.length; i++)
113 {
114 ActionListener listener = listeners[i];
115 if (listener instanceof UpdateActionListener == false)
116 continue;
117 UpdateActionListener al = (UpdateActionListener) listener;
118 if (al.getPropertyBinding().getExpressionString().equals(
119 this.propertyAttr.getValue()))
120 {
121 logger.debug("Action listener already has a listener for "
122 + this.propertyAttr.getValue());
123 return true;
124 }
125 }
126 logger.debug("Action listener for property is not present. Property: "
127 + this.propertyAttr.getValue());
128 return false;
129 }
130 }