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.util.Arrays; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
import java.util.EnumSet; |
25 | |
import java.util.Map; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import javax.el.ValueExpression; |
29 | |
import javax.faces.component.StateHelper; |
30 | |
import javax.faces.context.FacesContext; |
31 | |
import javax.faces.event.AjaxBehaviorListener; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class AjaxBehavior extends ClientBehaviorBase |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public static final String BEHAVIOR_ID = "javax.faces.behavior.Ajax"; |
46 | |
|
47 | |
private static final String ATTR_EXECUTE = "execute"; |
48 | |
private static final String ATTR_ON_ERROR = "onerror"; |
49 | |
private static final String ATTR_ON_EVENT = "onevent"; |
50 | |
private static final String ATTR_RENDER = "render"; |
51 | |
private static final String ATTR_DISABLED = "disabled"; |
52 | |
private static final String ATTR_IMMEDIATE = "immediate"; |
53 | |
private static final String ATTR_DELAY = "delay"; |
54 | |
private static final String ATTR_RESET_VALUES = "resetValues"; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private static final String VAL_FORM = "@form"; |
60 | |
private static final String VAL_ALL = "@all"; |
61 | |
private static final String VAL_THIS = "@this"; |
62 | |
private static final String VAL_NONE = "@none"; |
63 | |
|
64 | 0 | private static final Collection<String> VAL_FORM_LIST = Collections.singletonList(VAL_FORM); |
65 | 0 | private static final Collection<String> VAL_ALL_LIST = Collections.singletonList(VAL_ALL); |
66 | 0 | private static final Collection<String> VAL_THIS_LIST = Collections.singletonList(VAL_THIS); |
67 | 0 | private static final Collection<String> VAL_NONE_LIST = Collections.singletonList(VAL_NONE); |
68 | |
|
69 | |
|
70 | 0 | private _DeltaStateHelper<AjaxBehavior> _stateHelper = null; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public AjaxBehavior() |
76 | |
{ |
77 | 0 | super(); |
78 | 0 | } |
79 | |
|
80 | |
public void addAjaxBehaviorListener(AjaxBehaviorListener listener) |
81 | |
{ |
82 | 0 | super.addBehaviorListener(listener); |
83 | 0 | } |
84 | |
|
85 | |
public void removeAjaxBehaviorListener(AjaxBehaviorListener listener) |
86 | |
{ |
87 | 0 | removeBehaviorListener(listener); |
88 | 0 | } |
89 | |
|
90 | |
public Collection<String> getExecute() |
91 | |
{ |
92 | |
|
93 | |
|
94 | |
|
95 | 0 | return evalForCollection(ATTR_EXECUTE); |
96 | |
} |
97 | |
|
98 | |
public void setExecute(Collection<String> execute) |
99 | |
{ |
100 | 0 | getStateHelper().put(ATTR_EXECUTE, execute); |
101 | 0 | } |
102 | |
|
103 | |
public String getOnerror() |
104 | |
{ |
105 | 0 | return (String) getStateHelper().eval(ATTR_ON_ERROR); |
106 | |
} |
107 | |
|
108 | |
public void setOnerror(String onError) |
109 | |
{ |
110 | 0 | getStateHelper().put(ATTR_ON_ERROR, onError); |
111 | 0 | } |
112 | |
|
113 | |
public String getOnevent() |
114 | |
{ |
115 | 0 | return (String) getStateHelper().eval(ATTR_ON_EVENT); |
116 | |
} |
117 | |
|
118 | |
public void setOnevent(String onEvent) |
119 | |
{ |
120 | 0 | getStateHelper().put(ATTR_ON_EVENT, onEvent); |
121 | 0 | } |
122 | |
|
123 | |
public Collection<String> getRender() |
124 | |
{ |
125 | |
|
126 | |
|
127 | |
|
128 | 0 | return evalForCollection(ATTR_RENDER); |
129 | |
} |
130 | |
|
131 | |
public void setRender(Collection<String> render) |
132 | |
{ |
133 | 0 | getStateHelper().put(ATTR_RENDER, render); |
134 | 0 | } |
135 | |
|
136 | |
@SuppressWarnings("unchecked") |
137 | |
public ValueExpression getValueExpression(String name) |
138 | |
{ |
139 | |
|
140 | 0 | if (name == null) |
141 | |
{ |
142 | 0 | throw new NullPointerException("name can not be null"); |
143 | |
} |
144 | |
|
145 | 0 | Map<String,Object> bindings = (Map<String,Object>) getStateHelper(). |
146 | |
get(PropertyKeys.bindings); |
147 | 0 | if (bindings != null) |
148 | |
{ |
149 | 0 | return (ValueExpression) bindings.get(name); |
150 | |
} |
151 | |
else |
152 | |
{ |
153 | 0 | return null; |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
public void setValueExpression(String name, ValueExpression expression) |
158 | |
{ |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | 0 | if (name == null) |
171 | |
{ |
172 | 0 | throw new NullPointerException("name"); |
173 | |
} |
174 | |
|
175 | 0 | if (expression == null) |
176 | |
{ |
177 | 0 | getStateHelper().remove(PropertyKeys.bindings, name); |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | getStateHelper().put(PropertyKeys.bindings, name, expression); |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
public boolean isDisabled() |
186 | |
{ |
187 | 0 | Boolean retVal = (Boolean) getStateHelper().eval(ATTR_DISABLED); |
188 | 0 | retVal = (retVal == null) ? false : retVal; |
189 | 0 | return retVal; |
190 | |
} |
191 | |
|
192 | |
public void setDisabled(boolean disabled) |
193 | |
{ |
194 | 0 | getStateHelper().put(ATTR_DISABLED, disabled); |
195 | 0 | } |
196 | |
|
197 | |
public boolean isImmediate() |
198 | |
{ |
199 | 0 | Boolean retVal = (Boolean) getStateHelper().eval(ATTR_IMMEDIATE); |
200 | 0 | retVal = (retVal == null) ? false : retVal; |
201 | 0 | return retVal; |
202 | |
} |
203 | |
|
204 | |
public void setImmediate(boolean immediate) |
205 | |
{ |
206 | 0 | getStateHelper().put(ATTR_IMMEDIATE, immediate); |
207 | 0 | } |
208 | |
|
209 | |
public boolean isImmediateSet() |
210 | |
{ |
211 | 0 | return (getStateHelper().get(ATTR_IMMEDIATE) != null) || |
212 | |
(getValueExpression(ATTR_IMMEDIATE) != null); |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public boolean isResetValues() |
219 | |
{ |
220 | 0 | Boolean retVal = (Boolean) getStateHelper().eval(ATTR_RESET_VALUES); |
221 | 0 | retVal = (retVal == null) ? false : retVal; |
222 | 0 | return retVal; |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public void setResetValues(boolean resetValues) |
229 | |
{ |
230 | 0 | getStateHelper().put(ATTR_RESET_VALUES, resetValues); |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public boolean isResetValuesSet() |
237 | |
{ |
238 | 0 | return (getStateHelper().get(ATTR_RESET_VALUES) != null) || |
239 | |
(getValueExpression(ATTR_RESET_VALUES) != null); |
240 | |
} |
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public String getDelay() |
247 | |
{ |
248 | 0 | return (String) getStateHelper().eval(ATTR_DELAY); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public void setDelay(String delay) |
256 | |
{ |
257 | 0 | getStateHelper().put(ATTR_DELAY, delay); |
258 | 0 | } |
259 | |
|
260 | |
@Override |
261 | |
public Set<ClientBehaviorHint> getHints() |
262 | |
{ |
263 | 0 | return EnumSet.of(ClientBehaviorHint.SUBMITTING); |
264 | |
} |
265 | |
|
266 | |
@Override |
267 | |
public String getRendererType() |
268 | |
{ |
269 | 0 | return BEHAVIOR_ID; |
270 | |
} |
271 | |
|
272 | |
@Override |
273 | |
public void restoreState(FacesContext facesContext, Object o) |
274 | |
{ |
275 | 0 | if (o == null) |
276 | |
{ |
277 | 0 | return; |
278 | |
} |
279 | 0 | Object[] values = (Object[]) o; |
280 | 0 | if (values[0] != null) |
281 | |
{ |
282 | 0 | super.restoreState(facesContext, values[0]); |
283 | |
} |
284 | 0 | getStateHelper().restoreState(facesContext, values[1]); |
285 | 0 | } |
286 | |
|
287 | |
private StateHelper getStateHelper() |
288 | |
{ |
289 | 0 | return getStateHelper(true); |
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
private StateHelper getStateHelper(boolean create) |
299 | |
{ |
300 | 0 | if(_stateHelper != null) |
301 | |
{ |
302 | 0 | return _stateHelper; |
303 | |
} |
304 | 0 | if(create) |
305 | |
{ |
306 | 0 | _stateHelper = new _DeltaStateHelper<AjaxBehavior>(this); |
307 | |
} |
308 | 0 | return _stateHelper; |
309 | |
} |
310 | |
|
311 | |
@Override |
312 | |
public Object saveState(FacesContext facesContext) |
313 | |
{ |
314 | 0 | if (initialStateMarked()) |
315 | |
{ |
316 | 0 | Object parentSaved = super.saveState(facesContext); |
317 | 0 | Object stateHelperSaved = null; |
318 | 0 | StateHelper stateHelper = getStateHelper(false); |
319 | 0 | if (stateHelper != null) |
320 | |
{ |
321 | 0 | stateHelperSaved = stateHelper.saveState(facesContext); |
322 | |
} |
323 | |
|
324 | 0 | if (parentSaved == null && stateHelperSaved == null) |
325 | |
{ |
326 | |
|
327 | 0 | return null; |
328 | |
} |
329 | 0 | return new Object[]{parentSaved, stateHelperSaved}; |
330 | |
} |
331 | |
else |
332 | |
{ |
333 | 0 | Object[] values = new Object[2]; |
334 | 0 | values[0] = super.saveState(facesContext); |
335 | 0 | StateHelper stateHelper = getStateHelper(false); |
336 | 0 | if (stateHelper != null) |
337 | |
{ |
338 | 0 | values[1] = stateHelper.saveState(facesContext); |
339 | |
} |
340 | 0 | return values; |
341 | |
} |
342 | |
} |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
@SuppressWarnings("unchecked") |
356 | |
private Collection<String> evalForCollection(String attributeName) |
357 | |
{ |
358 | 0 | Object value = getStateHelper().eval(attributeName); |
359 | 0 | if (value == null) |
360 | |
{ |
361 | 0 | return Collections.<String>emptyList(); |
362 | |
} |
363 | 0 | else if (value instanceof Collection) |
364 | |
{ |
365 | 0 | return (Collection<String>) value; |
366 | |
} |
367 | 0 | else if (value instanceof String) |
368 | |
{ |
369 | 0 | return getCollectionFromSpaceSplitString((String) value); |
370 | |
} |
371 | |
else |
372 | |
{ |
373 | 0 | throw new IllegalArgumentException("Type " + value.getClass() |
374 | |
+ " not supported for attribute " + attributeName); |
375 | |
} |
376 | |
} |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
private Collection<String> getCollectionFromSpaceSplitString(String stringValue) |
385 | |
{ |
386 | |
|
387 | 0 | if (stringValue.equals(VAL_FORM)) |
388 | |
{ |
389 | 0 | return VAL_FORM_LIST; |
390 | |
} |
391 | 0 | else if (stringValue.equals(VAL_ALL)) |
392 | |
{ |
393 | 0 | return VAL_ALL_LIST; |
394 | |
} |
395 | 0 | else if (stringValue.equals(VAL_NONE)) |
396 | |
{ |
397 | 0 | return VAL_NONE_LIST; |
398 | |
} |
399 | 0 | else if (stringValue.equals(VAL_THIS)) |
400 | |
{ |
401 | 0 | return VAL_THIS_LIST; |
402 | |
} |
403 | |
|
404 | |
|
405 | 0 | String[] arrValue = stringValue.split(" "); |
406 | 0 | return Arrays.asList(arrValue); |
407 | |
} |
408 | |
|
409 | 0 | private enum PropertyKeys |
410 | |
{ |
411 | 0 | bindings, |
412 | |
} |
413 | |
} |