1 /*
2 * $Id: UICommand.java,v 1.79.4.3 2007/10/18 16:57:29 rlubke Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.component;
42
43 import javax.el.ELException;
44 import javax.el.MethodExpression;
45 import javax.el.ValueExpression;
46 import javax.faces.FacesException;
47 import javax.faces.application.Application;
48 import javax.faces.context.FacesContext;
49 import javax.faces.el.MethodBinding;
50 import javax.faces.event.AbortProcessingException;
51 import javax.faces.event.ActionEvent;
52 import javax.faces.event.ActionListener;
53 import javax.faces.event.FacesEvent;
54 import javax.faces.event.PhaseId;
55 import javax.faces.render.Renderer;
56
57
58 /**
59 * <p><strong>UICommand</strong> is a {@link UIComponent} that represents
60 * a user interface component which, when activated by the user, triggers
61 * an application specific "command" or "action". Such a component is
62 * typically rendered as a push button, a menu item, or a hyperlink.</p>
63 *
64 * <p>When the <code>decode()</code> method of this {@link UICommand}, or
65 * its corresponding {@link Renderer}, detects that this control has been
66 * activated, it will queue an {@link ActionEvent}.
67 * Later on, the <code>broadcast()</code> method will ensure that this
68 * event is broadcast to all interested listeners.</p>
69 *
70 * <p>Listeners will be invoked in the following order:
71 * <ol>
72 * <li>{@link ActionListener}s, in the order in which they were registered.
73 * <li>The "actionListener" {@link MethodExpression} (which will cover
74 * the "actionListener" that was set as a <code>MethodBinding</code>).
75 * <li>The default {@link ActionListener}, retrieved from the
76 * {@link Application} - and therefore, any attached "action"
77 * {@link MethodExpression}.
78 * </ol>
79 * </p>
80 * <p>By default, the <code>rendererType</code> property must be set to
81 * "<code>javax.faces.Button</code>". This value can be changed by calling the
82 * <code>setRendererType()</code> method.</p>
83 */
84
85 public class UICommand extends UIComponentBase
86 implements ActionSource2 {
87
88
89 // ------------------------------------------------------ Manifest Constants
90
91
92 /**
93 * <p>The standard component type for this component.</p>
94 */
95 public static final String COMPONENT_TYPE = "javax.faces.Command";
96
97
98 /**
99 * <p>The standard component family for this component.</p>
100 */
101 public static final String COMPONENT_FAMILY = "javax.faces.Command";
102
103
104 // ------------------------------------------------------------ Constructors
105
106
107 /**
108 * <p>Create a new {@link UICommand} instance with default property
109 * values.</p>
110 */
111 public UICommand() {
112
113 super();
114 setRendererType("javax.faces.Button");
115
116 }
117
118
119 // ------------------------------------------------------ Instance Variables
120
121
122 private Object value = null;
123
124
125 // -------------------------------------------------------------- Properties
126
127
128 public String getFamily() {
129
130 return (COMPONENT_FAMILY);
131
132 }
133
134
135 // ------------------------------------------------- ActionSource/ActionSource2 Properties
136
137
138 /**
139 * {@inheritDoc}
140 * @deprecated This has been replaced by {@link #getActionExpression}.
141 */
142 public MethodBinding getAction() {
143 MethodBinding result = null;
144 MethodExpression me;
145
146 if (null != (me = getActionExpression())) {
147 // if the MethodExpression is an instance of our private
148 // wrapper class.
149 if (me.getClass().equals(MethodExpressionMethodBindingAdapter.class)) {
150 result = ((MethodExpressionMethodBindingAdapter)me).getWrapped();
151 }
152 else {
153 // otherwise, this is a real MethodExpression. Wrap it
154 // in a MethodBinding.
155 result = new MethodBindingMethodExpressionAdapter(me);
156 }
157 }
158 return result;
159
160 }
161
162 /**
163 * {@inheritDoc}
164 * @deprecated This has been replaced by {@link #setActionExpression(javax.el.MethodExpression)}.
165 */
166 public void setAction(MethodBinding action) {
167 MethodExpressionMethodBindingAdapter adapter;
168 if (null != action) {
169 adapter = new MethodExpressionMethodBindingAdapter(action);
170 setActionExpression(adapter);
171 }
172 else {
173 setActionExpression(null);
174 }
175 }
176
177 /**
178 * {@inheritDoc}
179 * @deprecated Use {@link #getActionListeners} instead.
180 */
181 public MethodBinding getActionListener() {
182 return this.methodBindingActionListener;
183 }
184
185 /**
186 * {@inheritDoc}
187 * @deprecated This has been replaced by {@link #addActionListener(javax.faces.event.ActionListener)}.
188 */
189 public void setActionListener(MethodBinding actionListener) {
190 this.methodBindingActionListener = actionListener;
191 }
192
193 /**
194 * <p>The immediate flag.</p>
195 */
196 private Boolean immediate;
197
198
199 public boolean isImmediate() {
200
201 if (this.immediate != null) {
202 return (this.immediate);
203 }
204 ValueExpression ve = getValueExpression("immediate");
205 if (ve != null) {
206 try {
207 return (Boolean.TRUE.equals(ve.getValue(getFacesContext().getELContext())));
208 }
209 catch (ELException e) {
210 throw new FacesException(e);
211 }
212 } else {
213 return (false);
214 }
215
216 }
217
218
219 public void setImmediate(boolean immediate) {
220
221 this.immediate = immediate;
222
223 }
224
225
226
227 /**
228 * <p>Returns the <code>value</code> property of the
229 * <code>UICommand</code>. This is most often rendered as a label.</p>
230 */
231 public Object getValue() {
232
233 if (this.value != null) {
234 return (this.value);
235 }
236 ValueExpression ve = getValueExpression("value");
237 if (ve != null) {
238 try {
239 return (ve.getValue(getFacesContext().getELContext()));
240 }
241 catch (ELException e) {
242 throw new FacesException(e);
243 }
244
245 } else {
246 return (null);
247 }
248
249 }
250
251
252 /**
253 * <p>Sets the <code>value</code> property of the <code>UICommand</code>.
254 * This is most often rendered as a label.</p>
255 *
256 * @param value the new value
257 */
258 public void setValue(Object value) {
259
260 this.value = value;
261
262 }
263
264 private MethodBinding methodBindingActionListener = null;
265
266
267 // ---------------------------------------------------- ActionSource / ActionSource2 Methods
268
269
270 /**
271 * <p>The {@link MethodExpression} that, when invoked, yields the
272 * literal outcome value.</p>
273 */
274 private MethodExpression actionExpression = null;
275
276 public MethodExpression getActionExpression() {
277 return actionExpression;
278 }
279
280 public void setActionExpression(MethodExpression actionExpression) {
281 this.actionExpression = actionExpression;
282 }
283
284 /**
285 * @throws NullPointerException {@inheritDoc}
286 */
287 public void addActionListener(ActionListener listener) {
288
289 addFacesListener(listener);
290
291 }
292
293 public ActionListener[] getActionListeners() {
294
295 ActionListener al[] = (ActionListener [])
296 getFacesListeners(ActionListener.class);
297 return (al);
298
299 }
300
301
302
303 /**
304 * @throws NullPointerException {@inheritDoc}
305 */
306 public void removeActionListener(ActionListener listener) {
307
308 removeFacesListener(listener);
309
310 }
311
312
313 // ----------------------------------------------------- StateHolder Methods
314
315
316 private Object[] values;
317
318 public Object saveState(FacesContext context) {
319
320 if (values == null) {
321 values = new Object[5];
322 }
323
324 values[0] = super.saveState(context);
325 values[1] = saveAttachedState(context, methodBindingActionListener);
326 values[2] = saveAttachedState(context, actionExpression);
327 values[3] = immediate;
328 values[4] = value;
329
330 return (values);
331
332 }
333
334
335 public void restoreState(FacesContext context, Object state) {
336 values = (Object[]) state;
337 super.restoreState(context, values[0]);
338 methodBindingActionListener = (MethodBinding)
339 restoreAttachedState(context, values[1]);
340 actionExpression =
341 (MethodExpression) restoreAttachedState(context, values[2]);
342 immediate = (Boolean) values[3];
343 value = values[4];
344
345 }
346
347
348 // ----------------------------------------------------- UIComponent Methods
349
350
351 /**
352 * <p>In addition to to the default {@link UIComponent#broadcast}
353 * processing, pass the {@link ActionEvent} being broadcast to the
354 * method referenced by <code>actionListener</code> (if any),
355 * and to the default {@link ActionListener} registered on the
356 * {@link javax.faces.application.Application}.</p>
357 *
358 * @param event {@link FacesEvent} to be broadcast
359 *
360 * @throws AbortProcessingException Signal the JavaServer Faces
361 * implementation that no further processing on the current event
362 * should be performed
363 * @throws IllegalArgumentException if the implementation class
364 * of this {@link FacesEvent} is not supported by this component
365 * @throws NullPointerException if <code>event</code> is
366 * <code>null</code>
367 */
368 public void broadcast(FacesEvent event) throws AbortProcessingException {
369
370 // Perform standard superclass processing (including calling our
371 // ActionListeners)
372 super.broadcast(event);
373
374 if (event instanceof ActionEvent) {
375 FacesContext context = getFacesContext();
376
377 // Notify the specified action listener method (if any)
378 MethodBinding mb = getActionListener();
379 if (mb != null) {
380 mb.invoke(context, new Object[] { event });
381 }
382
383 // Invoke the default ActionListener
384 ActionListener listener =
385 context.getApplication().getActionListener();
386 if (listener != null) {
387 listener.processAction((ActionEvent) event);
388 }
389 }
390 }
391
392 /**
393 * <p>Intercept <code>queueEvent</code> and, for {@link ActionEvent}s,
394 * mark the phaseId for the event to be
395 * <code>PhaseId.APPLY_REQUEST_VALUES</code> if the
396 * <code>immediate</code> flag is true,
397 * <code>PhaseId.INVOKE_APPLICATION</code> otherwise.</p>
398 */
399
400 public void queueEvent(FacesEvent e) {
401 UIComponent c = e.getComponent();
402 if (e instanceof ActionEvent && c instanceof ActionSource) {
403 if (((ActionSource) c).isImmediate()) {
404 e.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
405 } else {
406 e.setPhaseId(PhaseId.INVOKE_APPLICATION);
407 }
408 }
409 super.queueEvent(e);
410 }
411 }