Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java @ 1500:2a8b5dcbe8ca
Issue 358.
Validate integer and double values in chart properties editor.
flys-client/trunk@3625 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 09 Jan 2012 09:17:52 +0000 |
parents | |
children | 02a9104c0451 |
comparison
equal
deleted
inserted
replaced
1499:0372797fc9cf | 1500:2a8b5dcbe8ca |
---|---|
1 package de.intevation.flys.client.client.utils; | |
2 import com.google.gwt.core.client.GWT; | |
3 import com.smartgwt.client.widgets.form.DynamicForm; | |
4 import com.smartgwt.client.widgets.form.fields.FormItem; | |
5 import com.google.gwt.i18n.client.NumberFormat; | |
6 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; | |
7 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; | |
8 import java.util.Map; | |
9 import de.intevation.flys.client.client.FLYSConstants; | |
10 | |
11 /** | |
12 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
13 * | |
14 * This validator is used for SmartGWT FormItems. | |
15 * | |
16 * To use this validator an attribute named 'internalType' has to be set. | |
17 * Values for 'internalType': | |
18 * 'double' | |
19 * 'integer' | |
20 */ | |
21 public class Validator implements ChangedHandler { | |
22 | |
23 /** The interface that provides i18n messages. */ | |
24 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
25 | |
26 | |
27 /** | |
28 * | |
29 */ | |
30 public void onChanged(ChangedEvent e) { | |
31 FormItem item = e.getItem(); | |
32 DynamicForm form = e.getForm(); | |
33 Map errors = form.getErrors(); | |
34 String type = item.getAttribute("internalType"); | |
35 | |
36 if(type.equals("double")) { | |
37 validateFloat(item, errors); | |
38 form.setErrors(errors, true); | |
39 } | |
40 else if(type.equals("integer")) { | |
41 validateInteger(item, errors); | |
42 form.setErrors(errors, true); | |
43 } | |
44 else { | |
45 GWT.log("No Attribute 'internalType' set. Not using any validator."); | |
46 } | |
47 } | |
48 | |
49 | |
50 /** | |
51 * | |
52 */ | |
53 protected boolean validateInteger(FormItem item, Map errors) { | |
54 boolean valid = true; | |
55 | |
56 String v = (String) item.getValue(); | |
57 | |
58 NumberFormat f = NumberFormat.getDecimalFormat(); | |
59 | |
60 try { | |
61 if (v == null) { | |
62 throw new NumberFormatException("empty"); | |
63 } | |
64 | |
65 int value = Integer.parseInt(v); | |
66 | |
67 errors.remove(item.getFieldName()); | |
68 } | |
69 catch (NumberFormatException nfe) { | |
70 errors.put(item.getFieldName(), MSG.wrongFormat()); | |
71 | |
72 item.focusInItem(); | |
73 | |
74 valid = false; | |
75 } | |
76 return valid; | |
77 } | |
78 | |
79 | |
80 /** | |
81 * | |
82 */ | |
83 protected boolean validateFloat(FormItem item, Map errors) { | |
84 boolean valid = true; | |
85 | |
86 String v = (String) item.getValue(); | |
87 | |
88 NumberFormat f = NumberFormat.getDecimalFormat(); | |
89 | |
90 try { | |
91 if (v == null) { | |
92 throw new NumberFormatException("empty"); | |
93 } | |
94 | |
95 double value = f.parse(v); | |
96 | |
97 errors.remove(item.getFieldName()); | |
98 } | |
99 catch (NumberFormatException nfe) { | |
100 errors.put(item.getFieldName(), MSG.wrongFormat()); | |
101 | |
102 item.focusInItem(); | |
103 | |
104 valid = false; | |
105 } | |
106 return valid; | |
107 } | |
108 } | |
109 |