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.text.ParseException;
|
12
|
|
13
|
import org.vaadin.addon.leaflet.LMap;
|
14
|
import org.vaadin.addon.leaflet.LMarker;
|
15
|
import org.vaadin.addon.leaflet.LOpenStreetMapLayer;
|
16
|
|
17
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
18
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
19
|
import com.vaadin.data.util.BeanItem;
|
20
|
import com.vaadin.server.UserError;
|
21
|
import com.vaadin.ui.Component;
|
22
|
import com.vaadin.ui.CssLayout;
|
23
|
import com.vaadin.ui.GridLayout;
|
24
|
import com.vaadin.ui.Label;
|
25
|
import com.vaadin.ui.TextField;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.location.Point;
|
28
|
import eu.etaxonomy.vaadin.component.CompositeCustomField;
|
29
|
|
30
|
/**
|
31
|
* @author a.kohlbecker
|
32
|
* @since Jun 22, 2017
|
33
|
*
|
34
|
*/
|
35
|
public class GeoLocationField extends CompositeCustomField<Point> {
|
36
|
|
37
|
|
38
|
private static final long serialVersionUID = 1122123034547920390L;
|
39
|
|
40
|
private static final String PRIMARY_STYLE = "v-geolocation-field";
|
41
|
|
42
|
private BeanFieldGroup<Point> fieldGroup = new BeanFieldGroup<>(Point.class);
|
43
|
|
44
|
Point parsedPoint = Point.NewInstance();
|
45
|
|
46
|
private TextField longitudeField = new TextField("Long.");
|
47
|
TextField latitudeField = new TextField("Lat.");
|
48
|
Label longLatParsed = new Label();
|
49
|
TextField errorRadiusField = new TextField("Error radius (m)");
|
50
|
TextField referenceSystemField = new TextField("ReferenceSystem");
|
51
|
|
52
|
private LMap map = new LMap();
|
53
|
private LMarker mapMarker = new LMarker();
|
54
|
|
55
|
private CssLayout mapWrapper;
|
56
|
|
57
|
/**
|
58
|
*
|
59
|
*/
|
60
|
public GeoLocationField() {
|
61
|
super();
|
62
|
}
|
63
|
|
64
|
public GeoLocationField(String caption) {
|
65
|
super();
|
66
|
setCaption(caption);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* {@inheritDoc}
|
71
|
*/
|
72
|
@Override
|
73
|
protected Component initContent() {
|
74
|
super.setPrimaryStyleName(PRIMARY_STYLE);
|
75
|
|
76
|
GridLayout root = new GridLayout();
|
77
|
root.setRows(2);
|
78
|
root.setColumns(3);
|
79
|
root.setStyleName("wrapper");
|
80
|
root.addComponent(longitudeField, 0, 0);
|
81
|
root.addComponent(latitudeField, 1, 0);
|
82
|
root.addComponent(errorRadiusField, 0, 1);
|
83
|
root.addComponent(referenceSystemField, 1, 1);
|
84
|
|
85
|
map = new LMap();
|
86
|
map.addBaseLayer(new LOpenStreetMapLayer(), null);
|
87
|
map.setDraggingEnabled(false);
|
88
|
map.setScrollWheelZoomEnabled(false);
|
89
|
map.removeControl(map.getLayersControl());
|
90
|
|
91
|
root.setColumnExpandRatio(2, 1.0f);
|
92
|
root.setRowExpandRatio(1, 1.0f);
|
93
|
|
94
|
root.addComponent(map, 2, 1);
|
95
|
mapWrapper = new CssLayout(longLatParsed, map);
|
96
|
root.addComponent(mapWrapper, 2, 0, 2, 1);
|
97
|
mapWrapper.setSizeFull();
|
98
|
mapWrapper.setStyleName("map-wrapper");
|
99
|
longLatParsed.setWidthUndefined();
|
100
|
|
101
|
longitudeField.addTextChangeListener(e -> updateParsedValue(longitudeField, e.getText()));
|
102
|
latitudeField.addTextChangeListener(e -> updateParsedValue(latitudeField, e.getText()));
|
103
|
|
104
|
addStyledComponents(longitudeField, latitudeField, errorRadiusField, referenceSystemField, longLatParsed);
|
105
|
addSizedComponent(root);
|
106
|
|
107
|
fieldGroup.bind(longitudeField, "longitude");
|
108
|
fieldGroup.bind(latitudeField, "latitude");
|
109
|
fieldGroup.bind(errorRadiusField, "errorRadius");
|
110
|
fieldGroup.bind(referenceSystemField, "referenceSystem");
|
111
|
|
112
|
return root;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* @param longitudeField2
|
117
|
* @param value
|
118
|
* @return
|
119
|
*/
|
120
|
private void updateParsedValue(TextField field, String value) {
|
121
|
field.setComponentError(null);
|
122
|
if(value != null){
|
123
|
try {
|
124
|
if(field == longitudeField){
|
125
|
|
126
|
parsedPoint.setLongitudeByParsing(value);
|
127
|
} else {
|
128
|
parsedPoint.setLatitudeByParsing(value);
|
129
|
}
|
130
|
} catch (ParseException e) {
|
131
|
field.setComponentError(new UserError(e.getMessage()));
|
132
|
}
|
133
|
}
|
134
|
|
135
|
updateMap();
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
*
|
140
|
*/
|
141
|
protected void updateMap() {
|
142
|
longLatParsed.setValue(parsedPoint.getLongitudeSexagesimal() + "/" + parsedPoint.getLatitudeSexagesimal());
|
143
|
map.removeComponent(mapMarker);
|
144
|
if(parsedPoint.getLongitude() != null && parsedPoint.getLatitude() != null){
|
145
|
map.setZoomLevel(10);
|
146
|
mapMarker.setPoint(new org.vaadin.addon.leaflet.shared.Point(parsedPoint.getLatitude(), parsedPoint.getLongitude()));
|
147
|
map.addComponents(mapMarker);
|
148
|
map.setCenter(parsedPoint.getLatitude(), parsedPoint.getLongitude());
|
149
|
} else {
|
150
|
map.setZoomLevel(1);
|
151
|
map.setCenter(40, 0);
|
152
|
}
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* {@inheritDoc}
|
157
|
*/
|
158
|
@Override
|
159
|
public Class<? extends Point> getType() {
|
160
|
return Point.class;
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
protected void setInternalValue(Point newValue) {
|
165
|
if(newValue == null){
|
166
|
newValue = Point.NewInstance();
|
167
|
}
|
168
|
super.setInternalValue(newValue);
|
169
|
fieldGroup.setItemDataSource(new BeanItem<Point>(newValue));
|
170
|
|
171
|
referenceSystemField.setEnabled(false); // disabled since not fully implemented
|
172
|
|
173
|
parsedPoint = newValue;
|
174
|
updateMap();
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* {@inheritDoc}
|
179
|
*/
|
180
|
@Override
|
181
|
protected void addDefaultStyles() {
|
182
|
// no default styles so far
|
183
|
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* {@inheritDoc}
|
188
|
*/
|
189
|
@Override
|
190
|
public FieldGroup getFieldGroup() {
|
191
|
return fieldGroup;
|
192
|
}
|
193
|
|
194
|
|
195
|
|
196
|
}
|