2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.util
.converter
;
11 import java
.text
.ParseException
;
12 import java
.util
.Locale
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
17 import com
.vaadin
.data
.Validator
;
18 import com
.vaadin
.data
.util
.converter
.Converter
;
20 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
21 import eu
.etaxonomy
.cdm
.model
.location
.Point
.Sexagesimal
;
24 * Converts longitute and latitute string representations into
25 * double values. At the same time this class can be used as Validator.
27 * @author a.kohlbecker
30 public class GeoLocationConverterValidator
implements Converter
<String
, Double
>, Validator
{
32 private static final Logger logger
= LogManager
.getLogger();
38 private static final long serialVersionUID
= -1780028474672132160L;
42 public GeoLocationConverterValidator(Axis axis
){
47 public Double
convertToModel(String value
, Class
<?
extends Double
> targetType
, Locale locale
)
48 throws com
.vaadin
.data
.util
.converter
.Converter
.ConversionException
{
54 if(axis
== Axis
.LONGITUDE
){
55 return Point
.parseLongitude(value
);
57 return Point
.parseLatitude(value
);
59 } catch (ParseException e
) {
61 throw new ConversionException(e
);
66 public String
convertToPresentation(Double value
, Class
<?
extends String
> targetType
, Locale locale
)
67 throws com
.vaadin
.data
.util
.converter
.Converter
.ConversionException
{
72 if(axis
== Axis
.LONGITUDE
){
73 return Sexagesimal
.valueOf(value
, false).toString();
75 return Sexagesimal
.valueOf(value
, true).toString();
80 public Class
<Double
> getModelType() {
85 public Class
<String
> getPresentationType() {
90 public void validate(Object value
) throws InvalidValueException
{
91 if(value
!= null && value
instanceof String
&& !((String
)value
).isEmpty()){
93 convertToModel((String
)value
, Double
.class, null);
94 } catch (com
.vaadin
.data
.util
.converter
.Converter
.ConversionException e
) {
95 throw new InvalidValueException("Invalid " + axis
.name().toLowerCase());