# HG changeset patch # User Raimund Renkert # Date 1326100672 0 # Node ID 2a8b5dcbe8cadeaab76d16230d7daaed3612bac3 # Parent 0372797fc9cf964f418e6fe30a62354a0254ff3f Issue 358. Validate integer and double values in chart properties editor. flys-client/trunk@3625 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 0372797fc9cf -r 2a8b5dcbe8ca flys-client/ChangeLog --- a/flys-client/ChangeLog Fri Jan 06 11:53:14 2012 +0000 +++ b/flys-client/ChangeLog Mon Jan 09 09:17:52 2012 +0000 @@ -1,3 +1,14 @@ +2012-01-09 Raimund Renkert + + Issue 358. Validation. + + * src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java: + Added validator to integer and double field items. + + * src/main/java/de/intevation/flys/client/client/utils/Validator.java: + New. Validator for SmartGWT FormItems. Currently validates integer and + double values. + 2012-01-06 Felix Wolfsteller * src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java: diff -r 0372797fc9cf -r 2a8b5dcbe8ca flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java Fri Jan 06 11:53:14 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java Mon Jan 09 09:17:52 2012 +0000 @@ -24,11 +24,11 @@ import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; + import com.smartgwt.client.types.Alignment; import de.intevation.flys.client.client.Config; import de.intevation.flys.client.client.FLYSConstants; -import de.intevation.flys.client.client.ui.OutputTab; import de.intevation.flys.client.shared.model.Property; import de.intevation.flys.client.shared.model.PropertyGroup; import de.intevation.flys.client.shared.model.PropertySetting; @@ -36,9 +36,9 @@ import de.intevation.flys.client.shared.model.DoubleProperty; import de.intevation.flys.client.shared.model.IntegerProperty; import de.intevation.flys.client.shared.model.StringProperty; -import de.intevation.flys.client.shared.model.Settings; import de.intevation.flys.client.shared.model.OutputSettings; import de.intevation.flys.client.shared.model.Collection; +import de.intevation.flys.client.client.utils.Validator; import de.intevation.flys.client.client.services.CollectionAttributeService; import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync; @@ -88,7 +88,7 @@ setCanDragReposition(true); setCanDragResize(true); - + Config config = Config.getInstance(); collection = tab.getCollectionView().getCollection(); String outputName = tab.getOutputName(); settings = (OutputSettings)collection.getSettings(outputName); @@ -135,7 +135,7 @@ addItem(tabs); addItem(buttons); - setWidth(340); + setWidth(380); setHeight(470); centerInPage(); } @@ -172,16 +172,22 @@ IntegerProperty fontsize = (IntegerProperty)pg.getPropertyByName("font-size"); FormItem fs = createIntegerProperty(fontsize); + fs.setAttribute("internalType", "integer"); + fs.addChangedHandler(new Validator()); DoubleProperty upper = (DoubleProperty)pg.getPropertyByName("upper"); final FormItem range1 = createDoubleProperty(upper); - range1.setWidth(50); + range1.setAttribute("internalType", "double"); + range1.addChangedHandler(new Validator()); + range1.setWidth(70); DoubleProperty lower = (DoubleProperty)pg.getPropertyByName("lower"); final FormItem range2 = createDoubleProperty(lower); - range2.setWidth(50); + range2.setAttribute("internalType", "double"); + range2.addChangedHandler(new Validator()); + range2.setWidth(70); BooleanProperty fixation = (BooleanProperty)pg.getPropertyByName("fixation"); @@ -231,7 +237,7 @@ * */ protected DynamicForm generatePropertySetting(Property setting) { - final PropertySetting s = (PropertySetting)setting; + PropertySetting s = (PropertySetting)setting; DynamicForm form = new DynamicForm(); FormItem item = new FormItem(); if (setting instanceof BooleanProperty) { @@ -239,9 +245,13 @@ } else if (setting instanceof DoubleProperty) { item = createDoubleProperty((DoubleProperty)setting); + item.setAttribute("internalType", "double"); + item.addChangedHandler(new Validator()); } else if (setting instanceof IntegerProperty) { item = createIntegerProperty((IntegerProperty)setting); + item.setAttribute("internalType", "integer"); + item.addChangedHandler(new Validator()); } else if (setting instanceof StringProperty) { item = createStringProperty((StringProperty)setting); diff -r 0372797fc9cf -r 2a8b5dcbe8ca flys-client/src/main/java/de/intevation/flys/client/client/utils/Validator.java --- /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 Raimund Renkert + * + * 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; + } +} +