001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.component;
030:
031: import java.util.*;
032:
033: import javax.el.*;
034:
035: import javax.faces.*;
036: import javax.faces.application.*;
037: import javax.faces.context.*;
038: import javax.faces.el.*;
039: import javax.faces.event.*;
040:
041: public class UICommand extends UIComponentBase implements ActionSource2 {
042: public static final String COMPONENT_FAMILY = "javax.faces.Command";
043: public static final String COMPONENT_TYPE = "javax.faces.Command";
044:
045: private static final HashMap<String, PropEnum> _propMap = new HashMap<String, PropEnum>();
046:
047: private static final ActionListener[] NULL_ACTION_LISTENERS = new ActionListener[0];
048:
049: private Object _value;
050: private ValueExpression _valueExpr;
051:
052: private Boolean _immediate;
053: private ValueExpression _immediateExpr;
054:
055: private MethodExpression _actionExpr;
056:
057: private ActionListener[] _actionListeners = NULL_ACTION_LISTENERS;
058:
059: public UICommand() {
060: setRendererType("javax.faces.Button");
061: }
062:
063: /**
064: * Returns the component family, used to select the renderer.
065: */
066: public String getFamily() {
067: return COMPONENT_FAMILY;
068: }
069:
070: //
071: // Properties
072: //
073:
074: public Object getValue() {
075: if (_value != null)
076: return _value;
077: else if (_valueExpr != null)
078: return Util.eval(_valueExpr, getFacesContext());
079: else
080: return null;
081: }
082:
083: public void setValue(Object value) {
084: _value = value;
085: }
086:
087: //
088: // Render Properties
089: //
090:
091: public boolean isImmediate() {
092: if (_immediate != null)
093: return _immediate;
094: else if (_immediateExpr != null)
095: return Util.evalBoolean(_immediateExpr, getFacesContext());
096: else
097: return false;
098: }
099:
100: public void setImmediate(boolean immediate) {
101: _immediate = immediate;
102: }
103:
104: //
105: // expression map override
106: //
107:
108: /**
109: * Returns the value expression with the given name.
110: */
111: @Override
112: public ValueExpression getValueExpression(String name) {
113: if ("value".equals(name))
114: return _valueExpr;
115: else if ("immediate".equals(name))
116: return _immediateExpr;
117: else {
118: return super .getValueExpression(name);
119: }
120: }
121:
122: /**
123: * Sets the value expression with the given name.
124: */
125: @Override
126: public void setValueExpression(String name, ValueExpression expr) {
127: if ("value".equals(name))
128: _valueExpr = expr;
129: else if ("immediate".equals(name))
130: _immediateExpr = expr;
131:
132: super .setValueExpression(name, expr);
133: }
134:
135: //
136: // Actions
137: //
138:
139: @Override
140: public void broadcast(FacesEvent event) {
141: super .broadcast(event);
142:
143: if (event instanceof ActionEvent) {
144: ActionEvent actionEvent = (ActionEvent) event;
145:
146: FacesContext context = FacesContext.getCurrentInstance();
147:
148: // jsf/0235, jsf/31h6
149:
150: /*
151: ActionListener []listeners = getActionListeners();
152:
153: for (int i = 0; i < listeners.length; i++)
154: listeners[i].processAction(actionEvent);
155: */
156:
157: MethodBinding binding = getActionListener();
158:
159: if (binding != null)
160: binding.invoke(context, new Object[] {});
161:
162: ActionListener listener = context.getApplication()
163: .getActionListener();
164:
165: if (listener != null) {
166: listener.processAction(actionEvent);
167: }
168: }
169: }
170:
171: @Override
172: public void queueEvent(FacesEvent event) {
173: if (event instanceof ActionEvent) {
174: event
175: .setPhaseId(isImmediate() ? PhaseId.APPLY_REQUEST_VALUES
176: : PhaseId.INVOKE_APPLICATION);
177: }
178:
179: super .queueEvent(event);
180: }
181:
182: @Deprecated
183: public MethodBinding getAction() {
184: if (_actionExpr == null)
185: return null;
186: else if (_actionExpr instanceof MethodExpressionAdapter)
187: return ((MethodExpressionAdapter) _actionExpr).getBinding();
188: else
189: return new MethodBindingAdapter(_actionExpr);
190: }
191:
192: @Deprecated
193: public void setAction(MethodBinding action) {
194: if (action != null)
195: _actionExpr = new MethodExpressionAdapter(action);
196: else
197: _actionExpr = null;
198: }
199:
200: @Deprecated
201: public MethodBinding getActionListener() {
202: FacesListener[] listeners = getFacesListeners(FacesListener.class);
203:
204: for (int i = 0; i < listeners.length; i++) {
205: if (listeners[i] instanceof ActionListenerAdapter) {
206: return ((ActionListenerAdapter) listeners[i])
207: .getBinding();
208: }
209: }
210:
211: return null;
212: }
213:
214: @Deprecated
215: public void setActionListener(MethodBinding action) {
216: if (action == null)
217: throw new NullPointerException();
218:
219: FacesListener[] listeners = getFacesListeners(FacesListener.class);
220:
221: for (int i = 0; i < listeners.length; i++) {
222: if (listeners[i] instanceof ActionListenerAdapter) {
223: listeners[i] = new ActionListenerAdapter(action);
224: _actionListeners = null;
225: return;
226: }
227: }
228:
229: addActionListener(new ActionListenerAdapter(action));
230: }
231:
232: public void addActionListener(ActionListener listener) {
233: if (listener == null)
234: throw new NullPointerException();
235:
236: addFacesListener(listener);
237:
238: _actionListeners = null;
239: }
240:
241: public ActionListener[] getActionListeners() {
242: if (_actionListeners == null) {
243: _actionListeners = (ActionListener[]) getFacesListeners(ActionListener.class);
244: }
245:
246: return _actionListeners;
247: }
248:
249: public void removeActionListener(ActionListener listener) {
250: if (listener == null)
251: throw new NullPointerException();
252:
253: removeFacesListener(listener);
254:
255: _actionListeners = null;
256: }
257:
258: public MethodExpression getActionExpression() {
259: return _actionExpr;
260: }
261:
262: public void setActionExpression(MethodExpression action) {
263: if (action == null)
264: throw new NullPointerException();
265:
266: _actionExpr = action;
267: }
268:
269: //
270: // state
271: //
272:
273: public Object saveState(FacesContext context) {
274: return new Object[] { super .saveState(context), _value,
275: _immediate, saveAttachedState(context, _actionExpr), };
276: }
277:
278: public void restoreState(FacesContext context, Object value) {
279: Object[] state = (Object[]) value;
280:
281: super .restoreState(context, state[0]);
282:
283: int i = 1;
284: _value = state[i++];
285: _immediate = (Boolean) state[i++];
286:
287: _actionListeners = null;
288:
289: _actionExpr = (MethodExpression) restoreAttachedState(context,
290: state[i++]);
291: }
292:
293: //
294: // private helpers
295: //
296:
297: private static enum PropEnum {
298: VALUE,
299: }
300:
301: static class MethodExpressionAdapter extends MethodExpression {
302: private MethodBinding _binding;
303:
304: MethodExpressionAdapter(MethodBinding binding) {
305: _binding = binding;
306: }
307:
308: MethodBinding getBinding() {
309: return _binding;
310: }
311:
312: public boolean isLiteralText() {
313: return false;
314: }
315:
316: public String getExpressionString() {
317: return _binding.getExpressionString();
318: }
319:
320: public MethodInfo getMethodInfo(ELContext context)
321: throws javax.el.PropertyNotFoundException,
322: javax.el.MethodNotFoundException, ELException {
323: throw new UnsupportedOperationException();
324: }
325:
326: public Object invoke(ELContext context, Object[] params)
327: throws javax.el.PropertyNotFoundException,
328: javax.el.MethodNotFoundException, ELException {
329: return _binding.invoke(FacesContext.getCurrentInstance(),
330: params);
331: }
332:
333: public int hashCode() {
334: return _binding.hashCode();
335: }
336:
337: public boolean equals(Object o) {
338: return (this == o);
339: }
340: }
341:
342: static class MethodBindingAdapter extends MethodBinding {
343: private MethodExpression _expr;
344:
345: public MethodBindingAdapter() {
346: }
347:
348: public MethodBindingAdapter(MethodExpression expr) {
349: _expr = expr;
350: }
351:
352: public String getExpressionString() {
353: return _expr.getExpressionString();
354: }
355:
356: @Deprecated
357: public Object invoke(FacesContext context, Object[] param)
358: throws EvaluationException,
359: javax.faces.el.MethodNotFoundException {
360: if (context == null)
361: throw new NullPointerException();
362:
363: try {
364: return _expr.invoke(context.getELContext(), param);
365: } catch (javax.el.MethodNotFoundException e) {
366: throw new javax.faces.el.MethodNotFoundException(e);
367: } catch (ELException e) {
368: if (e.getCause() != null)
369: throw new EvaluationException(e.getCause());
370: else
371: throw new EvaluationException(e);
372: } catch (Exception e) {
373: throw new EvaluationException(e);
374: }
375: }
376:
377: @Deprecated
378: public Class getType(FacesContext context)
379: throws EvaluationException,
380: javax.faces.el.PropertyNotFoundException {
381: try {
382: MethodInfo info = _expr.getMethodInfo(context
383: .getELContext());
384:
385: return info.getReturnType();
386: } catch (javax.el.MethodNotFoundException e) {
387: throw new javax.faces.el.MethodNotFoundException(e);
388: } catch (RuntimeException e) {
389: throw e;
390: } catch (Exception e) {
391: throw new EvaluationException(e);
392: }
393: }
394:
395: public String toString() {
396: return "MethodBindingAdapter["
397: + _expr.getExpressionString() + "]";
398: }
399: }
400:
401: static class ActionListenerAdapter implements ActionListener,
402: StateHolder {
403: private MethodBinding _binding;
404:
405: ActionListenerAdapter() {
406: }
407:
408: ActionListenerAdapter(MethodBinding binding) {
409: _binding = binding;
410: }
411:
412: MethodBinding getBinding() {
413: return _binding;
414: }
415:
416: public void processAction(ActionEvent event) {
417: _binding.invoke(FacesContext.getCurrentInstance(),
418: new Object[] { event });
419: }
420:
421: public Object saveState(FacesContext context) {
422: return _binding.getExpressionString();
423: }
424:
425: public void restoreState(FacesContext context, Object state) {
426: Application app = context.getApplication();
427:
428: String expr = (String) state;
429:
430: _binding = app.createMethodBinding(expr,
431: new Class[] { ActionEvent.class });
432: }
433:
434: public boolean isTransient() {
435: return false;
436: }
437:
438: public void setTransient(boolean isTransient) {
439: }
440:
441: public String toString() {
442: return "ActionListenerAdapter[" + _binding + "]";
443: }
444: }
445:
446: static {
447: _propMap.put("value", PropEnum.VALUE);
448: }
449: }
|