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 javax.el.ELException;
22 import javax.el.ValueExpression;
23 import javax.faces.FacesException;
24 import javax.faces.component.StateHolder;
25 import javax.faces.context.FacesContext;
26 import javax.faces.event.AbortProcessingException;
27 import javax.faces.event.ActionEvent;
28 import javax.faces.event.ActionListener;
29
30 import org.apache.myfaces.shared.util.ClassUtils;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class DelegateActionListener implements ActionListener, StateHolder
51 {
52
53 private ValueExpression _type;
54 private ValueExpression _binding;
55
56 public DelegateActionListener()
57 {
58 }
59
60 public DelegateActionListener(ValueExpression type, ValueExpression binding)
61 {
62 super();
63 _type = type;
64 _binding = binding;
65 }
66
67 public boolean isTransient()
68 {
69 return false;
70 }
71
72 public void restoreState(FacesContext facesContext, Object state)
73 {
74 Object[] values = (Object[]) state;
75 _type = (ValueExpression) values[0];
76 _binding = (ValueExpression) values[1];
77 }
78
79 public Object saveState(FacesContext facesContext)
80 {
81 Object[] values = new Object[2];
82 values[0] = _type;
83 values[1] = _binding;
84 return values;
85 }
86
87 public void setTransient(boolean arg0)
88 {
89
90 }
91
92 private ActionListener _getDelegate()
93 {
94 return _createActionListener();
95 }
96
97 private ActionListener _createActionListener()
98 {
99 FacesContext facesContext = FacesContext.getCurrentInstance();
100 ActionListener listener = null;
101
102 try
103 {
104 if (null != _binding)
105 {
106 try
107 {
108 listener = (ActionListener) _binding.getValue(facesContext
109 .getELContext());
110 if (null != listener)
111 {
112 return listener;
113 }
114 }
115 catch (ELException e)
116 {
117 throw new ELException("Exception while evaluating the binding attribute", e);
118 }
119 }
120 if (null != _type)
121 {
122 String className;
123 if (_type.isLiteralText())
124 {
125 className = _type.getExpressionString();
126 }
127 else
128 {
129 className = (String) _type.getValue(facesContext
130 .getELContext());
131 }
132
133 listener = (ActionListener) ClassUtils.newInstance(className);
134 if (null != _binding)
135 {
136 try
137 {
138 _binding
139 .setValue(facesContext.getELContext(), listener);
140 }
141 catch (ELException e)
142 {
143 throw new ELException("Exception while evaluating the binding attribute ");
144 }
145 }
146 return listener;
147 }
148 }
149 catch (ClassCastException e)
150 {
151 throw new FacesException(e);
152 }
153 return listener;
154 }
155
156 public void processAction(ActionEvent event)
157 throws AbortProcessingException
158 {
159 _getDelegate().processAction(event);
160 }
161
162 }