View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.legend;
23  
24  import java.io.IOException;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.primefaces.extensions.util.Attrs;
33  import org.primefaces.renderkit.CoreRenderer;
34  import org.primefaces.util.FacetUtils;
35  import org.primefaces.util.HTML;
36  import org.primefaces.util.WidgetBuilder;
37  
38  /**
39   * Renderer for the {@link Legend} component.
40   *
41   * @author Melloware mellowaredev@gmail.com
42   * @since 7.1
43   */
44  public class LegendRenderer extends CoreRenderer {
45  
46      @Override
47      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
48          final Legend legend = (Legend) component;
49          encodeMarkup(context, legend);
50          encodeScript(context, legend);
51      }
52  
53      private void encodeMarkup(FacesContext context, Legend legend) throws IOException {
54          final ResponseWriter writer = context.getResponseWriter();
55          final String clientId = legend.getClientId(context);
56          final String widgetVar = legend.resolveWidgetVar();
57          final String styleClass = getStyleClassBuilder(context)
58                      .add(legend.getLayout().equalsIgnoreCase("vertical")
59                                  ? Legend.STYLE_CLASS_VERTICAL
60                                  : Legend.STYLE_CLASS_HORIZONTAL)
61                      .add(legend.getStyleClass())
62                      .build();
63  
64          writer.startElement("div", legend);
65          writer.writeAttribute("id", clientId, "id");
66          writer.writeAttribute(HTML.WIDGET_VAR, widgetVar, null);
67          writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
68          if (legend.getStyle() != null) {
69              writer.writeAttribute(Attrs.STYLE, legend.getStyle(), Attrs.STYLE);
70          }
71  
72          // title
73          encodeTitle(context, legend);
74  
75          // items
76          encodeItems(context, legend);
77  
78          // footer
79          encodeFooter(context, legend);
80  
81          writer.endElement("div");
82      }
83  
84      private void encodeItems(FacesContext context, Legend legend) throws IOException {
85          final ResponseWriter writer = context.getResponseWriter();
86          // scales
87          writer.startElement("div", null);
88          writer.writeAttribute(Attrs.CLASS, Legend.SCALE_STYLE, null);
89          writer.startElement("ul", null);
90          writer.writeAttribute(Attrs.CLASS, Legend.LABELS_STYLE, null);
91  
92          // Key=text, Value=Color
93          final Map<String, String> values = legend.getValues();
94          for (final Entry<String, String> item : values.entrySet()) {
95              writer.startElement("li", null);
96              writer.startElement("span", null);
97              writer.writeAttribute(Attrs.STYLE, "background:" + item.getValue(), null);
98              writer.endElement("span");
99              writer.writeText(item.getKey(), null);
100             writer.endElement("li");
101         }
102         writer.endElement("ul");
103         writer.endElement("div");
104     }
105 
106     private void encodeTitle(FacesContext context, Legend legend) throws IOException {
107         final ResponseWriter writer = context.getResponseWriter();
108         final UIComponent facet = legend.getFacet("title");
109         final String title = legend.getTitle();
110         writer.startElement("div", null);
111         writer.writeAttribute(Attrs.CLASS, Legend.TITLE_STYLE, null);
112         if (FacetUtils.shouldRenderFacet(facet)) {
113             facet.encodeAll(context);
114         }
115         else if (title != null) {
116             writer.writeText(title, null);
117         }
118         else {
119             writer.write("&nbsp;");
120         }
121         writer.endElement("div");
122     }
123 
124     private void encodeFooter(FacesContext context, Legend legend) throws IOException {
125         final ResponseWriter writer = context.getResponseWriter();
126         final UIComponent facet = legend.getFacet("footer");
127         final String footer = legend.getFooter();
128         writer.startElement("div", null);
129         writer.writeAttribute(Attrs.CLASS, Legend.FOOTER_STYLE, null);
130         if (FacetUtils.shouldRenderFacet(facet)) {
131             facet.encodeAll(context);
132         }
133         else if (footer != null) {
134             writer.writeText(footer, null);
135         }
136         else {
137             writer.write("&nbsp;");
138         }
139         writer.endElement("div");
140     }
141 
142     private void encodeScript(FacesContext context, Legend legend) throws IOException {
143         final WidgetBuilder wb = getWidgetBuilder(context);
144         wb.init("ExtLegend", legend);
145         wb.attr("layout", legend.getLayout());
146         wb.finish();
147     }
148 
149 }