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