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.speedtest;
23  
24  import java.io.IOException;
25  
26  import javax.faces.FacesException;
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  
31  import org.primefaces.extensions.util.Attrs;
32  import org.primefaces.renderkit.CoreRenderer;
33  import org.primefaces.util.ComponentTraversalUtils;
34  import org.primefaces.util.HTML;
35  import org.primefaces.util.WidgetBuilder;
36  
37  /**
38   * Renderer for the {@link Speedtest} component.
39   *
40   * @author ssibitz ssibitz@me.com
41   * @since 6.2
42   */
43  public class SpeedtestRenderer extends CoreRenderer {
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void decode(final FacesContext context, final UIComponent component) {
50          decodeBehaviors(context, component);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
58          final Speedtest speedtest = (Speedtest) component;
59          encodeMarkup(context, speedtest);
60          encodeScript(context, speedtest);
61      }
62  
63      /**
64       * Create the HTML markup for the DOM.
65       */
66      private void encodeMarkup(final FacesContext context, final Speedtest speedtest) throws IOException {
67          final ResponseWriter writer = context.getResponseWriter();
68          final String clientId = speedtest.getClientId(context);
69          final String widgetVar = speedtest.resolveWidgetVar();
70          // Generate Speedtest:
71          writer.startElement("div", speedtest);
72          writer.writeAttribute("id", clientId + "SpeedTest", "id");
73          if (speedtest.getStyleClass() != null) {
74              writer.writeAttribute(Attrs.CLASS, speedtest.getStyleClass(), "styleClass");
75          }
76          if (speedtest.getStyle() != null) {
77              writer.writeAttribute(Attrs.STYLE, speedtest.getStyle(), Attrs.STYLE);
78          }
79          writer.writeAttribute(HTML.WIDGET_VAR, widgetVar, null);
80          writer.startElement("div", speedtest);
81          writer.writeAttribute(Attrs.CLASS, "ui-g", null);
82          // Download Gauge
83          writer.startElement("div", speedtest);
84          writer.writeAttribute(Attrs.CLASS, "ui-g-3", null);
85          writer.writeAttribute("id", clientId + "ggdown", "id");
86          writer.endElement("div");
87          // Upload Gauge
88          writer.startElement("div", speedtest);
89          writer.writeAttribute(Attrs.CLASS, "ui-g-3", null);
90          writer.writeAttribute("id", clientId + "ggup", "id");
91          writer.endElement("div");
92          // Ping Gauge
93          writer.startElement("div", speedtest);
94          writer.writeAttribute(Attrs.CLASS, "ui-g-3", null);
95          writer.writeAttribute("id", clientId + "ggping", "id");
96          writer.endElement("div");
97          // Jitter Gauge
98          writer.startElement("div", speedtest);
99          writer.writeAttribute(Attrs.CLASS, "ui-g-3", null);
100         writer.writeAttribute("id", clientId + "ggjitter", "id");
101         writer.endElement("div");
102         // Row End
103         writer.endElement("div");
104         // Speedtest End
105         writer.endElement("div");
106     }
107 
108     /**
109      * Create the Javascript.
110      */
111     private void encodeScript(final FacesContext context, final Speedtest speedtest) throws IOException {
112         final String clientId = speedtest.getClientId(context);
113         final UIComponent form = ComponentTraversalUtils.closestForm(speedtest);
114         if (form == null) {
115             throw new FacesException("Speedtest:" + clientId + " needs to be enclosed in a form component");
116         }
117 
118         final WidgetBuilder wb = getWidgetBuilder(context);
119         wb.init("ExtSpeedtest", speedtest);
120         wb.attr("idDown", clientId + "ggdown");
121         wb.attr("idUp", clientId + "ggup");
122         wb.attr("idPing", clientId + "ggping");
123         wb.attr("idJitter", clientId + "ggjitter");
124         wb.attr("captionPing", speedtest.getCaptionPing());
125         wb.attr("captionJitter", speedtest.getCaptionJitter());
126         wb.attr("captionDownload", speedtest.getCaptionDownload());
127         wb.attr("captionUpload", speedtest.getCaptionUpload());
128         wb.attr("colorPing", speedtest.getColorPing());
129         wb.attr("colorJitter", speedtest.getColorJitter());
130         wb.attr("colorDownload", speedtest.getColorDownload());
131         wb.attr("colorUpload", speedtest.getColorUpload());
132         wb.attr("file", speedtest.getFile());
133         encodeClientBehaviors(context, speedtest);
134         wb.finish();
135     }
136 }