Project

General

Profile

Download (7.81 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.component.common;
10

    
11
import java.util.Optional;
12

    
13
import org.apache.commons.lang3.StringUtils;
14
import org.apache.log4j.Logger;
15
import org.vaadin.addon.leaflet.LCircle;
16
import org.vaadin.addon.leaflet.LMap;
17
import org.vaadin.addon.leaflet.LMarker;
18
import org.vaadin.addon.leaflet.LTileLayer;
19
import org.vaadin.addon.leaflet.LeafletClickEvent;
20

    
21
import com.vaadin.data.Validator.InvalidValueException;
22
import com.vaadin.data.fieldgroup.BeanFieldGroup;
23
import com.vaadin.data.fieldgroup.FieldGroup;
24
import com.vaadin.data.util.BeanItem;
25
import com.vaadin.data.util.converter.Converter.ConversionException;
26
import com.vaadin.ui.Component;
27
import com.vaadin.ui.CssLayout;
28
import com.vaadin.ui.GridLayout;
29
import com.vaadin.ui.Label;
30
import com.vaadin.ui.NativeSelect;
31
import com.vaadin.ui.TextField;
32

    
33
import eu.etaxonomy.cdm.model.location.Point;
34
import eu.etaxonomy.cdm.vaadin.component.LongLatField;
35
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
36
import eu.etaxonomy.cdm.vaadin.util.converter.GeoLocationConverterValidator.Axis;
37
import eu.etaxonomy.cdm.vaadin.util.converter.IntegerConverter;
38
import eu.etaxonomy.vaadin.component.CompositeCustomField;
39

    
40
/**
41
 * @author a.kohlbecker
42
 * @since Jun 22, 2017
43
 *
44
 */
45
public class GeoLocationField extends CompositeCustomField<Point> {
46

    
47
    private static final Logger logger = Logger.getLogger(GeoLocationField.class);
48

    
49
    private static final long serialVersionUID = 1122123034547920390L;
50

    
51
    private static final String PRIMARY_STYLE = "v-geolocation-field";
52

    
53
    private BeanFieldGroup<Point> fieldGroup = new BeanFieldGroup<>(Point.class);
54

    
55
    private TextField longitudeField = new LongLatField("Longitude", Axis.LONGITUDE);
56
    private TextField latitudeField = new LongLatField("Latitude", Axis.LATITUDE);
57
    private Label longLatParsed = new Label();
58
    private TextField errorRadiusField = new TextFieldNFix("Error radius (m)");
59
    private NativeSelect referenceSystemSelect;
60

    
61
    private LMap map = new LMap();
62
    private LMarker mapMarker = new LMarker();
63
    private LCircle errorRadiusMarker = null;
64

    
65
    private CssLayout mapWrapper;
66

    
67

    
68
    /**
69
     *
70
     */
71
    public GeoLocationField() {
72
        super();
73
    }
74

    
75
    public GeoLocationField(String caption) {
76
        super();
77
        setCaption(caption);
78
    }
79

    
80
    /**
81
     * {@inheritDoc}
82
     */
83
    @Override
84
    protected Component initContent() {
85
        super.setPrimaryStyleName(PRIMARY_STYLE);
86

    
87
        errorRadiusField.setConverter(new IntegerConverter());
88

    
89

    
90
        map = new LMap();
91
        // LTileLayer baseLayer = new LOpenStreetMapLayer();
92
        LTileLayer baseLayer = new LTileLayer("https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png");
93
        baseLayer.setAttributionString("Map data © <a href=\"https://openstreetmap.org/copyright\">OpenStreetMap</a>-contributors, SRTM | Map style: © <a href=\"http://opentopomap.org\">OpenTopoMap</a> (<a href=\"https://creativecommons.org/licenses/by-sa/3.0/\">CC-BY-SA</a>)");
94
        map.addBaseLayer(baseLayer, null);
95
        map.setDraggingEnabled(true);
96
        map.setScrollWheelZoomEnabled(false);
97
        map.removeControl(map.getLayersControl());
98
        map.addClickListener(e -> refreshMap(e));
99
        // map.getZoomControl().addListener(ClickEvent.class, target, method);
100

    
101
        mapWrapper = new CssLayout(longLatParsed, map);
102
        mapWrapper.setSizeFull();
103
        mapWrapper.setStyleName("map-wrapper");
104
        longLatParsed.setWidthUndefined();
105

    
106
        longitudeField.setBuffered(false);
107
        longitudeField.addValueChangeListener(e -> updateMap());
108

    
109
        latitudeField.setBuffered(false);
110
        latitudeField.addValueChangeListener(e -> updateMap());
111

    
112
        errorRadiusField.addValueChangeListener( e -> updateMap());
113

    
114
        referenceSystemSelect = new NativeSelect("Reference system");
115
        referenceSystemSelect.setNullSelectionAllowed(false);
116
        referenceSystemSelect.setWidth(100, Unit.PERCENTAGE);
117

    
118
        GridLayout root = new GridLayout();
119
        root.setRows(2);
120
        root.setColumns(3);
121
        root.setStyleName("wrapper");
122
        root.addComponent(longitudeField, 0, 0);
123
        root.addComponent(latitudeField, 1, 0);
124
        root.addComponent(errorRadiusField, 0, 1);
125
        root.addComponent(referenceSystemSelect, 1, 1);
126
        // root.addComponent(map, 2, 1);
127
        root.addComponent(mapWrapper, 2, 0, 2, 1);
128
        root.setColumnExpandRatio(2, 1.0f);
129
        root.setRowExpandRatio(1, 1.0f);
130

    
131
        addStyledComponents(longitudeField, latitudeField, errorRadiusField, referenceSystemSelect, longLatParsed);
132
        addSizedComponent(root);
133

    
134
        fieldGroup.bind(longitudeField, "longitude");
135
        fieldGroup.bind(latitudeField, "latitude");
136
        fieldGroup.bind(errorRadiusField, "errorRadius");
137
        fieldGroup.bind(referenceSystemSelect, "referenceSystem");
138

    
139
        return root;
140
    }
141

    
142
    /**
143
     *
144
     */
145
    protected void updateMap() {
146

    
147
        Double longitude  = null;
148
        Double latitude = null;
149

    
150
        // using the string representations for UI display
151
        longLatParsed.setValue(longitudeField.getValue() + "/" + latitudeField.getValue());
152
        map.removeComponent(mapMarker);
153
        if(errorRadiusMarker != null){
154
            map.removeComponent(errorRadiusMarker);
155
        }
156

    
157
        try {
158
            longitudeField.validate();
159
            longitude = (Double) longitudeField.getConverter().convertToModel(longitudeField.getValue(), Double.class, null);
160
            latitudeField.validate();
161
            latitude = (Double) latitudeField.getConverter().convertToModel(latitudeField.getValue(), Double.class, null);
162
        } catch (InvalidValueException | ConversionException e){
163
            // IGNORE validation error have been set in the UI in the validate() methods
164
        }
165

    
166
        logger.debug("panning map to " + longitude + "/" + latitude);
167
        if(longitude != null && latitude != null){
168
            map.setZoomLevel(10);
169
            if(!StringUtils.isEmpty(errorRadiusField.getValue())){
170
                try{
171
                    double errorRadius = Double.parseDouble(errorRadiusField.getValue());
172
                    if(errorRadius > 0){
173
                        errorRadiusMarker = new LCircle(latitude, longitude, errorRadius);
174
                        errorRadiusMarker.setColor("#ff0000");
175
                        errorRadiusMarker.setWeight(1);
176
                        map.addComponents(errorRadiusMarker);
177
                    }
178
                } catch(Exception e){ /* IGNORE */ }
179
            }
180
            mapMarker.setPoint(new org.vaadin.addon.leaflet.shared.Point(latitude, longitude));
181
            map.addComponents(mapMarker);
182
            map.setCenter(latitude, longitude);
183
        } else {
184
            map.setZoomLevel(1);
185
            map.setCenter(40, 0);
186
        }
187
    }
188

    
189
    protected void refreshMap(LeafletClickEvent e) {
190
        logger.debug("map click");
191
    }
192

    
193
    /**
194
     * {@inheritDoc}
195
     */
196
    @Override
197
    public Class<? extends Point> getType() {
198
        return Point.class;
199
    }
200

    
201
    @Override
202
    protected void setInternalValue(Point newValue) {
203
        if(newValue == null){
204
            newValue = Point.NewInstance();
205
        }
206
        super.setInternalValue(newValue);
207
        fieldGroup.setItemDataSource(new BeanItem<Point>(newValue));
208
        updateMap();
209
    }
210

    
211
    /**
212
     * {@inheritDoc}
213
     */
214
    @Override
215
    protected void addDefaultStyles() {
216
        // no default styles so far
217

    
218
    }
219

    
220
    /**
221
     * {@inheritDoc}
222
     */
223
    @Override
224
    public Optional<FieldGroup> getFieldGroup() {
225
        return Optional.of(fieldGroup);
226
    }
227

    
228
    public NativeSelect getReferenceSystemSelect() {
229
        return referenceSystemSelect;
230
    }
231

    
232

    
233

    
234
}
(3-3/8)