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.tobago.internal.renderkit.renderer;
20  
21  import org.apache.myfaces.tobago.component.Facets;
22  import org.apache.myfaces.tobago.context.Markup;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIBox;
24  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
25  import org.apache.myfaces.tobago.internal.util.RenderUtils;
26  import org.apache.myfaces.tobago.model.CollapseMode;
27  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
28  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.util.ComponentUtils;
32  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33  
34  import javax.faces.component.UIComponent;
35  import javax.faces.context.FacesContext;
36  import java.io.IOException;
37  
38  public class BoxRenderer<T extends AbstractUIBox> extends CollapsiblePanelRendererBase<T> {
39  
40    @Override
41    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
42  
43      final TobagoResponseWriter writer = getResponseWriter(facesContext);
44      final Markup markup = component.getMarkup();
45      final boolean collapsed = component.isCollapsed();
46  
47      writer.startElement(HtmlElements.TOBAGO_BOX);
48      writer.writeClassAttribute(
49          null,
50          TobagoClass.BOX.createMarkup(markup),
51          BootstrapClass.CARD,
52          collapsed ? TobagoClass.COLLAPSED : null,
53          component.getCustomClass(),
54          markup != null && markup.contains(Markup.SPREAD) ? TobagoClass.SPREAD : null);
55      final String clientId = component.getClientId(facesContext);
56      writer.writeIdAttribute(clientId);
57      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
58      if (title != null) {
59        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
60      }
61      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
62  
63      if (component.getCollapsedMode() != CollapseMode.none) {
64        encodeHidden(writer, clientId, collapsed);
65      }
66  
67      final UIComponent labelFacet = ComponentUtils.getFacet(component, Facets.label);
68      final String labelString = component.getLabel();
69      final UIComponent bar = ComponentUtils.getFacet(component, Facets.bar);
70      if (labelFacet != null || labelString != null || bar != null) {
71        writer.startElement(HtmlElements.DIV);
72        writer.writeClassAttribute(BootstrapClass.CARD_HEADER, TobagoClass.BOX__HEADER);
73  
74        writer.startElement(HtmlElements.H3);
75        if (labelFacet != null) {
76          for (final UIComponent child : RenderUtils.getFacetChildren(labelFacet)) {
77            child.encodeAll(facesContext);
78          }
79        } else if (labelString != null) {
80          writer.writeText(labelString);
81        }
82        writer.endElement(HtmlElements.H3);
83        if (bar != null) {
84          bar.encodeAll(facesContext);
85        }
86  
87        writer.endElement(HtmlElements.DIV);
88      }
89  
90      writer.startElement(HtmlElements.DIV);
91      writer.writeClassAttribute(BootstrapClass.CARD_BODY);
92    }
93  
94    @Override
95    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
96      final TobagoResponseWriter writer = getResponseWriter(facesContext);
97      writer.endElement(HtmlElements.DIV);
98      writer.endElement(HtmlElements.TOBAGO_BOX);
99    }
100 }