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