root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / ExploitCADTool.java @ 38539
History | View | Annotate | Download (16.6 KB)
1 | 37138 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | 5884 | caballero | *
|
3 | 37138 | cordinyana | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | 5884 | caballero | * 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 | 37138 | cordinyana | *
|
11 | 5884 | caballero | * 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 | 37138 | cordinyana | *
|
16 | 5884 | caballero | * 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 | 37138 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 5884 | caballero | */
|
22 | 29616 | jpiera | package org.gvsig.editing.gui.cad.tools; |
23 | 5884 | caballero | |
24 | import java.awt.event.InputEvent; |
||
25 | import java.awt.geom.PathIterator; |
||
26 | 15612 | vcaballero | import java.awt.geom.Point2D; |
27 | 5884 | caballero | |
28 | 38243 | jldominguez | import javax.swing.JOptionPane; |
29 | |||
30 | import org.slf4j.Logger; |
||
31 | import org.slf4j.LoggerFactory; |
||
32 | |||
33 | 29616 | jpiera | import org.gvsig.andami.PluginServices; |
34 | import org.gvsig.andami.messages.NotificationManager; |
||
35 | 38243 | jldominguez | import org.gvsig.app.ApplicationLocator; |
36 | 29616 | jpiera | import org.gvsig.editing.CADExtension; |
37 | import org.gvsig.editing.gui.cad.DefaultCADTool; |
||
38 | import org.gvsig.editing.gui.cad.exception.CommandException; |
||
39 | import org.gvsig.editing.gui.cad.tools.smc.ExploitCADToolContext; |
||
40 | import org.gvsig.editing.layers.VectorialLayerEdited; |
||
41 | 24500 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
42 | import org.gvsig.fmap.dal.exception.ReadException; |
||
43 | 24490 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeature; |
44 | import org.gvsig.fmap.dal.feature.Feature; |
||
45 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
46 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
47 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
48 | 21668 | vcaballero | import org.gvsig.fmap.geom.Geometry; |
49 | 38096 | jjdelcerro | import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
50 | 21668 | vcaballero | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
51 | 30335 | jpiera | import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
52 | 37138 | cordinyana | import org.gvsig.tools.dispose.DisposableIterator; |
53 | 21668 | vcaballero | |
54 | 38096 | jjdelcerro | //import com.vividsolutions.jts.geom.GeometryCollection;
|
55 | |||
56 | 5884 | caballero | /**
|
57 | * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
|
||
58 | 8938 | caballero | * componen, implementado para polil?neas y pol?gonos.
|
59 | 37138 | cordinyana | *
|
60 | 5884 | caballero | * @author Vicente Caballero Navarro
|
61 | */
|
||
62 | public class ExploitCADTool extends DefaultCADTool { |
||
63 | 38243 | jldominguez | |
64 | private static Logger logger = LoggerFactory.getLogger(ExploitCADTool.class); |
||
65 | 37138 | cordinyana | |
66 | 26921 | jpiera | protected ExploitCADToolContext _fsm;
|
67 | 27270 | vcaballero | |
68 | 5884 | caballero | /**
|
69 | * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
||
70 | * carga previa a la utilizaci?n de la herramienta.
|
||
71 | */
|
||
72 | public void init() { |
||
73 | _fsm = new ExploitCADToolContext(this); |
||
74 | } |
||
75 | |||
76 | 37138 | cordinyana | /*
|
77 | * (non-Javadoc)
|
||
78 | *
|
||
79 | * @see
|
||
80 | * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
||
81 | * .layers.FBitSet, double, double)
|
||
82 | 5884 | caballero | */
|
83 | public void transition(double x, double y, InputEvent event) { |
||
84 | _fsm.addPoint(x, y, event); |
||
85 | } |
||
86 | |||
87 | 37138 | cordinyana | /*
|
88 | * (non-Javadoc)
|
||
89 | *
|
||
90 | * @see
|
||
91 | * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
||
92 | * .layers.FBitSet, double)
|
||
93 | 5884 | caballero | */
|
94 | public void transition(double d) { |
||
95 | _fsm.addValue(d); |
||
96 | } |
||
97 | |||
98 | 37138 | cordinyana | /*
|
99 | * (non-Javadoc)
|
||
100 | *
|
||
101 | * @see
|
||
102 | * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
||
103 | * .layers.FBitSet, java.lang.String)
|
||
104 | 5884 | caballero | */
|
105 | public void transition(String s) throws CommandException { |
||
106 | 37138 | cordinyana | if (!super.changeCommand(s)) { |
107 | _fsm.addOption(s); |
||
108 | } |
||
109 | 5884 | caballero | } |
110 | |||
111 | /**
|
||
112 | * DOCUMENT ME!
|
||
113 | */
|
||
114 | public void selection() { |
||
115 | 37138 | cordinyana | FeatureSet selection = null;
|
116 | try {
|
||
117 | selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
||
118 | 24263 | vcaballero | |
119 | 37138 | cordinyana | if (selection.getSize() == 0 |
120 | && !CADExtension |
||
121 | .getCADTool() |
||
122 | .getClass() |
||
123 | .getName() |
||
124 | .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
||
125 | CADExtension.setCADTool("_selection", false); |
||
126 | ((SelectionCADTool) CADExtension.getCADTool()) |
||
127 | .setNextTool("_exploit");
|
||
128 | } |
||
129 | } catch (ReadException e) {
|
||
130 | // TODO Auto-generated catch block
|
||
131 | e.printStackTrace(); |
||
132 | } catch (DataException e) {
|
||
133 | // TODO Auto-generated catch block
|
||
134 | e.printStackTrace(); |
||
135 | } |
||
136 | 5884 | caballero | } |
137 | |||
138 | /**
|
||
139 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
140 | * editableFeatureSource que ya estar? creado.
|
||
141 | 37138 | cordinyana | *
|
142 | * @param x
|
||
143 | * par?metro x del punto que se pase en esta transici?n.
|
||
144 | * @param y
|
||
145 | * par?metro y del punto que se pase en esta transici?n.
|
||
146 | 5884 | caballero | */
|
147 | 37138 | cordinyana | public void addPoint(double x, double y, InputEvent event) { |
148 | 5884 | caballero | } |
149 | |||
150 | /**
|
||
151 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
152 | * encontremos.
|
||
153 | 37138 | cordinyana | *
|
154 | * @param g
|
||
155 | * Graphics sobre el que dibujar.
|
||
156 | * @param x
|
||
157 | * par?metro x del punto que se pase para dibujar.
|
||
158 | * @param y
|
||
159 | * par?metro x del punto que se pase para dibujar.
|
||
160 | 5884 | caballero | */
|
161 | 30335 | jpiera | public void drawOperation(MapControlDrawer renderer, double x, double y) { |
162 | 5884 | caballero | } |
163 | 37138 | cordinyana | |
164 | 5884 | caballero | public void exploit() { |
165 | 37138 | cordinyana | VectorialLayerEdited vle = getVLE(); |
166 | DisposableIterator iterator = null;
|
||
167 | try {
|
||
168 | FeatureStore featureStore = vle.getFeatureStore(); |
||
169 | iterator = |
||
170 | ((FeatureSelection) featureStore.getSelection()).iterator(); |
||
171 | FeatureSelection newSelection = |
||
172 | featureStore.createFeatureSelection(); |
||
173 | // ArrayList selectedRowAux = new ArrayList();
|
||
174 | featureStore.beginEditingGroup(getName()); |
||
175 | 27270 | vcaballero | |
176 | 37138 | cordinyana | while (iterator.hasNext()) {
|
177 | Feature feature = (Feature) iterator.next(); |
||
178 | Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
||
179 | featureStore.delete(feature); |
||
180 | 38096 | jjdelcerro | if (geom instanceof MultiPrimitive) { |
181 | 37138 | cordinyana | exploitGeometryCollection(feature, newSelection); |
182 | } else {
|
||
183 | 37328 | cordinyana | if (geom.getType() == CURVE) {
|
184 | 37138 | cordinyana | exploitLine(feature, newSelection); |
185 | } else
|
||
186 | 37328 | cordinyana | if (geom.getType() == SURFACE) {
|
187 | 37138 | cordinyana | exploitPolygon(feature, newSelection); |
188 | } |
||
189 | } |
||
190 | 15605 | vcaballero | |
191 | 37138 | cordinyana | } |
192 | // clearSelection();
|
||
193 | featureStore.setSelection(newSelection); |
||
194 | featureStore.endEditingGroup(); |
||
195 | } catch (DataException e) {
|
||
196 | NotificationManager.addError(e.getMessage(), e); |
||
197 | } finally {
|
||
198 | if (iterator != null) { |
||
199 | iterator.dispose(); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | 24263 | vcaballero | |
204 | 37138 | cordinyana | private void exploitGeometryCollection(Feature feature, |
205 | FeatureSelection selectedRowAux) { |
||
206 | Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
||
207 | 38096 | jjdelcerro | MultiPrimitive gc = (MultiPrimitive) geom; |
208 | 37138 | cordinyana | int numGeoms = gc.getPrimitivesNumber();
|
209 | FeatureStore featureStore; |
||
210 | 38243 | jldominguez | Feature f = null;
|
211 | 37138 | cordinyana | try {
|
212 | featureStore = getVLE().getFeatureStore(); |
||
213 | for (int i = 0; i < numGeoms; i++) { |
||
214 | Geometry primitiveGeom = gc.getPrimitiveAt(i); |
||
215 | 38243 | jldominguez | f = insertGeometry(primitiveGeom, feature); |
216 | /*
|
||
217 | 37138 | cordinyana | EditableFeature eFeature =
|
218 | featureStore.createNewFeature(feature.getType(), feature);
|
||
219 | eFeature.setGeometry(featureStore.getDefaultFeatureType()
|
||
220 | .getDefaultGeometryAttributeName(), primitiveGeom);
|
||
221 | 38243 | jldominguez | */
|
222 | selectedRowAux.select(f); |
||
223 | 37138 | cordinyana | } |
224 | } catch (ReadException e) {
|
||
225 | // TODO Auto-generated catch block
|
||
226 | e.printStackTrace(); |
||
227 | } catch (DataException e) {
|
||
228 | // TODO Auto-generated catch block
|
||
229 | e.printStackTrace(); |
||
230 | } |
||
231 | refresh(); |
||
232 | } |
||
233 | 5884 | caballero | |
234 | 37138 | cordinyana | private void exploitLine(Feature feature, FeatureSelection selectedRowAux) { |
235 | GeneralPathX newGp1 = new GeneralPathX();
|
||
236 | // DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
|
||
237 | // .cloneRow();
|
||
238 | // Point2D firstPoint=null;
|
||
239 | // Point2D lastPoint=null;
|
||
240 | // Object[] attributes=new Object[feature.size()];
|
||
241 | // for (int j = 0; j < feature.size(); j++) {
|
||
242 | // attributes[j]=feature.get(j);
|
||
243 | // }
|
||
244 | FeatureStore featureStore; |
||
245 | // try {
|
||
246 | try {
|
||
247 | featureStore = getVLE().getFeatureStore(); |
||
248 | 5884 | caballero | |
249 | 37138 | cordinyana | // EditableFeature
|
250 | // eFeature=featureStore.createNewFeature(feature.getType(),
|
||
251 | // feature);
|
||
252 | PathIterator theIterator =
|
||
253 | (feature.getDefaultGeometry()).getPathIterator(null,
|
||
254 | geomManager.getFlatness()); |
||
255 | double[] theData = new double[6]; |
||
256 | int theType;
|
||
257 | int numParts = 0; |
||
258 | Point2D initialPoint = null; |
||
259 | 38243 | jldominguez | Feature f = null;
|
260 | |||
261 | 37138 | cordinyana | while (!theIterator.isDone()) {
|
262 | theType = theIterator.currentSegment(theData); |
||
263 | switch (theType) {
|
||
264 | 5884 | caballero | |
265 | 37138 | cordinyana | case PathIterator.SEG_MOVETO: |
266 | numParts++; |
||
267 | newGp1 = new GeneralPathX();
|
||
268 | // firstPoint=new Point2D.Double(theData[0], theData[1]);
|
||
269 | initialPoint = new Point2D.Double(theData[0], theData[1]); |
||
270 | newGp1.moveTo(theData[0], theData[1]); |
||
271 | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
||
272 | break;
|
||
273 | |||
274 | case PathIterator.SEG_LINETO: |
||
275 | newGp1.lineTo(theData[0], theData[1]); |
||
276 | Geometry geomLine = createCurve(newGp1); |
||
277 | // DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
|
||
278 | // dfLine.setGeometry(geomLine);
|
||
279 | // EditableFeature
|
||
280 | // eFeature=featureStore.createNewFeature(feature.getType(),
|
||
281 | // feature);
|
||
282 | // eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
|
||
283 | 38243 | jldominguez | f = insertGeometry(geomLine, feature); |
284 | 37138 | cordinyana | // featureStore.insert(eFeature);
|
285 | selectedRowAux.select(f); |
||
286 | newGp1 = new GeneralPathX();
|
||
287 | newGp1.moveTo(theData[0], theData[1]); |
||
288 | // lastPoint=new Point2D.Double(theData[0], theData[1]);
|
||
289 | break;
|
||
290 | case PathIterator.SEG_CLOSE: |
||
291 | if (initialPoint != null) { |
||
292 | newGp1.lineTo(initialPoint.getX(), initialPoint.getY()); |
||
293 | geomLine = createCurve(newGp1); |
||
294 | // dfLine = (DefaultFeature) df.cloneRow();
|
||
295 | // dfLine.setGeometry(geomLine);
|
||
296 | 38243 | jldominguez | /*
|
297 | 37138 | cordinyana | EditableFeature eFeatureClose =
|
298 | featureStore.createNewFeature(feature.getType(),
|
||
299 | feature);
|
||
300 | eFeatureClose.setGeometry(featureStore
|
||
301 | .getDefaultFeatureType()
|
||
302 | .getDefaultGeometryAttributeName(), geomLine);
|
||
303 | selectedRowAux.select(eFeatureClose);
|
||
304 | 38243 | jldominguez | */
|
305 | |||
306 | f = insertGeometry(geomLine, feature); |
||
307 | selectedRowAux.select(f); |
||
308 | |||
309 | 37138 | cordinyana | newGp1 = new GeneralPathX();
|
310 | newGp1.moveTo(initialPoint.getX(), initialPoint.getY()); |
||
311 | } |
||
312 | break;
|
||
313 | } // end switch
|
||
314 | |||
315 | theIterator.next(); |
||
316 | } // end while loop
|
||
317 | } catch (ReadException e) {
|
||
318 | // TODO Auto-generated catch block
|
||
319 | e.printStackTrace(); |
||
320 | } catch (DataException e) {
|
||
321 | // TODO Auto-generated catch block
|
||
322 | e.printStackTrace(); |
||
323 | } |
||
324 | refresh(); |
||
325 | 5884 | caballero | } |
326 | 24263 | vcaballero | |
327 | 37138 | cordinyana | private void exploitPolygon(Feature feature, FeatureSelection selectedRowAux) { |
328 | GeneralPathX newGp1 = null;
|
||
329 | // Object[] attributes=new Object[feature.size()];
|
||
330 | // for (int j = 0; j < feature.size(); j++) {
|
||
331 | // attributes[j]=feature.get(j);
|
||
332 | // }
|
||
333 | 24263 | vcaballero | |
334 | 37138 | cordinyana | FeatureStore featureStore; |
335 | 38243 | jldominguez | Feature added_feat = null;
|
336 | 37138 | cordinyana | try {
|
337 | featureStore = getVLE().getFeatureStore(); |
||
338 | 24263 | vcaballero | |
339 | 37138 | cordinyana | PathIterator theIterator =
|
340 | (feature.getDefaultGeometry()).getPathIterator(null,
|
||
341 | geomManager.getFlatness()); |
||
342 | double[] theData = new double[6]; |
||
343 | int theType;
|
||
344 | int numParts = 0; |
||
345 | Point2D initialPoint = null; |
||
346 | while (!theIterator.isDone()) {
|
||
347 | theType = theIterator.currentSegment(theData); |
||
348 | switch (theType) {
|
||
349 | 5884 | caballero | |
350 | 37138 | cordinyana | case PathIterator.SEG_MOVETO: |
351 | numParts++; |
||
352 | if (newGp1 != null) { |
||
353 | Geometry geomLine = createCurve(newGp1); |
||
354 | 38243 | jldominguez | |
355 | /*
|
||
356 | 37138 | cordinyana | EditableFeature eFeature =
|
357 | featureStore.createNewFeature(feature.getType(),
|
||
358 | feature);
|
||
359 | eFeature.setGeometry(featureStore
|
||
360 | .getDefaultFeatureType()
|
||
361 | .getDefaultGeometryAttributeName(), geomLine);
|
||
362 | 38243 | jldominguez | */
|
363 | |||
364 | added_feat = insertGeometry(geomLine, feature); |
||
365 | selectedRowAux.select(added_feat); |
||
366 | 37138 | cordinyana | } |
367 | newGp1 = new GeneralPathX();
|
||
368 | initialPoint = new Point2D.Double(theData[0], theData[1]); |
||
369 | newGp1.moveTo(theData[0], theData[1]); |
||
370 | break;
|
||
371 | 5884 | caballero | |
372 | 37138 | cordinyana | case PathIterator.SEG_LINETO: |
373 | newGp1.lineTo(theData[0], theData[1]); |
||
374 | // newGp1 = new GeneralPathX();
|
||
375 | // newGp1.moveTo(theData[0], theData[1]);
|
||
376 | break;
|
||
377 | 5884 | caballero | |
378 | 37138 | cordinyana | case PathIterator.SEG_QUADTO: |
379 | newGp1.quadTo(theData[0], theData[1], theData[2], |
||
380 | theData[3]);
|
||
381 | // newGp1 = new GeneralPathX();
|
||
382 | // newGp1.moveTo(theData[0], theData[1]);
|
||
383 | break;
|
||
384 | case PathIterator.SEG_CUBICTO: |
||
385 | newGp1.curveTo(theData[0], theData[1], theData[2], |
||
386 | theData[3], theData[4], theData[5]); |
||
387 | // newGp1 = new GeneralPathX();
|
||
388 | // newGp1.moveTo(theData[0], theData[1]);
|
||
389 | break;
|
||
390 | case PathIterator.SEG_CLOSE: |
||
391 | if (initialPoint != null) { |
||
392 | newGp1.lineTo(initialPoint.getX(), initialPoint.getY()); |
||
393 | } |
||
394 | // System.out.println("aqu?");
|
||
395 | // if (isFirstPart)
|
||
396 | // newGp1.closePath();
|
||
397 | // else
|
||
398 | // newGp2.closePath();
|
||
399 | break;
|
||
400 | } // end switch
|
||
401 | 5884 | caballero | |
402 | 37138 | cordinyana | theIterator.next(); |
403 | } // end while loop
|
||
404 | if (newGp1 != null) { |
||
405 | Geometry geomLine = createCurve(newGp1); |
||
406 | 38243 | jldominguez | /*
|
407 | 37138 | cordinyana | EditableFeature eFeature =
|
408 | 38243 | jldominguez | featureStore.createNewFeature(feature.getType(),
|
409 | feature);
|
||
410 | eFeature.setGeometry(featureStore
|
||
411 | .getDefaultFeatureType()
|
||
412 | 37138 | cordinyana | .getDefaultGeometryAttributeName(), geomLine);
|
413 | 38243 | jldominguez | */
|
414 | |||
415 | added_feat = insertGeometry(geomLine, feature); |
||
416 | selectedRowAux.select(added_feat); |
||
417 | 37138 | cordinyana | } |
418 | 38243 | jldominguez | } catch (Throwable e) { |
419 | logger.info("While doing exploit of polygon.", e);
|
||
420 | ApplicationLocator.getManager().message( |
||
421 | "_Unable_to_exploit_polygon", JOptionPane.ERROR_MESSAGE); |
||
422 | 37138 | cordinyana | // TODO Auto-generated catch block
|
423 | 38243 | jldominguez | // e.printStackTrace();
|
424 | 37138 | cordinyana | } |
425 | refresh(); |
||
426 | 5884 | caballero | } |
427 | 37138 | cordinyana | |
428 | 5884 | caballero | /**
|
429 | 37138 | cordinyana | * Add a diferent option.
|
430 | *
|
||
431 | * @param s
|
||
432 | * Diferent option.
|
||
433 | */
|
||
434 | 5884 | caballero | public void addOption(String s) { |
435 | } |
||
436 | |||
437 | /*
|
||
438 | 37138 | cordinyana | * (non-Javadoc)
|
439 | *
|
||
440 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
441 | */
|
||
442 | 5884 | caballero | public void addValue(double d) { |
443 | |||
444 | } |
||
445 | |||
446 | 37138 | cordinyana | public String getName() { |
447 | return PluginServices.getText(this, "exploit_"); |
||
448 | } |
||
449 | 5884 | caballero | |
450 | 37138 | cordinyana | public String toString() { |
451 | return "_exploit"; |
||
452 | } |
||
453 | |||
454 | 37328 | cordinyana | @Override
|
455 | protected int[] getSupportedGeometryTypes() { |
||
456 | return new int[] { CURVE, SURFACE }; |
||
457 | 37138 | cordinyana | } |
458 | 37328 | cordinyana | |
459 | 5884 | caballero | } |