Project

General

Profile

« Previous | Next » 

Revision f610b87d

Added by Andreas Kohlbecker over 5 years ago

ref #7217 validator and converter for geolocations used in GeoLocationField

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/component/common/GeoLocationField.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.component.common;
10 10

  
11
import java.text.ParseException;
12

  
11
import org.apache.log4j.Logger;
13 12
import org.vaadin.addon.leaflet.LMap;
14 13
import org.vaadin.addon.leaflet.LMarker;
15 14
import org.vaadin.addon.leaflet.LOpenStreetMapLayer;
......
17 16
import com.vaadin.data.fieldgroup.BeanFieldGroup;
18 17
import com.vaadin.data.fieldgroup.FieldGroup;
19 18
import com.vaadin.data.util.BeanItem;
20
import com.vaadin.server.UserError;
21 19
import com.vaadin.ui.Component;
22 20
import com.vaadin.ui.CssLayout;
23 21
import com.vaadin.ui.GridLayout;
......
26 24

  
27 25
import eu.etaxonomy.cdm.model.location.Point;
28 26
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
27
import eu.etaxonomy.cdm.vaadin.util.converter.GeoLocationConverterValidator;
28
import eu.etaxonomy.cdm.vaadin.util.converter.GeoLocationConverterValidator.Axis;
29 29
import eu.etaxonomy.cdm.vaadin.util.converter.IntegerConverter;
30 30
import eu.etaxonomy.vaadin.component.CompositeCustomField;
31 31

  
......
36 36
 */
37 37
public class GeoLocationField extends CompositeCustomField<Point> {
38 38

  
39
    private static final Logger logger = Logger.getLogger(GeoLocationField.class);
39 40

  
40 41
    private static final long serialVersionUID = 1122123034547920390L;
41 42

  
......
43 44

  
44 45
    private BeanFieldGroup<Point> fieldGroup = new BeanFieldGroup<>(Point.class);
45 46

  
46
    Point parsedPoint = Point.NewInstance();
47

  
48 47
    private TextField longitudeField = new TextFieldNFix("Longitude");
49 48
    TextField latitudeField = new TextFieldNFix("Latitude");
50 49
    Label longLatParsed = new Label();
......
102 101
        mapWrapper.setStyleName("map-wrapper");
103 102
        longLatParsed.setWidthUndefined();
104 103

  
105
        longitudeField.addTextChangeListener(e -> updateParsedValue(longitudeField, e.getText()));
106
        latitudeField.addTextChangeListener(e -> updateParsedValue(latitudeField, e.getText()));
104
        longitudeField.setConverter(new GeoLocationConverterValidator(Axis.LONGITUDE));
105
        longitudeField.addValidator(new GeoLocationConverterValidator(Axis.LONGITUDE));
106
        longitudeField.setBuffered(false);
107
        longitudeField.addValueChangeListener(e -> updateMap());
108

  
109
        latitudeField.setConverter(new GeoLocationConverterValidator(Axis.LATITUDE));
110
        latitudeField.addValidator(new GeoLocationConverterValidator(Axis.LATITUDE));
111
        latitudeField.setBuffered(false);
112
        latitudeField.addValueChangeListener(e -> updateMap());
107 113

  
108 114
        addStyledComponents(longitudeField, latitudeField, errorRadiusField, referenceSystemField, longLatParsed);
109 115
        addSizedComponent(root);
......
116 122
        return root;
117 123
    }
118 124

  
119
    /**
120
     * @param longitudeField2
121
     * @param value
122
     * @return
123
     */
124
    private void updateParsedValue(TextField field, String value) {
125
        field.setComponentError(null);
126
        if(value != null){
127
            try {
128
            if(field == longitudeField){
129

  
130
                parsedPoint.setLongitudeByParsing(value);
131
            } else {
132
                parsedPoint.setLatitudeByParsing(value);
133
            }
134
            } catch (ParseException e) {
135
                field.setComponentError(new UserError(e.getMessage()));
136
            }
137
        }
138

  
139
        updateMap();
140
    }
125
//    /**
126
//     * @param longitudeField2
127
//     * @param value
128
//     * @return
129
//     */
130
//    private void updateParsedValue(TextField field, String value) {
131
//        field.setComponentError(null);
132
//        updateMap();
133
//    }
141 134

  
142 135
    /**
143 136
     *
144 137
     */
145 138
    protected void updateMap() {
146
        longLatParsed.setValue(parsedPoint.getLatitudeSexagesimal() + "/" + parsedPoint.getLongitudeSexagesimal());
139
        // using the string representations for UI display
140
        longLatParsed.setValue(longitudeField.getValue() + "/" + latitudeField.getValue());
147 141
        map.removeComponent(mapMarker);
148
        if(parsedPoint.getLongitude() != null && parsedPoint.getLatitude() != null){
142
        Double longitude = (Double) longitudeField.getConverter().convertToModel(longitudeField.getValue(), Double.class, null);
143
        Double latitude = (Double) latitudeField.getConverter().convertToModel(latitudeField.getValue(), Double.class, null);
144
        logger.debug("panning map to " + longitude + "/" + latitude);
145
        if(longitude != null && latitude != null){
149 146
            map.setZoomLevel(10);
150
            mapMarker.setPoint(new org.vaadin.addon.leaflet.shared.Point(parsedPoint.getLatitude(), parsedPoint.getLongitude()));
147
            mapMarker.setPoint(new org.vaadin.addon.leaflet.shared.Point(latitude, longitude));
151 148
            map.addComponents(mapMarker);
152
            map.setCenter(parsedPoint.getLatitude(), parsedPoint.getLongitude());
149
            map.setCenter(latitude, longitude);
153 150
        } else {
154 151
            map.setZoomLevel(1);
155 152
            map.setCenter(40, 0);
......
171 168
        }
172 169
        super.setInternalValue(newValue);
173 170
        fieldGroup.setItemDataSource(new BeanItem<Point>(newValue));
174

  
175 171
        referenceSystemField.setEnabled(false); // disabled since not fully implemented
176

  
177
        parsedPoint = newValue;
178 172
        updateMap();
179 173
    }
180 174

  
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/GeoLocationConverterValidator.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.ParseException;
12
import java.util.Locale;
13

  
14
import org.apache.log4j.Logger;
15

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

  
19
import eu.etaxonomy.cdm.model.location.Point;
20
import eu.etaxonomy.cdm.model.location.Point.Sexagesimal;
21

  
22
/**
23
 * Converts longitute and latitute string representations into
24
 * double values. At the same time this class can be used as Validator.
25
 *
26
 * @author a.kohlbecker
27
 * @since Nov 20, 2018
28
 *
29
 */
30
public class GeoLocationConverterValidator implements Converter<String, Double>, Validator {
31

  
32
    public enum Axis {
33
        LONGITUDE, LATITUDE;
34
    }
35

  
36
    private static final long serialVersionUID = -1780028474672132160L;
37

  
38
    private Axis axis;
39

  
40
    public GeoLocationConverterValidator(Axis axis){
41
        this.axis = axis;
42
    }
43

  
44
    @Override
45
    public Double convertToModel(String value, Class<? extends Double> targetType, Locale locale)
46
            throws com.vaadin.data.util.converter.Converter.ConversionException {
47

  
48
        if(value == null){
49
            return null;
50
        }
51
        try {
52
            if(axis == Axis.LONGITUDE){
53
                return Point.parseLongitude(value);
54
            } else {
55
                return Point.parseLatitude(value);
56
            }
57
        } catch (ParseException e) {
58
            Logger.getLogger(GeoLocationConverterValidator.class).error(e);
59
            throw new ConversionException(e);
60
        }
61
    }
62

  
63
    @Override
64
    public String convertToPresentation(Double value, Class<? extends String> targetType, Locale locale)
65
            throws com.vaadin.data.util.converter.Converter.ConversionException {
66

  
67
        if(value == null){
68
            return null;
69
        }
70
        if(axis == Axis.LONGITUDE){
71
            return Sexagesimal.valueOf(value, false).toString();
72
        } else {
73
            return Sexagesimal.valueOf(value, true).toString();
74
        }
75

  
76
    }
77

  
78
    @Override
79
    public Class<Double> getModelType() {
80
        return Double.class;
81
    }
82

  
83
    @Override
84
    public Class<String> getPresentationType() {
85
        return String.class;
86
    }
87

  
88
    /**
89
     * {@inheritDoc}
90
     */
91
    @Override
92
    public void validate(Object value) throws InvalidValueException {
93
        if(value != null && value instanceof String && !((String)value).isEmpty()){
94
            try {
95
                convertToModel((String)value, Double.class, null);
96
            } catch (com.vaadin.data.util.converter.Converter.ConversionException e) {
97
                throw new InvalidValueException("Value can not be converted to " + axis.name());
98
            }
99
        }
100

  
101

  
102
    }
103

  
104
}

Also available in: Unified diff