comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleRangePanel.java @ 7745:1d6b957d8953

issue1549: Add live-validation to wsplgen to-field. Unfortunately, controlling the displayed message proved difficult due to mixed error/validation-handling code. It shows wrong format message for now.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 11 Feb 2014 12:37:16 +0100
parents ea9eef426962
children 316a9eeb0836
comparison
equal deleted inserted replaced
7744:8abc86160959 7745:1d6b957d8953
42 public static final String FIELD_TO = "to"; 42 public static final String FIELD_TO = "to";
43 43
44 /** The constant name of the input field to enter the step width of a 44 /** The constant name of the input field to enter the step width of a
45 * distance.*/ 45 * distance.*/
46 public static final String FIELD_WIDTH = "step"; 46 public static final String FIELD_WIDTH = "step";
47
48 /** Whether or not negative 'to' values are allowed. */
49 protected boolean negativeToAllowed = true;
47 50
48 /** The textboxes */ 51 /** The textboxes */
49 protected FloatItem fromItem; 52 protected FloatItem fromItem;
50 protected FloatItem toItem; 53 protected FloatItem toItem;
51 protected FloatItem stepItem; 54 protected FloatItem stepItem;
151 fromItem.setValue(f.format(from)); 154 fromItem.setValue(f.format(from));
152 toItem.setValue(f.format(to)); 155 toItem.setValue(f.format(to));
153 stepItem.setValue(f.format(steps)); 156 stepItem.setValue(f.format(steps));
154 } 157 }
155 158
159 public void setNegativeToAllowed(boolean isAllowed) {
160 negativeToAllowed = isAllowed;
161 }
162
156 163
157 public boolean validateForm() { 164 public boolean validateForm() {
158 try { 165 try {
159 return 166 return
160 validateForm(fromItem) && 167 validateForm(fromItem) &&
169 /** 176 /**
170 * This method validates the entered text in the input fields. If 177 * This method validates the entered text in the input fields. If
171 * there are values that doesn't represent a valid float, an error is 178 * there are values that doesn't represent a valid float, an error is
172 * displayed. 179 * displayed.
173 * 180 *
181 * Also if negativeToAllowed is false, an error is registered if
182 * the 'to' field contains a negative value.
183 *
174 * @param item The FormItem. 184 * @param item The FormItem.
175 */ 185 */
176 @SuppressWarnings("unchecked") 186 @SuppressWarnings("unchecked")
177 protected boolean validateForm(FormItem item) { 187 protected boolean validateForm(FormItem item) {
178 if (item instanceof StaticTextItem) { 188 if (item instanceof StaticTextItem) {
185 195
186 NumberFormat f = NumberFormat.getDecimalFormat(); 196 NumberFormat f = NumberFormat.getDecimalFormat();
187 @SuppressWarnings("rawtypes") 197 @SuppressWarnings("rawtypes")
188 Map errors = getErrors(); 198 Map errors = getErrors();
189 199
200 Double d = 0d;
201
190 try { 202 try {
191 if (v == null) { 203 if (v == null) {
192 throw new NumberFormatException("empty"); 204 throw new NumberFormatException("empty");
193 } 205 }
194 206
195 f.parse(v); 207 d = f.parse(v);
196 208
197 errors.remove(item.getFieldName()); 209 errors.remove(item.getFieldName());
198 } 210 }
199 catch (NumberFormatException nfe) { 211 catch (NumberFormatException nfe) {
200 errors.put(item.getFieldName(), MESSAGES.wrongFormat()); 212 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
202 item.focusInItem(); 214 item.focusInItem();
203 215
204 valid = false; 216 valid = false;
205 } 217 }
206 218
219 if (negativeToAllowed == false
220 && item.getFieldName().equals(FIELD_TO) && d < 0d) {
221 errors.put(item.getFieldName(), MESSAGES.toShouldNotBeNegative());
222
223 item.setValue("");
224
225 item.focusInItem();
226
227 valid = false;
228 }
229
207 setErrors(errors, true); 230 setErrors(errors, true);
208 231
209 return valid; 232 return valid;
210 } 233 }
211 234

http://dive4elements.wald.intevation.org