1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
40
41
42
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
73 encodeTitle(context, legend);
74
75
76 encodeItems(context, legend);
77
78
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
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
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(" ");
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(" ");
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 }