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