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.counter;
23  
24  import java.io.IOException;
25  import java.text.DecimalFormat;
26  import java.util.Locale;
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.Constants;
35  import org.primefaces.util.LangUtils;
36  import org.primefaces.util.WidgetBuilder;
37  
38  /**
39   * <code>Counter</code> component.
40   *
41   * @author https://github.com/aripddev
42   * @since 8.0.1
43   */
44  public class CounterRenderer extends CoreRenderer {
45  
46      @Override
47      public void decode(final FacesContext context, final UIComponent component) {
48          decodeBehaviors(context, component);
49      }
50  
51      @Override
52      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
53          final Counter counter = (Counter) component;
54  
55          encodeMarkup(context, counter);
56          encodeScript(context, counter);
57      }
58  
59      private void encodeScript(final FacesContext context, final Counter counter) throws IOException {
60          final WidgetBuilder wb = getWidgetBuilder(context);
61  
62          final Locale locale = counter.calculateLocale();
63          final DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);
64  
65          String groupingSeparator = counter.getSeparator();
66          String decimalSeparator = counter.getDecimal();
67          if (LangUtils.isBlank(groupingSeparator)) {
68              groupingSeparator = String.valueOf(formatter.getDecimalFormatSymbols().getGroupingSeparator());
69          }
70          if (LangUtils.isBlank(decimalSeparator)) {
71              decimalSeparator = String.valueOf(formatter.getDecimalFormatSymbols().getDecimalSeparator());
72          }
73  
74          wb.init("ExtCounter", counter)
75                      .attr("start", counter.getStart())
76                      .attr("end", counter.getEnd())
77                      .attr("decimals", counter.getDecimals())
78                      .attr("duration", counter.getDuration())
79                      .attr("useGrouping", counter.isUseGrouping())
80                      .attr("useEasing", counter.isUseEasing())
81                      .attr("smartEasingThreshold", counter.getSmartEasingThreshold())
82                      .attr("smartEasingAmount", counter.getSmartEasingAmount())
83                      .attr("separator", groupingSeparator)
84                      .attr("decimal", decimalSeparator)
85                      .attr("prefix", counter.getPrefix())
86                      .attr("suffix", counter.getSuffix())
87                      .attr("autoStart", counter.isAutoStart());
88  
89          if (!LangUtils.isBlank(counter.getOnstart())) {
90              wb.callback("onstart", "function()", counter.getOnstart());
91          }
92          if (!LangUtils.isBlank(counter.getOnend())) {
93              wb.callback("onend", "function()", counter.getOnend());
94          }
95  
96          encodeClientBehaviors(context, counter);
97  
98          wb.finish();
99      }
100 
101     private void encodeMarkup(final FacesContext context, final Counter counter) throws IOException {
102         final ResponseWriter writer = context.getResponseWriter();
103         final String styleClass = getStyleClassBuilder(context)
104                     .add(Counter.STYLE_CLASS)
105                     .add(counter.getStyleClass())
106                     .build();
107 
108         writer.startElement("span", counter);
109         writer.writeAttribute("id", counter.getClientId(context), "id");
110         writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
111         writer.writeAttribute(Attrs.STYLE,
112                     (!counter.isVisible() ? "display:none;" : Constants.EMPTY_STRING) + counter.getStyle(),
113                     Attrs.STYLE);
114         writer.endElement("span");
115     }
116 
117 }