gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.ellipse / src / main / java / org / gvsig / vectorediting / lib / prov / ellipse / FilledEllipseEditingProvider.java @ 2616
History | View | Annotate | Download (14.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.ellipse; |
26 |
|
27 |
import java.awt.geom.Point2D; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashMap; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
import org.gvsig.euclidean.EuclideanLine2D; |
33 |
import org.gvsig.euclidean.EuclideanManager; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.geom.GeometryLocator; |
37 |
import org.gvsig.fmap.geom.GeometryUtils; |
38 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
39 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
40 |
import org.gvsig.fmap.geom.primitive.Curve; |
41 |
import org.gvsig.fmap.geom.primitive.Ellipse; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.fmap.geom.type.GeometryType; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 |
import org.gvsig.tools.dynobject.DynObject; |
46 |
import org.gvsig.tools.service.spi.ProviderServices; |
47 |
import org.gvsig.tools.util.ToolsUtilLocator; |
48 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
49 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
54 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
55 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
56 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
57 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
62 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
63 |
|
64 |
public class FilledEllipseEditingProvider extends AbstractEditingProvider implements |
65 |
EditingProvider { |
66 |
|
67 |
protected EditingServiceParameter firstPointAxisA;
|
68 |
|
69 |
protected EditingServiceParameter secondPointAxisA;
|
70 |
|
71 |
protected EditingServiceParameter lengthSemiAxisB;
|
72 |
|
73 |
protected Map<EditingServiceParameter, Object> values; |
74 |
|
75 |
protected FeatureStore featureStore;
|
76 |
|
77 |
public FilledEllipseEditingProvider(ProviderServices providerServices,
|
78 |
DynObject parameters) { |
79 |
super(providerServices);
|
80 |
|
81 |
this.featureStore =
|
82 |
(FeatureStore) parameters |
83 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
84 |
|
85 |
this.firstPointAxisA =
|
86 |
new DefaultEditingServiceParameter("first_point_A_axis", |
87 |
"first_point_A_axis", EditingServiceParameter.TYPE.POSITION);
|
88 |
|
89 |
this.secondPointAxisA =
|
90 |
new DefaultEditingServiceParameter("second_point_A_axis", |
91 |
"second_point_A_axis", EditingServiceParameter.TYPE.POSITION);
|
92 |
|
93 |
this.lengthSemiAxisB =
|
94 |
new DefaultEditingServiceParameter("length_of_B_semiaxis", |
95 |
"length_of_B_semiaxis", EditingServiceParameter.TYPE.POSITION,
|
96 |
EditingServiceParameter.TYPE.VALUE); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public EditingServiceParameter next() {
|
101 |
if (values.get(firstPointAxisA) == null) { |
102 |
return firstPointAxisA;
|
103 |
} else if (values.get(secondPointAxisA) == null) { |
104 |
return secondPointAxisA;
|
105 |
} else if (values.get(lengthSemiAxisB) == null) { |
106 |
return lengthSemiAxisB;
|
107 |
} |
108 |
return null; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
113 |
throws DrawServiceException {
|
114 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
115 |
|
116 |
EditingProviderManager editingProviderManager = |
117 |
EditingProviderLocator.getProviderManager(); |
118 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
119 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
120 |
|
121 |
ISymbol previewSymbol = this.getPreviewSymbol();
|
122 |
drawingStatus.setPreviewSymbol(previewSymbol); |
123 |
|
124 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
125 |
|
126 |
|
127 |
if (values != null) { |
128 |
EditingProviderServices editingServiceProvider = |
129 |
(EditingProviderServices) getProviderServices(); |
130 |
|
131 |
Point firstPointAxis1Value = (Point) values.get(firstPointAxisA); |
132 |
Point secondPointAxis1Value = (Point) values.get(secondPointAxisA); |
133 |
|
134 |
if (firstPointAxis1Value != null) { |
135 |
drawingStatus.addStatus(firstPointAxis1Value, auxiliaryPointSymbolEditing, "");
|
136 |
|
137 |
|
138 |
if (secondPointAxis1Value == null) { |
139 |
|
140 |
try {
|
141 |
int subtype =
|
142 |
editingServiceProvider.getSubType(featureStore); |
143 |
Curve line = |
144 |
editingServiceProvider.createLine( |
145 |
firstPointAxis1Value, mousePosition, subtype); |
146 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
147 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
148 |
return drawingStatus;
|
149 |
} catch (Exception e) { |
150 |
throw new DrawServiceException(e); |
151 |
} |
152 |
|
153 |
} else {
|
154 |
|
155 |
try {
|
156 |
int subtype =
|
157 |
editingServiceProvider.getSubType(featureStore); |
158 |
Point center =
|
159 |
editingServiceProvider.getMidPoint( |
160 |
firstPointAxis1Value, secondPointAxis1Value, |
161 |
subtype); |
162 |
drawingStatus.addStatus(secondPointAxis1Value, auxiliaryPointSymbolEditing, "");
|
163 |
drawingStatus.addStatus(center, auxiliaryPointSymbolEditing, "");
|
164 |
|
165 |
Curve aAxis = |
166 |
editingServiceProvider.createLine( |
167 |
firstPointAxis1Value, secondPointAxis1Value, |
168 |
subtype); |
169 |
drawingStatus.addStatus(aAxis, auxiliaryLineSymbolEditing, "");
|
170 |
|
171 |
EuclideanLine2D lineAxisA = euclideanManager.createLine2D( |
172 |
firstPointAxis1Value.getX(), |
173 |
firstPointAxis1Value.getY(), |
174 |
secondPointAxis1Value.getX(), |
175 |
secondPointAxis1Value.getY()); |
176 |
|
177 |
double distance = lineAxisA.getDistance(mousePosition.getX(), mousePosition.getY());
|
178 |
|
179 |
addTemporaryGeometryToDrawingStatus(drawingStatus, firstPointAxis1Value, secondPointAxis1Value, |
180 |
subtype, distance); |
181 |
|
182 |
EuclideanLine2D lineAxisB = lineAxisA.getPerpendicular(center.getX(), center.getY()); |
183 |
|
184 |
Point2D pointAxisB = lineAxisB.getNearestPoint(mousePosition.getX(), mousePosition.getY());
|
185 |
|
186 |
Curve line = editingServiceProvider.createLine( |
187 |
center.getX(), |
188 |
center.getY(), |
189 |
pointAxisB.getX(), |
190 |
pointAxisB.getY(), |
191 |
subtype); |
192 |
|
193 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
194 |
|
195 |
drawingStatus.addStatus(aAxis, auxiliaryLineSymbolEditing, "");
|
196 |
|
197 |
} catch (Exception e) { |
198 |
throw new DrawServiceException(e); |
199 |
} |
200 |
} |
201 |
} |
202 |
} |
203 |
return drawingStatus;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* @param drawingStatus
|
208 |
* @param firstPointAxis1Value
|
209 |
* @param secondPointAxis1Value
|
210 |
* @param subtype
|
211 |
* @param distance
|
212 |
* @throws CreateGeometryException
|
213 |
*/
|
214 |
protected void addTemporaryGeometryToDrawingStatus(DefaultDrawingStatus drawingStatus, Point firstPointAxis1Value, |
215 |
Point secondPointAxis1Value, int subtype, double distance) throws CreateGeometryException { |
216 |
Ellipse ellipse = (Ellipse) GeometryLocator.getGeometryManager().create(Geometry.TYPES.ELLIPSE, subtype); |
217 |
ellipse.setPoints(firstPointAxis1Value, secondPointAxis1Value, distance); |
218 |
|
219 |
EditingProviderManager editingProviderManager = EditingProviderLocator.getProviderManager(); |
220 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
221 |
drawingStatus.addStatus(ellipse, polygonSymbolEditing, "");
|
222 |
drawingStatus.addStatus(ellipse, drawingStatus.getPreviewSymbol(), "");
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public void stop() throws StopServiceException { |
227 |
if (values != null) { |
228 |
values.clear(); |
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public List<EditingServiceParameter> getParameters() { |
235 |
List<EditingServiceParameter> list = new ArrayList<>(); |
236 |
list.add(firstPointAxisA); |
237 |
list.add(secondPointAxisA); |
238 |
list.add(lengthSemiAxisB); |
239 |
return list;
|
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 = this.next();
|
250 |
validateAndInsertValue(parameter, value); |
251 |
} |
252 |
|
253 |
private void validateAndInsertValue(EditingServiceParameter param, |
254 |
Object value) throws InvalidEntryException { |
255 |
|
256 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
257 |
|
258 |
if ((param == firstPointAxisA) || (param == secondPointAxisA)) {
|
259 |
if (value instanceof Point) { |
260 |
values.put(param, value); |
261 |
} |
262 |
|
263 |
} else if (param == lengthSemiAxisB) { |
264 |
if (value instanceof Point) { |
265 |
Double length = null; |
266 |
|
267 |
try {
|
268 |
EditingProviderServices editingProviderServices = |
269 |
(EditingProviderServices) getProviderServices(); |
270 |
|
271 |
Point firstPointAxis1Value =
|
272 |
(Point) values.get(firstPointAxisA);
|
273 |
Point secondPointAxis1Value =
|
274 |
(Point) values.get(secondPointAxisA);
|
275 |
int subtype =
|
276 |
editingProviderServices.getSubType(featureStore); |
277 |
Point center
|
278 |
= GeometryUtils.getMidPoint(firstPointAxis1Value, |
279 |
secondPointAxis1Value, subtype); |
280 |
|
281 |
EuclideanLine2D lineAxisA = euclideanManager.createLine2D( |
282 |
firstPointAxis1Value.getX(), |
283 |
firstPointAxis1Value.getY(), |
284 |
secondPointAxis1Value.getX(), |
285 |
secondPointAxis1Value.getY()); |
286 |
|
287 |
length = lineAxisA.getDistance(((Point)value).getX(), ((Point)value).getY()); |
288 |
|
289 |
} catch (Exception e) { |
290 |
throw new InvalidEntryException(e); |
291 |
} |
292 |
|
293 |
if (length != null) { |
294 |
values.put(param, length); |
295 |
} |
296 |
|
297 |
} else if (value instanceof Double) { |
298 |
|
299 |
if ((((Double) value) - 0.01) > 0) { |
300 |
values.put(param, value); |
301 |
} |
302 |
} |
303 |
} |
304 |
} |
305 |
|
306 |
@Override
|
307 |
public Geometry finish() throws FinishServiceException { |
308 |
if (values != null) { |
309 |
|
310 |
EditingProviderServices editingProviderServices = |
311 |
(EditingProviderServices) getProviderServices(); |
312 |
|
313 |
Point firstPointAxis1Value = (Point) values.get(firstPointAxisA); |
314 |
Point secondPointAxis1Value = (Point) values.get(secondPointAxisA); |
315 |
double lengthSemiaxis2Value = (Double) values.get(lengthSemiAxisB); |
316 |
|
317 |
if ((firstPointAxis1Value != null) |
318 |
&& (secondPointAxis1Value != null) && (lengthSemiaxis2Value > 0)) { |
319 |
|
320 |
|
321 |
try {
|
322 |
int subtype = editingProviderServices.getSubType(featureStore);
|
323 |
GeometryType storeGeomType = editingProviderServices.getGeomType(featureStore); |
324 |
|
325 |
Ellipse ellipse = |
326 |
(Ellipse) GeometryLocator.getGeometryManager().create(Geometry.TYPES.ELLIPSE, subtype); |
327 |
ellipse.setPoints(firstPointAxis1Value, secondPointAxis1Value, lengthSemiaxis2Value); |
328 |
|
329 |
if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
330 |
MultiSurface multisurface; |
331 |
multisurface = |
332 |
GeometryLocator.getGeometryManager().createMultiSurface( |
333 |
storeGeomType.getSubType()); |
334 |
multisurface.addSurface((Ellipse) ellipse); |
335 |
return multisurface;
|
336 |
} |
337 |
|
338 |
return ellipse;
|
339 |
} catch (Exception e) { |
340 |
throw new FinishServiceException(e); |
341 |
} |
342 |
} |
343 |
} |
344 |
return null; |
345 |
} |
346 |
|
347 |
@Override
|
348 |
public void finishAndStore() throws FinishServiceException { |
349 |
EditingProviderServices editingProviderServices = |
350 |
(EditingProviderServices) getProviderServices(); |
351 |
Geometry geometry = finish(); |
352 |
editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
353 |
featureStore); |
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void start() throws StartServiceException { |
358 |
values = new HashMap<>(); |
359 |
} |
360 |
|
361 |
@Override
|
362 |
public String getName() { |
363 |
return FilledEllipseEditingProviderFactory.PROVIDER_NAME;
|
364 |
} |
365 |
|
366 |
@Override
|
367 |
public Object getValue(EditingServiceParameter parameter) { |
368 |
return values!=null?values.get(parameter):null; |
369 |
} |
370 |
} |