raimund@1500: package de.intevation.flys.client.client.utils; raimund@1500: import com.google.gwt.core.client.GWT; raimund@1500: import com.smartgwt.client.widgets.form.DynamicForm; raimund@1500: import com.smartgwt.client.widgets.form.fields.FormItem; raimund@1500: import com.google.gwt.i18n.client.NumberFormat; raimund@1500: import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; raimund@1500: import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; raimund@1500: import java.util.Map; raimund@1500: import de.intevation.flys.client.client.FLYSConstants; raimund@1500: raimund@1500: /** raimund@1500: * @author Raimund Renkert raimund@1500: * raimund@1500: * This validator is used for SmartGWT FormItems. raimund@1500: * raimund@1500: * To use this validator an attribute named 'internalType' has to be set. raimund@1500: * Values for 'internalType': raimund@1500: * 'double' raimund@1500: * 'integer' raimund@1500: */ raimund@1500: public class Validator implements ChangedHandler { raimund@1500: raimund@1500: /** The interface that provides i18n messages. */ raimund@1500: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); raimund@1500: raimund@1500: raimund@1500: /** raimund@1500: * raimund@1500: */ raimund@1500: public void onChanged(ChangedEvent e) { raimund@1500: FormItem item = e.getItem(); raimund@1500: DynamicForm form = e.getForm(); raimund@1500: Map errors = form.getErrors(); raimund@1500: String type = item.getAttribute("internalType"); raimund@1500: raimund@1500: if(type.equals("double")) { raimund@1500: validateFloat(item, errors); raimund@1500: form.setErrors(errors, true); raimund@1500: } raimund@1500: else if(type.equals("integer")) { raimund@1500: validateInteger(item, errors); raimund@1500: form.setErrors(errors, true); raimund@1500: } raimund@1500: else { raimund@1500: GWT.log("No Attribute 'internalType' set. Not using any validator."); raimund@1500: } raimund@1500: } raimund@1500: raimund@1500: raimund@1500: /** raimund@1500: * raimund@1500: */ raimund@1500: protected boolean validateInteger(FormItem item, Map errors) { raimund@1500: boolean valid = true; raimund@1500: raimund@1500: String v = (String) item.getValue(); raimund@1500: raimund@1500: NumberFormat f = NumberFormat.getDecimalFormat(); raimund@1500: raimund@1500: try { raimund@1500: if (v == null) { raimund@1500: throw new NumberFormatException("empty"); raimund@1500: } raimund@1500: raimund@1500: int value = Integer.parseInt(v); raimund@1500: raimund@1500: errors.remove(item.getFieldName()); raimund@1500: } raimund@1500: catch (NumberFormatException nfe) { raimund@1500: errors.put(item.getFieldName(), MSG.wrongFormat()); raimund@1500: raimund@1500: item.focusInItem(); raimund@1500: raimund@1500: valid = false; raimund@1500: } raimund@1500: return valid; raimund@1500: } raimund@1500: raimund@1500: raimund@1500: /** raimund@1500: * raimund@1500: */ raimund@1500: protected boolean validateFloat(FormItem item, Map errors) { raimund@1500: boolean valid = true; raimund@1500: raimund@1500: String v = (String) item.getValue(); raimund@1500: raimund@1500: NumberFormat f = NumberFormat.getDecimalFormat(); raimund@1500: raimund@1500: try { raimund@1500: if (v == null) { raimund@1500: throw new NumberFormatException("empty"); raimund@1500: } raimund@1500: raimund@1500: double value = f.parse(v); raimund@1500: raimund@1500: errors.remove(item.getFieldName()); raimund@1500: } raimund@1500: catch (NumberFormatException nfe) { raimund@1500: errors.put(item.getFieldName(), MSG.wrongFormat()); raimund@1500: raimund@1500: item.focusInItem(); raimund@1500: raimund@1500: valid = false; raimund@1500: } raimund@1500: return valid; raimund@1500: } raimund@1500: } raimund@1500: