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.ppr;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.myfaces.renderkit.html.ext.HtmlGroupRenderer;
32  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
33  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
34  
35  /**
36   * 
37   * @JSFRenderer
38   *   renderKitId = "HTML_BASIC" 
39   *   family = "org.apache.myfaces.PPRPanelGroup"
40   *   type = "org.apache.myfaces.PPRPanelGroup"
41   * 
42   * @author Ernst Fastl
43   */
44  public class PPRPanelGroupRenderer extends HtmlGroupRenderer
45  {
46      private static Log log = LogFactory.getLog(PPRPanelGroupRenderer.class);
47  
48      private static final String DISABLE_RENDER_CHILDREN = "org.apache.myfaces.PPRPanelGroup.disableRenderChildren";
49  
50      public static final String TRANSIENT_MARKER_ATTRIBUTE = "org.apache.myfaces.PPRPanelGroup.transientComponent";
51  
52      /**
53       * Renders the start of a span element. Iterates over all child
54       * components and sets transient components to transient=false. Those
55       * components are marked with the TRANSIENT_MARKER_ATTRIBUTE so the
56       * {@link PPRPhaseListener} can reset them to transient in the next
57       * non-PPR Request
58       *
59       * @param facesContext the current {@link FacesContext}
60       * @param uiComponent  the {@link PPRPanelGroup} to render
61       */
62      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException
63      {
64          if (uiComponent.getId() == null || uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
65              throw new IllegalArgumentException("'id' is a required attribute for the PPRPanelGroup");
66          }
67  
68          // todo: in 1.2, better use a combo of
69          // invokeComponent/RendererUtils.renderChildren() instead
70          uiComponent.getAttributes().put(DISABLE_RENDER_CHILDREN, Boolean.TRUE);
71  
72          // Iterate over the transient child components and set transient to
73          // false
74          // This is necessary to have those components available for PPR
75          // responses later on
76          for (Iterator iter = uiComponent.getChildren().iterator(); iter.hasNext();) {
77              UIComponent child = (UIComponent) iter.next();
78              if (child.isTransient()) {
79                  child.setTransient(false);
80                  child.getAttributes().put(TRANSIENT_MARKER_ATTRIBUTE, Boolean.TRUE);
81              }
82          }
83  
84          super.encodeBegin(facesContext, uiComponent);
85      }
86  
87      /**
88       * todo: in 1.2, better use a combo of
89       * invokeComponent/RendererUtils.renderChildren() instead
90       *
91       * @param context
92       * @param component
93       * @throws IOException
94       */
95      public void encodeChildren(FacesContext context, UIComponent component) throws IOException
96      {
97          Boolean disableRenderChildren = (Boolean) component.getAttributes().get(DISABLE_RENDER_CHILDREN);
98  
99          if (disableRenderChildren == null || disableRenderChildren.booleanValue() == false) {
100             RendererUtils.renderChildren(context, component);
101         }
102         
103         if (component instanceof PPRPanelGroup)
104         {
105             PPRPanelGroup pprPanelGroup = (PPRPanelGroup) component; 
106             if (PPRSupport.isPartialRequest(context) && pprPanelGroup.getAfterUpdateJSHook() != null)
107             {
108                 // Write the afterUpdateJSHook script to CDATA section
109                 ResponseWriter writer = context.getResponseWriter();
110                 writer.startElement(HTML.SCRIPT_ELEM, pprPanelGroup);
111                 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,
112                         HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,
113                         null);
114                 writer.writeText(pprPanelGroup.getAfterUpdateJSHook(),null);
115                 writer.endElement(HTML.SCRIPT_ELEM);
116             }
117         }
118     }
119 
120     /**
121      * Encodes the end of the span-element and afterwards the inline
122      * JavaScript for the client side initialization of the
123      * {@link PPRPanelGroup}.
124      *
125      * @param facesContext the current {@link FacesContext}
126      * @param uiComponent  the {@link PPRPanelGroup} to render
127      */
128     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException
129     {
130         // Render the span end element
131         super.encodeEnd(facesContext, uiComponent);
132         if (uiComponent instanceof PPRPanelGroup) {
133             PPRPanelGroup pprGroup = (PPRPanelGroup) uiComponent;
134 
135             final String triggers = pprGroup.getPartialTriggers();
136             final String triggerPattern = pprGroup.getPartialTriggerPattern();
137 
138             // Check if triggers, a pattern or a periodical update is
139             // defined
140             if ((triggers != null && triggers.length() > 0) || (triggerPattern != null && triggerPattern.length() > 0) || pprGroup.getPeriodicalUpdate() != null) {
141                 if (PPRSupport.isPartialRequest(facesContext)) {
142                     return;
143                 }
144                 // encode the initialization inline JavaScript
145                 PPRSupport.initPPR(facesContext, pprGroup);
146                 PPRSupport.encodeJavaScript(facesContext, pprGroup);
147             }
148         }
149 
150         // todo: in 1.2, better use a combo of
151         // invokeComponent/RendererUtils.renderChildren() instead
152         uiComponent.getAttributes().put(DISABLE_RENDER_CHILDREN, Boolean.FALSE);
153     }
154 
155 }