gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.align / src / main / java / org / gvsig / vectorediting / lib / prov / align / AlignEdgesEditingProvider.java @ 2444
History | View | Annotate | Download (27.1 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 |
package org.gvsig.vectorediting.lib.prov.align; |
25 |
|
26 |
import java.awt.Rectangle; |
27 |
import java.awt.geom.AffineTransform; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashMap; |
30 |
import java.util.LinkedHashMap; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
39 |
import org.gvsig.fmap.geom.GeometryUtils; |
40 |
import org.gvsig.fmap.geom.primitive.Envelope; |
41 |
import org.gvsig.fmap.geom.primitive.Line; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.fmap.mapcontext.MapContext; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.dynobject.DynObject; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.service.spi.ProviderServices; |
49 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
50 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
51 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
54 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
55 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
56 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
57 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
58 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
59 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
62 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
63 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
64 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
65 |
|
66 |
/**
|
67 |
* @author fdiaz
|
68 |
*
|
69 |
*/
|
70 |
public class AlignEdgesEditingProvider extends AbstractEditingProvider implements EditingProvider { |
71 |
|
72 |
private static final String ALIGN_MODE = "_align_mode"; |
73 |
|
74 |
private static final String ALIGN_UP = "_up"; |
75 |
private static final String ALIGN_DOWN = "_down"; |
76 |
private static final String ALIGN_LEFT = "_left"; |
77 |
private static final String ALIGN_RIGHT = "_right"; |
78 |
private static final String ALIGN_HORIZONTAL_CENTER = "_horizontal_centering"; |
79 |
private static final String ALIGN_VERTICAL_CENTER = "_vertical_centering"; |
80 |
private static final String ALIGN_BOTH_CENTER = "_both_centering"; |
81 |
|
82 |
private static final String SHORT_ALIGN_UP = "_short_up"; |
83 |
private static final String SHORT_ALIGN_DOWN = "_short_down"; |
84 |
private static final String SHORT_ALIGN_LEFT = "_short_left"; |
85 |
private static final String SHORT_ALIGN_RIGHT = "_short_right"; |
86 |
private static final String SHORT_ALIGN_HORIZONTAL_CENTER = "_short_horizontal_centering"; |
87 |
private static final String SHORT_ALIGN_VERTICAL_CENTER = "_short_vertical_centering"; |
88 |
private static final String SHORT_ALIGN_BOTH_CENTER = "_short_both_centering"; |
89 |
|
90 |
private static final String DO_COPY_QUESTION = "_do_copy_question"; |
91 |
|
92 |
private static final String DELETE_ORIGINAL_GEOMETRIES = "_delete_original_geometries"; |
93 |
private static final String DO_COPY = "_do_copy"; |
94 |
|
95 |
private static final String YES = "_yes"; |
96 |
private static final String NO = "_no"; |
97 |
|
98 |
private static final String SHORT_YES = "_short_yes"; |
99 |
private static final String SHORT_NO = "_short_no"; |
100 |
|
101 |
//Variables est?ticas para guardarnos el align_mode y el do_copy
|
102 |
private static String savedAlignMode; |
103 |
private static Boolean savedDoCopy; |
104 |
|
105 |
private final EditingServiceParameter geometryBaseParameter; |
106 |
|
107 |
private final EditingServiceParameter alignModeParameter; |
108 |
|
109 |
private final EditingServiceParameter doCopyParameter; |
110 |
|
111 |
private final EditingServiceParameter featureToAlignParameter; |
112 |
|
113 |
private final Map<EditingServiceParameter, Object> values; |
114 |
|
115 |
private final FeatureStore featureStore; |
116 |
|
117 |
private final MapContext mapContext; |
118 |
|
119 |
/**
|
120 |
* Default constructor.
|
121 |
*
|
122 |
* @param providerServices available services for this provider
|
123 |
* @param parameters of this provider
|
124 |
*/
|
125 |
public AlignEdgesEditingProvider(ProviderServices providerServices,
|
126 |
DynObject parameters) { |
127 |
super(providerServices);
|
128 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
129 |
|
130 |
this.featureStore
|
131 |
= (FeatureStore) parameters |
132 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
133 |
|
134 |
this.mapContext
|
135 |
= (MapContext) parameters |
136 |
.getDynValue(EditingProviderFactory.MAPCONTEXT_FIELD); |
137 |
|
138 |
this.geometryBaseParameter
|
139 |
= new DefaultEditingServiceParameter("_geometry_base", "_geometry_base", |
140 |
TYPE.POSITION); |
141 |
|
142 |
this.featureToAlignParameter
|
143 |
= new DefaultEditingServiceParameter("_geometry_to_align", "_geometry_to_align", |
144 |
TYPE.POSITION); |
145 |
|
146 |
Map<String, String> alignModeOptions = new HashMap<>(); |
147 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_UP), i18nManager.getTranslation(ALIGN_UP)); |
148 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_DOWN), i18nManager.getTranslation(ALIGN_DOWN)); |
149 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_LEFT), i18nManager.getTranslation(ALIGN_LEFT)); |
150 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_RIGHT), i18nManager.getTranslation(ALIGN_RIGHT)); |
151 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_HORIZONTAL_CENTER), i18nManager.getTranslation(ALIGN_HORIZONTAL_CENTER)); |
152 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_VERTICAL_CENTER), i18nManager.getTranslation(ALIGN_VERTICAL_CENTER)); |
153 |
alignModeOptions.put(i18nManager.getTranslation(SHORT_ALIGN_BOTH_CENTER), i18nManager.getTranslation(ALIGN_BOTH_CENTER)); |
154 |
|
155 |
String alignModeConsoleMsg
|
156 |
= ((EditingProviderServices) providerServices).makeConsoleMessage( |
157 |
i18nManager.getTranslation(ALIGN_MODE), alignModeOptions); |
158 |
|
159 |
this.alignModeParameter
|
160 |
= new DefaultEditingServiceParameter(ALIGN_MODE,
|
161 |
alignModeConsoleMsg, |
162 |
alignModeOptions, |
163 |
i18nManager.getTranslation(SHORT_ALIGN_BOTH_CENTER), |
164 |
true, TYPE.OPTION);
|
165 |
|
166 |
Map<String, String> doCopyOptions = new LinkedHashMap<>(); |
167 |
|
168 |
doCopyOptions.put(i18nManager.getTranslation(SHORT_YES), |
169 |
DO_COPY); |
170 |
doCopyOptions.put(i18nManager.getTranslation(SHORT_NO), |
171 |
DELETE_ORIGINAL_GEOMETRIES); |
172 |
|
173 |
String consoleMsg
|
174 |
= ((EditingProviderServices) providerServices).makeConsoleMessage(DO_COPY_QUESTION, doCopyOptions); |
175 |
|
176 |
this.doCopyParameter
|
177 |
= new DefaultEditingServiceParameter(DO_COPY,
|
178 |
consoleMsg, doCopyOptions, i18nManager.getTranslation(SHORT_YES), true, TYPE.OPTION);
|
179 |
|
180 |
this.values = new HashMap<>(); |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public String getName() { |
185 |
return AlignEdgesEditingProviderFactory.PROVIDER_NAME;
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public EditingServiceParameter next() {
|
190 |
if (values.get(geometryBaseParameter) == null) { |
191 |
return this.geometryBaseParameter; |
192 |
} else if (values.get(alignModeParameter) == null) { |
193 |
return this.alignModeParameter; |
194 |
} else if (values.get(doCopyParameter) == null) { |
195 |
return this.doCopyParameter; |
196 |
} else if (values.get(featureToAlignParameter) == null) { |
197 |
return this.featureToAlignParameter; |
198 |
} |
199 |
return null; |
200 |
} |
201 |
|
202 |
@Override
|
203 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
204 |
throws DrawServiceException {
|
205 |
DefaultDrawingStatus geometries = new DefaultDrawingStatus();
|
206 |
|
207 |
EditingProviderManager editingProviderManager |
208 |
= EditingProviderLocator.getProviderManager(); |
209 |
ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
|
210 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
211 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
212 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
213 |
|
214 |
EditingProviderServices editingProviderServices |
215 |
= (EditingProviderServices) getProviderServices(); |
216 |
|
217 |
try {
|
218 |
int subtype = editingProviderServices.getSubType(featureStore);
|
219 |
if (values != null) { |
220 |
Geometry geometryBaseValue = (Geometry) values.get(geometryBaseParameter); |
221 |
String alignModeValue = (String) values.get(alignModeParameter); |
222 |
Boolean doCopyValue = (Boolean) values.get(doCopyParameter); |
223 |
if (geometryBaseValue != null) { |
224 |
if (geometryBaseValue.getGeometryType().isTypeOf(Geometry.TYPES.POINT) ||
|
225 |
geometryBaseValue.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
226 |
geometries.addStatus(geometryBaseValue, auxiliaryPointSymbolEditing, "");
|
227 |
} else if (geometryBaseValue.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) || |
228 |
geometryBaseValue.getGeometryType().isTypeOf(Geometry.TYPES.MULTICURVE)) { |
229 |
geometries.addStatus(geometryBaseValue, auxiliaryLineSymbolEditing, "");
|
230 |
} else {
|
231 |
geometries.addStatus(geometryBaseValue, polygonSymbolEditing, "");
|
232 |
} |
233 |
|
234 |
if (alignModeValue != null && doCopyValue != null) { |
235 |
Geometry geom = getGeometryToAlign(mousePosition); //getFeatureToAlign(mousePosition);
|
236 |
if (geom != null) { |
237 |
if (geom.getGeometryType().isTypeOf(Geometry.TYPES.POINT) ||
|
238 |
geom.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
239 |
geometries.addStatus(geom, auxiliaryPointSymbolEditing, "");
|
240 |
} else if (geom.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) || |
241 |
geom.getGeometryType().isTypeOf(Geometry.TYPES.MULTICURVE)) { |
242 |
geometries.addStatus(geom, auxiliaryLineSymbolEditing, "");
|
243 |
} else if (geom.getGeometryType().isTypeOf(Geometry.TYPES.SURFACE)) { |
244 |
geometries.addStatus(geom, polygonSymbolEditing, "");
|
245 |
} |
246 |
} |
247 |
Geometry alignedGeom = align(geometryBaseValue, geom, alignModeValue); |
248 |
if (alignedGeom != null) { |
249 |
|
250 |
Point basePoint = getAlignPoint(geometryBaseValue, alignModeValue);
|
251 |
Point geomPoint = getAlignPoint(alignedGeom, alignModeValue);
|
252 |
Line alignLine = GeometryUtils.createLine(basePoint, geomPoint, subtype);
|
253 |
geometries.addStatus(alignLine, auxiliaryLineSymbolEditing, "");
|
254 |
|
255 |
if (alignedGeom.getGeometryType().isTypeOf(Geometry.TYPES.POINT) ||
|
256 |
alignedGeom.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
257 |
geometries.addStatus(alignedGeom, auxiliaryPointSymbolEditing, "");
|
258 |
} else if (alignedGeom.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) || |
259 |
alignedGeom.getGeometryType().isTypeOf(Geometry.TYPES.MULTICURVE)) { |
260 |
geometries.addStatus(alignedGeom, lineSymbolEditing, "");
|
261 |
} else if (alignedGeom.getGeometryType().isTypeOf(Geometry.TYPES.SURFACE)) { |
262 |
geometries.addStatus(alignedGeom, polygonSymbolEditing, "");
|
263 |
} |
264 |
} |
265 |
} |
266 |
} |
267 |
} |
268 |
} catch (Exception e) { |
269 |
throw new DrawServiceException(e); |
270 |
} |
271 |
|
272 |
return geometries;
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
public List<EditingServiceParameter> getParameters() { |
277 |
List<EditingServiceParameter> parameters
|
278 |
= new ArrayList<>(); |
279 |
parameters.add(geometryBaseParameter); |
280 |
parameters.add(alignModeParameter); |
281 |
parameters.add(doCopyParameter); |
282 |
parameters.add(featureToAlignParameter); |
283 |
return parameters;
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public boolean isEnabled(EditingServiceParameter parameter) { |
288 |
return true; |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException { |
293 |
validateAndInsertValue(parameter, value); |
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void setValue(Object value) throws InvalidEntryException { |
298 |
EditingServiceParameter param = next(); |
299 |
validateAndInsertValue(param, value); |
300 |
} |
301 |
|
302 |
private void validateAndInsertValue(EditingServiceParameter param, |
303 |
Object value) throws InvalidEntryException { |
304 |
if (param == geometryBaseParameter) {
|
305 |
if (value instanceof Point) { |
306 |
Point point = (Point) value; |
307 |
|
308 |
Geometry geom = getGeometryOfVisibleLayers(point); |
309 |
if (geom != null) { |
310 |
values.put(param, geom); |
311 |
return;
|
312 |
} |
313 |
} |
314 |
} else if (param == featureToAlignParameter) { |
315 |
if (value instanceof Point) { |
316 |
Point point = (Point) value; |
317 |
|
318 |
Feature feat = getFeatureToAlign(point); |
319 |
if (feat != null) { |
320 |
values.put(param, feat); |
321 |
return;
|
322 |
} |
323 |
} |
324 |
} else if (param == alignModeParameter) { |
325 |
if (value instanceof String) { |
326 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
327 |
String alignTrim = ((String) value).trim(); |
328 |
if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_UP))
|
329 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_UP)) |
330 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_UP), alignTrim)) { |
331 |
values.put(param, ALIGN_UP); |
332 |
savedAlignMode = ALIGN_UP; |
333 |
return;
|
334 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_DOWN)) |
335 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_DOWN)) |
336 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_DOWN), alignTrim)) { |
337 |
values.put(param, ALIGN_DOWN); |
338 |
savedAlignMode = ALIGN_DOWN; |
339 |
return;
|
340 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_LEFT)) |
341 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_LEFT)) |
342 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_LEFT), alignTrim)) { |
343 |
values.put(param, ALIGN_LEFT); |
344 |
savedAlignMode = ALIGN_LEFT; |
345 |
return;
|
346 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_RIGHT)) |
347 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_RIGHT)) |
348 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_RIGHT), alignTrim)) { |
349 |
values.put(param, ALIGN_RIGHT); |
350 |
savedAlignMode = ALIGN_RIGHT; |
351 |
return;
|
352 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_HORIZONTAL_CENTER)) |
353 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_HORIZONTAL_CENTER)) |
354 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_HORIZONTAL_CENTER), alignTrim)) { |
355 |
values.put(param, ALIGN_HORIZONTAL_CENTER); |
356 |
savedAlignMode = ALIGN_HORIZONTAL_CENTER; |
357 |
return;
|
358 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_VERTICAL_CENTER)) |
359 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_VERTICAL_CENTER)) |
360 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_VERTICAL_CENTER), alignTrim)) { |
361 |
values.put(param, ALIGN_VERTICAL_CENTER); |
362 |
savedAlignMode = ALIGN_VERTICAL_CENTER; |
363 |
return;
|
364 |
} else if (StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(ALIGN_BOTH_CENTER)) |
365 |
|| StringUtils.equalsIgnoreCase(alignTrim, i18nManager.getTranslation(SHORT_ALIGN_BOTH_CENTER)) |
366 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(ALIGN_BOTH_CENTER), alignTrim)) { |
367 |
values.put(param, ALIGN_BOTH_CENTER); |
368 |
savedAlignMode = ALIGN_BOTH_CENTER; |
369 |
return;
|
370 |
} |
371 |
} |
372 |
} else if (param == doCopyParameter) { |
373 |
if (value instanceof String) { |
374 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
375 |
String doCopyTrim = ((String) value).trim(); |
376 |
if (StringUtils.equalsIgnoreCase(doCopyTrim, YES)
|
377 |
|| StringUtils.equalsIgnoreCase(doCopyTrim, SHORT_YES) |
378 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(YES), doCopyTrim) |
379 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(DO_COPY), doCopyTrim)) { |
380 |
values.put(param, true);
|
381 |
savedDoCopy = true;
|
382 |
return;
|
383 |
} else if (StringUtils.equalsIgnoreCase(doCopyTrim, NO) |
384 |
|| StringUtils.equalsIgnoreCase(doCopyTrim, SHORT_NO) |
385 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(NO), doCopyTrim) |
386 |
|| StringUtils.startsWithIgnoreCase(i18nManager.getTranslation(DELETE_ORIGINAL_GEOMETRIES), doCopyTrim)) { |
387 |
values.put(param, false);
|
388 |
savedDoCopy = false;
|
389 |
return;
|
390 |
} |
391 |
} |
392 |
} |
393 |
throw new InvalidEntryException(null); |
394 |
} |
395 |
|
396 |
private Geometry getGeometryOfVisibleLayers(Point point) { |
397 |
EditingProviderServices editingProviderServices = (EditingProviderServices) getProviderServices(); |
398 |
Geometry geometry = editingProviderServices.getGeometryOfVisibleLayers(point, featureStore, mapContext); |
399 |
return geometry;
|
400 |
} |
401 |
|
402 |
private Feature getFeatureToAlign(Point point) { |
403 |
EditingProviderServices editingProviderServices = (EditingProviderServices) getProviderServices(); |
404 |
Feature feature = editingProviderServices.getFeature(point, featureStore, mapContext); |
405 |
return feature;
|
406 |
} |
407 |
|
408 |
private Geometry getGeometryToAlign(Point point) { |
409 |
EditingProviderServices editingProviderServices = (EditingProviderServices) getProviderServices(); |
410 |
return editingProviderServices.getGeometry(point, featureStore, mapContext);
|
411 |
} |
412 |
|
413 |
@Override
|
414 |
public Geometry finish() throws FinishServiceException { |
415 |
return null; |
416 |
} |
417 |
|
418 |
@Override
|
419 |
public void finishAndStore() throws FinishServiceException { |
420 |
Geometry geometryBaseValue = (Geometry) values.get(geometryBaseParameter); |
421 |
Feature featureValue = (Feature) values.get(featureToAlignParameter); |
422 |
String alignModeValue = (String) values.get(alignModeParameter); |
423 |
Boolean doCopy = (Boolean) values.get(doCopyParameter); |
424 |
|
425 |
EditingProviderServices editingProviderServices |
426 |
= (EditingProviderServices) getProviderServices(); |
427 |
|
428 |
try {
|
429 |
|
430 |
Geometry geom; |
431 |
geom = align(geometryBaseValue, featureValue.getDefaultGeometry(), alignModeValue); |
432 |
|
433 |
if (doCopy) {
|
434 |
// Se crea una feature nueva copiando los valores de
|
435 |
// la feature original excepto aquellos que sean PK
|
436 |
EditableFeature editableFeature |
437 |
= editingProviderServices |
438 |
.getFeatureCopyWithoutPK(featureStore, |
439 |
featureValue); |
440 |
editableFeature.setDefaultGeometry(geom); |
441 |
editingProviderServices |
442 |
.insertFeatureIntoFeatureStore(editableFeature, |
443 |
featureStore); |
444 |
} else {
|
445 |
// Se sustituye la geometr?a original por la
|
446 |
// calculada
|
447 |
EditableFeature editableFeature |
448 |
= featureValue.getEditable(); |
449 |
editableFeature.setDefaultGeometry(geom); |
450 |
((EditingProviderServices) getProviderServices()) |
451 |
.updateFeatureInFeatureStore(editableFeature, |
452 |
featureStore); |
453 |
|
454 |
} |
455 |
|
456 |
} catch (Exception e) { |
457 |
throw new FinishServiceException(e); |
458 |
} |
459 |
} |
460 |
|
461 |
private Geometry align(Geometry geometryBase, Geometry geometry, String alignMode) { |
462 |
|
463 |
if (geometryBase != null && geometry != null) { |
464 |
geometry = geometry.cloneGeometry(); |
465 |
AffineTransform at = getAffineTransform(geometryBase, geometry, alignMode);
|
466 |
geometry.transform(at); |
467 |
} |
468 |
return geometry;
|
469 |
} |
470 |
|
471 |
private Point getAlignPoint(Geometry geometry, String alignMode) { |
472 |
|
473 |
Point point = null; |
474 |
if (geometry != null) { |
475 |
if (geometry instanceof Point) { |
476 |
point = (Point) geometry;
|
477 |
} else {
|
478 |
Envelope env = geometry.getEnvelope(); |
479 |
switch (alignMode) {
|
480 |
case ALIGN_UP:
|
481 |
point = GeometryUtils.createPoint(env.getCenter(DIMENSIONS.X), env.getMaximum(DIMENSIONS.Y)); |
482 |
break;
|
483 |
case ALIGN_DOWN:
|
484 |
point = GeometryUtils.createPoint(env.getCenter(DIMENSIONS.X), env.getMinimum(DIMENSIONS.Y)); |
485 |
break;
|
486 |
case ALIGN_LEFT:
|
487 |
point = GeometryUtils.createPoint(env.getMinimum(DIMENSIONS.X), env.getCenter(DIMENSIONS.Y)); |
488 |
break;
|
489 |
case ALIGN_RIGHT:
|
490 |
point = GeometryUtils.createPoint(env.getMaximum(DIMENSIONS.X), env.getCenter(DIMENSIONS.Y)); |
491 |
break;
|
492 |
case ALIGN_HORIZONTAL_CENTER:
|
493 |
point = GeometryUtils.createPoint(env.getCenter(DIMENSIONS.X), env.getCenter(DIMENSIONS.Y)); |
494 |
break;
|
495 |
case ALIGN_VERTICAL_CENTER:
|
496 |
point = GeometryUtils.createPoint(env.getCenter(DIMENSIONS.X), env.getCenter(DIMENSIONS.Y)); |
497 |
break;
|
498 |
case ALIGN_BOTH_CENTER:
|
499 |
point = GeometryUtils.createPoint(env.getCenter(DIMENSIONS.X), env.getCenter(DIMENSIONS.Y)); |
500 |
break;
|
501 |
} |
502 |
} |
503 |
} |
504 |
return point;
|
505 |
} |
506 |
|
507 |
private AffineTransform getAffineTransform(Geometry geometryBase, Geometry geometry, String alignMode) { |
508 |
|
509 |
Point basePoint = getAlignPoint(geometryBase, alignMode);
|
510 |
Point geomPoint = getAlignPoint(geometry, alignMode);
|
511 |
AffineTransform at = null; |
512 |
|
513 |
switch (alignMode) {
|
514 |
case ALIGN_UP:
|
515 |
at = new AffineTransform( |
516 |
1d, 0d, |
517 |
0d, 1d, |
518 |
0d, basePoint.getY() - geomPoint.getY());
|
519 |
|
520 |
break;
|
521 |
case ALIGN_DOWN:
|
522 |
at = new AffineTransform( |
523 |
1d, 0d, |
524 |
0d, 1d, |
525 |
0d, basePoint.getY() - geomPoint.getY());
|
526 |
|
527 |
break;
|
528 |
case ALIGN_LEFT:
|
529 |
at = new AffineTransform( |
530 |
1d, 0d, |
531 |
0d, 1d, |
532 |
basePoint.getX() - geomPoint.getX(), 0d);
|
533 |
|
534 |
break;
|
535 |
case ALIGN_RIGHT:
|
536 |
at = new AffineTransform( |
537 |
1d, 0d, |
538 |
0d, 1d, |
539 |
basePoint.getX() - geomPoint.getX(), 0d);
|
540 |
|
541 |
break;
|
542 |
case ALIGN_HORIZONTAL_CENTER:
|
543 |
at = new AffineTransform( |
544 |
1d, 0d, |
545 |
0d, 1d, |
546 |
0d, basePoint.getY() - geomPoint.getY());
|
547 |
|
548 |
break;
|
549 |
case ALIGN_VERTICAL_CENTER:
|
550 |
at = new AffineTransform( |
551 |
1d, 0d, |
552 |
0d, 1d, |
553 |
basePoint.getX() - geomPoint.getX(), 0d);
|
554 |
|
555 |
break;
|
556 |
case ALIGN_BOTH_CENTER:
|
557 |
at = new AffineTransform( |
558 |
1d, 0d, |
559 |
0d, 1d, |
560 |
basePoint.getX() - geomPoint.getX(), basePoint.getY() - geomPoint.getY()); |
561 |
|
562 |
break;
|
563 |
} |
564 |
return at;
|
565 |
} |
566 |
|
567 |
@Override
|
568 |
public void activate() { |
569 |
//Se limpia todo pero se recuperan los valores guardados de alignMode y doCopy
|
570 |
this.values.clear();
|
571 |
if(savedAlignMode != null){ |
572 |
this.values.put(alignModeParameter, savedAlignMode);
|
573 |
} |
574 |
if(savedDoCopy != null){ |
575 |
this.values.put(doCopyParameter, savedDoCopy);
|
576 |
} |
577 |
} |
578 |
|
579 |
@Override
|
580 |
public void start() throws StartServiceException { |
581 |
|
582 |
} |
583 |
|
584 |
@Override
|
585 |
public void stop() throws StopServiceException { |
586 |
//Se limpiar solo la geometr?a a alinear
|
587 |
this.values.remove(featureToAlignParameter);
|
588 |
} |
589 |
|
590 |
@Override
|
591 |
public Object getValue(EditingServiceParameter parameter) { |
592 |
return values != null ? values.get(parameter) : null; |
593 |
} |
594 |
|
595 |
} |