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 org.apache.commons.lang3.StringUtils;
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.vaadin.addon.leaflet.LCircle;
|
14
|
import org.vaadin.addon.leaflet.LMap;
|
15
|
import org.vaadin.addon.leaflet.LMarker;
|
16
|
import org.vaadin.addon.leaflet.LTileLayer;
|
17
|
import org.vaadin.addon.leaflet.LeafletClickEvent;
|
18
|
|
19
|
import com.vaadin.data.Validator.InvalidValueException;
|
20
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
21
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
22
|
import com.vaadin.data.util.BeanItem;
|
23
|
import com.vaadin.data.util.converter.Converter.ConversionException;
|
24
|
import com.vaadin.ui.Component;
|
25
|
import com.vaadin.ui.CssLayout;
|
26
|
import com.vaadin.ui.GridLayout;
|
27
|
import com.vaadin.ui.Label;
|
28
|
import com.vaadin.ui.ListSelect;
|
29
|
import com.vaadin.ui.TextField;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.location.Point;
|
32
|
import eu.etaxonomy.cdm.vaadin.component.LongLatField;
|
33
|
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
|
34
|
import eu.etaxonomy.cdm.vaadin.util.converter.GeoLocationConverterValidator.Axis;
|
35
|
import eu.etaxonomy.cdm.vaadin.util.converter.IntegerConverter;
|
36
|
import eu.etaxonomy.vaadin.component.CompositeCustomField;
|
37
|
|
38
|
/**
|
39
|
* @author a.kohlbecker
|
40
|
* @since Jun 22, 2017
|
41
|
*
|
42
|
*/
|
43
|
public class GeoLocationField extends CompositeCustomField<Point> {
|
44
|
|
45
|
private static final Logger logger = Logger.getLogger(GeoLocationField.class);
|
46
|
|
47
|
private static final long serialVersionUID = 1122123034547920390L;
|
48
|
|
49
|
private static final String PRIMARY_STYLE = "v-geolocation-field";
|
50
|
|
51
|
private BeanFieldGroup<Point> fieldGroup = new BeanFieldGroup<>(Point.class);
|
52
|
|
53
|
private TextField longitudeField = new LongLatField("Longitude", Axis.LONGITUDE);
|
54
|
private TextField latitudeField = new LongLatField("Latitude", Axis.LATITUDE);
|
55
|
private Label longLatParsed = new Label();
|
56
|
private TextField errorRadiusField = new TextFieldNFix("Error radius (m)");
|
57
|
private ListSelect referenceSystemSelect;
|
58
|
|
59
|
private LMap map = new LMap();
|
60
|
private LMarker mapMarker = new LMarker();
|
61
|
private LCircle errorRadiusMarker = null;
|
62
|
|
63
|
private CssLayout mapWrapper;
|
64
|
|
65
|
|
66
|
/**
|
67
|
*
|
68
|
*/
|
69
|
public GeoLocationField() {
|
70
|
super();
|
71
|
}
|
72
|
|
73
|
public GeoLocationField(String caption) {
|
74
|
super();
|
75
|
setCaption(caption);
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* {@inheritDoc}
|
80
|
*/
|
81
|
@Override
|
82
|
protected Component initContent() {
|
83
|
super.setPrimaryStyleName(PRIMARY_STYLE);
|
84
|
|
85
|
errorRadiusField.setConverter(new IntegerConverter());
|
86
|
|
87
|
|
88
|
map = new LMap();
|
89
|
// LTileLayer baseLayer = new LOpenStreetMapLayer();
|
90
|
LTileLayer baseLayer = new LTileLayer("https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png");
|
91
|
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>)");
|
92
|
map.addBaseLayer(baseLayer, null);
|
93
|
map.setDraggingEnabled(true);
|
94
|
map.setScrollWheelZoomEnabled(false);
|
95
|
map.removeControl(map.getLayersControl());
|
96
|
map.addClickListener(e -> refreshMap(e));
|
97
|
// map.getZoomControl().addListener(ClickEvent.class, target, method);
|
98
|
|
99
|
mapWrapper = new CssLayout(longLatParsed, map);
|
100
|
mapWrapper.setSizeFull();
|
101
|
mapWrapper.setStyleName("map-wrapper");
|
102
|
longLatParsed.setWidthUndefined();
|
103
|
|
104
|
longitudeField.setBuffered(false);
|
105
|
longitudeField.addValueChangeListener(e -> updateMap());
|
106
|
|
107
|
latitudeField.setBuffered(false);
|
108
|
latitudeField.addValueChangeListener(e -> updateMap());
|
109
|
|
110
|
errorRadiusField.addValueChangeListener( e -> updateMap());
|
111
|
|
112
|
referenceSystemSelect = new ListSelect("Reference system");
|
113
|
referenceSystemSelect.setNullSelectionAllowed(false);
|
114
|
referenceSystemSelect.setRows(1);
|
115
|
referenceSystemSelect.setWidth(100, Unit.PERCENTAGE);
|
116
|
|
117
|
GridLayout root = new GridLayout();
|
118
|
root.setRows(2);
|
119
|
root.setColumns(3);
|
120
|
root.setStyleName("wrapper");
|
121
|
root.addComponent(longitudeField, 0, 0);
|
122
|
root.addComponent(latitudeField, 1, 0);
|
123
|
root.addComponent(errorRadiusField, 0, 1);
|
124
|
root.addComponent(referenceSystemSelect, 1, 1);
|
125
|
// root.addComponent(map, 2, 1);
|
126
|
root.addComponent(mapWrapper, 2, 0, 2, 1);
|
127
|
root.setColumnExpandRatio(2, 1.0f);
|
128
|
root.setRowExpandRatio(1, 1.0f);
|
129
|
|
130
|
addStyledComponents(longitudeField, latitudeField, errorRadiusField, referenceSystemSelect, longLatParsed);
|
131
|
addSizedComponent(root);
|
132
|
|
133
|
fieldGroup.bind(longitudeField, "longitude");
|
134
|
fieldGroup.bind(latitudeField, "latitude");
|
135
|
fieldGroup.bind(errorRadiusField, "errorRadius");
|
136
|
fieldGroup.bind(referenceSystemSelect, "referenceSystem");
|
137
|
|
138
|
return root;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
*
|
143
|
*/
|
144
|
protected void updateMap() {
|
145
|
|
146
|
Double longitude = null;
|
147
|
Double latitude = null;
|
148
|
|
149
|
// using the string representations for UI display
|
150
|
longLatParsed.setValue(longitudeField.getValue() + "/" + latitudeField.getValue());
|
151
|
map.removeComponent(mapMarker);
|
152
|
if(errorRadiusMarker != null){
|
153
|
map.removeComponent(errorRadiusMarker);
|
154
|
}
|
155
|
|
156
|
try {
|
157
|
longitudeField.validate();
|
158
|
longitude = (Double) longitudeField.getConverter().convertToModel(longitudeField.getValue(), Double.class, null);
|
159
|
latitudeField.validate();
|
160
|
latitude = (Double) latitudeField.getConverter().convertToModel(latitudeField.getValue(), Double.class, null);
|
161
|
} catch (InvalidValueException | ConversionException e){
|
162
|
// IGNORE validation error have been set in the UI in the validate() methods
|
163
|
}
|
164
|
|
165
|
logger.debug("panning map to " + longitude + "/" + latitude);
|
166
|
if(longitude != null && latitude != null){
|
167
|
map.setZoomLevel(10);
|
168
|
if(!StringUtils.isEmpty(errorRadiusField.getValue())){
|
169
|
try{
|
170
|
double errorRadius = Double.parseDouble(errorRadiusField.getValue());
|
171
|
if(errorRadius > 0){
|
172
|
errorRadiusMarker = new LCircle(latitude, longitude, errorRadius);
|
173
|
errorRadiusMarker.setColor("#ff0000");
|
174
|
errorRadiusMarker.setWeight(1);
|
175
|
map.addComponents(errorRadiusMarker);
|
176
|
}
|
177
|
} catch(Exception e){ /* IGNORE */ }
|
178
|
}
|
179
|
mapMarker.setPoint(new org.vaadin.addon.leaflet.shared.Point(latitude, longitude));
|
180
|
map.addComponents(mapMarker);
|
181
|
map.setCenter(latitude, longitude);
|
182
|
} else {
|
183
|
map.setZoomLevel(1);
|
184
|
map.setCenter(40, 0);
|
185
|
}
|
186
|
}
|
187
|
|
188
|
protected void refreshMap(LeafletClickEvent e) {
|
189
|
logger.debug("map click");
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* {@inheritDoc}
|
194
|
*/
|
195
|
@Override
|
196
|
public Class<? extends Point> getType() {
|
197
|
return Point.class;
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
protected void setInternalValue(Point newValue) {
|
202
|
if(newValue == null){
|
203
|
newValue = Point.NewInstance();
|
204
|
}
|
205
|
super.setInternalValue(newValue);
|
206
|
fieldGroup.setItemDataSource(new BeanItem<Point>(newValue));
|
207
|
updateMap();
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* {@inheritDoc}
|
212
|
*/
|
213
|
@Override
|
214
|
protected void addDefaultStyles() {
|
215
|
// no default styles so far
|
216
|
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* {@inheritDoc}
|
221
|
*/
|
222
|
@Override
|
223
|
public FieldGroup getFieldGroup() {
|
224
|
return fieldGroup;
|
225
|
}
|
226
|
|
227
|
public ListSelect getReferenceSystemSelect() {
|
228
|
return referenceSystemSelect;
|
229
|
}
|
230
|
|
231
|
|
232
|
|
233
|
}
|