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.custom.autoscroll;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIForm;
25  import javax.faces.context.FacesContext;
26  import javax.faces.context.ResponseWriter;
27  
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
29  import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
30  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlFormRendererBase;
31  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
33  
34  /**
35   * 
36   * @since 1.1.10
37   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
38   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (miƩ, 03 sep 2008) $
39   */
40  @JSFRenderer(renderKitId = "HTML_BASIC",
41      family = "org.apache.myfaces.custom.autoscroll.AutoscrollHiddenField",
42      type = "org.apache.myfaces.custom.autoscroll.AutoscrollHiddenField")
43  public class AutoscrollHiddenFieldRenderer extends HtmlRenderer
44  {
45      private static final String SCROLL_HIDDEN_INPUT = "org.apache.myfaces.SCROLL_HIDDEN_INPUT";
46      
47      @Override
48      public void encodeEnd(FacesContext context, UIComponent component)
49              throws IOException
50      {
51          //Check npe
52          super.encodeEnd(context, component);
53          
54          //It is necessary a small to obtain the renderer parent of this field.
55          //In theory the current component is called from a form renderer,
56          //because this component is only added by
57          //facesContext.getViewRoot().addComponentResource()
58          //with target="form".
59          component.popComponentFromEL(context);
60          try
61          {
62              UIComponent form = UIComponent.getCurrentComponent(context);
63  
64              if (form instanceof UIForm)
65              {
66                  ResponseWriter writer = context.getResponseWriter();
67                  if (MyfacesConfig.getCurrentInstance(context.getExternalContext()).isAutoScroll())
68                  {
69                      //Render as original, to prevent duplicate hidden input rendering
70                      HtmlFormRendererBase.renderScrollHiddenInputIfNecessary(form, context, writer);
71                  }
72                  else
73                  {
74                      // No automatic scroll, but use of t:autoscroll, so force the rendering of the hidden field.
75                      // It is not necessary to check if the hidden input has been rendered, because there is no
76                      // other component that renders it.
77                      HtmlRendererUtils.renderAutoScrollHiddenInput(context, writer);
78                      context.getExternalContext().getRequestMap().put(getScrollHiddenInputName(context, form), Boolean.TRUE);
79                  }
80              }
81          }
82          finally
83          {
84              //Restore the stack
85              component.pushComponentToEL(context, component);
86          }
87      }
88      
89      private static String getScrollHiddenInputName(FacesContext facesContext, UIComponent form) {
90          StringBuffer buf = new StringBuffer();
91          buf.append(SCROLL_HIDDEN_INPUT);
92          buf.append("_");
93          buf.append(form.getClientId(facesContext));
94          return buf.toString();
95      }
96  }