Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / ExploitCADTool.java @ 37138

History | View | Annotate | Download (19 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.geom.PathIterator;
26
import java.awt.geom.Point2D;
27

    
28
import com.vividsolutions.jts.geom.GeometryCollection;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.editing.CADExtension;
33
import org.gvsig.editing.gui.cad.DefaultCADTool;
34
import org.gvsig.editing.gui.cad.exception.CommandException;
35
import org.gvsig.editing.gui.cad.tools.smc.ExploitCADToolContext;
36
import org.gvsig.editing.layers.VectorialLayerEdited;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSelection;
42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.aggregate.impl.BaseMultiPrimitive;
46
import org.gvsig.fmap.geom.primitive.GeneralPathX;
47
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.tools.dispose.DisposableIterator;
49

    
50
/**
51
 * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
52
 * componen, implementado para polil?neas y pol?gonos.
53
 * 
54
 * @author Vicente Caballero Navarro
55
 */
56
public class ExploitCADTool extends DefaultCADTool {
57

    
58
    protected ExploitCADToolContext _fsm;
59

    
60
    /**
61
     * Crea un nuevo ExploitCADTool.
62
     */
63
    public ExploitCADTool() {
64
    }
65

    
66
    /**
67
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
68
     * carga previa a la utilizaci?n de la herramienta.
69
     */
70
    public void init() {
71
        _fsm = new ExploitCADToolContext(this);
72
    }
73

    
74
    /*
75
     * (non-Javadoc)
76
     * 
77
     * @see
78
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
79
     * .layers.FBitSet, double, double)
80
     */
81
    public void transition(double x, double y, InputEvent event) {
82
        _fsm.addPoint(x, y, event);
83
    }
84

    
85
    /*
86
     * (non-Javadoc)
87
     * 
88
     * @see
89
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
90
     * .layers.FBitSet, double)
91
     */
92
    public void transition(double d) {
93
        _fsm.addValue(d);
94
    }
95

    
96
    /*
97
     * (non-Javadoc)
98
     * 
99
     * @see
100
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
101
     * .layers.FBitSet, java.lang.String)
102
     */
103
    public void transition(String s) throws CommandException {
104
        if (!super.changeCommand(s)) {
105
            _fsm.addOption(s);
106
        }
107
    }
108

    
109
    /**
110
     * DOCUMENT ME!
111
     */
112
    public void selection() {
113
        FeatureSet selection = null;
114
        try {
115
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
116

    
117
            if (selection.getSize() == 0
118
                && !CADExtension
119
                    .getCADTool()
120
                    .getClass()
121
                    .getName()
122
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
123
                CADExtension.setCADTool("_selection", false);
124
                ((SelectionCADTool) CADExtension.getCADTool())
125
                    .setNextTool("_exploit");
126
            }
127
        } catch (ReadException e) {
128
            // TODO Auto-generated catch block
129
            e.printStackTrace();
130
        } catch (DataException e) {
131
            // TODO Auto-generated catch block
132
            e.printStackTrace();
133
        }
134
    }
135

    
136
    /**
137
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
138
     * editableFeatureSource que ya estar? creado.
139
     * 
140
     * @param x
141
     *            par?metro x del punto que se pase en esta transici?n.
142
     * @param y
143
     *            par?metro y del punto que se pase en esta transici?n.
144
     */
145
    public void addPoint(double x, double y, InputEvent event) {
146
    }
147

    
148
    /**
149
     * M?todo para dibujar la lo necesario para el estado en el que nos
150
     * encontremos.
151
     * 
152
     * @param g
153
     *            Graphics sobre el que dibujar.
154
     * @param x
155
     *            par?metro x del punto que se pase para dibujar.
156
     * @param y
157
     *            par?metro x del punto que se pase para dibujar.
158
     */
159
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
160
    }
161

    
162
    public void exploit() {
163
        VectorialLayerEdited vle = getVLE();
164
        DisposableIterator iterator = null;
165
        try {
166
            FeatureStore featureStore = vle.getFeatureStore();
167
            iterator =
168
                ((FeatureSelection) featureStore.getSelection()).iterator();
169
            FeatureSelection newSelection =
170
                featureStore.createFeatureSelection();
171
            // ArrayList selectedRowAux = new ArrayList();
172
            featureStore.beginEditingGroup(getName());
173

    
174
            while (iterator.hasNext()) {
175
                Feature feature = (Feature) iterator.next();
176
                Geometry geom = (feature.getDefaultGeometry()).cloneGeometry();
177
                featureStore.delete(feature);
178
                if (geom instanceof GeometryCollection) {
179
                    exploitGeometryCollection(feature, newSelection);
180
                } else {
181
                    if (geom.getType() == Geometry.TYPES.CURVE) {
182
                        exploitLine(feature, newSelection);
183
                    } else
184
                        if (geom.getType() == Geometry.TYPES.SURFACE) {
185
                            exploitPolygon(feature, newSelection);
186
                        }
187
                }
188

    
189
            }
190
            // clearSelection();
191
            featureStore.setSelection(newSelection);
192
            featureStore.endEditingGroup();
193
        } catch (DataException e) {
194
            NotificationManager.addError(e.getMessage(), e);
195
        } finally {
196
            if (iterator != null) {
197
                iterator.dispose();
198
            }
199
        }
200
    }
201

    
202
    private void exploitGeometryCollection(Feature feature,
203
        FeatureSelection selectedRowAux) {
204
        Geometry geom = (feature.getDefaultGeometry()).cloneGeometry();
205
        BaseMultiPrimitive gc = (BaseMultiPrimitive) geom;
206
        int numGeoms = gc.getPrimitivesNumber();
207
        FeatureStore featureStore;
208
        try {
209
            featureStore = getVLE().getFeatureStore();
210
            for (int i = 0; i < numGeoms; i++) {
211
                Geometry primitiveGeom = gc.getPrimitiveAt(i);
212
                EditableFeature eFeature =
213
                    featureStore.createNewFeature(feature.getType(), feature);
214
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
215
                    .getDefaultGeometryAttributeName(), primitiveGeom);
216
                selectedRowAux.select(feature);
217
            }
218
        } catch (ReadException e) {
219
            // TODO Auto-generated catch block
220
            e.printStackTrace();
221
        } catch (DataException e) {
222
            // TODO Auto-generated catch block
223
            e.printStackTrace();
224
        }
225
        refresh();
226
    }
227

    
228
    private void exploitLine(Feature feature, FeatureSelection selectedRowAux) {
229
        GeneralPathX newGp1 = new GeneralPathX();
230
        // DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
231
        // .cloneRow();
232
        // Point2D firstPoint=null;
233
        // Point2D lastPoint=null;
234
        // Object[] attributes=new Object[feature.size()];
235
        // for (int j = 0; j < feature.size(); j++) {
236
        // attributes[j]=feature.get(j);
237
        // }
238
        FeatureStore featureStore;
239
        // try {
240
        try {
241
            featureStore = getVLE().getFeatureStore();
242

    
243
            // EditableFeature
244
            // eFeature=featureStore.createNewFeature(feature.getType(),
245
            // feature);
246
            PathIterator theIterator =
247
                (feature.getDefaultGeometry()).getPathIterator(null,
248
                    geomManager.getFlatness());
249
            double[] theData = new double[6];
250
            int theType;
251
            int numParts = 0;
252
            Point2D initialPoint = null;
253
            while (!theIterator.isDone()) {
254
                theType = theIterator.currentSegment(theData);
255
                switch (theType) {
256

    
257
                case PathIterator.SEG_MOVETO:
258
                    numParts++;
259
                    newGp1 = new GeneralPathX();
260
                    // firstPoint=new Point2D.Double(theData[0], theData[1]);
261
                    initialPoint = new Point2D.Double(theData[0], theData[1]);
262
                    newGp1.moveTo(theData[0], theData[1]);
263
                    // lastPoint=new Point2D.Double(theData[0], theData[1]);
264
                    break;
265

    
266
                case PathIterator.SEG_LINETO:
267
                    newGp1.lineTo(theData[0], theData[1]);
268
                    Geometry geomLine = createCurve(newGp1);
269
                    // DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
270
                    // dfLine.setGeometry(geomLine);
271
                    // EditableFeature
272
                    // eFeature=featureStore.createNewFeature(feature.getType(),
273
                    // feature);
274
                    // eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
275
                    Feature f = insertGeometry(geomLine, feature);
276
                    // featureStore.insert(eFeature);
277
                    selectedRowAux.select(f);
278
                    newGp1 = new GeneralPathX();
279
                    newGp1.moveTo(theData[0], theData[1]);
280
                    // lastPoint=new Point2D.Double(theData[0], theData[1]);
281
                    break;
282
                case PathIterator.SEG_CLOSE:
283
                    if (initialPoint != null) {
284
                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
285
                        geomLine = createCurve(newGp1);
286
                        // dfLine = (DefaultFeature) df.cloneRow();
287
                        // dfLine.setGeometry(geomLine);
288
                        EditableFeature eFeatureClose =
289
                            featureStore.createNewFeature(feature.getType(),
290
                                feature);
291
                        eFeatureClose.setGeometry(featureStore
292
                            .getDefaultFeatureType()
293
                            .getDefaultGeometryAttributeName(), geomLine);
294
                        selectedRowAux.select(eFeatureClose);
295
                        newGp1 = new GeneralPathX();
296
                        newGp1.moveTo(initialPoint.getX(), initialPoint.getY());
297
                    }
298
                    break;
299
                /*
300
                 * case PathIterator.SEG_QUADTO:
301
                 * Point2D current=newGp1.getCurrentPoint();
302
                 * newGp1.quadTo(theData[0], theData[1], theData[2],
303
                 * theData[3]);
304
                 * IGeometry geomQuad = ShapeFactory
305
                 * .createArc(current,new Point2D.Double(theData[0],
306
                 * theData[1]),new Point2D.Double(theData[2], theData[3]));
307
                 * DefaultFeature dfQuad = (DefaultFeature) df.cloneRow();
308
                 * dfQuad.setGeometry(geomQuad);
309
                 * int indexQuad = addGeometry(geomQuad, dfQuad
310
                 * .getAttributes());
311
                 * selectedRowAux.add(new DefaultRowEdited(dfQuad,
312
                 * IRowEdited.STATUS_ADDED, indexQuad));
313
                 * newGp1 = new GeneralPathX();
314
                 * newGp1.moveTo(theData[0], theData[1]);
315
                 * // lastPoint=new Point2D.Double(theData[0], theData[1]);
316
                 * break;
317
                 * case PathIterator.SEG_CUBICTO:
318
                 * newGp1.curveTo(theData[0], theData[1], theData[2],
319
                 * theData[3], theData[4], theData[5]);
320
                 * IGeometry geomCurve = ShapeFactory
321
                 * .createArc(new Point2D.Double(theData[0], theData[1]),new
322
                 * Point2D.Double(theData[2], theData[3]),new
323
                 * Point2D.Double(theData[4], theData[5]));
324
                 * DefaultFeature dfCurve = (DefaultFeature) df.cloneRow();
325
                 * dfCurve.setGeometry(geomCurve);
326
                 * int indexCurve = addGeometry(geomCurve, dfCurve
327
                 * .getAttributes());
328
                 * selectedRowAux.add(new DefaultRowEdited(dfCurve,
329
                 * IRowEdited.STATUS_ADDED, indexCurve));
330
                 * newGp1 = new GeneralPathX();
331
                 * newGp1.moveTo(theData[0], theData[1]);
332
                 * // lastPoint=new Point2D.Double(theData[0], theData[1]);
333
                 * break;
334
                 * case PathIterator.SEG_CLOSE:
335
                 * // if (firstPoint!=null) {
336
                 * // newGp1=new GeneralPathX();
337
                 * // newGp1.moveTo(firstPoint.getX(),firstPoint.getY());
338
                 * // newGp1.lineTo(lastPoint.getX(),lastPoint.getY());
339
                 * // IGeometry geomClose = ShapeFactory
340
                 * // .createPolyline2D(newGp1);
341
                 * // DefaultFeature dfClose = (DefaultFeature) df.cloneRow();
342
                 * // dfClose.setGeometry(geomClose);
343
                 * // int indexClose = addGeometry(geomClose, dfClose
344
                 * // .getAttributes());
345
                 * // selectedRowAux.add(new DefaultRowEdited(dfClose,
346
                 * // IRowEdited.STATUS_ADDED, indexClose));
347
                 * // newGp1 = new GeneralPathX();
348
                 * // newGp1.moveTo(theData[0], theData[1]);
349
                 * // }
350
                 * // if (isFirstPart)
351
                 * // newGp1.closePath();
352
                 * // else
353
                 * // newGp2.closePath();
354
                 * break;
355
                 */
356
                } // end switch
357

    
358
                theIterator.next();
359
            } // end while loop
360
        } catch (ReadException e) {
361
            // TODO Auto-generated catch block
362
            e.printStackTrace();
363
        } catch (DataException e) {
364
            // TODO Auto-generated catch block
365
            e.printStackTrace();
366
        }
367
        refresh();
368
    }
369

    
370
    private void exploitPolygon(Feature feature, FeatureSelection selectedRowAux) {
371
        GeneralPathX newGp1 = null;
372
        // Object[] attributes=new Object[feature.size()];
373
        // for (int j = 0; j < feature.size(); j++) {
374
        // attributes[j]=feature.get(j);
375
        // }
376

    
377
        FeatureStore featureStore;
378
        try {
379
            featureStore = getVLE().getFeatureStore();
380

    
381
            PathIterator theIterator =
382
                (feature.getDefaultGeometry()).getPathIterator(null,
383
                    geomManager.getFlatness());
384
            double[] theData = new double[6];
385
            int theType;
386
            int numParts = 0;
387
            Point2D initialPoint = null;
388
            while (!theIterator.isDone()) {
389
                theType = theIterator.currentSegment(theData);
390
                switch (theType) {
391

    
392
                case PathIterator.SEG_MOVETO:
393
                    numParts++;
394
                    if (newGp1 != null) {
395
                        Geometry geomLine = createCurve(newGp1);
396
                        EditableFeature eFeature =
397
                            featureStore.createNewFeature(feature.getType(),
398
                                feature);
399
                        eFeature.setGeometry(featureStore
400
                            .getDefaultFeatureType()
401
                            .getDefaultGeometryAttributeName(), geomLine);
402

    
403
                        selectedRowAux.select(eFeature);
404
                    }
405
                    newGp1 = new GeneralPathX();
406
                    initialPoint = new Point2D.Double(theData[0], theData[1]);
407
                    newGp1.moveTo(theData[0], theData[1]);
408
                    break;
409

    
410
                case PathIterator.SEG_LINETO:
411
                    newGp1.lineTo(theData[0], theData[1]);
412
                    // newGp1 = new GeneralPathX();
413
                    // newGp1.moveTo(theData[0], theData[1]);
414
                    break;
415

    
416
                case PathIterator.SEG_QUADTO:
417
                    newGp1.quadTo(theData[0], theData[1], theData[2],
418
                        theData[3]);
419
                    // newGp1 = new GeneralPathX();
420
                    // newGp1.moveTo(theData[0], theData[1]);
421
                    break;
422
                case PathIterator.SEG_CUBICTO:
423
                    newGp1.curveTo(theData[0], theData[1], theData[2],
424
                        theData[3], theData[4], theData[5]);
425
                    // newGp1 = new GeneralPathX();
426
                    // newGp1.moveTo(theData[0], theData[1]);
427
                    break;
428
                case PathIterator.SEG_CLOSE:
429
                    if (initialPoint != null) {
430
                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
431
                    }
432
                    // System.out.println("aqu?");
433
                    // if (isFirstPart)
434
                    // newGp1.closePath();
435
                    // else
436
                    // newGp2.closePath();
437
                    break;
438
                } // end switch
439

    
440
                theIterator.next();
441
            } // end while loop
442
            if (newGp1 != null) {
443
                Geometry geomLine = createCurve(newGp1);
444
                EditableFeature eFeature =
445
                    featureStore.createNewFeature(feature.getType(), feature);
446
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
447
                    .getDefaultGeometryAttributeName(), geomLine);
448
                selectedRowAux.select(eFeature);
449
            }
450
        } catch (ReadException e) {
451
            // TODO Auto-generated catch block
452
            e.printStackTrace();
453
        } catch (DataException e) {
454
            // TODO Auto-generated catch block
455
            e.printStackTrace();
456
        }
457
        refresh();
458
    }
459

    
460
    /**
461
     * Add a diferent option.
462
     * 
463
     * @param s
464
     *            Diferent option.
465
     */
466
    public void addOption(String s) {
467
    }
468

    
469
    /*
470
     * (non-Javadoc)
471
     * 
472
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
473
     */
474
    public void addValue(double d) {
475

    
476
    }
477

    
478
    public String getName() {
479
        return PluginServices.getText(this, "exploit_");
480
    }
481

    
482
    public String toString() {
483
        return "_exploit";
484
    }
485

    
486
    public boolean isApplicable(int shapeType) {
487
        switch (shapeType) {
488
        case Geometry.TYPES.POINT:
489
        case Geometry.TYPES.CIRCLE:
490
        case Geometry.TYPES.ARC:
491
        case Geometry.TYPES.ELLIPSE:
492
        case Geometry.TYPES.SURFACE:
493
        case Geometry.TYPES.MULTIPOINT:
494
            return false;
495
        }
496
        return true;
497
    }
498
}