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.internal.component.AbstractUIFigure;
24  import org.apache.myfaces.tobago.renderkit.RendererBase;
25  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
26  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
31  
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  import java.io.IOException;
35  
36  public class FigureRenderer<T extends AbstractUIFigure> extends RendererBase<T> {
37  
38    @Override
39    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
40      final TobagoResponseWriter writer = getResponseWriter(facesContext);
41      writer.startElement(HtmlElements.FIGURE);
42  
43      writer.writeClassAttribute(
44          TobagoClass.FIGURE,
45          TobagoClass.FIGURE.createMarkup(component.getMarkup()),
46          BootstrapClass.FIGURE,
47          component.getCustomClass());
48      final String tip = component.getTip();
49      if (tip != null) {
50        writer.writeAttribute(HtmlAttributes.TITLE, tip, true);
51      }
52  
53      writer.startElement(HtmlElements.DIV);
54      writer.writeClassAttribute(BootstrapClass.FIGURE_IMG);
55    }
56  
57    @Override
58    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
59      final UIComponent label = ComponentUtils.getFacet(component, Facets.label);
60      final String labelString = component.getLabel();
61  
62      final TobagoResponseWriter writer = getResponseWriter(facesContext);
63  
64      writer.endElement(HtmlElements.DIV);
65  
66      if (labelString != null || label != null) {
67        writer.startElement(HtmlElements.FIGCAPTION);
68        writer.writeClassAttribute(BootstrapClass.FIGURE_CAPTION);
69        if (labelString != null) {
70          writer.writeText(labelString);
71        }
72        if (label != null) {
73          label.encodeAll(facesContext);
74        }
75        writer.endElement(HtmlElements.FIGCAPTION);
76      }
77      writer.endElement(HtmlElements.FIGURE);
78    }
79  }