1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import javax.el.MethodExpression; |
22 | |
import javax.el.ValueExpression; |
23 | |
import javax.faces.context.FacesContext; |
24 | |
import javax.faces.el.MethodBinding; |
25 | |
import javax.faces.event.AbortProcessingException; |
26 | |
import javax.faces.event.ActionEvent; |
27 | |
import javax.faces.event.ActionListener; |
28 | |
import javax.faces.event.FacesEvent; |
29 | |
import javax.faces.event.PhaseId; |
30 | |
|
31 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent; |
32 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFListener; |
33 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
@JSFComponent(defaultRendererType = "javax.faces.Button") |
41 | |
public class UICommand extends UIComponentBase implements ActionSource2 |
42 | |
{ |
43 | |
public static final String COMPONENT_TYPE = "javax.faces.Command"; |
44 | |
public static final String COMPONENT_FAMILY = "javax.faces.Command"; |
45 | |
|
46 | |
private boolean _immediate; |
47 | |
private boolean _immediateSet; |
48 | |
private Object _value; |
49 | |
private MethodExpression _actionExpression; |
50 | |
private MethodBinding _actionListener; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public UICommand() |
56 | 0 | { |
57 | 0 | setRendererType("javax.faces.Button"); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public MethodBinding getAction() |
78 | |
{ |
79 | 0 | MethodExpression actionExpression = getActionExpression(); |
80 | 0 | if (actionExpression instanceof _MethodBindingToMethodExpression) |
81 | |
{ |
82 | 0 | return ((_MethodBindingToMethodExpression) actionExpression) |
83 | |
.getMethodBinding(); |
84 | |
} |
85 | 0 | if (actionExpression != null) |
86 | |
{ |
87 | 0 | return new _MethodExpressionToMethodBinding(actionExpression); |
88 | |
} |
89 | 0 | return null; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void setAction(MethodBinding action) |
96 | |
{ |
97 | 0 | if (action != null) |
98 | |
{ |
99 | 0 | setActionExpression(new _MethodBindingToMethodExpression(action)); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | setActionExpression(null); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
@Override |
108 | |
public void broadcast(FacesEvent event) throws AbortProcessingException |
109 | |
{ |
110 | 0 | super.broadcast(event); |
111 | |
|
112 | 0 | if (event instanceof ActionEvent) |
113 | |
{ |
114 | 0 | FacesContext context = getFacesContext(); |
115 | |
|
116 | 0 | MethodBinding mb = getActionListener(); |
117 | 0 | if (mb != null) |
118 | |
{ |
119 | 0 | mb.invoke(context, new Object[] |
120 | |
{ event }); |
121 | |
} |
122 | |
|
123 | 0 | ActionListener defaultActionListener = context.getApplication() |
124 | |
.getActionListener(); |
125 | 0 | if (defaultActionListener != null) |
126 | |
{ |
127 | 0 | defaultActionListener.processAction((ActionEvent) event); |
128 | |
} |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
public void queueEvent(FacesEvent event) |
134 | |
{ |
135 | 0 | if (event != null && event instanceof ActionEvent) |
136 | |
{ |
137 | 0 | if (isImmediate()) |
138 | |
{ |
139 | 0 | event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | event.setPhaseId(PhaseId.INVOKE_APPLICATION); |
144 | |
} |
145 | |
} |
146 | 0 | super.queueEvent(event); |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
@JSFProperty |
159 | |
public boolean isImmediate() |
160 | |
{ |
161 | 0 | if (_immediateSet) |
162 | |
{ |
163 | 0 | return _immediate; |
164 | |
} |
165 | 0 | ValueExpression expression = getValueExpression("immediate"); |
166 | 0 | if (expression != null) |
167 | |
{ |
168 | 0 | return (Boolean) expression.getValue(getFacesContext() |
169 | |
.getELContext()); |
170 | |
} |
171 | 0 | return false; |
172 | |
} |
173 | |
|
174 | |
public void setImmediate(boolean immediate) |
175 | |
{ |
176 | 0 | this._immediate = immediate; |
177 | 0 | this._immediateSet = true; |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
@JSFProperty |
184 | |
public Object getValue() |
185 | |
{ |
186 | 0 | if (_value != null) |
187 | |
{ |
188 | 0 | return _value; |
189 | |
} |
190 | 0 | ValueExpression expression = getValueExpression("value"); |
191 | 0 | if (expression != null) |
192 | |
{ |
193 | 0 | return expression.getValue(getFacesContext().getELContext()); |
194 | |
} |
195 | 0 | return null; |
196 | |
} |
197 | |
|
198 | |
public void setValue(Object value) |
199 | |
{ |
200 | 0 | this._value = value; |
201 | 0 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
@JSFProperty(stateHolder = true, returnSignature = "java.lang.Object", jspName = "action") |
217 | |
public MethodExpression getActionExpression() |
218 | |
{ |
219 | 0 | if (_actionExpression != null) |
220 | |
{ |
221 | 0 | return _actionExpression; |
222 | |
} |
223 | 0 | ValueExpression expression = getValueExpression("actionExpression"); |
224 | 0 | if (expression != null) |
225 | |
{ |
226 | 0 | return (MethodExpression) expression.getValue(getFacesContext() |
227 | |
.getELContext()); |
228 | |
} |
229 | 0 | return null; |
230 | |
} |
231 | |
|
232 | |
public void setActionExpression(MethodExpression actionExpression) |
233 | |
{ |
234 | 0 | this._actionExpression = actionExpression; |
235 | 0 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
@JSFProperty(stateHolder = true, returnSignature = "void", methodSignature = "javax.faces.event.ActionEvent") |
247 | |
public MethodBinding getActionListener() |
248 | |
{ |
249 | 0 | if (_actionListener != null) |
250 | |
{ |
251 | 0 | return _actionListener; |
252 | |
} |
253 | 0 | ValueExpression expression = getValueExpression("actionListener"); |
254 | 0 | if (expression != null) |
255 | |
{ |
256 | 0 | return (MethodBinding) expression.getValue(getFacesContext() |
257 | |
.getELContext()); |
258 | |
} |
259 | 0 | return null; |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
@JSFProperty(returnSignature="void",methodSignature="javax.faces.event.ActionEvent") |
266 | |
public void setActionListener(MethodBinding actionListener) |
267 | |
{ |
268 | 0 | this._actionListener = actionListener; |
269 | 0 | } |
270 | |
|
271 | |
public void addActionListener(ActionListener listener) |
272 | |
{ |
273 | 0 | addFacesListener(listener); |
274 | 0 | } |
275 | |
|
276 | |
public void removeActionListener(ActionListener listener) |
277 | |
{ |
278 | 0 | removeFacesListener(listener); |
279 | 0 | } |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
@JSFListener(event="javax.faces.event.ActionEvent", |
287 | |
phases="Invoke Application, Apply Request Values") |
288 | |
public ActionListener[] getActionListeners() |
289 | |
{ |
290 | 0 | return (ActionListener[]) getFacesListeners(ActionListener.class); |
291 | |
} |
292 | |
|
293 | |
@Override |
294 | |
public Object saveState(FacesContext facesContext) |
295 | |
{ |
296 | 0 | Object[] values = new Object[6]; |
297 | 0 | values[0] = super.saveState(facesContext); |
298 | 0 | values[1] = _immediate; |
299 | 0 | values[2] = _immediateSet; |
300 | 0 | values[3] = _value; |
301 | 0 | values[4] = saveAttachedState(facesContext, _actionExpression); |
302 | 0 | values[5] = saveAttachedState(facesContext, _actionListener); |
303 | |
|
304 | 0 | return values; |
305 | |
} |
306 | |
|
307 | |
@Override |
308 | |
public void restoreState(FacesContext facesContext, Object state) |
309 | |
{ |
310 | 0 | Object[] values = (Object[]) state; |
311 | 0 | super.restoreState(facesContext, values[0]); |
312 | 0 | _immediate = (Boolean) values[1]; |
313 | 0 | _immediateSet = (Boolean) values[2]; |
314 | 0 | _value = values[3]; |
315 | 0 | _actionExpression = (MethodExpression) restoreAttachedState( |
316 | |
facesContext, values[4]); |
317 | 0 | _actionListener = (MethodBinding) restoreAttachedState(facesContext, |
318 | |
values[5]); |
319 | 0 | } |
320 | |
|
321 | |
@Override |
322 | |
public String getFamily() |
323 | |
{ |
324 | 0 | return COMPONENT_FAMILY; |
325 | |
} |
326 | |
} |