Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / ExploitCADTool.java @ 39296

History | View | Annotate | Download (21.4 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.BorderLayout;
25
import java.awt.Component;
26
import java.awt.Font;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.InputEvent;
31
import java.awt.geom.PathIterator;
32
import java.awt.geom.Point2D;
33

    
34
import javax.swing.JLabel;
35
import javax.swing.JOptionPane;
36
import javax.swing.JPanel;
37

    
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.messages.NotificationManager;
43
import org.gvsig.app.ApplicationLocator;
44
import org.gvsig.editing.CADExtension;
45
import org.gvsig.editing.gui.cad.DefaultCADTool;
46
import org.gvsig.editing.gui.cad.exception.CommandException;
47
import org.gvsig.editing.gui.cad.tools.smc.ExploitCADToolContext;
48
import org.gvsig.editing.layers.VectorialLayerEdited;
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.exception.ReadException;
51
import org.gvsig.fmap.dal.feature.EditableFeature;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureSelection;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
59
import org.gvsig.fmap.geom.primitive.Curve;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61
import org.gvsig.fmap.geom.primitive.Surface;
62
import org.gvsig.fmap.geom.type.GeometryType;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
65
import org.gvsig.i18n.Messages;
66
import org.gvsig.tools.dispose.DisposableIterator;
67

    
68
//import com.vividsolutions.jts.geom.GeometryCollection;
69

    
70
/**
71
 * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
72
 * componen, implementado para polil?neas y pol?gonos.
73
 * 
74
 * @author Vicente Caballero Navarro
75
 */
76
public class ExploitCADTool extends DefaultCADTool {
77
    
78
    private static Logger logger = LoggerFactory.getLogger(ExploitCADTool.class);
79

    
80
    protected ExploitCADToolContext _fsm;
81
    
82

    
83
    /**
84
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
85
     * carga previa a la utilizaci?n de la herramienta.
86
     */
87
    public void init() {
88
        _fsm = new ExploitCADToolContext(this);
89
    }
90

    
91
    /*
92
     * (non-Javadoc)
93
     * 
94
     * @see
95
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
96
     * .layers.FBitSet, double, double)
97
     */
98
    public void transition(double x, double y, InputEvent event) {
99
        _fsm.addPoint(x, y, event);
100
    }
101

    
102
    /*
103
     * (non-Javadoc)
104
     * 
105
     * @see
106
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
107
     * .layers.FBitSet, double)
108
     */
109
    public void transition(double d) {
110
        _fsm.addValue(d);
111
    }
112

    
113
    /*
114
     * (non-Javadoc)
115
     * 
116
     * @see
117
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
118
     * .layers.FBitSet, java.lang.String)
119
     */
120
    public void transition(String s) throws CommandException {
121
        if (!super.changeCommand(s)) {
122
            _fsm.addOption(s);
123
        }
124
    }
125

    
126
    /**
127
     * DOCUMENT ME!
128
     */
129
    public void selection() {
130
        FeatureSet selection = null;
131
        try {
132
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
133

    
134
            if (selection.getSize() == 0
135
                && !CADExtension
136
                    .getCADTool()
137
                    .getClass()
138
                    .getName()
139
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
140
                CADExtension.setCADTool("_selection", false);
141
                ((SelectionCADTool) CADExtension.getCADTool())
142
                    .setNextTool("_exploit");
143
            }
144
        } catch (ReadException e) {
145
            // TODO Auto-generated catch block
146
            e.printStackTrace();
147
        } catch (DataException e) {
148
            // TODO Auto-generated catch block
149
            e.printStackTrace();
150
        }
151
    }
152

    
153
    /**
154
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
155
     * editableFeatureSource que ya estar? creado.
156
     * 
157
     * @param x
158
     *            par?metro x del punto que se pase en esta transici?n.
159
     * @param y
160
     *            par?metro y del punto que se pase en esta transici?n.
161
     */
162
    public void addPoint(double x, double y, InputEvent event) {
163
    }
164

    
165
    /**
166
     * M?todo para dibujar la lo necesario para el estado en el que nos
167
     * encontremos.
168
     * 
169
     * @param g
170
     *            Graphics sobre el que dibujar.
171
     * @param x
172
     *            par?metro x del punto que se pase para dibujar.
173
     * @param y
174
     *            par?metro x del punto que se pase para dibujar.
175
     */
176
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
177
    }
178

    
179
    public void exploit() {
180
        
181
        VectorialLayerEdited vle = getVLE();
182
        DisposableIterator iterator = null;
183
        FeatureStore featureStore = null;
184
        FeatureType fety = null;
185

    
186
        try {
187
            featureStore = vle.getFeatureStore();
188
            fety = featureStore.getDefaultFeatureType();
189
        } catch (Exception ex) {
190
            NotificationManager.addError(ex.getMessage(), ex);
191
            return;
192
        }
193

    
194
        
195
        GeometryType gety = fety.getDefaultGeometryAttribute().getGeomType();
196
        
197
        /*
198
         * If we find a geometry which will violate
199
         * geometry type retriction after being exploded,
200
         * then we will ask user what to do (we'll ask once
201
         * for all cases)
202
         */
203
        boolean decisionHasBeenTaken = false;
204
        boolean differentTypesAllowed = false;
205

    
206
        if (gety.getType() == Geometry.TYPES.GEOMETRY) {
207
            /*
208
             * Multi allowed, so we don't need to ask
209
             */
210
            decisionHasBeenTaken = true;
211
            differentTypesAllowed = true;
212
        }
213
        // ==============================
214

    
215
        try {
216
            iterator =
217
                ((FeatureSelection) featureStore.getSelection()).iterator();
218
            FeatureSelection newSelection =
219
                featureStore.createFeatureSelection();
220
            // ArrayList selectedRowAux = new ArrayList();
221
            featureStore.beginEditingGroup(getName());
222

    
223
            while (iterator.hasNext()) {
224
                Feature feature = (Feature) iterator.next();
225
                Geometry geom = (feature.getDefaultGeometry()).cloneGeometry();
226
                featureStore.delete(feature);
227
                if (geom instanceof MultiPrimitive) {
228
                    exploitGeometryCollection(feature, newSelection);
229
                } else {
230
                    
231
                    if (geom instanceof Curve) {
232
                        /*
233
                         * Splitting line gives lines, no problem
234
                         */
235
                        exploitLine(feature, newSelection);
236
                    } else {
237
                        if (geom instanceof Surface) {
238
                            
239
                            if (decisionHasBeenTaken) {
240
                                
241
                                /*
242
                                 * We have already decided what to do
243
                                 * in these cases
244
                                 */
245
                                exploitPolygon(
246
                                    feature,
247
                                    newSelection,
248
                                    differentTypesAllowed);
249
                                
250
                            } else {
251
                                
252
                                differentTypesAllowed = askUserAllowOther();
253
                                decisionHasBeenTaken = true;
254
                                /*
255
                                 * User has taken a decision. The same
256
                                 * decision applies for other geometries
257
                                 * in the current explode process
258
                                 */
259
                                exploitPolygon(
260
                                    feature,
261
                                    newSelection,
262
                                    differentTypesAllowed);
263
                            }
264
                        }
265
                    }
266
                }
267
            }
268
            // clearSelection();
269
            featureStore.setSelection(newSelection);
270
            featureStore.endEditingGroup();
271
        } catch (Exception e) {
272
            NotificationManager.addError(e.getMessage(), e);
273
        } finally {
274
            if (iterator != null) {
275
                iterator.dispose();
276
            }
277
        }
278

    
279
    }
280

    
281
    private void exploitGeometryCollection(Feature feature,
282
        FeatureSelection selectedRowAux) throws ReadException, DataException {
283
        Geometry geom = (feature.getDefaultGeometry()).cloneGeometry();
284
        MultiPrimitive gc = (MultiPrimitive) geom;
285
        int numGeoms = gc.getPrimitivesNumber();
286
        Feature f = null;
287

    
288
        for (int i = 0; i < numGeoms; i++) {
289
            Geometry primitiveGeom = gc.getPrimitiveAt(i);
290
            f = insertGeometry(primitiveGeom, feature);
291
            selectedRowAux.select(f);
292
        }
293
        refresh();
294
    }
295

    
296
    private void exploitLine(Feature feature,
297
        FeatureSelection selectedRowAux) {
298
        
299
        GeneralPathX newGp1 = new GeneralPathX();
300
        // DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
301
        // .cloneRow();
302
        // Point2D firstPoint=null;
303
        // Point2D lastPoint=null;
304
        // Object[] attributes=new Object[feature.size()];
305
        // for (int j = 0; j < feature.size(); j++) {
306
        // attributes[j]=feature.get(j);
307
        // }
308
        FeatureStore featureStore;
309
        // try {
310
        try {
311
            featureStore = getVLE().getFeatureStore();
312

    
313
            // EditableFeature
314
            // eFeature=featureStore.createNewFeature(feature.getType(),
315
            // feature);
316
            PathIterator theIterator =
317
                (feature.getDefaultGeometry()).getPathIterator(null,
318
                    geomManager.getFlatness());
319
            double[] theData = new double[6];
320
            int theType;
321
            int numParts = 0;
322
            Point2D initialPoint = null;
323
            Feature f = null;
324
            
325
            while (!theIterator.isDone()) {
326
                theType = theIterator.currentSegment(theData);
327
                switch (theType) {
328

    
329
                case PathIterator.SEG_MOVETO:
330
                    numParts++;
331
                    newGp1 = new GeneralPathX();
332
                    // firstPoint=new Point2D.Double(theData[0], theData[1]);
333
                    initialPoint = new Point2D.Double(theData[0], theData[1]);
334
                    newGp1.moveTo(theData[0], theData[1]);
335
                    // lastPoint=new Point2D.Double(theData[0], theData[1]);
336
                    break;
337

    
338
                case PathIterator.SEG_LINETO:
339
                    newGp1.lineTo(theData[0], theData[1]);
340
                    Geometry geomLine = createCurve(newGp1);
341
                    // DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
342
                    // dfLine.setGeometry(geomLine);
343
                    // EditableFeature
344
                    // eFeature=featureStore.createNewFeature(feature.getType(),
345
                    // feature);
346
                    // eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
347
                    f = insertGeometry(geomLine, feature);
348
                    // featureStore.insert(eFeature);
349
                    selectedRowAux.select(f);
350
                    newGp1 = new GeneralPathX();
351
                    newGp1.moveTo(theData[0], theData[1]);
352
                    // lastPoint=new Point2D.Double(theData[0], theData[1]);
353
                    break;
354
                case PathIterator.SEG_CLOSE:
355
                    if (initialPoint != null) {
356
                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
357
                        geomLine = createCurve(newGp1);
358

    
359
                        f = insertGeometry(geomLine, feature);
360
                        selectedRowAux.select(f);
361

    
362
                        newGp1 = new GeneralPathX();
363
                        newGp1.moveTo(initialPoint.getX(), initialPoint.getY());
364
                    }
365
                    break;
366
                } // end switch
367

    
368
                theIterator.next();
369
            } // end while loop
370
        } catch (ReadException e) {
371
            // TODO Auto-generated catch block
372
            e.printStackTrace();
373
        } catch (DataException e) {
374
            // TODO Auto-generated catch block
375
            e.printStackTrace();
376
        }
377
        refresh();
378
    }
379

    
380
    private void exploitPolygon(
381
        Feature deleted_feature,
382
        FeatureSelection selectedRowAux,
383
        boolean allows_other) {
384
        
385
        GeneralPathX newGp1 = null;
386
        // Object[] attributes=new Object[feature.size()];
387
        // for (int j = 0; j < feature.size(); j++) {
388
        // attributes[j]=feature.get(j);
389
        // }
390

    
391
        FeatureStore featureStore;
392
        Feature added_feat = null;
393
        try {
394
            featureStore = getVLE().getFeatureStore();
395

    
396
            PathIterator theIterator =
397
                (deleted_feature.getDefaultGeometry()).getPathIterator(null,
398
                    geomManager.getFlatness());
399
            double[] theData = new double[6];
400
            int theType;
401
            int numParts = 0;
402
            Point2D initialPoint = null;
403
            while (!theIterator.isDone()) {
404
                theType = theIterator.currentSegment(theData);
405
                switch (theType) {
406

    
407
                case PathIterator.SEG_MOVETO:
408
                    numParts++;
409
                    if (newGp1 != null) {
410
                        Geometry new_part = null;
411

    
412
                        if (allows_other) {
413
                            new_part = createCurve(newGp1);
414
                            added_feat = cloneFeatureWithGeometry(new_part, deleted_feature);
415
                            /*
416
                             * added_feat not added
417
                             * Now explode resulting line
418
                             */
419
                            exploitLine(added_feat, selectedRowAux);
420
                        } else {
421
                            new_part = createSurface(newGp1);
422
                            added_feat = insertGeometry(new_part, deleted_feature);
423
                            selectedRowAux.select(added_feat);
424
                        }
425

    
426
                    }
427
                    newGp1 = new GeneralPathX();
428
                    initialPoint = new Point2D.Double(theData[0], theData[1]);
429
                    newGp1.moveTo(theData[0], theData[1]);
430
                    break;
431

    
432
                case PathIterator.SEG_LINETO:
433
                    newGp1.lineTo(theData[0], theData[1]);
434
                    // newGp1 = new GeneralPathX();
435
                    // newGp1.moveTo(theData[0], theData[1]);
436
                    break;
437

    
438
                case PathIterator.SEG_QUADTO:
439
                    newGp1.quadTo(theData[0], theData[1], theData[2],
440
                        theData[3]);
441
                    // newGp1 = new GeneralPathX();
442
                    // newGp1.moveTo(theData[0], theData[1]);
443
                    break;
444
                case PathIterator.SEG_CUBICTO:
445
                    newGp1.curveTo(theData[0], theData[1], theData[2],
446
                        theData[3], theData[4], theData[5]);
447
                    // newGp1 = new GeneralPathX();
448
                    // newGp1.moveTo(theData[0], theData[1]);
449
                    break;
450
                case PathIterator.SEG_CLOSE:
451
                    if (initialPoint != null) {
452
                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
453
                    }
454
                    // System.out.println("aqu?");
455
                    // if (isFirstPart)
456
                    // newGp1.closePath();
457
                    // else
458
                    // newGp2.closePath();
459
                    break;
460
                } // end switch
461

    
462
                theIterator.next();
463
            } // end while loop
464
            if (newGp1 != null) {
465
                
466
                Geometry new_part = null;
467
                
468
                if (allows_other) {
469
                    new_part = createCurve(newGp1);
470
                    added_feat = cloneFeatureWithGeometry(new_part, deleted_feature);
471
                    /*
472
                     * added_feat not added
473
                     * Now explode resulting line
474
                     */
475
                    exploitLine(added_feat, selectedRowAux);
476
                } else {
477
                    new_part = createSurface(newGp1);
478
                    added_feat = insertGeometry(new_part, deleted_feature);
479
                    selectedRowAux.select(added_feat);
480
                }
481
                
482
            }
483
        } catch (Throwable e) {
484
            logger.info("While doing exploit of polygon.", e);
485
            ApplicationLocator.getManager().message(
486
                "_Unable_to_exploit_polygon", JOptionPane.ERROR_MESSAGE);
487
            // TODO Auto-generated catch block
488
            // e.printStackTrace();
489
        }
490
        refresh();
491
    }
492

    
493
    /**
494
     * Add a diferent option.
495
     * 
496
     * @param s
497
     *            Diferent option.
498
     */
499
    public void addOption(String s) {
500
    }
501

    
502
    /*
503
     * (non-Javadoc)
504
     * 
505
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
506
     */
507
    public void addValue(double d) {
508

    
509
    }
510

    
511
    public String getName() {
512
        return PluginServices.getText(this, "exploit_");
513
    }
514

    
515
    public String toString() {
516
        return "_exploit";
517
    }
518

    
519
    @Override
520
    protected int[] getSupportedGeometryTypes() {
521
        return new int[] {
522
            CURVE,
523
            MULTICURVE,
524
            SURFACE,
525
            MULTISURFACE };
526
    }
527
    
528
    /**
529
     * @return
530
     */
531
    private boolean askUserAllowOther() {
532
        
533
        int resp = 0;
534
        
535
        Object[] options = {
536
            Messages.getText("_Allow_lines"),
537
            Messages.getText("_Keep_as_polygons") };
538
        
539
        JPanel explanation_panel = getExplanationPanel();
540
        Component parent = ApplicationLocator.getManager().getRootComponent();
541
        
542
        resp = JOptionPane.showOptionDialog(
543
                    parent,
544
                    explanation_panel,
545
                    Messages.getText("exploit"),
546
                    JOptionPane.YES_NO_OPTION,
547
                    JOptionPane.QUESTION_MESSAGE, null, options,
548
                    options[1]);
549
        
550
        return (resp == JOptionPane.YES_OPTION);
551
    }
552
    
553
    
554
    private JPanel getExplanationPanel() {
555
        
556
        JPanel resp = new JPanel(new BorderLayout(10, 10));
557
        
558
        String msg = Messages.getText("_Polygons_will_exploded_in_polygon_only_layer_Decision_question");
559
        JLabel topLabel = new JLabel(msg);
560
        
561
        JPanel mainPanel = new JPanel(new GridBagLayout());
562
        GridBagConstraints cc = new GridBagConstraints();
563
        
564
        cc.gridx = 0;
565
        cc.gridy = 0;
566
        cc.anchor = GridBagConstraints.WEST;
567
        
568
        cc.insets = new Insets(3, 6, 3, 6);
569
        
570
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
571

    
572
        JLabel lbl = new JLabel(Messages.getText("_Allow_lines"));
573
        lbl.setFont(boldf);
574
        mainPanel.add(lbl, cc);
575
        cc.gridx = 1;
576
        mainPanel.add(new JLabel(Messages.getText("_Lines_will_exist_in_editing_mode_Must_remove_or_cancel")), cc);
577
        
578
        cc.gridx = 0;
579
        cc.gridy = 1;
580
        lbl = new JLabel(Messages.getText("_Keep_as_polygons"));
581
        lbl.setFont(boldf);
582
        mainPanel.add(lbl, cc);
583
        cc.gridx = 1;
584
        mainPanel.add(new JLabel(Messages.getText("_Multi_polygons_separated_Simple_polygons_kept")), cc);
585

    
586
        resp.add(mainPanel, BorderLayout.CENTER);
587
        resp.add(topLabel, BorderLayout.NORTH);
588
        return resp;
589
    }
590
    
591
    
592
    private Feature cloneFeatureWithGeometry(
593
        Geometry geometry, Feature feature) {
594
        
595
        VectorialLayerEdited vle = getVLE();
596

    
597
        try {
598
            FeatureStore featureStore =
599
                ((FLyrVect) vle.getLayer()).getFeatureStore();
600
            EditableFeature eFeature =
601
                featureStore.createNewFeature(
602
                    featureStore.getDefaultFeatureType(), feature);
603
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
604
                .getDefaultGeometryAttributeName(), geometry);
605
            return eFeature;
606
        } catch (ReadException e) {
607
            NotificationManager.addError(e.getMessage(), e);
608
            return null;
609
        } catch (DataException e) {
610
            NotificationManager.addError(e.getMessage(), e);
611
            return null;
612
        }
613
    }
614

    
615
}