1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component.behavior; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import javax.faces.component.PartialStateHolder; |
26 | |
import javax.faces.component.StateHolder; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import javax.faces.event.BehaviorEvent; |
29 | |
import javax.faces.event.BehaviorListener; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class BehaviorBase implements Behavior, PartialStateHolder |
35 | |
{ |
36 | |
private _DeltaList<BehaviorListener> _behaviorListeners; |
37 | |
|
38 | |
private boolean _initialState; |
39 | |
|
40 | |
private transient boolean _transient; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public BehaviorBase() |
46 | 0 | { |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public void broadcast(BehaviorEvent event) |
54 | |
{ |
55 | 0 | if (event == null) |
56 | |
{ |
57 | 0 | throw new NullPointerException("event"); |
58 | |
} |
59 | |
|
60 | 0 | if (_behaviorListeners != null) |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | for (int i = 0; i < _behaviorListeners.size() ; i++) |
66 | |
{ |
67 | 0 | BehaviorListener listener = _behaviorListeners.get(i); |
68 | 0 | if (event.isAppropriateListener(listener)) |
69 | |
{ |
70 | 0 | event.processListener(listener); |
71 | |
} |
72 | |
} |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
public void clearInitialState() |
77 | |
{ |
78 | 0 | _initialState = false; |
79 | 0 | if (_behaviorListeners != null) |
80 | |
{ |
81 | 0 | _behaviorListeners.clearInitialState(); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
public boolean initialStateMarked() |
86 | |
{ |
87 | 0 | return _initialState; |
88 | |
} |
89 | |
|
90 | |
public boolean isTransient() |
91 | |
{ |
92 | 0 | return _transient; |
93 | |
} |
94 | |
|
95 | |
public void markInitialState() |
96 | |
{ |
97 | 0 | _initialState = true; |
98 | 0 | if (_behaviorListeners != null) |
99 | |
{ |
100 | 0 | _behaviorListeners.markInitialState(); |
101 | |
} |
102 | 0 | } |
103 | |
|
104 | |
public void restoreState(FacesContext context, Object state) |
105 | |
{ |
106 | 0 | if (state == null) |
107 | |
{ |
108 | 0 | return; |
109 | |
} |
110 | 0 | else if (state instanceof _AttachedDeltaWrapper) |
111 | |
{ |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | 0 | ((StateHolder)_behaviorListeners).restoreState(context, |
117 | |
((_AttachedDeltaWrapper) state).getWrappedStateObject()); |
118 | |
|
119 | |
} |
120 | |
else |
121 | |
{ |
122 | |
|
123 | 0 | _behaviorListeners = (_DeltaList<BehaviorListener>) |
124 | |
restoreAttachedState(context, state); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
public Object saveState(FacesContext context) |
129 | |
{ |
130 | 0 | return saveBehaviorListenersList(context); |
131 | |
} |
132 | |
|
133 | |
private Object saveBehaviorListenersList(FacesContext facesContext) |
134 | |
{ |
135 | 0 | PartialStateHolder holder = (PartialStateHolder) _behaviorListeners; |
136 | 0 | if (initialStateMarked() && _behaviorListeners != null && holder.initialStateMarked()) |
137 | |
{ |
138 | 0 | Object attachedState = holder.saveState(facesContext); |
139 | 0 | if (attachedState != null) |
140 | |
{ |
141 | 0 | return new _AttachedDeltaWrapper(_behaviorListeners.getClass(), |
142 | |
attachedState); |
143 | |
} |
144 | |
|
145 | 0 | return null; |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | return saveAttachedState(facesContext,_behaviorListeners); |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
private static Object saveAttachedState(FacesContext context, Object attachedObject) |
154 | |
{ |
155 | 0 | if (context == null) |
156 | |
{ |
157 | 0 | throw new NullPointerException ("context"); |
158 | |
} |
159 | |
|
160 | 0 | if (attachedObject == null) |
161 | |
{ |
162 | 0 | return null; |
163 | |
} |
164 | |
|
165 | |
|
166 | 0 | if (attachedObject instanceof StateHolder) |
167 | |
{ |
168 | 0 | StateHolder holder = (StateHolder) attachedObject; |
169 | 0 | if (holder.isTransient()) |
170 | |
{ |
171 | 0 | return null; |
172 | |
} |
173 | |
|
174 | 0 | return new _AttachedStateWrapper(attachedObject.getClass(), holder.saveState(context)); |
175 | |
} |
176 | 0 | else if (attachedObject instanceof List) |
177 | |
{ |
178 | 0 | List<Object> lst = new ArrayList<Object>(((List<?>) attachedObject).size()); |
179 | 0 | for (Object item : (List<?>) attachedObject) |
180 | |
{ |
181 | 0 | if (item != null) |
182 | |
{ |
183 | 0 | lst.add(saveAttachedState(context, item)); |
184 | |
} |
185 | 0 | } |
186 | |
|
187 | 0 | return new _AttachedListStateWrapper(lst); |
188 | |
} |
189 | 0 | else if (attachedObject instanceof Serializable) |
190 | |
{ |
191 | 0 | return attachedObject; |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | return new _AttachedStateWrapper(attachedObject.getClass(), null); |
196 | |
} |
197 | |
} |
198 | |
|
199 | |
private static Object restoreAttachedState(FacesContext context, Object stateObj) throws IllegalStateException |
200 | |
{ |
201 | 0 | if (context == null) |
202 | |
{ |
203 | 0 | throw new NullPointerException("context"); |
204 | |
} |
205 | 0 | if (stateObj == null) |
206 | |
{ |
207 | 0 | return null; |
208 | |
} |
209 | 0 | if (stateObj instanceof _AttachedListStateWrapper) |
210 | |
{ |
211 | 0 | List<Object> lst = ((_AttachedListStateWrapper) stateObj).getWrappedStateList(); |
212 | 0 | List<Object> restoredList = new ArrayList<Object>(lst.size()); |
213 | 0 | for (Object item : lst) |
214 | |
{ |
215 | 0 | restoredList.add(restoreAttachedState(context, item)); |
216 | 0 | } |
217 | 0 | return restoredList; |
218 | |
} |
219 | 0 | else if (stateObj instanceof _AttachedStateWrapper) |
220 | |
{ |
221 | 0 | Class<?> clazz = ((_AttachedStateWrapper) stateObj).getClazz(); |
222 | |
Object restoredObject; |
223 | |
try |
224 | |
{ |
225 | 0 | restoredObject = clazz.newInstance(); |
226 | |
} |
227 | 0 | catch (InstantiationException e) |
228 | |
{ |
229 | 0 | throw new RuntimeException("Could not restore StateHolder of type " + clazz.getName() |
230 | |
+ " (missing no-args constructor?)", e); |
231 | |
} |
232 | 0 | catch (IllegalAccessException e) |
233 | |
{ |
234 | 0 | throw new RuntimeException(e); |
235 | 0 | } |
236 | 0 | if (restoredObject instanceof StateHolder) |
237 | |
{ |
238 | 0 | _AttachedStateWrapper wrapper = (_AttachedStateWrapper) stateObj; |
239 | 0 | Object wrappedState = wrapper.getWrappedStateObject(); |
240 | |
|
241 | 0 | StateHolder holder = (StateHolder) restoredObject; |
242 | 0 | holder.restoreState(context, wrappedState); |
243 | |
} |
244 | 0 | return restoredObject; |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 0 | return stateObj; |
249 | |
} |
250 | |
} |
251 | |
|
252 | |
public void setTransient(boolean newTransientValue) |
253 | |
{ |
254 | 0 | _transient = newTransientValue; |
255 | 0 | } |
256 | |
|
257 | |
protected void addBehaviorListener(BehaviorListener listener) |
258 | |
{ |
259 | 0 | if (listener == null) |
260 | |
{ |
261 | 0 | throw new NullPointerException("listener"); |
262 | |
} |
263 | |
|
264 | 0 | if (_behaviorListeners == null) |
265 | |
{ |
266 | |
|
267 | |
|
268 | 0 | _behaviorListeners = new _DeltaList<BehaviorListener>(new ArrayList<BehaviorListener>(1)); |
269 | |
} |
270 | |
|
271 | 0 | _behaviorListeners.add(listener); |
272 | 0 | } |
273 | |
|
274 | |
protected void removeBehaviorListener(BehaviorListener listener) |
275 | |
{ |
276 | 0 | if (listener == null) |
277 | |
{ |
278 | 0 | throw new NullPointerException("listener"); |
279 | |
} |
280 | |
|
281 | 0 | if (_behaviorListeners != null) |
282 | |
{ |
283 | 0 | _behaviorListeners.remove(listener); |
284 | |
} |
285 | 0 | } |
286 | |
} |