1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.component.calculator;
23
24 import java.math.BigDecimal;
25 import java.util.*;
26
27 import javax.faces.application.ResourceDependency;
28 import javax.faces.component.UIComponentBase;
29 import javax.faces.component.behavior.ClientBehaviorHolder;
30 import javax.faces.context.FacesContext;
31 import javax.faces.event.AjaxBehaviorEvent;
32 import javax.faces.event.FacesEvent;
33
34 import org.primefaces.component.api.RTLAware;
35 import org.primefaces.component.api.Widget;
36 import org.primefaces.extensions.event.ButtonEvent;
37 import org.primefaces.extensions.event.CloseEvent;
38 import org.primefaces.extensions.event.OpenEvent;
39 import org.primefaces.util.Constants;
40 import org.primefaces.util.LocaleUtils;
41
42
43
44
45
46
47
48 @ResourceDependency(library = "primefaces", name = "components.css")
49 @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
50 @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
51 @ResourceDependency(library = "primefaces", name = "core.js")
52 @ResourceDependency(library = "primefaces-extensions", name = "calculator/calculator.css")
53 @ResourceDependency(library = "primefaces-extensions", name = "calculator/calculator.js")
54 public class Calculator extends UIComponentBase implements ClientBehaviorHolder, Widget, RTLAware {
55
56 public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.Calculator";
57 public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
58 private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.CalculatorRenderer";
59
60 private static final Collection<String> EVENT_NAMES = Collections
61 .unmodifiableCollection(Arrays.asList(OpenEvent.NAME, CloseEvent.NAME, ButtonEvent.NAME));
62
63 private Locale appropriateLocale;
64
65 @SuppressWarnings("java:S115")
66 protected enum PropertyKeys {
67
68
69 widgetVar,
70 showOn,
71 layout,
72 locale,
73 precision,
74 dir,
75 styleClass,
76 onopen,
77 onbutton,
78 onclose,
79 forValue("for");
80
81
82 private final String toString;
83
84 PropertyKeys(String toString) {
85 this.toString = toString;
86 }
87
88 PropertyKeys() {
89 toString = null;
90 }
91
92 @Override
93 public String toString() {
94 return ((toString != null) ? toString : super.toString());
95 }
96 }
97
98
99
100
101 public Calculator() {
102 setRendererType(DEFAULT_RENDERER);
103 }
104
105
106
107
108 @Override
109 public String getFamily() {
110 return COMPONENT_FAMILY;
111 }
112
113
114
115
116 @Override
117 public Collection<String> getEventNames() {
118 return EVENT_NAMES;
119 }
120
121
122
123
124 @Override
125 public String getDefaultEventName() {
126 return OpenEvent.NAME;
127 }
128
129
130
131
132 @Override
133 public void processDecodes(final FacesContext fc) {
134 if (isSelfRequest(fc)) {
135 decode(fc);
136 }
137 else {
138 super.processDecodes(fc);
139 }
140 }
141
142
143
144
145 @Override
146 public void processValidators(final FacesContext fc) {
147 if (!isSelfRequest(fc)) {
148 super.processValidators(fc);
149 }
150 }
151
152
153
154
155 @Override
156 public void processUpdates(final FacesContext fc) {
157 if (!isSelfRequest(fc)) {
158 super.processUpdates(fc);
159 }
160 }
161
162
163
164
165 @Override
166 public void queueEvent(final FacesEvent event) {
167 final FacesContext fc = FacesContext.getCurrentInstance();
168
169 if (isSelfRequest(fc) && event instanceof AjaxBehaviorEvent) {
170 final Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
171 final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
172 final String clientId = getClientId(fc);
173 final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
174
175 if (OpenEvent.NAME.equals(eventName)) {
176 final OpenEvent openEvent = new OpenEvent(this, behaviorEvent.getBehavior());
177 openEvent.setPhaseId(event.getPhaseId());
178 super.queueEvent(openEvent);
179
180 return;
181 }
182 else if (CloseEvent.NAME.equals(eventName)) {
183 final CloseEvent closeEvent = new CloseEvent(this, behaviorEvent.getBehavior());
184 closeEvent.setPhaseId(event.getPhaseId());
185 super.queueEvent(closeEvent);
186
187 return;
188 }
189 else if (ButtonEvent.NAME.equals(eventName)) {
190 final String name = params.get(clientId + "_button");
191 final BigDecimal value = new BigDecimal(params.get(clientId + "_value"));
192 final ButtonEvent buttonEvent = new ButtonEvent(this, behaviorEvent.getBehavior(), name, value);
193 buttonEvent.setPhaseId(event.getPhaseId());
194 super.queueEvent(buttonEvent);
195
196 return;
197 }
198 }
199
200 super.queueEvent(event);
201 }
202
203 public Locale calculateLocale() {
204 if (appropriateLocale == null) {
205 final FacesContext fc = FacesContext.getCurrentInstance();
206 appropriateLocale = LocaleUtils.resolveLocale(fc, getLocale(), getClientId(fc));
207 }
208 return appropriateLocale;
209 }
210
211 public String getWidgetVar() {
212 return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
213 }
214
215 public void setWidgetVar(final String _widgetVar) {
216 getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
217 }
218
219 public String getFor() {
220 return (String) getStateHelper().eval(PropertyKeys.forValue, null);
221 }
222
223 public void setFor(final String _for) {
224 getStateHelper().put(PropertyKeys.forValue, _for);
225 }
226
227 public String getShowOn() {
228 return (String) getStateHelper().eval(PropertyKeys.showOn, "focus");
229 }
230
231 public void setShowOn(final String _showOn) {
232 getStateHelper().put(PropertyKeys.showOn, _showOn);
233 }
234
235 public String getLayout() {
236 return (String) getStateHelper().eval(PropertyKeys.layout, "standard");
237 }
238
239 public void setLayout(final String _layout) {
240 getStateHelper().put(PropertyKeys.layout, _layout);
241 }
242
243 public Object getLocale() {
244 return getStateHelper().eval(PropertyKeys.locale, null);
245 }
246
247 public void setLocale(final Object locale) {
248 getStateHelper().put(PropertyKeys.locale, locale);
249 }
250
251 public int getPrecision() {
252 return (Integer) getStateHelper().eval(PropertyKeys.precision, 10);
253 }
254
255 public void setPrecision(final int _precision) {
256 getStateHelper().put(PropertyKeys.precision, _precision);
257 }
258
259 public void setDir(final String _dir) {
260 getStateHelper().put(PropertyKeys.dir, _dir);
261 }
262
263 @Override
264 public String getDir() {
265 return (String) getStateHelper().eval(PropertyKeys.dir, "ltr");
266 }
267
268 public String getStyleClass() {
269 return (String) getStateHelper().eval(PropertyKeys.styleClass, null);
270 }
271
272 public void setStyleClass(final String _styleClass) {
273 getStateHelper().put(PropertyKeys.styleClass, _styleClass);
274 }
275
276 public String getOnopen() {
277 return (String) getStateHelper().eval(PropertyKeys.onopen, null);
278 }
279
280 public void setOnopen(final String _onOpen) {
281 getStateHelper().put(PropertyKeys.onopen, _onOpen);
282 }
283
284 public String getOnclose() {
285 return (String) getStateHelper().eval(PropertyKeys.onclose, null);
286 }
287
288 public void setOnclose(final String _onClose) {
289 getStateHelper().put(PropertyKeys.onclose, _onClose);
290 }
291
292 public String getOnbutton() {
293 return (String) getStateHelper().eval(PropertyKeys.onbutton, null);
294 }
295
296 public void setOnbutton(final String _onButton) {
297 getStateHelper().put(PropertyKeys.onbutton, _onButton);
298 }
299
300 private boolean isSelfRequest(final FacesContext context) {
301 return getClientId(context)
302 .equals(context.getExternalContext().getRequestParameterMap().get(
303 Constants.RequestParams.PARTIAL_SOURCE_PARAM));
304 }
305
306 @Override
307 public Object saveState(FacesContext context) {
308
309 appropriateLocale = null;
310
311 return super.saveState(context);
312 }
313 }