raimund@1504: package de.intevation.flys.client.client.utils; raimund@1504: raimund@1504: import java.util.Map; raimund@1504: raimund@1507: import com.google.gwt.core.client.GWT; raimund@1504: import com.google.gwt.i18n.client.NumberFormat; raimund@1504: raimund@1504: import com.smartgwt.client.widgets.form.fields.FormItem; raimund@1504: raimund@1507: import de.intevation.flys.client.client.FLYSConstants; raimund@1507: raimund@1504: /** raimund@1504: * @author Raimund Renkert raimund@1504: */ raimund@1507: public class DoubleValidator implements Validator { raimund@1507: raimund@1507: /** The interface that provides i18n messages. */ raimund@1507: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); raimund@1504: felix@2929: felix@2929: /** Statically determine doubility of String value. */ felix@2929: public static boolean isDouble(Object obj) { felix@2929: if (obj == null) { felix@2929: return false; felix@2929: } felix@2929: felix@2929: boolean valid = true; felix@2929: String v = obj.toString(); felix@2929: felix@2929: NumberFormat f = NumberFormat.getDecimalFormat(); felix@2929: felix@2929: try { felix@2929: if (v == null) { felix@2929: throw new NumberFormatException("empty"); felix@2929: } felix@2929: felix@2929: double value = f.parse(v); felix@2929: } felix@2929: catch (NumberFormatException nfe) { felix@2929: valid = false; felix@2929: } felix@2929: return valid; sascha@2937: felix@2929: } felix@2929: felix@3351: raimund@1504: /** felix@3351: * @return true if items value can be converted to double, if false, felix@3351: * expect error message in \param errors map. raimund@1504: */ raimund@1507: public boolean validate(FormItem item, Map errors) { raimund@1504: boolean valid = true; raimund@1504: raimund@1536: if(item.getValue() == null) { raimund@1536: return false; raimund@1536: } raimund@1507: String v = item.getValue().toString(); raimund@1504: raimund@1504: NumberFormat f = NumberFormat.getDecimalFormat(); raimund@1504: raimund@1504: try { raimund@1504: if (v == null) { raimund@1504: throw new NumberFormatException("empty"); raimund@1504: } raimund@1504: raimund@1504: double value = f.parse(v); raimund@1504: raimund@1504: errors.remove(item.getFieldName()); raimund@1504: } raimund@1504: catch (NumberFormatException nfe) { raimund@1504: errors.put(item.getFieldName(), MSG.wrongFormat()); raimund@1504: raimund@1504: item.focusInItem(); raimund@1504: raimund@1504: valid = false; raimund@1504: } raimund@1504: return valid; raimund@1504: } raimund@1504: } felix@3351: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : raimund@1504: