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  
20  package org.apache.myfaces.tobago.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.component.Facets;
23  import org.apache.myfaces.tobago.context.Markup;
24  import org.apache.myfaces.tobago.internal.component.AbstractUISection;
25  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
26  import org.apache.myfaces.tobago.internal.util.RenderUtils;
27  import org.apache.myfaces.tobago.model.CollapseMode;
28  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
35  import java.io.IOException;
36  
37  public class SectionRenderer<T extends AbstractUISection> extends CollapsiblePanelRendererBase<T> {
38  
39    @Override
40    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
41  
42      final TobagoResponseWriter writer = getResponseWriter(facesContext);
43      final String clientId = component.getClientId(facesContext);
44      final boolean collapsed = component.isCollapsed();
45      final Markup markup = component.getMarkup();
46  
47      writer.startElement(HtmlElements.TOBAGO_SECTION);
48      writer.writeIdAttribute(clientId);
49      writer.writeClassAttribute(
50          null,
51          TobagoClass.SECTION.createMarkup(markup),
52          collapsed ? TobagoClass.COLLAPSED : null,
53          component.getCustomClass());
54      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
55  
56      final HtmlElements tag;
57      switch (component.getLevel()) {
58        case 1:
59          tag = HtmlElements.H1;
60          break;
61        case 2:
62          tag = HtmlElements.H2;
63          break;
64        case 3:
65          tag = HtmlElements.H3;
66          break;
67        case 4:
68          tag = HtmlElements.H4;
69          break;
70        case 5:
71          tag = HtmlElements.H5;
72          break;
73        default:
74          tag = HtmlElements.H6;
75      }
76  
77      if (component.getCollapsedMode() != CollapseMode.none) {
78        encodeHidden(writer, clientId, collapsed);
79      }
80  
81      writer.startElement(HtmlElements.DIV);
82      writer.startElement(tag);
83  
84      final String image = component.getImage();
85      HtmlRendererUtils.encodeIconOrImage(writer, image);
86  
87      final UIComponent labelFacet = ComponentUtils.getFacet(component, Facets.label);
88      final String labelString = component.getLabel();
89      if (labelFacet != null) {
90        for (final UIComponent child : RenderUtils.getFacetChildren(labelFacet)) {
91          child.encodeAll(facesContext);
92        }
93      } else if (labelString != null) {
94        writer.startElement(HtmlElements.SPAN);
95        writer.writeText(labelString);
96        writer.endElement(HtmlElements.SPAN);
97      }
98      writer.endElement(tag);
99  
100     final UIComponent bar = ComponentUtils.getFacet(component, Facets.bar);
101     if (bar != null) {
102       bar.encodeAll(facesContext);
103     }
104 
105     writer.endElement(HtmlElements.DIV);
106 
107     writer.startElement(HtmlElements.DIV);
108     writer.writeClassAttribute(TobagoClass.SECTION__CONTENT);
109   }
110 
111   @Override
112   public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
113 
114     final TobagoResponseWriter writer = getResponseWriter(facesContext);
115     writer.endElement(HtmlElements.DIV);
116     writer.endElement(HtmlElements.TOBAGO_SECTION);
117   }
118 }