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.renderkit.html.ext;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.component.behavior.ClientBehavior;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
33  import org.apache.myfaces.component.html.ext.HtmlPanelGroup;
34  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.CommonEventUtils;
36  import org.apache.myfaces.shared_tomahawk.renderkit.html.CommonPropertyUtils;
37  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
38  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlGroupRendererBase;
39  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
40  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
41  
42  
43  /**
44   * 
45   * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
46   * @version $Revision: 167446 $ $Date: 2004-12-23 13:03:09Z $
47   */
48  @JSFRenderer(
49     renderKitId = "HTML_BASIC",
50     family = "javax.faces.Panel",
51     type = "org.apache.myfaces.Group")
52  public class HtmlGroupRenderer
53      extends HtmlGroupRendererBase
54  {
55  
56      @Override
57      protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
58      {
59          return true;
60      }
61  
62      @Override
63      protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext)
64      {
65          return true;
66      }
67  
68      @Override
69      public void decode(FacesContext context, UIComponent component)
70      {
71          super.decode(context, component);
72          
73          HtmlRendererUtils.decodeClientBehaviors(context, component);
74      }
75  
76      public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
77          ResponseWriter writer = context.getResponseWriter();
78          boolean span = false;
79          String element = getHtmlElement(component);
80          
81          Map<String, List<ClientBehavior>> behaviors = null;
82          if (component instanceof ClientBehaviorHolder)
83          {
84              behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
85              if (!behaviors.isEmpty())
86              {
87                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(context, writer);
88              }
89          }
90  
91          if (behaviors != null && !behaviors.isEmpty())
92          {
93              //Render element and id to make javascript work
94              span = true;
95              writer.startElement(element, component);
96              writer.writeAttribute(HTML.ID_ATTR, component.getClientId(context),null);
97              if (isCommonPropertiesOptimizationEnabled(context))
98              {
99                  long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
100                 
101                 CommonPropertyUtils.renderUniversalProperties(writer, commonPropertiesMarked, component);
102                 CommonPropertyUtils.renderStyleProperties(writer, commonPropertiesMarked, component);
103 
104                 if (isCommonEventsOptimizationEnabled(context))
105                 {
106                     Long commonEventsMarked = CommonEventUtils.getCommonEventsMarked(component);
107                     CommonEventUtils.renderBehaviorizedEventHandlers(context, writer, 
108                             commonPropertiesMarked, commonEventsMarked, component, behaviors);
109                 }
110                 else
111                 {
112                     HtmlRendererUtils.renderBehaviorizedEventHandlers(context, writer, component, behaviors);
113                 }
114             }
115             else
116             {
117                 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UNIVERSAL_ATTRIBUTES);
118                 HtmlRendererUtils.renderBehaviorizedEventHandlers(context, writer, component, behaviors);
119             }
120         }
121         else
122         {
123             //No behaviors, do it as usual
124             if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
125             {
126                 span = true;
127 
128                 writer.startElement(element, component);
129 
130                 HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
131 
132                 if (isCommonPropertiesOptimizationEnabled(context))
133                 {
134                     CommonPropertyUtils.renderCommonPassthroughProperties(writer, 
135                             CommonPropertyUtils.getCommonPropertiesMarked(component), component);
136                 }
137                 else
138                 {
139                     HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
140                 }
141             }
142             else
143             {
144                 if (isCommonPropertiesOptimizationEnabled(context))
145                 {
146                     long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
147                     if (commonPropertiesMarked > 0)
148                     {
149                         span = true;
150                         writer.startElement(element, component);
151                         HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
152 
153                         CommonPropertyUtils.renderCommonPassthroughProperties(writer, commonPropertiesMarked, component);
154                     }
155                 }
156                 else
157                 {
158                     span=HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,
159                                                                                      component,
160                                                                                      element,
161                                                                                      HTML.COMMON_PASSTROUGH_ATTRIBUTES);
162                 }
163             }
164         }
165 
166         RendererUtils.renderChildren(context, component);
167         if (span)
168         {
169             writer.endElement(element);
170         }
171     }
172     
173     private String getHtmlElement(UIComponent component) {
174         if (component instanceof HtmlPanelGroup) {
175             HtmlPanelGroup group = (HtmlPanelGroup) component;
176             if (HtmlPanelGroup.BLOCK_LAYOUT.equals(group.getLayout())) {
177                 return HTML.DIV_ELEM;
178             }
179         }
180         return HTML.SPAN_ELEM;
181     }
182 }