Project

General

Profile

Download (2.34 KB) Statistics
| Branch: | Tag: | Revision:
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.NumberFormat;
12
import java.text.ParseException;
13
import java.util.Locale;
14

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

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

    
19
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
20

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

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

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

    
37
        if(StringUtils.isBlank(value)){
38
            return null;
39
        }
40
        NumberFormat nf = numberFormat(locale);
41
        try {
42
            return nf.parse(value).intValue();
43
        } catch (ParseException e){
44
            throw new ConversionException(e);
45
        }
46
    }
47

    
48
    /**
49
     * {@inheritDoc}
50
     */
51
    @Override
52
    public String convertToPresentation(Integer value, Class<? extends String> targetType, Locale locale)
53
            throws com.vaadin.data.util.converter.Converter.ConversionException {
54

    
55
        if(value == null){
56
            return null;
57
        }
58
        NumberFormat nf = numberFormat(locale);
59
        return nf.format(value);
60
    }
61

    
62
    /**
63
     * @param locale
64
     * @return
65
     */
66
    public NumberFormat numberFormat(Locale locale) {
67
        NumberFormat nf;
68
        if(RegistrationUIDefaults.NUMBER_FORMAT_OVERRIDE != null){
69
            nf = RegistrationUIDefaults.NUMBER_FORMAT_OVERRIDE;
70
        } else {
71
            if(locale == null){
72
                locale = Locale.getDefault();
73
            }
74
            nf = NumberFormat.getInstance(locale);
75
        }
76
        return nf;
77
    }
78

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

    
87
    /**
88
     * {@inheritDoc}
89
     */
90
    @Override
91
    public Class<String> getPresentationType() {
92
        return String.class;
93
    }
94

    
95
}
(5-5/14)