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.rectangle / src / main / java / org / gvsig / vectorediting / lib / prov / rectangle / RectangleEditingProvider.java @ 2204

History | View | Annotate | Download (11.3 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.rectangle;
48

    
49
import java.util.ArrayList;
50
import java.util.HashMap;
51
import java.util.List;
52
import java.util.Map;
53

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

    
82
/**
83
 * @author llmarques
84
 *
85
 */
86
public class RectangleEditingProvider extends AbstractEditingProvider implements
87
EditingProvider {
88

    
89
    protected EditingServiceParameter firstPoint;
90

    
91
    protected EditingServiceParameter oppositePoint;
92

    
93
    protected Map<EditingServiceParameter, Object> values;
94

    
95
    protected FeatureStore featureStore;
96

    
97
    /**
98
     * Default constructor.
99
     *
100
     * @param parameters
101
     *            of service.
102
     * @param providerServices
103
     *            available services. See {@link EditingProviderServices}.
104
     */
105
    public RectangleEditingProvider(ProviderServices providerServices,
106
        DynObject parameters) {
107
        super(providerServices);
108

    
109
        this.featureStore =
110
            (FeatureStore) parameters
111
            .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
112

    
113
        this.firstPoint =
114
            new DefaultEditingServiceParameter("first_point", "first_point",
115
                EditingServiceParameter.TYPE.POSITION);
116

    
117
        this.oppositePoint =
118
            new DefaultEditingServiceParameter("second_point", "second_point",
119
                EditingServiceParameter.TYPE.POSITION);
120
    }
121

    
122
    public EditingServiceParameter next() {
123
        if (values.get(firstPoint) == null) {
124
            return firstPoint;
125
        } else if (values.get(oppositePoint) == null) {
126
            return oppositePoint;
127
        }
128
        return null;
129
    }
130

    
131
    public DrawingStatus getDrawingStatus(Point mousePosition)
132
        throws DrawServiceException {
133
        DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
134

    
135
        EditingProviderManager editingProviderManager =
136
            EditingProviderLocator.getProviderManager();
137
        ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
138
//        ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
139
        ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
140

    
141

    
142
        if (values != null) {
143

    
144
            Point firstPointValue = (Point) values.get(firstPoint);
145

    
146
            if (firstPointValue != null) {
147
                drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
148
                drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
149

    
150
                EditingProviderServices editingProviderServices =
151
                    (EditingProviderServices) getProviderServices();
152
                GeometryManager geometryManager =
153
                    GeometryLocator.getGeometryManager();
154

    
155
                try {
156
                    int subtype =
157
                        editingProviderServices.getSubType(featureStore);
158
                    Line rectangle = geometryManager.createLine(subtype);
159

    
160
                    rectangle.addVertex(firstPointValue);
161

    
162
                    Point secondPoint =
163
                        editingProviderServices.createPoint(
164
                            mousePosition.getX(), firstPointValue.getY(),
165
                            subtype);
166

    
167
                    rectangle.addVertex(secondPoint);
168

    
169
                    rectangle.addVertex(mousePosition);
170

    
171
                    Point thirdPoint =
172
                        editingProviderServices.createPoint(
173
                            firstPointValue.getX(), mousePosition.getY(),
174
                            subtype);
175

    
176
                    rectangle.addVertex(thirdPoint);
177

    
178
                    rectangle.addVertex(firstPointValue);
179
                    drawingStatus.addStatus(rectangle, lineSymbolEditing, "");
180

    
181
//                    drawingStatus.addGeometry(rectangle);
182
                } catch (BaseException e) {
183
                    throw new DrawServiceException(e);
184
                }
185
            }
186
        }
187

    
188
        return drawingStatus;
189
    }
190

    
191
    public void stop() throws StopServiceException {
192
        values.clear();
193
    }
194

    
195
    public List<EditingServiceParameter> getParameters() {
196
        List<EditingServiceParameter> parameters =
197
            new ArrayList<EditingServiceParameter>();
198
        parameters.add(firstPoint);
199
        parameters.add(oppositePoint);
200
        return parameters;
201
    }
202

    
203
    @Override
204
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
205
        validateAndInsertValue(parameter, value);
206
    }
207

    
208
    public void setValue(Object value) throws InvalidEntryException {
209
        EditingServiceParameter param = next();
210
        validateAndInsertValue(param, value);
211
    }
212

    
213
    /**
214
     * Checks if value is valid to parameter received as parameter.
215
     *
216
     * @param param
217
     *            of value
218
     * @param value
219
     *            to e inserted
220
     * @throws InvalidEntryException
221
     *             If values is no valid throw an InvalidEntryException
222
     */
223
    private void validateAndInsertValue(EditingServiceParameter param,
224
        Object value) throws InvalidEntryException {
225
        if (param == firstPoint) {
226
            if (value instanceof Point) {
227
                values.put(param, value);
228
                return;
229
            }
230
        } else if (param == oppositePoint) {
231
            if (value instanceof Point) {
232
                values.put(param, value);
233
                return;
234
            }
235
        }
236
        throw new InvalidEntryException(null);
237
    }
238

    
239
    public Geometry finish() throws FinishServiceException {
240

    
241
        if (values != null) {
242
            Point oppistePointValue = (Point) values.get(oppositePoint);
243

    
244
            EditingProviderServices editingProviderServices =
245
                (EditingProviderServices) getProviderServices();
246
            GeometryManager geometryManager =
247
                GeometryLocator.getGeometryManager();
248

    
249
            try {
250
                int subtype =
251
                    editingProviderServices.getSubType(featureStore);
252

    
253
                Line rectangle = geometryManager.createLine(subtype);
254
                Point firstPointValue = (Point) values.get(firstPoint);
255
                rectangle.addVertex(firstPointValue);
256
                Point secondPoint =
257
                    editingProviderServices.createPoint(
258
                        oppistePointValue.getX(), firstPointValue.getY(),
259
                        subtype);
260

    
261
                rectangle.addVertex(secondPoint);
262

    
263
                rectangle.addVertex(oppistePointValue);
264

    
265
                Point fourthPoint =
266
                    editingProviderServices.createPoint(
267
                        firstPointValue.getX(), oppistePointValue.getY(),
268
                        subtype);
269

    
270
                rectangle.addVertex(fourthPoint);
271

    
272
                rectangle.addVertex(firstPointValue);
273

    
274
                GeometryType geomType =
275
                    editingProviderServices.getGeomType(featureStore);
276

    
277
                if (geomType.isTypeOf(MULTICURVE)) {
278
                    MultiCurve multicurve;
279
                    multicurve =
280
                        GeometryLocator.getGeometryManager().createMultiCurve(
281
                            geomType.getSubType());
282
                    multicurve.addCurve(rectangle);
283
                    return multicurve;
284
                }
285

    
286
                return rectangle;
287
            } catch (BaseException e) {
288
                throw new FinishServiceException(
289
                    "There are some problem finalize" + this.getName(), e);
290
            }
291
        }
292
        return null;
293
    }
294

    
295
    public void finishAndStore() throws FinishServiceException {
296
        Geometry geometry = this.finish();
297
        EditingProviderServices editingProviderServices =
298
            (EditingProviderServices) getProviderServices();
299
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
300
            featureStore);
301

    
302
    }
303

    
304
    public void start() throws StartServiceException {
305
        values = new HashMap<EditingServiceParameter, Object>();
306
    }
307

    
308
    public String getName() {
309
        return RectangleEditingProviderFactory.PROVIDER_NAME;
310
    }
311

    
312
}