1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.tomahawk.application;
20
21 import java.io.IOException;
22 import java.util.Map;
23
24 import javax.faces.FacesException;
25 import javax.faces.application.ViewHandler;
26 import javax.faces.application.ViewHandlerWrapper;
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.context.FacesContext;
30
31 import org.apache.myfaces.custom.autoscroll.AutoscrollBehaviorTagHandler;
32 import org.apache.myfaces.custom.autoscroll.AutoscrollBodyScript;
33 import org.apache.myfaces.custom.autoscroll.AutoscrollHiddenField;
34 import org.apache.myfaces.renderkit.html.util.AddResource;
35 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
36 import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
37 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
38 import org.apache.myfaces.tomahawk.util.TomahawkResourceUtils;
39
40 public class ResourceViewHandlerWrapper extends ViewHandlerWrapper
41 {
42 private ViewHandler _delegate;
43
44 public ResourceViewHandlerWrapper(ViewHandler delegate)
45 {
46 super();
47 _delegate = delegate;
48 }
49
50 @Override
51 public ViewHandler getWrapped()
52 {
53 return _delegate;
54 }
55
56 @Override
57 public void renderView(FacesContext context, UIViewRoot viewToRender)
58 throws IOException, FacesException
59 {
60 if (MyfacesConfig.getCurrentInstance(context.getExternalContext()).isAutoScroll() ||
61 viewToRender.getAttributes().containsKey(AutoscrollBehaviorTagHandler.AUTOSCROLL_TAG_ON_PAGE))
62 {
63 AddResource addResource = AddResourceFactory.getInstance(context);
64
65 if (!addResource.requiresBuffer())
66 {
67
68
69 AutoscrollBodyScript autoscrollBodyScript = (AutoscrollBodyScript)
70 context.getApplication().createComponent(context,
71 AutoscrollBodyScript.COMPONENT_TYPE,
72 AutoscrollBodyScript.DEFAULT_RENDERER_TYPE);
73 autoscrollBodyScript.setTransient(true);
74 autoscrollBodyScript.getAttributes().put(
75 JSFAttr.TARGET_ATTR,
76 TomahawkResourceUtils.BODY_LOCATION);
77 viewToRender.addComponentResource(context, autoscrollBodyScript);
78 }
79
80 AutoscrollHiddenField autoscrollHiddenField = (AutoscrollHiddenField) context.getApplication().
81 createComponent(context,
82 AutoscrollHiddenField.COMPONENT_TYPE,
83 AutoscrollHiddenField.DEFAULT_RENDERER_TYPE);
84 autoscrollHiddenField.setTransient(true);
85 autoscrollHiddenField.getAttributes().put(JSFAttr.TARGET_ATTR, TomahawkResourceUtils.FORM_LOCATION);
86 viewToRender.addComponentResource(context, autoscrollHiddenField);
87 }
88
89
90 TomahawkResourceUtils.resetAddedResources(context);
91
92 _publishPreRenderViewAddResourceEvent(context, viewToRender);
93 super.renderView(context, viewToRender);
94 }
95
96 private void _publishPreRenderViewAddResourceEvent(FacesContext context, UIComponent component)
97 {
98 context.getApplication().publishEvent(context, PreRenderViewAddResourceEvent.class, UIComponent.class, component);
99
100
101 if (component.getChildCount() > 0)
102 {
103 for (UIComponent child : component.getChildren())
104 {
105 _publishPreRenderViewAddResourceEvent(context, child);
106 }
107 }
108
109
110 if (component.getFacetCount() > 0)
111 {
112 for (Map.Entry<String, UIComponent> entry : component.getFacets().entrySet())
113 {
114 _publishPreRenderViewAddResourceEvent(context, entry.getValue());
115 }
116 }
117 }
118 }