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.view.facelets.component;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  import javax.faces.FacesException;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.behavior.ClientBehavior;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  import javax.faces.render.Renderer;
30  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
31  import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
32  import org.apache.myfaces.shared.renderkit.RendererUtils;
33  import org.apache.myfaces.shared.renderkit.html.CommonEventUtils;
34  import org.apache.myfaces.shared.renderkit.html.CommonPropertyUtils;
35  import org.apache.myfaces.shared.renderkit.html.HTML;
36  import org.apache.myfaces.shared.renderkit.html.HtmlRenderer;
37  import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
38  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
39  
40  /**
41   *
42   * @author Leonardo Uribe
43   */
44  @JSFRenderer(
45      renderKitId = "HTML_BASIC",
46      family = "javax.faces.Panel",
47      type = "javax.faces.passthrough.Element")
48  public class JsfElementRenderer extends HtmlRenderer
49  {
50  
51      public boolean getRendersChildren()
52      {
53          return true;
54      }
55      
56      @Override
57      public void decode(FacesContext context, UIComponent component)
58      {
59          // Check for npe
60          super.decode(context, component);
61          
62          HtmlRendererUtils.decodeClientBehaviors(context, component);
63      }
64  
65      public void encodeBegin(FacesContext facesContext, UIComponent component)
66          throws IOException
67      {
68          ResponseWriter writer = facesContext.getResponseWriter();
69          String elementName = (String) 
70              component.getPassThroughAttributes().get(Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY);
71  
72          if (elementName == null)
73          {
74              throw new FacesException("jsf:element with clientId"
75                  + component.getClientId(facesContext) + " requires 'elementName' passthrough attribute");
76          }
77          JsfElement jsfElement = (JsfElement) component;
78          Map<String, List<ClientBehavior>> behaviors = jsfElement.getClientBehaviors();
79          
80          if (behaviors != null && !behaviors.isEmpty())
81          {
82              ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
83          }
84          
85          writer.startElement(elementName, component);
86  
87          if (!behaviors.isEmpty())
88          {
89              HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
90          }
91          else
92          {
93              HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
94          }
95          
96          // Write in the optimized way, because this is a renderer for internal use only
97          long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
98          if (behaviors.isEmpty())
99          {
100             CommonPropertyUtils.renderEventProperties(writer, commonPropertiesMarked, component);
101             CommonPropertyUtils.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
102             CommonPropertyUtils.renderChangeSelectEventProperties(writer, commonPropertiesMarked, component);
103         }
104         else
105         {
106             long commonEventsMarked = CommonEventUtils.getCommonEventsMarked(component);
107             CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
108                    commonPropertiesMarked, commonEventsMarked, component, behaviors);
109             CommonEventUtils.renderBehaviorizedFieldEventHandlers(facesContext, writer, 
110                    commonPropertiesMarked, commonEventsMarked, component, 
111                    component.getClientId(facesContext), behaviors);
112         }
113         CommonPropertyUtils.renderStyleProperties(writer, commonPropertiesMarked, component);
114         HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONLOAD_ATTR, component,
115                 ClientBehaviorEvents.LOAD, behaviors, HTML.ONLOAD_ATTR);
116         HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONUNLOAD_ATTR, component,
117                 ClientBehaviorEvents.UNLOAD, behaviors, HTML.ONUNLOAD_ATTR);
118         
119     }
120 
121     public void encodeChildren(FacesContext facesContext, UIComponent component)
122         throws IOException
123     {
124         RendererUtils.renderChildren(facesContext, component);
125     }
126 
127     public void encodeEnd(FacesContext facesContext, UIComponent component)
128         throws IOException
129     {
130         ResponseWriter writer = facesContext.getResponseWriter();
131         String elementName = (String) component.getPassThroughAttributes().get(
132             Renderer.PASSTHROUGH_RENDERER_LOCALNAME_KEY);
133         writer.endElement(elementName);
134     }
135     
136     protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
137     {
138         return true;
139     }
140     
141     protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext)
142     {
143         return true;
144     }
145 }