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.gchart;
23
24 import java.io.IOException;
25
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29
30 import org.primefaces.extensions.component.gchart.model.GChartModel;
31 import org.primefaces.renderkit.CoreRenderer;
32 import org.primefaces.util.LangUtils;
33 import org.primefaces.util.WidgetBuilder;
34
35 public class GChartRenderer extends CoreRenderer {
36
37 @Override
38 public void decode(final FacesContext context, final UIComponent component) {
39 super.decode(context, component);
40 decodeBehaviors(context, component);
41 }
42
43 @Override
44 public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
45 final GChart gChart = (GChart) component;
46
47 encodeMarkup(context, gChart);
48 encodeScript(context, gChart);
49 }
50
51 protected void encodeMarkup(final FacesContext context, final GChart chart) throws IOException {
52 final ResponseWriter writer = context.getResponseWriter();
53
54 renderHiddenInput(context, chart.getClientId() + "_hidden", null, false);
55
56 writer.startElement("div", chart);
57 writer.writeAttribute("id", chart.getClientId(), null);
58 writer.endElement("div");
59 }
60
61 protected void encodeScript(final FacesContext context, final GChart chart) throws IOException {
62 String apiKey = chart.getApiKey();
63 if (LangUtils.isBlank(apiKey)) {
64 apiKey = getApiKey(context);
65 }
66
67 final WidgetBuilder wb = getWidgetBuilder(context);
68 wb.init("ExtGChart", chart)
69 .attr("chart", ((GChartModel) chart.getValue()).toJson())
70 .attr("title", chart.getTitle())
71 .attr("apiKey", apiKey)
72 .attr("language", chart.getLanguage())
73 .attr("width", chart.getWidth())
74 .attr("height", chart.getHeight());
75 if (chart.getExtender() != null) {
76 wb.nativeAttr("extender", chart.getExtender());
77 }
78 encodeClientBehaviors(context, chart);
79
80 wb.finish();
81 }
82
83 protected String getApiKey(final FacesContext context) {
84 String key;
85 try {
86 final String initParam = context.getExternalContext().getInitParameter(GChart.API_KEY);
87 key = context.getApplication().evaluateExpressionGet(context, initParam, String.class);
88 }
89 catch (final Exception e) {
90 key = null;
91 }
92 return key;
93 }
94 }