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.calculator;
23  
24  import java.io.IOException;
25  
26  import javax.faces.FacesException;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIInput;
29  import javax.faces.context.FacesContext;
30  
31  import org.primefaces.expression.SearchExpressionUtils;
32  import org.primefaces.extensions.util.ExtLangUtils;
33  import org.primefaces.renderkit.CoreRenderer;
34  import org.primefaces.util.ComponentUtils;
35  import org.primefaces.util.WidgetBuilder;
36  
37  /**
38   * Renderer for the {@link Calculator} component.
39   *
40   * @author Melloware mellowaredev@gmail.com
41   * @since 6.1
42   */
43  public class CalculatorRenderer extends CoreRenderer {
44  
45      @Override
46      public void decode(final FacesContext context, final UIComponent component) {
47          decodeBehaviors(context, component);
48      }
49  
50      @Override
51      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
52          final Calculator calculator = (Calculator) component;
53          encodeScript(context, calculator);
54      }
55  
56      private void encodeScript(final FacesContext context, final Calculator calculator) throws IOException {
57          String target = SearchExpressionUtils.resolveClientIdsForClientSide(context, calculator, calculator.getFor());
58          if (isValueBlank(target)) {
59              target = calculator.getParent().getClientId(context);
60          }
61  
62          final UIComponent targetComponent = SearchExpressionUtils.resolveComponent(target, calculator);
63          if (!(targetComponent instanceof UIInput)) {
64              throw new FacesException("Calculator must use for=\"target\" or be nested inside an input!");
65          }
66  
67          final WidgetBuilder wb = getWidgetBuilder(context);
68          wb.init("ExtCalculator", calculator);
69          wb.attr("target", target);
70          wb.attr("showOn", ExtLangUtils.lowerCase(calculator.getShowOn()));
71          wb.attr("layout", ExtLangUtils.lowerCase(calculator.getLayout()));
72          wb.attr("precision", calculator.getPrecision());
73          wb.attr("locale", calculator.calculateLocale().toString());
74          wb.attr("isRTL", ComponentUtils.isRTL(context, calculator));
75          wb.attr("calculatorClass", calculator.getStyleClass());
76  
77          if (calculator.getOnopen() != null) {
78              // Define a callback function before the panel is opened
79              wb.callback("onOpen", "function(value, inst)", calculator.getOnopen());
80          }
81          if (calculator.getOnclose() != null) {
82              // Define a callback function when the panel is closed
83              wb.callback("onClose", "function(value, inst)", calculator.getOnclose());
84          }
85          if (calculator.getOnbutton() != null) {
86              // Define a callback function when a button is activated
87              wb.callback("onButton", "function(label, value, inst)", calculator.getOnbutton());
88          }
89  
90          encodeClientBehaviors(context, calculator);
91  
92          wb.finish();
93      }
94  
95  }