Project

General

Profile

Download (5.2 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.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 leafletMap = new LMap();
53

    
54
    private CssLayout mapWrapper;
55

    
56
    /**
57
     *
58
     */
59
    public GeoLocationField() {
60
        super();
61
    }
62

    
63
    public GeoLocationField(String caption) {
64
        super();
65
        setCaption(caption);
66
    }
67

    
68
    /**
69
     * {@inheritDoc}
70
     */
71
    @Override
72
    protected Component initContent() {
73
        super.setPrimaryStyleName(PRIMARY_STYLE);
74

    
75
        GridLayout root = new GridLayout();
76
        root.setRows(2);
77
        root.setColumns(3);
78
        root.setStyleName("wrapper");
79
        root.addComponent(longitudeField, 0, 0);
80
        root.addComponent(latitudeField, 1, 0);
81
        root.addComponent(errorRadiusField, 0, 1);
82
        root.addComponent(referenceSystemField, 1, 1);
83

    
84
        leafletMap = new LMap();
85
        leafletMap.setZoomLevel(7);
86
        leafletMap.addBaseLayer(new LOpenStreetMapLayer(), null);
87

    
88
        root.setColumnExpandRatio(2, 1.0f);
89
        root.setRowExpandRatio(1, 1.0f);
90

    
91
        root.addComponent(leafletMap, 2, 1);
92
        mapWrapper = new CssLayout(longLatParsed, leafletMap);
93
        root.addComponent(mapWrapper, 2, 0, 2, 1);
94
        mapWrapper.setSizeFull();
95
        mapWrapper.setStyleName("map-wrapper");
96

    
97
        longitudeField.addTextChangeListener(e -> updateParsedValue(longitudeField, e.getText()));
98
        latitudeField.addTextChangeListener(e -> updateParsedValue(latitudeField, e.getText()));
99

    
100
        addStyledComponents(longitudeField, latitudeField, errorRadiusField, referenceSystemField, longLatParsed);
101
        addSizedComponent(root);
102

    
103
        fieldGroup.bind(longitudeField, "longitude");
104
        fieldGroup.bind(latitudeField, "latitude");
105
        fieldGroup.bind(errorRadiusField, "errorRadius");
106
        fieldGroup.bind(referenceSystemField, "referenceSystem");
107

    
108
        return root;
109
    }
110

    
111
    /**
112
     * @param longitudeField2
113
     * @param value
114
     * @return
115
     */
116
    private void updateParsedValue(TextField field, String value) {
117
        field.setComponentError(null);
118
        if(value != null){
119
            try {
120
            if(field == longitudeField){
121

    
122
                parsedPoint.setLongitudeByParsing(value);
123
            } else {
124
                parsedPoint.setLatitudeByParsing(value);
125
            }
126
            } catch (ParseException e) {
127
                field.setComponentError(new UserError(e.getMessage()));
128
            }
129
        }
130

    
131
        updateMap();
132
    }
133

    
134
    /**
135
     *
136
     */
137
    protected void updateMap() {
138
        longLatParsed.setValue(parsedPoint.getLongitudeSexagesimal() + "/" + parsedPoint.getLatitudeSexagesimal());
139
        leafletMap.removeAllComponents();
140
        if(parsedPoint.getLongitude() != null && parsedPoint.getLatitude() != null){
141
            leafletMap.setCenter(parsedPoint.getLongitude(), parsedPoint.getLatitude());
142
            leafletMap.addComponents(new LMarker(parsedPoint.getLongitude(), parsedPoint.getLatitude()));
143
        }
144
    }
145

    
146
    /**
147
     * {@inheritDoc}
148
     */
149
    @Override
150
    public Class<? extends Point> getType() {
151
        return Point.class;
152
    }
153

    
154
    @Override
155
    protected void setInternalValue(Point newValue) {
156
        if(newValue == null){
157
            newValue = Point.NewInstance();
158
        }
159
        super.setInternalValue(newValue);
160
        fieldGroup.setItemDataSource(new BeanItem<Point>(newValue));
161

    
162
        referenceSystemField.setEnabled(false); // disabled since not fully implemented
163

    
164
        parsedPoint = newValue;
165
        updateMap();
166
    }
167

    
168
    /**
169
     * {@inheritDoc}
170
     */
171
    @Override
172
    protected void addDefaultStyles() {
173
        // no default styles so far
174

    
175
    }
176

    
177
    /**
178
     * {@inheritDoc}
179
     */
180
    @Override
181
    public FieldGroup getFieldGroup() {
182
        return fieldGroup;
183
    }
184

    
185

    
186

    
187
}
(1-1/5)