gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circle3p / src / main / java / org / gvsig / vectorediting / lib / prov / circle3p / Circle3PEditingProvider.java @ 2204
History | View | Annotate | Download (12.9 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.circle3p; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.GeometryManager; |
36 |
import org.gvsig.fmap.geom.GeometryUtils; |
37 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
38 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
39 |
import org.gvsig.fmap.geom.primitive.Circle; |
40 |
import org.gvsig.fmap.geom.primitive.Curve; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.fmap.geom.type.GeometryType; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
44 |
import org.gvsig.tools.dynobject.DynObject; |
45 |
import org.gvsig.tools.service.spi.ProviderServices; |
46 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
47 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
48 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
54 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
55 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
56 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
62 |
|
63 |
public class Circle3PEditingProvider extends AbstractEditingProvider implements |
64 |
EditingProvider { |
65 |
|
66 |
protected FeatureStore featureStore;
|
67 |
|
68 |
protected EditingServiceParameter firstPoint;
|
69 |
|
70 |
protected EditingServiceParameter secondPoint;
|
71 |
|
72 |
protected EditingServiceParameter thirdPoint;
|
73 |
|
74 |
protected Map<EditingServiceParameter, Object> values; |
75 |
|
76 |
/**
|
77 |
* Default constructor.
|
78 |
*
|
79 |
* @param services
|
80 |
* available services to this provider
|
81 |
* @param parameters
|
82 |
* to create this provider
|
83 |
*/
|
84 |
public Circle3PEditingProvider(ProviderServices services,
|
85 |
DynObject parameters) { |
86 |
super(services);
|
87 |
|
88 |
this.featureStore =
|
89 |
(FeatureStore) parameters |
90 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
91 |
|
92 |
this.firstPoint =
|
93 |
new DefaultEditingServiceParameter("first_point", "first_point", |
94 |
TYPE.POSITION); |
95 |
|
96 |
this.secondPoint =
|
97 |
new DefaultEditingServiceParameter("second_point", "second_point", |
98 |
TYPE.POSITION); |
99 |
|
100 |
this.thirdPoint =
|
101 |
new DefaultEditingServiceParameter("third_point", "third_point", |
102 |
TYPE.POSITION); |
103 |
} |
104 |
|
105 |
public EditingServiceParameter next() {
|
106 |
if (values != null) { |
107 |
|
108 |
if (values.get(firstPoint) == null) { |
109 |
return firstPoint;
|
110 |
} else if (values.get(secondPoint) == null) { |
111 |
return secondPoint;
|
112 |
} else if (values.get(thirdPoint) == null) { |
113 |
return thirdPoint;
|
114 |
} |
115 |
} |
116 |
return null; |
117 |
} |
118 |
|
119 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
120 |
throws DrawServiceException {
|
121 |
|
122 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
123 |
EditingProviderManager editingProviderManager = |
124 |
EditingProviderLocator.getProviderManager(); |
125 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
126 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
127 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
128 |
|
129 |
if (values != null) { |
130 |
|
131 |
Point firstPointValue = (Point) values.get(firstPoint); |
132 |
Point secondPointValue = (Point) values.get(secondPoint); |
133 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
134 |
|
135 |
EditingProviderServices editingProviderServices = |
136 |
(EditingProviderServices) getProviderServices(); |
137 |
|
138 |
if (firstPointValue != null && secondPointValue == null) { |
139 |
|
140 |
try {
|
141 |
|
142 |
int subtype =
|
143 |
editingProviderServices.getSubType(featureStore); |
144 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
145 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
146 |
|
147 |
Curve line = |
148 |
GeometryUtils.createLine(firstPointValue, |
149 |
mousePosition, subtype); |
150 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
151 |
|
152 |
} catch (Exception e) { |
153 |
throw new DrawServiceException(e); |
154 |
} |
155 |
|
156 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue == null) { |
157 |
|
158 |
try {
|
159 |
|
160 |
int subtype =
|
161 |
editingProviderServices.getSubType(featureStore); |
162 |
|
163 |
Curve line1 = |
164 |
GeometryUtils.createLine(firstPointValue, |
165 |
secondPointValue, subtype); |
166 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
167 |
|
168 |
Curve line2 = |
169 |
GeometryUtils.createLine(secondPointValue, |
170 |
mousePosition, subtype); |
171 |
drawingStatus.addStatus(line2, auxiliaryLineSymbolEditing, "");
|
172 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
173 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
174 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
175 |
|
176 |
Point midPointA = GeometryUtils.getMidPoint(firstPointValue, secondPointValue, subtype);
|
177 |
Point midPointB = GeometryUtils.getMidPoint(secondPointValue, mousePosition, subtype);
|
178 |
|
179 |
Point center =
|
180 |
editingProviderServices.getCenter(firstPointValue, |
181 |
secondPointValue, mousePosition, subtype); |
182 |
|
183 |
Curve midPointLine1 = |
184 |
GeometryUtils.createLine(midPointA, |
185 |
center, subtype); |
186 |
drawingStatus.addStatus(midPointLine1, auxiliaryLineSymbolEditing, "");
|
187 |
|
188 |
Curve midPointLine2 = |
189 |
GeometryUtils.createLine(midPointB, |
190 |
center, subtype); |
191 |
drawingStatus.addStatus(midPointLine2, auxiliaryLineSymbolEditing, "");
|
192 |
|
193 |
drawingStatus.addStatus(midPointA, auxiliaryPointSymbolEditing, "");
|
194 |
drawingStatus.addStatus(midPointB, auxiliaryPointSymbolEditing, "");
|
195 |
|
196 |
double radius = center.distance(firstPointValue);
|
197 |
|
198 |
addTemporaryGeometryToDrawingStatus(drawingStatus, subtype, center, radius); |
199 |
drawingStatus.addStatus(center, auxiliaryPointSymbolEditing, "");
|
200 |
|
201 |
} catch (Exception e) { |
202 |
throw new DrawServiceException(e); |
203 |
} |
204 |
} |
205 |
} |
206 |
return drawingStatus;
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* @param drawingStatus
|
211 |
* @param subtype
|
212 |
* @param center
|
213 |
* @param radius
|
214 |
* @throws CreateGeometryException
|
215 |
*/
|
216 |
protected void addTemporaryGeometryToDrawingStatus(DefaultDrawingStatus drawingStatus, int subtype, Point center, double radius) |
217 |
throws CreateGeometryException {
|
218 |
EditingProviderManager editingProviderManager = |
219 |
EditingProviderLocator.getProviderManager(); |
220 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
221 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
222 |
Circle circle = (Circle)geomManager.create(Geometry.TYPES.CIRCLE, subtype); |
223 |
circle.setPoints(center, radius); |
224 |
drawingStatus.addStatus(circle, polygonSymbolEditing, "");
|
225 |
} |
226 |
|
227 |
public void stop() throws StopServiceException { |
228 |
if (values != null) { |
229 |
values.clear(); |
230 |
} |
231 |
} |
232 |
|
233 |
public List<EditingServiceParameter> getParameters() { |
234 |
List<EditingServiceParameter> parameters =
|
235 |
new ArrayList<EditingServiceParameter>(); |
236 |
parameters.add(firstPoint); |
237 |
parameters.add(secondPoint); |
238 |
parameters.add(thirdPoint); |
239 |
return parameters;
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException { |
244 |
validateAndInsertValue(parameter, value); |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void setValue(Object value) throws InvalidEntryException { |
249 |
EditingServiceParameter parameter = next(); |
250 |
validateAndInsertValue(parameter, value); |
251 |
} |
252 |
|
253 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
254 |
Object value) throws InvalidEntryException { |
255 |
if (parameter == firstPoint) {
|
256 |
if (value instanceof Point) { |
257 |
values.put(parameter, value); |
258 |
return;
|
259 |
} |
260 |
} else if (parameter == secondPoint) { |
261 |
if (value instanceof Point) { |
262 |
values.put(parameter, value); |
263 |
return;
|
264 |
} |
265 |
} else if (parameter == thirdPoint) { |
266 |
if (value instanceof Point) { |
267 |
values.put(parameter, value); |
268 |
return;
|
269 |
} |
270 |
} |
271 |
throw new InvalidEntryException(null); |
272 |
} |
273 |
|
274 |
public Geometry finish() throws FinishServiceException { |
275 |
if (values != null) { |
276 |
|
277 |
Point firstPointValue = (Point) values.get(firstPoint); |
278 |
Point secondPointValue = (Point) values.get(secondPoint); |
279 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
280 |
|
281 |
EditingProviderServices editingProviderServices = |
282 |
(EditingProviderServices) getProviderServices(); |
283 |
|
284 |
try {
|
285 |
|
286 |
int subtype = editingProviderServices.getSubType(featureStore);
|
287 |
|
288 |
Point center =
|
289 |
editingProviderServices.getCenter(firstPointValue, |
290 |
secondPointValue, thirdPointValue, subtype); |
291 |
double radius = center.distance(firstPointValue);
|
292 |
|
293 |
Circle circle = |
294 |
editingProviderServices.createCircle(center, radius, |
295 |
subtype); |
296 |
|
297 |
GeometryType geomType = |
298 |
editingProviderServices.getGeomType(featureStore); |
299 |
|
300 |
if (geomType.isTypeOf(MULTISURFACE)) {
|
301 |
|
302 |
MultiSurface multiSurface; |
303 |
multiSurface = |
304 |
GeometryLocator.getGeometryManager() |
305 |
.createMultiSurface(geomType.getSubType()); |
306 |
multiSurface.addSurface(circle); |
307 |
return multiSurface;
|
308 |
} |
309 |
|
310 |
return circle;
|
311 |
} catch (Exception e) { |
312 |
throw new FinishServiceException(e); |
313 |
} |
314 |
} |
315 |
throw new FinishServiceException(null); |
316 |
} |
317 |
|
318 |
public void finishAndStore() throws FinishServiceException { |
319 |
Geometry circle = finish(); |
320 |
EditingProviderServices editingProviderServices = |
321 |
(EditingProviderServices) getProviderServices(); |
322 |
editingProviderServices.insertGeometryIntoFeatureStore(circle, |
323 |
featureStore); |
324 |
} |
325 |
|
326 |
public void start() throws StartServiceException { |
327 |
values = new HashMap<EditingServiceParameter, Object>(); |
328 |
|
329 |
} |
330 |
|
331 |
public String getName() { |
332 |
return Circle3PEditingProviderFactory.PROVIDER_NAME;
|
333 |
} |
334 |
|
335 |
} |