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