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.shared.renderkit.html;
20  
21  import org.apache.myfaces.shared.renderkit.RendererUtils;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.component.html.HtmlPanelGroup;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  import java.io.IOException;
29  import java.util.List;
30  import java.util.Map;
31  import javax.faces.component.behavior.ClientBehavior;
32  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
33  
34  public class HtmlGroupRendererBase
35          extends HtmlRenderer 
36  {
37      private static final String LAYOUT_BLOCK_VALUE = "block";
38  
39      public boolean getRendersChildren()
40      {
41          return true;
42      }
43  
44      @Override
45      public void decode(FacesContext context, UIComponent component)
46      {
47          // Check for npe
48          super.decode(context, component);
49          
50          HtmlRendererUtils.decodeClientBehaviors(context, component);
51      }
52  
53      public void encodeBegin(FacesContext context, UIComponent component)
54              throws IOException
55      {
56      }
57  
58      public void encodeChildren(FacesContext context, UIComponent component)
59          throws IOException
60      {
61      }
62  
63      public void encodeEnd(FacesContext context, UIComponent component)
64              throws IOException
65      {
66          ResponseWriter writer = context.getResponseWriter();
67          boolean span = false;
68  
69          // will be SPAN or DIV, depending on the layout attribute value
70          String layoutElement = HTML.SPAN_ELEM;
71  
72          HtmlPanelGroup panelGroup = (HtmlPanelGroup) component;
73  
74          // if layout is 'block', render DIV instead SPAN
75          String layout = panelGroup.getLayout();
76          if (layout != null && layout.equals(LAYOUT_BLOCK_VALUE))
77          {
78              layoutElement = HTML.DIV_ELEM;
79          }
80          
81          Map<String, List<ClientBehavior>> behaviors = panelGroup.getClientBehaviors();
82          if (behaviors != null && !behaviors.isEmpty())
83          {
84              ResourceUtils.renderDefaultJsfJsInlineIfNecessary(context, writer);
85          }
86  
87          if( (!behaviors.isEmpty()) || 
88                  (component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)))
89          {
90              span = true;
91  
92              writer.startElement(layoutElement, component);
93  
94              //HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
95              writer.writeAttribute(HTML.ID_ATTR, component.getClientId(context), null);
96  
97              long commonPropertiesMarked = 0L;
98              if (isCommonPropertiesOptimizationEnabled(context))
99              {
100                 commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
101                 CommonPropertyUtils.renderCommonPassthroughPropertiesWithoutEvents(writer, 
102                         commonPropertiesMarked, component);
103             }
104             else
105             {
106                 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UNIVERSAL_ATTRIBUTES);
107             }
108             if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(context))
109             {
110                 CommonPropertyUtils.renderEventProperties(writer, 
111                         commonPropertiesMarked, component);
112             }
113             else
114             {
115                 if (isCommonEventsOptimizationEnabled(context))
116                 {
117                     CommonEventUtils.renderBehaviorizedEventHandlers(context, writer, 
118                            commonPropertiesMarked,
119                            CommonEventUtils.getCommonEventsMarked(component), component, behaviors);
120                 }
121                 else
122                 {
123                     HtmlRendererUtils.renderBehaviorizedEventHandlers(context, writer, component, behaviors);
124                 }
125             }
126         }
127         else
128         {
129             if (isCommonPropertiesOptimizationEnabled(context))
130             {
131                 long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
132                 if (commonPropertiesMarked > 0)
133                 {
134                     span = true;
135                     writer.startElement(layoutElement, component);
136                     HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
137 
138                     CommonPropertyUtils.renderCommonPassthroughProperties(writer, commonPropertiesMarked, component);
139                 }
140             }
141             else
142             {
143                 span=HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,
144                                                                                  component,
145                                                                                  layoutElement,
146                                                                                  HTML.COMMON_PASSTROUGH_ATTRIBUTES);
147             }
148         }
149 
150         RendererUtils.renderChildren(context, component);
151         if (span)
152         {
153             writer.endElement(layoutElement);
154         }
155     }
156 
157 }