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.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  }