01: /*
02: * Copyright 2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package javax.faces.event;
18:
19: import javax.el.ELContext;
20: import javax.el.MethodExpression;
21: import javax.faces.component.StateHolder;
22: import javax.faces.context.FacesContext;
23:
24: /**
25: * See Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
26: *
27: * @author Stan Silvert
28: */
29: public class MethodExpressionValueChangeListener implements
30: ValueChangeListener, StateHolder {
31:
32: private MethodExpression methodExpression;
33:
34: private boolean isTransient = false;
35:
36: /** Creates a new instance of MethodExpressionValueChangeListener */
37: public MethodExpressionValueChangeListener() {
38: }
39:
40: public MethodExpressionValueChangeListener(
41: MethodExpression methodExpression) {
42: this .methodExpression = methodExpression;
43: }
44:
45: public void processValueChange(ValueChangeEvent event)
46: throws AbortProcessingException {
47: try {
48: Object[] params = new Object[] { event };
49: methodExpression.invoke(elContext(), params);
50: } catch (Exception e) {
51: throw new AbortProcessingException(e);
52: }
53: }
54:
55: private ELContext elContext() {
56: return FacesContext.getCurrentInstance().getELContext();
57: }
58:
59: public void restoreState(FacesContext context, Object state) {
60: methodExpression = (MethodExpression) state;
61: }
62:
63: public Object saveState(FacesContext context) {
64: return methodExpression;
65: }
66:
67: public void setTransient(boolean newTransientValue) {
68: isTransient = newTransientValue;
69: }
70:
71: public boolean isTransient() {
72: return isTransient;
73: }
74:
75: }
|