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 @ 575

History | View | Annotate | Download (8.68 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

    
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.aggregate.MultiCurve;
58
import org.gvsig.fmap.geom.primitive.Line;
59
import org.gvsig.fmap.geom.primitive.Point;
60
import org.gvsig.fmap.geom.type.GeometryType;
61
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
62
import org.gvsig.tools.dynobject.DynObject;
63
import org.gvsig.tools.service.spi.ProviderServices;
64
import org.gvsig.vectorediting.lib.api.DrawingStatus;
65
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
66
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
67
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
68
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
69
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
70
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
71
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
72
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
73
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
74
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
75
import org.gvsig.vectorediting.lib.spi.EditingProvider;
76
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
77
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
78
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
79
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
80

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

    
88
    private EditingServiceParameter firstPoint;
89

    
90
    private EditingServiceParameter secondPoint;
91

    
92
    private Map<EditingServiceParameter, Object> values;
93

    
94
    private FeatureStore featureStore;
95

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

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

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

    
119
    }
120

    
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
    public DrawingStatus getDrawingStatus(Point mousePosition)
131
        throws DrawServiceException {
132
        DefaultDrawingStatus geometries = new DefaultDrawingStatus();
133
        EditingProviderManager editingProviderManager =
134
            EditingProviderLocator.getProviderManager();
135
        ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
136
        ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
137

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

    
151
            } catch (Exception e) {
152
                throw new DrawServiceException(e);
153
            }
154

    
155
            return geometries;
156
        }
157

    
158
        return null;
159

    
160
    }
161

    
162
    public void stop() throws StopServiceException {
163
        values.clear();
164
    }
165

    
166
    public List<EditingServiceParameter> getParameters() {
167
        List<EditingServiceParameter> parameters =
168
            new ArrayList<EditingServiceParameter>();
169
        parameters.add(firstPoint);
170
        parameters.add(secondPoint);
171
        return parameters;
172
    }
173

    
174
    public void setValue(Object value) throws InvalidEntryException {
175
        EditingServiceParameter param = next();
176
        validateAndInsertValue(param, value);
177
    }
178

    
179
    private void validateAndInsertValue(EditingServiceParameter param,
180
        Object value) throws InvalidEntryException {
181
        if (param == firstPoint) {
182
            if (value instanceof Point) {
183
                values.put(param, value);
184
                return;
185
            }
186
        } else if (param == secondPoint) {
187
            if (value instanceof Point) {
188
                values.put(param, value);
189
                return;
190
            }
191
        }
192
        throw new InvalidEntryException(null);
193
    }
194

    
195
    public Geometry finish() throws FinishServiceException {
196
        Point secondPointValue = (Point) values.get(secondPoint);
197
        EditingProviderServices editingProviderServices =
198
            (EditingProviderServices) getProviderServices();
199

    
200
        try {
201

    
202
            DrawingStatus drawingStatus = getDrawingStatus(secondPointValue);
203
            Line line = (Line) drawingStatus.getGeometries().get(0);
204

    
205
            GeometryType storeGeomType =
206
                editingProviderServices.getGeomType(featureStore);
207

    
208
            if (storeGeomType.isTypeOf(MULTICURVE)) {
209

    
210
                MultiCurve multiCurve;
211
                multiCurve =
212
                    GeometryLocator.getGeometryManager().createMultiCurve(
213
                        storeGeomType.getSubType());
214
                multiCurve.addCurve(line);
215
                return multiCurve;
216
            }
217

    
218
            return line;
219
        } catch (Exception e) {
220
            throw new FinishServiceException(e);
221
        }
222
    }
223

    
224
    public void finishAndStore() throws FinishServiceException {
225
        Geometry geometry = this.finish();
226
        EditingProviderServices editingProviderServices =
227
            (EditingProviderServices) getProviderServices();
228
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
229
            featureStore);
230
    }
231

    
232
    public void start() throws StartServiceException {
233
        values = new HashMap<EditingServiceParameter, Object>();
234
    }
235

    
236
    public String getName() {
237
        return LineEditingProviderFactory.PROVIDER_NAME;
238
    }
239

    
240
}