1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent; |
22 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty; |
23 | |
|
24 | |
import javax.faces.FacesException; |
25 | |
import javax.faces.component.visit.VisitCallback; |
26 | |
import javax.faces.component.visit.VisitContext; |
27 | |
import javax.faces.component.visit.VisitResult; |
28 | |
import javax.faces.context.FacesContext; |
29 | |
import javax.faces.event.PostValidateEvent; |
30 | |
import javax.faces.event.PreValidateEvent; |
31 | |
import javax.faces.view.Location; |
32 | |
|
33 | |
import java.util.Collection; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
@JSFComponent(type = "javax.faces.Form", family = "javax.faces.Form") |
39 | |
public class UIForm extends UIComponentBase implements NamingContainer, UniqueIdVendor |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public String createUniqueId(FacesContext context, String seed) |
52 | |
{ |
53 | 0 | StringBuilder bld = null; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | if (!isPrependId() && seed==null ) |
60 | |
{ |
61 | 0 | bld = new StringBuilder(); |
62 | 0 | UniqueIdVendor parentUniqueIdVendor = _ComponentUtils.findParentUniqueIdVendor(this); |
63 | 0 | if (parentUniqueIdVendor == null) |
64 | |
{ |
65 | 0 | UIViewRoot viewRoot = context.getViewRoot(); |
66 | 0 | if (viewRoot != null) |
67 | |
{ |
68 | 0 | bld.append(viewRoot.createUniqueId()); |
69 | 0 | bld.append('_'); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | |
|
74 | 0 | String location = getComponentLocation(this); |
75 | 0 | throw new FacesException("Cannot create clientId. No id is assigned for component" |
76 | |
+ " to create an id and UIViewRoot is not defined: " |
77 | |
+ getPathToComponent(this) |
78 | |
+ (location != null ? " created from: " + location : "")); |
79 | |
} |
80 | 0 | } |
81 | |
else |
82 | |
{ |
83 | 0 | bld.append(parentUniqueIdVendor.createUniqueId(context, null)); |
84 | 0 | bld.append('_'); |
85 | |
} |
86 | 0 | } |
87 | |
else |
88 | |
{ |
89 | 0 | bld = _getSharedStringBuilder(context); |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | 0 | if(seed==null) |
95 | |
{ |
96 | 0 | Integer uniqueIdCounter = (Integer) getStateHelper().get(PropertyKeys.uniqueIdCounter); |
97 | 0 | uniqueIdCounter = (uniqueIdCounter == null) ? 0 : uniqueIdCounter; |
98 | 0 | getStateHelper().put(PropertyKeys.uniqueIdCounter, (uniqueIdCounter+1)); |
99 | 0 | return bld.append(UIViewRoot.UNIQUE_ID_PREFIX).append(uniqueIdCounter).toString(); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
else |
104 | |
{ |
105 | 0 | return bld.append(UIViewRoot.UNIQUE_ID_PREFIX).append(seed).toString(); |
106 | |
} |
107 | |
} |
108 | |
|
109 | |
public boolean isSubmitted() |
110 | |
{ |
111 | |
|
112 | 0 | return (Boolean) getTransientStateHelper().getTransient(PropertyKeys.submitted, false); |
113 | |
} |
114 | |
|
115 | |
public void setSubmitted(boolean submitted) |
116 | |
{ |
117 | 0 | getTransientStateHelper().putTransient(PropertyKeys.submitted, submitted); |
118 | |
|
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
public void processDecodes(FacesContext context) |
123 | |
{ |
124 | 0 | if (context == null) |
125 | |
{ |
126 | 0 | throw new NullPointerException("context"); |
127 | |
} |
128 | |
try |
129 | |
{ |
130 | 0 | setCachedFacesContext(context); |
131 | |
try |
132 | |
{ |
133 | 0 | pushComponentToEL(context, this); |
134 | |
|
135 | 0 | decode(context); |
136 | |
|
137 | 0 | if (!isSubmitted()) |
138 | |
{ |
139 | |
return; |
140 | |
} |
141 | |
|
142 | 0 | int facetCount = getFacetCount(); |
143 | 0 | if (facetCount > 0) |
144 | |
{ |
145 | 0 | for (UIComponent facet : getFacets().values()) |
146 | |
{ |
147 | 0 | facet.processDecodes(context); |
148 | 0 | } |
149 | |
} |
150 | |
|
151 | 0 | for (int i = 0, childCount = getChildCount(); i < childCount; i++) |
152 | |
{ |
153 | 0 | UIComponent child = getChildren().get(i); |
154 | 0 | child.processDecodes(context); |
155 | |
} |
156 | |
|
157 | |
} |
158 | |
finally |
159 | |
{ |
160 | 0 | popComponentFromEL(context); |
161 | 0 | } |
162 | |
} |
163 | |
finally |
164 | |
{ |
165 | 0 | setCachedFacesContext(null); |
166 | 0 | } |
167 | 0 | } |
168 | |
|
169 | |
@Override |
170 | |
public void processValidators(FacesContext context) |
171 | |
{ |
172 | 0 | if (context == null) |
173 | |
{ |
174 | 0 | throw new NullPointerException("context"); |
175 | |
} |
176 | |
|
177 | |
try |
178 | |
{ |
179 | 0 | setCachedFacesContext(context); |
180 | |
try |
181 | |
{ |
182 | 0 | pushComponentToEL(context, this); |
183 | |
|
184 | |
|
185 | |
|
186 | 0 | if (!isSubmitted()) |
187 | |
{ |
188 | 0 | decode(context); |
189 | |
} |
190 | 0 | if (!isSubmitted()) |
191 | |
{ |
192 | |
return; |
193 | |
} |
194 | |
|
195 | |
|
196 | 0 | context.getApplication().publishEvent(context, PreValidateEvent.class, getClass(), this); |
197 | |
|
198 | 0 | int facetCount = getFacetCount(); |
199 | 0 | if (facetCount > 0) |
200 | |
{ |
201 | 0 | for (UIComponent facet : getFacets().values()) |
202 | |
{ |
203 | 0 | facet.processValidators(context); |
204 | 0 | } |
205 | |
} |
206 | |
|
207 | 0 | for (int i = 0, childCount = getChildCount(); i < childCount; i++) |
208 | |
{ |
209 | 0 | UIComponent child = getChildren().get(i); |
210 | 0 | child.processValidators(context); |
211 | |
} |
212 | |
|
213 | |
} |
214 | |
finally |
215 | |
{ |
216 | 0 | context.getApplication().publishEvent(context, PostValidateEvent.class, getClass(), this); |
217 | 0 | popComponentFromEL(context); |
218 | 0 | } |
219 | |
} |
220 | |
finally |
221 | |
{ |
222 | 0 | setCachedFacesContext(null); |
223 | 0 | } |
224 | 0 | } |
225 | |
|
226 | |
@Override |
227 | |
public void processUpdates(FacesContext context) |
228 | |
{ |
229 | 0 | if (context == null) |
230 | |
{ |
231 | 0 | throw new NullPointerException("context"); |
232 | |
} |
233 | |
|
234 | |
try |
235 | |
{ |
236 | 0 | setCachedFacesContext(context); |
237 | |
try |
238 | |
{ |
239 | 0 | pushComponentToEL(context, this); |
240 | |
|
241 | |
|
242 | |
|
243 | 0 | if (!isSubmitted()) |
244 | |
{ |
245 | 0 | decode(context); |
246 | |
} |
247 | 0 | if (!isSubmitted()) |
248 | |
{ |
249 | |
return; |
250 | |
} |
251 | |
|
252 | 0 | int facetCount = getFacetCount(); |
253 | 0 | if (facetCount > 0) |
254 | |
{ |
255 | 0 | for (UIComponent facet : getFacets().values()) |
256 | |
{ |
257 | 0 | facet.processUpdates(context); |
258 | 0 | } |
259 | |
} |
260 | |
|
261 | 0 | for (int i = 0, childCount = getChildCount(); i < childCount; i++) |
262 | |
{ |
263 | 0 | UIComponent child = getChildren().get(i); |
264 | 0 | child.processUpdates(context); |
265 | |
} |
266 | |
|
267 | |
} |
268 | |
finally |
269 | |
{ |
270 | 0 | popComponentFromEL(context); |
271 | 0 | } |
272 | |
} |
273 | |
finally |
274 | |
{ |
275 | 0 | setCachedFacesContext(null); |
276 | 0 | } |
277 | 0 | } |
278 | |
|
279 | 0 | enum PropertyKeys |
280 | |
{ |
281 | 0 | prependId, |
282 | 0 | uniqueIdCounter, |
283 | 0 | submitted, |
284 | |
} |
285 | |
|
286 | |
@Override |
287 | |
public boolean visitTree(VisitContext context, VisitCallback callback) |
288 | |
{ |
289 | 0 | if (!isPrependId()) |
290 | |
{ |
291 | |
|
292 | |
|
293 | |
|
294 | 0 | return super.visitTree(context, callback); |
295 | |
} |
296 | |
else |
297 | |
{ |
298 | 0 | pushComponentToEL(context.getFacesContext(), this); |
299 | 0 | boolean isCachedFacesContext = isCachedFacesContext(); |
300 | |
try |
301 | |
{ |
302 | 0 | if (!isCachedFacesContext) |
303 | |
{ |
304 | 0 | setCachedFacesContext(context.getFacesContext()); |
305 | |
} |
306 | |
|
307 | 0 | if (!isVisitable(context)) |
308 | |
{ |
309 | 0 | return false; |
310 | |
} |
311 | |
|
312 | 0 | VisitResult res = context.invokeVisitCallback(this, callback); |
313 | 0 | switch (res) |
314 | |
{ |
315 | |
|
316 | |
case COMPLETE: |
317 | 0 | return true; |
318 | |
|
319 | |
case REJECT: |
320 | 0 | return false; |
321 | |
|
322 | |
|
323 | |
default: |
324 | |
|
325 | |
|
326 | 0 | Collection<String> subtreeIdsToVisit = context.getSubtreeIdsToVisit(this); |
327 | |
|
328 | 0 | if (subtreeIdsToVisit != null && !subtreeIdsToVisit.isEmpty()) |
329 | |
{ |
330 | 0 | if (getFacetCount() > 0) |
331 | |
{ |
332 | 0 | for (UIComponent facet : getFacets().values()) |
333 | |
{ |
334 | 0 | if (facet.visitTree(context, callback)) |
335 | |
{ |
336 | 0 | return true; |
337 | |
} |
338 | 0 | } |
339 | |
} |
340 | 0 | for (int i = 0, childCount = getChildCount(); i < childCount; i++) |
341 | |
{ |
342 | 0 | UIComponent child = getChildren().get(i); |
343 | 0 | if (child.visitTree(context, callback)) |
344 | |
{ |
345 | 0 | return true; |
346 | |
} |
347 | |
} |
348 | |
} |
349 | 0 | return false; |
350 | |
} |
351 | |
} |
352 | |
finally |
353 | |
{ |
354 | |
|
355 | 0 | popComponentFromEL(context.getFacesContext()); |
356 | 0 | if (!isCachedFacesContext) |
357 | |
{ |
358 | 0 | setCachedFacesContext(null); |
359 | |
} |
360 | |
} |
361 | |
} |
362 | |
} |
363 | |
|
364 | |
|
365 | |
|
366 | |
public static final String COMPONENT_TYPE = "javax.faces.Form"; |
367 | |
public static final String COMPONENT_FAMILY = "javax.faces.Form"; |
368 | |
private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Form"; |
369 | |
|
370 | |
public UIForm() |
371 | 0 | { |
372 | 0 | setRendererType(DEFAULT_RENDERER_TYPE); |
373 | 0 | } |
374 | |
|
375 | |
@Override |
376 | |
public String getFamily() |
377 | |
{ |
378 | 0 | return COMPONENT_FAMILY; |
379 | |
} |
380 | |
|
381 | |
private String getComponentLocation(UIComponent component) |
382 | |
{ |
383 | 0 | Location location = (Location) component.getAttributes() |
384 | |
.get(UIComponent.VIEW_LOCATION_KEY); |
385 | 0 | if (location != null) |
386 | |
{ |
387 | 0 | return location.toString(); |
388 | |
} |
389 | 0 | return null; |
390 | |
} |
391 | |
|
392 | |
private String getPathToComponent(UIComponent component) |
393 | |
{ |
394 | 0 | StringBuffer buf = new StringBuffer(); |
395 | |
|
396 | 0 | if (component == null) |
397 | |
{ |
398 | 0 | buf.append("{Component-Path : "); |
399 | 0 | buf.append("[null]}"); |
400 | 0 | return buf.toString(); |
401 | |
} |
402 | |
|
403 | 0 | getPathToComponent(component, buf); |
404 | |
|
405 | 0 | buf.insert(0, "{Component-Path : "); |
406 | 0 | buf.append("}"); |
407 | |
|
408 | 0 | return buf.toString(); |
409 | |
} |
410 | |
|
411 | |
private void getPathToComponent(UIComponent component, StringBuffer buf) |
412 | |
{ |
413 | 0 | if (component == null) |
414 | |
{ |
415 | 0 | return; |
416 | |
} |
417 | |
|
418 | 0 | StringBuffer intBuf = new StringBuffer(); |
419 | |
|
420 | 0 | intBuf.append("[Class: "); |
421 | 0 | intBuf.append(component.getClass().getName()); |
422 | 0 | if (component instanceof UIViewRoot) |
423 | |
{ |
424 | 0 | intBuf.append(",ViewId: "); |
425 | 0 | intBuf.append(((UIViewRoot) component).getViewId()); |
426 | |
} |
427 | |
else |
428 | |
{ |
429 | 0 | intBuf.append(",Id: "); |
430 | 0 | intBuf.append(component.getId()); |
431 | |
} |
432 | 0 | intBuf.append("]"); |
433 | |
|
434 | 0 | buf.insert(0, intBuf.toString()); |
435 | |
|
436 | 0 | getPathToComponent(component.getParent(), buf); |
437 | 0 | } |
438 | |
|
439 | |
|
440 | |
|
441 | |
@Override |
442 | |
public String getContainerClientId(FacesContext ctx) |
443 | |
{ |
444 | 0 | if (isPrependId()) |
445 | |
{ |
446 | 0 | return super.getContainerClientId(ctx); |
447 | |
} |
448 | 0 | UIComponent parentNamingContainer = _ComponentUtils.findParentNamingContainer(this, false); |
449 | 0 | if (parentNamingContainer != null) |
450 | |
{ |
451 | 0 | return parentNamingContainer.getContainerClientId(ctx); |
452 | |
} |
453 | 0 | return null; |
454 | |
} |
455 | |
|
456 | |
@JSFProperty(defaultValue = "true") |
457 | |
public boolean isPrependId() |
458 | |
{ |
459 | 0 | return (Boolean) getStateHelper().eval(PropertyKeys.prependId, true); |
460 | |
} |
461 | |
|
462 | |
public void setPrependId(boolean prependId) |
463 | |
{ |
464 | 0 | getStateHelper().put(PropertyKeys.prependId, prependId ); |
465 | 0 | } |
466 | |
} |