1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.taglib.core; |
20 | |
|
21 | |
import org.apache.myfaces.shared_impl.util.ClassUtils; |
22 | |
|
23 | |
import javax.el.ELException; |
24 | |
import javax.el.ValueExpression; |
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.event.ActionListener; |
28 | |
import javax.faces.webapp.UIComponentClassicTagBase; |
29 | |
import javax.faces.webapp.UIComponentELTag; |
30 | |
import javax.servlet.jsp.JspException; |
31 | |
import javax.servlet.jsp.tagext.Tag; |
32 | |
import javax.servlet.jsp.tagext.TagSupport; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public abstract class GenericListenerTag<_Holder, _Listener> |
40 | |
extends TagSupport |
41 | |
{ |
42 | 0 | private ValueExpression _type = null; |
43 | 0 | private ValueExpression _binding = null; |
44 | |
private Class<_Holder> _holderClazz; |
45 | |
|
46 | |
protected GenericListenerTag(Class<_Holder> holderClazz) |
47 | |
{ |
48 | 0 | super(); |
49 | 0 | _holderClazz = holderClazz; |
50 | 0 | } |
51 | |
|
52 | |
public void setType(ValueExpression type) |
53 | |
{ |
54 | 0 | _type = type; |
55 | 0 | } |
56 | |
|
57 | |
public void setBinding(ValueExpression binding) |
58 | |
{ |
59 | 0 | _binding = binding; |
60 | 0 | } |
61 | |
|
62 | |
public void release() |
63 | |
{ |
64 | 0 | super.release(); |
65 | 0 | _type = null; |
66 | 0 | _binding = null; |
67 | 0 | } |
68 | |
|
69 | |
protected abstract void addListener(_Holder holder, _Listener listener); |
70 | |
|
71 | |
protected abstract _Listener createDelegateListener(ValueExpression type, ValueExpression binding); |
72 | |
|
73 | |
public int doStartTag() throws JspException |
74 | |
{ |
75 | 0 | UIComponentClassicTagBase componentTag = UIComponentELTag.getParentUIComponentClassicTagBase(pageContext); |
76 | 0 | if (componentTag == null) |
77 | |
{ |
78 | 0 | throw new JspException("no parent UIComponentTag found"); |
79 | |
} |
80 | |
|
81 | 0 | if (_type == null && _binding == null) |
82 | |
{ |
83 | 0 | throw new JspException("\"actionListener\" must have binding and/or type attribute."); |
84 | |
} |
85 | |
|
86 | 0 | if (!componentTag.getCreated()) |
87 | |
{ |
88 | 0 | return Tag.SKIP_BODY; |
89 | |
} |
90 | |
|
91 | 0 | _Holder holder = null; |
92 | 0 | UIComponent component = componentTag.getComponentInstance(); |
93 | |
try |
94 | |
{ |
95 | 0 | holder = (_Holder) component; |
96 | 0 | } catch (ClassCastException e) |
97 | |
{ |
98 | 0 | throw new JspException( |
99 | |
"Component " + ((UIComponent) holder).getId() + " is not instance of " + _holderClazz.getName()); |
100 | 0 | } |
101 | |
|
102 | 0 | if (_type != null && _type.isLiteralText()) |
103 | |
{ |
104 | 0 | createListener(holder,component); |
105 | |
}else{ |
106 | 0 | addListener(holder, createDelegateListener(_type,_binding)); |
107 | |
} |
108 | |
|
109 | 0 | return Tag.SKIP_BODY; |
110 | |
} |
111 | |
|
112 | |
protected void createListener(_Holder holder, UIComponent component) |
113 | |
throws JspException |
114 | |
{ |
115 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
116 | |
_Listener listener; |
117 | |
|
118 | |
try |
119 | |
{ |
120 | 0 | if (null != _binding) |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | listener = (_Listener) _binding.getValue(facesContext.getELContext()); |
125 | 0 | if (null != listener) |
126 | |
{ |
127 | 0 | addListener(holder, listener); |
128 | |
|
129 | 0 | return; |
130 | |
} |
131 | |
} |
132 | 0 | catch (ELException e) |
133 | |
{ |
134 | 0 | throw new JspException("Exception while evaluating the binding attribute of Component " |
135 | |
+ component.getId(), e); |
136 | 0 | } |
137 | |
} |
138 | 0 | if (null != _type) |
139 | |
{ |
140 | |
String className; |
141 | 0 | if (_type.isLiteralText()) |
142 | |
{ |
143 | 0 | className = _type.getExpressionString(); |
144 | |
|
145 | |
|
146 | 0 | listener = (_Listener) ClassUtils.newInstance(className); |
147 | |
} else |
148 | |
{ |
149 | 0 | className = (String) _type.getValue(facesContext.getELContext()); |
150 | 0 | listener = null; |
151 | |
} |
152 | |
|
153 | 0 | if (null != _binding) |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 0 | _binding.setValue(facesContext.getELContext(), listener); |
158 | 0 | } catch (ELException e) |
159 | |
{ |
160 | 0 | throw new JspException("Exception while evaluating the binding attribute of Component " |
161 | |
+ component.getId(), e); |
162 | 0 | } |
163 | |
}else{ |
164 | |
|
165 | |
|
166 | |
|
167 | 0 | listener = (_Listener) ClassUtils.newInstance(className); |
168 | |
} |
169 | 0 | addListener(holder, listener); |
170 | |
} |
171 | 0 | } catch (ClassCastException e) |
172 | |
{ |
173 | 0 | throw new JspException(e); |
174 | 0 | } |
175 | 0 | } |
176 | |
|
177 | |
} |