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.codescanner;
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.component.api.InputHolder;
31  import org.primefaces.expression.SearchExpressionUtils;
32  import org.primefaces.extensions.util.Attrs;
33  import org.primefaces.renderkit.CoreRenderer;
34  import org.primefaces.util.WidgetBuilder;
35  
36  /**
37   * Renderer for the {@link CodeScanner} component.
38   *
39   * @author Jasper de Vries <jepsar@gmail.com>
40   * @since 10.0
41   */
42  public class CodeScannerRenderer extends CoreRenderer {
43  
44      @Override
45      public void decode(final FacesContext context, final UIComponent component) {
46          decodeBehaviors(context, component);
47      }
48  
49      @Override
50      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
51          final CodeScanner codeScanner = (CodeScanner) component;
52          encodeMarkup(context, codeScanner);
53          encodeScript(context, codeScanner);
54      }
55  
56      protected void encodeMarkup(final FacesContext context, final CodeScanner codeScanner) throws IOException {
57          final ResponseWriter writer = context.getResponseWriter();
58          final String clientId = codeScanner.getClientId(context);
59          final String styleClass = getStyleClassBuilder(context)
60                      .add(CodeScanner.STYLE_CLASS)
61                      .add(codeScanner.getStyleClass())
62                      .build();
63          writer.startElement("span", codeScanner);
64          writer.writeAttribute("id", clientId, null);
65          writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
66  
67          if (codeScanner.getStyle() != null) {
68              writer.writeAttribute(Attrs.STYLE, codeScanner.getStyle(), Attrs.STYLE);
69          }
70  
71          if (codeScanner.isVideo()) {
72              encodeVideo(context, codeScanner);
73          }
74  
75          writer.endElement("span");
76      }
77  
78      protected void encodeVideo(final FacesContext context, final CodeScanner codeScanner) throws IOException {
79          final ResponseWriter writer = context.getResponseWriter();
80          writer.startElement("video", null);
81          if (codeScanner.getWidth() != null) {
82              writer.writeAttribute("width", codeScanner.getWidth(), null);
83          }
84          if (codeScanner.getHeight() != null) {
85              writer.writeAttribute("height", codeScanner.getHeight(), null);
86          }
87          writer.endElement("video");
88      }
89  
90      protected void encodeScript(final FacesContext context, final CodeScanner codeScanner) throws IOException {
91          final WidgetBuilder wb = getWidgetBuilder(context);
92          wb.init("ExtCodeScanner", codeScanner)
93                      .attr("type", codeScanner.getTypeEnum().name())
94                      .attr("autoStart", codeScanner.isAutoStart());
95          if (codeScanner.getDeviceId() != null) {
96              wb.attr("deviceId", codeScanner.getDeviceId());
97          }
98          if (codeScanner.getFor() != null) {
99              String forInputClientId = getForInputClientId(context, codeScanner);
100             if (forInputClientId != null) {
101                 wb.attr("forInput", forInputClientId);
102             }
103         }
104         if (codeScanner.getOnsuccess() != null) {
105             wb.callback("onsuccess", "function()", codeScanner.getOnsuccess());
106         }
107         if (codeScanner.getOnerror() != null) {
108             wb.callback("onerror", "function()", codeScanner.getOnerror());
109         }
110 
111         encodeClientBehaviors(context, codeScanner);
112 
113         wb.finish();
114     }
115 
116     protected String getForInputClientId(final FacesContext context, final CodeScanner codeScanner) {
117         UIComponent forComponent = SearchExpressionUtils.resolveComponent(codeScanner.getFor(), codeScanner);
118         if (forComponent == null) {
119             return null;
120         }
121         if (forComponent instanceof InputHolder) {
122             return ((InputHolder) forComponent).getInputClientId();
123         }
124         return forComponent.getClientId(context);
125     }
126 
127 }