comparison flys-client/src/main/java/org/dive4elements/river/client/client/utils/DoubleValidator.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/utils/DoubleValidator.java@24e9b5eb83f8
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.utils;
2
3 import java.util.Map;
4
5 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.i18n.client.NumberFormat;
7
8 import com.smartgwt.client.widgets.form.fields.FormItem;
9
10 import de.intevation.flys.client.client.FLYSConstants;
11
12 /**
13 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
14 */
15 public class DoubleValidator implements Validator {
16
17 /** The interface that provides i18n messages. */
18 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
19
20
21 /** Statically determine doubility of String value. */
22 public static boolean isDouble(Object obj) {
23 if (obj == null) {
24 return false;
25 }
26
27 boolean valid = true;
28 String v = obj.toString();
29
30 NumberFormat f = NumberFormat.getDecimalFormat();
31
32 try {
33 if (v == null) {
34 throw new NumberFormatException("empty");
35 }
36
37 double value = f.parse(v);
38 }
39 catch (NumberFormatException nfe) {
40 valid = false;
41 }
42 return valid;
43
44 }
45
46
47 /**
48 * @return true if items value can be converted to double, if false,
49 * expect error message in \param errors map.
50 */
51 public boolean validate(FormItem item, Map errors) {
52 boolean valid = true;
53
54 if(item.getValue() == null) {
55 return false;
56 }
57 String v = item.getValue().toString();
58
59 NumberFormat f = NumberFormat.getDecimalFormat();
60
61 try {
62 if (v == null) {
63 throw new NumberFormatException("empty");
64 }
65
66 double value = f.parse(v);
67
68 errors.remove(item.getFieldName());
69 }
70 catch (NumberFormatException nfe) {
71 errors.put(item.getFieldName(), MSG.wrongFormat());
72
73 item.focusInItem();
74
75 valid = false;
76 }
77 return valid;
78 }
79 }
80 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
81

http://dive4elements.wald.intevation.org