Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java Mon Jan 09 09:17:52 2012 +0000 @@ -0,0 +1,109 @@ +package de.intevation.flys.client.client.utils; +import com.google.gwt.core.client.GWT; +import com.smartgwt.client.widgets.form.DynamicForm; +import com.smartgwt.client.widgets.form.fields.FormItem; +import com.google.gwt.i18n.client.NumberFormat; +import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; +import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; +import java.util.Map; +import de.intevation.flys.client.client.FLYSConstants; + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + * + * This validator is used for SmartGWT FormItems. + * + * To use this validator an attribute named 'internalType' has to be set. + * Values for 'internalType': + * 'double' + * 'integer' + */ +public class Validator implements ChangedHandler { + + /** The interface that provides i18n messages. */ + protected FLYSConstants MSG = GWT.create(FLYSConstants.class); + + + /** + * + */ + public void onChanged(ChangedEvent e) { + FormItem item = e.getItem(); + DynamicForm form = e.getForm(); + Map errors = form.getErrors(); + String type = item.getAttribute("internalType"); + + if(type.equals("double")) { + validateFloat(item, errors); + form.setErrors(errors, true); + } + else if(type.equals("integer")) { + validateInteger(item, errors); + form.setErrors(errors, true); + } + else { + GWT.log("No Attribute 'internalType' set. Not using any validator."); + } + } + + + /** + * + */ + protected boolean validateInteger(FormItem item, Map errors) { + boolean valid = true; + + String v = (String) item.getValue(); + + NumberFormat f = NumberFormat.getDecimalFormat(); + + try { + if (v == null) { + throw new NumberFormatException("empty"); + } + + int value = Integer.parseInt(v); + + errors.remove(item.getFieldName()); + } + catch (NumberFormatException nfe) { + errors.put(item.getFieldName(), MSG.wrongFormat()); + + item.focusInItem(); + + valid = false; + } + return valid; + } + + + /** + * + */ + protected boolean validateFloat(FormItem item, Map errors) { + boolean valid = true; + + String v = (String) item.getValue(); + + NumberFormat f = NumberFormat.getDecimalFormat(); + + try { + if (v == null) { + throw new NumberFormatException("empty"); + } + + double value = f.parse(v); + + errors.remove(item.getFieldName()); + } + catch (NumberFormatException nfe) { + errors.put(item.getFieldName(), MSG.wrongFormat()); + + item.focusInItem(); + + valid = false; + } + return valid; + } +} +