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.util.Collection;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import javax.faces.application.ResourceDependency;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.AjaxBehaviorEvent;
31  import javax.faces.event.BehaviorEvent;
32  import javax.faces.event.FacesEvent;
33  
34  import org.primefaces.event.SelectEvent;
35  import org.primefaces.util.Constants;
36  import org.primefaces.util.LocaleUtils;
37  import org.primefaces.util.MapBuilder;
38  
39  /**
40   * <code>Counter</code> component.
41   *
42   * @author https://github.com/aripddev
43   * @since 8.0.1
44   */
45  @ResourceDependency(library = "primefaces", name = "components.css")
46  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
47  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
48  @ResourceDependency(library = "primefaces", name = "core.js")
49  @ResourceDependency(library = "primefaces", name = "components.js")
50  @ResourceDependency(library = "primefaces-extensions", name = "counter/counter.js")
51  public class Counter extends CounterBase {
52  
53      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.Counter";
54  
55      public static final String STYLE_CLASS = "ui-counter";
56  
57      private static final String DEFAULT_EVENT = "end";
58  
59      private static final Map<String, Class<? extends BehaviorEvent>> BEHAVIOR_EVENT_MAPPING = MapBuilder.<String, Class<? extends BehaviorEvent>> builder()
60                  .put("start", null)
61                  .put(DEFAULT_EVENT, null)
62                  .build();
63  
64      private static final Collection<String> EVENT_NAMES = BEHAVIOR_EVENT_MAPPING.keySet();
65  
66      private Locale appropriateLocale;
67  
68      public Locale calculateLocale() {
69          if (appropriateLocale == null) {
70              final FacesContext fc = FacesContext.getCurrentInstance();
71              appropriateLocale = LocaleUtils.resolveLocale(fc, getLocale(), getClientId(fc));
72          }
73          return appropriateLocale;
74      }
75  
76      @Override
77      public Map<String, Class<? extends BehaviorEvent>> getBehaviorEventMapping() {
78          return BEHAVIOR_EVENT_MAPPING;
79      }
80  
81      @Override
82      public Collection<String> getEventNames() {
83          return EVENT_NAMES;
84      }
85  
86      @Override
87      public String getDefaultEventName() {
88          return DEFAULT_EVENT;
89      }
90  
91      @Override
92      public void queueEvent(final FacesEvent event) {
93          if (event instanceof AjaxBehaviorEvent) {
94              final FacesContext context = getFacesContext();
95              final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
96              final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
97              final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
98  
99              if ("start".equals(eventName) || DEFAULT_EVENT.equals(eventName)) {
100                 final Double value = Double.parseDouble(params.get(getClientId(context) + "_value"));
101                 final SelectEvent<Double> selectEvent = new SelectEvent<>(this, behaviorEvent.getBehavior(), value);
102                 selectEvent.setPhaseId(event.getPhaseId());
103                 super.queueEvent(selectEvent);
104             }
105             else {
106                 super.queueEvent(event);
107             }
108         }
109         else {
110             super.queueEvent(event);
111         }
112     }
113 
114     @Override
115     public Object saveState(FacesContext context) {
116         // reset component for MyFaces view pooling
117         appropriateLocale = null;
118 
119         return super.saveState(context);
120     }
121 }