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
|
}
|