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 javax.el.ValueExpression;
22 import javax.faces.application.Application;
23 import javax.faces.component.ActionSource;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.convert.Converter;
27 import javax.faces.webapp.UIComponentClassicTagBase;
28 import javax.faces.webapp.UIComponentTag;
29 import javax.servlet.jsp.JspException;
30 import javax.servlet.jsp.tagext.Tag;
31 import javax.servlet.jsp.tagext.TagSupport;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class UpdateActionListenerTag
53 extends TagSupport
54 {
55 private static final long serialVersionUID = -6916153064327074092L;
56
57 private ValueExpression _property;
58 private ValueExpression _value;
59 private String _converter;
60
61 public UpdateActionListenerTag()
62 {
63 }
64
65
66
67
68
69
70
71
72 public void setProperty(ValueExpression property)
73 {
74 _property = property;
75 }
76
77
78
79
80
81
82
83
84
85 public void setValue(ValueExpression value)
86 {
87 _value = value;
88 }
89
90
91
92
93
94
95
96
97
98 public void setConverter(String converter)
99 {
100 _converter = converter;
101 }
102
103 public int doStartTag() throws JspException
104 {
105 if (_property == null) throw new JspException("property attribute not set");
106 if (_value == null) throw new JspException("value attribute not set");
107 if (_property.isLiteralText()) throw new JspException("property attribute is no valid value reference: " + _property);
108
109
110 UIComponentClassicTagBase componentELTag = UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
111 if (componentELTag == null){
112 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
113 if (componentTag == null)
114 {
115 throw new JspException("UpdateActionListenerTag has no UIComponentTag ancestor");
116 }
117 }
118
119 if (componentELTag.getCreated())
120 {
121
122 UIComponent component = componentELTag.getComponentInstance();
123 if (component instanceof ActionSource)
124 {
125 FacesContext facesContext = FacesContext.getCurrentInstance();
126 Application application = facesContext.getApplication();
127 UpdateActionListener al = new UpdateActionListener();
128 al.setPropertyBinding(application.createValueBinding(_property.getExpressionString()));
129 if (!_value.isLiteralText())
130 {
131 al.setValueBinding(application.createValueBinding(_value.getExpressionString()));
132 }
133 else
134 {
135 al.setValue(_value);
136 }
137 if (_converter != null)
138 {
139 Converter converter = application.createConverter(_converter);
140 al.setConverter(converter);
141 }
142 ((ActionSource)component).addActionListener(al);
143 }
144 else
145 {
146 throw new JspException("Component " + component.getId() + " is no ActionSource");
147 }
148 }
149
150 return Tag.SKIP_BODY;
151 }
152
153 public void release()
154 {
155 _property = null;
156 _converter = null;
157 _value = null;
158 }
159
160 }