Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.point / src / main / java / org / gvsig / vectorediting / lib / prov / point / PointEditingProvider.java @ 2616

History | View | Annotate | Download (6.14 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.vectorediting.lib.prov.point;
26

    
27
import java.util.ArrayList;
28
import java.util.List;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.aggregate.MultiPoint;
33
import org.gvsig.fmap.geom.primitive.Point;
34
import org.gvsig.fmap.geom.type.GeometryType;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.service.spi.ProviderServices;
37
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
38
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
39
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
40
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
41
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
42
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
43
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
44
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
45
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
46
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
47
import org.gvsig.vectorediting.lib.spi.EditingProvider;
48
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
49
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
50

    
51
public class PointEditingProvider extends AbstractEditingProvider
52
implements EditingProvider {
53

    
54
    private final EditingProviderServices editingProviderServices =
55
        (EditingProviderServices) getProviderServices();
56

    
57
    private final EditingServiceParameter point;
58

    
59
    private Point valuePoint;
60

    
61
    private final FeatureStore featureStore;
62

    
63
    public PointEditingProvider(ProviderServices services,
64
        DynObject parameters) {
65
        super(services);
66
        this.featureStore =
67
            (FeatureStore) parameters
68
            .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
69

    
70
        this.point =
71
            new DefaultEditingServiceParameter("insert_point",
72
                "indicate_new_point", TYPE.POSITION);
73
    }
74

    
75
    @Override
76
    public DefaultDrawingStatus getDrawingStatus(Point mousePosition)
77
        throws DrawServiceException {
78
        return null;
79
    }
80

    
81
    @Override
82
    public void stop() throws StopServiceException {
83
        valuePoint = null;
84
    }
85

    
86
    private boolean isValidValue(EditingServiceParameter param, Object value) {
87
        return (value instanceof Point);
88
    }
89

    
90
    @Override
91
    public EditingServiceParameter next() {
92
        if (valuePoint == null) {
93
            return this.point;
94
        }
95
        return null;
96
    }
97

    
98
    @Override
99
    public List<EditingServiceParameter> getParameters() {
100
        List<EditingServiceParameter> list =
101
            new ArrayList<>();
102
        list.add(point);
103
        return list;
104
    }
105

    
106
    @Override
107
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
108
        validateAndInsertValue(parameter, value);
109
    }
110

    
111
    @Override
112
    public void setValue(Object value) throws InvalidEntryException {
113
        EditingServiceParameter param = next();
114
        validateAndInsertValue(param, value);
115
    }
116
    
117
    private void validateAndInsertValue(EditingServiceParameter parameter,
118
        Object value) throws InvalidEntryException {
119

    
120
        if (isValidValue(parameter, value)) {
121
            this.valuePoint = (Point) value;
122
        } else {
123
            throw new InvalidEntryException(null);
124
        }
125
    }
126

    
127

    
128
    @Override
129
    public void finishAndStore() throws FinishServiceException {
130
        Geometry geometry;
131
        try {
132
            geometry = finish();
133
            editingProviderServices.insertGeometryIntoFeatureStore(geometry,
134
                featureStore);
135
        } catch (Exception e) {
136
            throw new FinishServiceException("Can't finalize " + this.getName()
137
                + "with X=" + valuePoint.getX() + " Y= " + valuePoint.getY(), e);
138
        }
139
    }
140

    
141
    @Override
142
    public void start() throws StartServiceException {
143
        this.valuePoint = null;
144
    }
145

    
146
    @Override
147
    public String getName() {
148
        return PointEditingProviderFactory.PROVIDER_NAME;
149
    }
150

    
151
    @Override
152
    public Geometry finish() throws FinishServiceException {
153
        Point geometry = null;
154
        try {
155
            int subtype = editingProviderServices.getSubType(featureStore);
156
            GeometryType geomType =
157
                editingProviderServices.getGeomType(featureStore);
158
            geometry =
159
                editingProviderServices.createPoint(valuePoint.getX(),
160
                    valuePoint.getY(), subtype);
161

    
162
            if (geomType.isTypeOf(MULTIPOINT)) {
163
                MultiPoint multiPoint;
164
                multiPoint = GeometryLocator.getGeometryManager().createMultiPoint(subtype);
165
                multiPoint.addPoint(geometry);
166
                return multiPoint;
167
            }
168

    
169
        } catch (Exception e) {
170
            throw new FinishServiceException("Can't finalize " + this.getName()
171
                + "with X=" + valuePoint.getX() + " Y= " + valuePoint.getY(), e);
172
        }
173
        return geometry;
174
    }
175
    @Override
176
    public Object getValue(EditingServiceParameter parameter) {
177
        if(parameter == this.point){
178
            return valuePoint;
179
        }
180
        return null;
181
    }
182
}