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.line / src / main / java / org / gvsig / vectorediting / lib / prov / line / LineEditingProvider.java @ 2616

History | View | Annotate | Download (9.2 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
/* gvSIG. Desktop Geographic Information System.
26
 *
27
 * Copyright ? 2007-2014 gvSIG Association
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
 * MA  02110-1301, USA.
43
 *
44
 * For any additional information, do not hesitate to contact us
45
 * at info AT gvsig.com, or visit our website www.gvsig.com.
46
 */
47
package org.gvsig.vectorediting.lib.prov.line;
48

    
49
import java.util.ArrayList;
50
import java.util.HashMap;
51
import java.util.List;
52
import java.util.Map;
53
import org.gvsig.fmap.dal.feature.FeatureStore;
54
import org.gvsig.fmap.geom.Geometry;
55
import org.gvsig.fmap.geom.GeometryLocator;
56
import org.gvsig.fmap.geom.aggregate.MultiCurve;
57
import org.gvsig.fmap.geom.primitive.Line;
58
import org.gvsig.fmap.geom.primitive.Point;
59
import org.gvsig.fmap.geom.type.GeometryType;
60
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
61
import org.gvsig.tools.dynobject.DynObject;
62
import org.gvsig.tools.service.spi.ProviderServices;
63
import org.gvsig.vectorediting.lib.api.DrawingStatus;
64
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
65
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
66
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
67
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
68
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
69
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
70
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
71
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
72
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
73
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
74
import org.gvsig.vectorediting.lib.spi.EditingProvider;
75
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
76
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
77
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
78
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
79

    
80
/**
81
 * @author llmarques
82
 *
83
 */
84
public class LineEditingProvider extends AbstractEditingProvider implements
85
EditingProvider {
86

    
87
    private final EditingServiceParameter firstPoint;
88

    
89
    private final EditingServiceParameter secondPoint;
90

    
91
    private Map<EditingServiceParameter, Object> values;
92

    
93
    private final FeatureStore featureStore;
94

    
95
    /**
96
     * Default constructor.
97
     *
98
     * @param providerServices
99
     *            available services for this provider
100
     * @param parameters
101
     *            of this provider
102
     */
103
    public LineEditingProvider(ProviderServices providerServices,
104
        DynObject parameters) {
105
        super(providerServices);
106
        this.featureStore =
107
            (FeatureStore) parameters
108
            .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
109

    
110
        this.firstPoint =
111
            new DefaultEditingServiceParameter("first_point", "first_point",
112
                TYPE.POSITION);
113

    
114
        this.secondPoint =
115
            new DefaultEditingServiceParameter("second_point", "second_point",
116
                TYPE.POSITION);
117

    
118
    }
119

    
120
    @Override
121
    public EditingServiceParameter next() {
122
        if (values.get(firstPoint) == null) {
123
            return this.firstPoint;
124
        } else if (values.get(secondPoint) == null) {
125
            return this.secondPoint;
126
        }
127
        return null;
128
    }
129

    
130
    @Override
131
    public DrawingStatus getDrawingStatus(Point mousePosition)
132
        throws DrawServiceException {
133
        DefaultDrawingStatus geometries = new DefaultDrawingStatus();
134
        EditingProviderManager editingProviderManager =
135
            EditingProviderLocator.getProviderManager();
136
        ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
137
        ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
138

    
139
        ISymbol previewSymbol = this.getPreviewSymbol();
140
        
141
        if ((values != null) && (values.get(firstPoint) != null)) {
142
            Point point = (Point) values.get(firstPoint);
143
            try {
144
                EditingProviderServices editingProviderServices =
145
                    (EditingProviderServices) getProviderServices();
146
                int subtype = editingProviderServices.getSubType(featureStore);
147
                Line line =
148
                    editingProviderServices.createLine(point, mousePosition,
149
                        subtype);
150
                geometries.addStatus(line, lineSymbolEditing, "");
151
                geometries.addStatus(line, previewSymbol, "");
152
                geometries.addStatus(point, auxiliaryPointSymbolEditing, "");
153
                geometries.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
154

    
155
            } catch (Exception e) {
156
                throw new DrawServiceException(e);
157
            }
158

    
159
            return geometries;
160
        }
161

    
162
        return null;
163

    
164
    }
165

    
166
    @Override
167
    public void stop() throws StopServiceException {
168
        values.clear();
169
    }
170

    
171
    @Override
172
    public List<EditingServiceParameter> getParameters() {
173
        List<EditingServiceParameter> parameters =
174
            new ArrayList<>();
175
        parameters.add(firstPoint);
176
        parameters.add(secondPoint);
177
        return parameters;
178
    }
179

    
180
    @Override
181
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
182
        validateAndInsertValue(parameter, value);
183
    }
184

    
185
    @Override
186
    public void setValue(Object value) throws InvalidEntryException {
187
        EditingServiceParameter param = next();
188
        validateAndInsertValue(param, value);
189
    }
190

    
191
    private void validateAndInsertValue(EditingServiceParameter param,
192
        Object value) throws InvalidEntryException {
193
        if (param == firstPoint) {
194
            if (value instanceof Point) {
195
                values.put(param, value);
196
                return;
197
            }
198
        } else if (param == secondPoint) {
199
            if (value instanceof Point) {
200
                values.put(param, value);
201
                return;
202
            }
203
        }
204
        throw new InvalidEntryException(null);
205
    }
206

    
207
    @Override
208
    public Geometry finish() throws FinishServiceException {
209
        Point secondPointValue = (Point) values.get(secondPoint);
210
        EditingProviderServices editingProviderServices =
211
            (EditingProviderServices) getProviderServices();
212

    
213
        try {
214

    
215
            DrawingStatus drawingStatus = getDrawingStatus(secondPointValue);
216
            Line line = (Line) drawingStatus.getGeometries().get(0);
217

    
218
            GeometryType storeGeomType =
219
                editingProviderServices.getGeomType(featureStore);
220

    
221
            if (storeGeomType.isTypeOf(MULTICURVE)) {
222

    
223
                MultiCurve multiCurve;
224
                multiCurve =
225
                    GeometryLocator.getGeometryManager().createMultiCurve(
226
                        storeGeomType.getSubType());
227
                multiCurve.addCurve(line);
228
                return multiCurve;
229
            }
230

    
231
            return line;
232
        } catch (Exception e) {
233
            throw new FinishServiceException(e);
234
        }
235
    }
236

    
237
    @Override
238
    public void finishAndStore() throws FinishServiceException {
239
        Geometry geometry = this.finish();
240
        EditingProviderServices editingProviderServices =
241
            (EditingProviderServices) getProviderServices();
242
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
243
            featureStore);
244
    }
245

    
246
    @Override
247
    public void start() throws StartServiceException {
248
        values = new HashMap<>();
249
    }
250

    
251
    @Override
252
    public String getName() {
253
        return LineEditingProviderFactory.PROVIDER_NAME;
254
    }
255

    
256
    @Override
257
    public Object getValue(EditingServiceParameter parameter) {
258
        return values!=null?values.get(parameter):null;
259
    }
260
}