View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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                  //If the response is buffered, addResource instance takes
68                  //the responsability of render this script.
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          //Reset the added resource 
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         //Scan children
101         if (component.getChildCount() > 0)
102         {
103             for (UIComponent child : component.getChildren())
104             {
105                 _publishPreRenderViewAddResourceEvent(context, child);
106             }
107         }
108 
109         //Scan facets
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 }