Project

General

Profile

« Previous | Next » 

Revision ee699257

Added by Andreas Kohlbecker about 6 years ago

fix #7314 DecimalConverter and IntegerConverter for better parsing of numbers in min max fields

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/component/common/MinMaxTextField.java
10 10

  
11 11
import org.apache.commons.lang.StringUtils;
12 12

  
13
import com.vaadin.data.util.converter.Converter;
13 14
import com.vaadin.ui.HorizontalLayout;
14 15
import com.vaadin.ui.TextField;
15 16

  
......
32 33
    TextFieldNFix minField, textField;
33 34
    MaxTextField maxField;
34 35

  
36
    Converter<String, ?> numberConverter;
37

  
35 38
    String unitOfMeasure;
36 39

  
37
    public MinMaxTextField(String caption, String unitOfMeasure){
40
    public MinMaxTextField(String caption, String unitOfMeasure, Converter<String, ?> numberConverter){
38 41

  
39 42
        this.unitOfMeasure = unitOfMeasure;
43
        this.numberConverter = numberConverter;
40 44

  
41 45
        setCaption(caption);
42 46
        setPrimaryStyleName(PRIMARY_STYLE);
......
45 49

  
46 50
    }
47 51

  
52
    public MinMaxTextField(String caption, String unitOfMeasure){
53
        this(caption, unitOfMeasure, null);
54
    }
55

  
48 56
    /**
49 57
     * @param unitOfMeasure
50 58
     */
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/DoubleConverter.java
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.util.converter;
10

  
11
import java.text.DecimalFormatSymbols;
12
import java.text.NumberFormat;
13
import java.text.ParseException;
14
import java.util.Locale;
15

  
16
import org.apache.commons.lang3.StringUtils;
17

  
18
import com.vaadin.data.util.converter.Converter;
19

  
20
/**
21
 * @author a.kohlbecker
22
 * @since Mar 28, 2018
23
 *
24
 */
25
public class DoubleConverter implements Converter<String, Double> {
26

  
27
    private static final long serialVersionUID = -8799792699785931554L;
28

  
29
    /**
30
     * {@inheritDoc}
31
     */
32
    @Override
33
    public Double convertToModel(String value, Class<? extends Double> targetType, Locale locale)
34
            throws com.vaadin.data.util.converter.Converter.ConversionException {
35

  
36
        if(StringUtils.isBlank(value)){
37
            return null;
38
        }
39
        if(locale == null){
40
            locale = Locale.getDefault();
41
        }
42
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
43
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
44
        String separator = symbols.getDecimalSeparator() + "";
45
        value = value.replaceAll("[.,;]", separator);
46
        try {
47
            return nf.parse(value).doubleValue();
48
        } catch (ParseException e){
49
            throw new ConversionException(e);
50
        }
51
    }
52

  
53
    /**
54
     * {@inheritDoc}
55
     */
56
    @Override
57
    public String convertToPresentation(Double value, Class<? extends String> targetType, Locale locale)
58
            throws com.vaadin.data.util.converter.Converter.ConversionException {
59

  
60
        if(value == null){
61
            return null;
62
        }
63
        if(locale == null){
64
            locale = Locale.getDefault();
65
        }
66
        NumberFormat nf = NumberFormat.getInstance(locale);
67
        return nf.format(value);
68
    }
69

  
70
    /**
71
     * {@inheritDoc}
72
     */
73
    @Override
74
    public Class<Double> getModelType() {
75
        return Double.class;
76
    }
77

  
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    public Class<String> getPresentationType() {
83
        return String.class;
84
    }
85

  
86
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/IntegerConverter.java
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.util.converter;
10

  
11
import java.text.DecimalFormatSymbols;
12
import java.text.NumberFormat;
13
import java.text.ParseException;
14
import java.util.Locale;
15

  
16
import org.apache.commons.lang3.StringUtils;
17

  
18
import com.vaadin.data.util.converter.Converter;
19

  
20
/**
21
 * @author a.kohlbecker
22
 * @since Mar 28, 2018
23
 *
24
 */
25
public class IntegerConverter implements Converter<String, Integer> {
26

  
27
    private static final long serialVersionUID = -8799792699785931554L;
28

  
29
    /**
30
     * {@inheritDoc}
31
     */
32
    @Override
33
    public Integer convertToModel(String value, Class<? extends Integer> targetType, Locale locale)
34
            throws com.vaadin.data.util.converter.Converter.ConversionException {
35

  
36
        if(StringUtils.isBlank(value)){
37
            return null;
38
        }
39
        if(locale == null){
40
            locale = Locale.getDefault();
41
        }
42
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
43
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
44
        String separator = symbols.getDecimalSeparator() + "";
45
        value = value.replaceAll("[.,;]", separator);
46
        try {
47
            return nf.parse(value).intValue();
48
        } catch (ParseException e){
49
            throw new ConversionException(e);
50
        }
51
    }
52

  
53
    /**
54
     * {@inheritDoc}
55
     */
56
    @Override
57
    public String convertToPresentation(Integer value, Class<? extends String> targetType, Locale locale)
58
            throws com.vaadin.data.util.converter.Converter.ConversionException {
59

  
60
        if(value == null){
61
            return null;
62
        }
63
        if(locale == null){
64
            locale = Locale.getDefault();
65
        }
66
        NumberFormat nf = NumberFormat.getInstance(locale);
67
        return nf.format(value);
68
    }
69

  
70
    /**
71
     * {@inheritDoc}
72
     */
73
    @Override
74
    public Class<Integer> getModelType() {
75
        return Integer.class;
76
    }
77

  
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    public Class<String> getPresentationType() {
83
        return String.class;
84
    }
85

  
86
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/SpecimenTypeDesignationWorkingsetPopupEditor.java
34 34
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
35 35
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
36 36
import eu.etaxonomy.cdm.vaadin.util.TeamOrPersonBaseCaptionGenerator;
37
import eu.etaxonomy.cdm.vaadin.util.converter.DoubleConverter;
38
import eu.etaxonomy.cdm.vaadin.util.converter.IntegerConverter;
37 39
import eu.etaxonomy.cdm.vaadin.view.PerEntityAuthorityGrantingEditor;
38 40
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
39 41

  
......
122 124
        bindField(absElevationMinMax.getMaxField(), "absoluteElevationMax");
123 125
        bindField(absElevationMinMax.getTextField(), "absoluteElevationText");
124 126

  
127
        absElevationMinMax.getMaxField().setConverter(new IntegerConverter());
128
        absElevationMinMax.getMinField().setConverter(new IntegerConverter());
129

  
125 130
        row++;
126 131
        MinMaxTextField distanceToWaterSurfaceMinMax = new MinMaxTextField("Distance to water surface", "m");
127 132
        distanceToWaterSurfaceMinMax.setWidth("100%");
......
131 136
        bindField(distanceToWaterSurfaceMinMax.getMinField(), "distanceToWaterSurface");
132 137
        bindField(distanceToWaterSurfaceMinMax.getMaxField(), "distanceToWaterSurfaceMax");
133 138
        bindField(distanceToWaterSurfaceMinMax.getTextField(), "distanceToWaterSurfaceText");
139
        distanceToWaterSurfaceMinMax.getMaxField().setConverter(new DoubleConverter());
140
        distanceToWaterSurfaceMinMax.getMinField().setConverter(new DoubleConverter());
134 141
        distanceToWaterSurfaceMinMax.getMaxField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
135 142
        distanceToWaterSurfaceMinMax.getMinField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
136 143

  
......
143 150
        bindField(distanceToGroundMinMax.getMinField(), "distanceToGround");
144 151
        bindField(distanceToGroundMinMax.getMaxField(), "distanceToGroundMax");
145 152
        bindField(distanceToGroundMinMax.getTextField(), "distanceToGroundText");
153
        distanceToGroundMinMax.getMaxField().setConverter(new DoubleConverter());
154
        distanceToGroundMinMax.getMinField().setConverter(new DoubleConverter());
146 155

  
147 156
        row++;
148 157
        collectorField = new TeamOrPersonField("Collector", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);

Also available in: Unified diff