Project

General

Profile

Download (7.84 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.ListSelect;
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 ListSelect 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 ListSelect("Reference system");
115
        referenceSystemSelect.setNullSelectionAllowed(false);
116
        referenceSystemSelect.setRows(1);
117
        referenceSystemSelect.setWidth(100, Unit.PERCENTAGE);
118

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

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

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

    
140
        return root;
141
    }
142

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

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

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

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

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

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

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

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

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

    
219
    }
220

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

    
229
    public ListSelect getReferenceSystemSelect() {
230
        return referenceSystemSelect;
231
    }
232

    
233

    
234

    
235
}
(3-3/8)