Revision 39088 branches/v2_0_0_prep/extensions/extEditing/src/org/gvsig/editing/gui/cad/tools/ExploitCADTool.java

View differences:

ExploitCADTool.java
21 21
 */
22 22
package org.gvsig.editing.gui.cad.tools;
23 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;
24 30
import java.awt.event.InputEvent;
25 31
import java.awt.geom.PathIterator;
26 32
import java.awt.geom.Point2D;
27 33

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

  
30 38
import org.slf4j.Logger;
31 39
import org.slf4j.LoggerFactory;
......
45 53
import org.gvsig.fmap.dal.feature.FeatureSelection;
46 54
import org.gvsig.fmap.dal.feature.FeatureSet;
47 55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
48 57
import org.gvsig.fmap.geom.Geometry;
49 58
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
50 59
import org.gvsig.fmap.geom.primitive.GeneralPathX;
60
import org.gvsig.fmap.geom.type.GeometryType;
61
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
51 62
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
63
import org.gvsig.i18n.Messages;
52 64
import org.gvsig.tools.dispose.DisposableIterator;
53 65

  
54 66
//import com.vividsolutions.jts.geom.GeometryCollection;
......
64 76
    private static Logger logger = LoggerFactory.getLogger(ExploitCADTool.class);
65 77

  
66 78
    protected ExploitCADToolContext _fsm;
79
    
67 80

  
68 81
    /**
69 82
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
......
162 175
    }
163 176

  
164 177
    public void exploit() {
178
        
165 179
        VectorialLayerEdited vle = getVLE();
166 180
        DisposableIterator iterator = null;
181
        FeatureStore featureStore = null;
182
        FeatureType fety = null;
183

  
167 184
        try {
168
            FeatureStore featureStore = vle.getFeatureStore();
185
            featureStore = vle.getFeatureStore();
186
            fety = featureStore.getDefaultFeatureType();
187
        } catch (Exception ex) {
188
            NotificationManager.addError(ex.getMessage(), ex);
189
            return;
190
        }
191

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

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

  
213
        try {
169 214
            iterator =
170 215
                ((FeatureSelection) featureStore.getSelection()).iterator();
171 216
            FeatureSelection newSelection =
......
180 225
                if (geom instanceof MultiPrimitive) {
181 226
                    exploitGeometryCollection(feature, newSelection);
182 227
                } else {
228
                    
183 229
                    if (geom.getType() == CURVE) {
230
                        /*
231
                         * Splitting line gives lines, no problem
232
                         */
184 233
                        exploitLine(feature, newSelection);
185
                    } else
234
                    } else {
186 235
                        if (geom.getType() == SURFACE) {
187
                            exploitPolygon(feature, newSelection);
236
                            
237
                            if (decisionHasBeenTaken) {
238
                                
239
                                /*
240
                                 * We have already decided what to do
241
                                 * in these cases
242
                                 */
243
                                exploitPolygon(
244
                                    feature,
245
                                    newSelection,
246
                                    differentTypesAllowed);
247
                                
248
                            } else {
249
                                
250
                                differentTypesAllowed = askUserAllowOther();
251
                                decisionHasBeenTaken = true;
252
                                /*
253
                                 * User has taken a decision. The same
254
                                 * decision applies for other geometries
255
                                 * in the current explode process
256
                                 */
257
                                exploitPolygon(
258
                                    feature,
259
                                    newSelection,
260
                                    differentTypesAllowed);
261
                            }
188 262
                        }
263
                    }
189 264
                }
190

  
191 265
            }
192 266
            // clearSelection();
193 267
            featureStore.setSelection(newSelection);
194 268
            featureStore.endEditingGroup();
195
        } catch (DataException e) {
269
        } catch (Exception e) {
196 270
            NotificationManager.addError(e.getMessage(), e);
197 271
        } finally {
198 272
            if (iterator != null) {
199 273
                iterator.dispose();
200 274
            }
201 275
        }
276

  
202 277
    }
203 278

  
204 279
    private void exploitGeometryCollection(Feature feature,
205
        FeatureSelection selectedRowAux) {
280
        FeatureSelection selectedRowAux) throws ReadException, DataException {
206 281
        Geometry geom = (feature.getDefaultGeometry()).cloneGeometry();
207 282
        MultiPrimitive gc = (MultiPrimitive) geom;
208 283
        int numGeoms = gc.getPrimitivesNumber();
209
        FeatureStore featureStore;
210 284
        Feature f = null;
211
        try {
212
            featureStore = getVLE().getFeatureStore();
213
            for (int i = 0; i < numGeoms; i++) {
214
                Geometry primitiveGeom = gc.getPrimitiveAt(i);
215
                f = insertGeometry(primitiveGeom, feature);
216
                /*
217
                EditableFeature eFeature =
218
                    featureStore.createNewFeature(feature.getType(), feature);
219
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
220
                    .getDefaultGeometryAttributeName(), primitiveGeom);
221
                    */
222
                selectedRowAux.select(f);
223
            }
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();
285

  
286
        for (int i = 0; i < numGeoms; i++) {
287
            Geometry primitiveGeom = gc.getPrimitiveAt(i);
288
            f = insertGeometry(primitiveGeom, feature);
289
            selectedRowAux.select(f);
230 290
        }
231 291
        refresh();
232 292
    }
233 293

  
234
    private void exploitLine(Feature feature, FeatureSelection selectedRowAux) {
294
    private void exploitLine(Feature feature,
295
        FeatureSelection selectedRowAux) {
296
        
235 297
        GeneralPathX newGp1 = new GeneralPathX();
236 298
        // DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
237 299
        // .cloneRow();
......
291 353
                    if (initialPoint != null) {
292 354
                        newGp1.lineTo(initialPoint.getX(), initialPoint.getY());
293 355
                        geomLine = createCurve(newGp1);
294
                        // dfLine = (DefaultFeature) df.cloneRow();
295
                        // dfLine.setGeometry(geomLine);
296
                        /*
297
                        EditableFeature eFeatureClose =
298
                            featureStore.createNewFeature(feature.getType(),
299
                                feature);
300
                        eFeatureClose.setGeometry(featureStore
301
                            .getDefaultFeatureType()
302
                            .getDefaultGeometryAttributeName(), geomLine);
303
                        selectedRowAux.select(eFeatureClose);
304
                        */
305
                        
356

  
306 357
                        f = insertGeometry(geomLine, feature);
307 358
                        selectedRowAux.select(f);
308 359

  
......
324 375
        refresh();
325 376
    }
326 377

  
327
    private void exploitPolygon(Feature feature, FeatureSelection selectedRowAux) {
378
    private void exploitPolygon(
379
        Feature deleted_feature,
380
        FeatureSelection selectedRowAux,
381
        boolean allows_other) {
382
        
328 383
        GeneralPathX newGp1 = null;
329 384
        // Object[] attributes=new Object[feature.size()];
330 385
        // for (int j = 0; j < feature.size(); j++) {
......
337 392
            featureStore = getVLE().getFeatureStore();
338 393

  
339 394
            PathIterator theIterator =
340
                (feature.getDefaultGeometry()).getPathIterator(null,
395
                (deleted_feature.getDefaultGeometry()).getPathIterator(null,
341 396
                    geomManager.getFlatness());
342 397
            double[] theData = new double[6];
343 398
            int theType;
......
350 405
                case PathIterator.SEG_MOVETO:
351 406
                    numParts++;
352 407
                    if (newGp1 != null) {
353
                        Geometry geomLine = createCurve(newGp1);
354
                        
355
                        /*
356
                        EditableFeature eFeature =
357
                            featureStore.createNewFeature(feature.getType(),
358
                                feature);
359
                        eFeature.setGeometry(featureStore
360
                            .getDefaultFeatureType()
361
                            .getDefaultGeometryAttributeName(), geomLine);
362
                            */
363
                        
364
                        added_feat = insertGeometry(geomLine, feature);
365
                        selectedRowAux.select(added_feat);
408
                        Geometry new_part = null;
409

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

  
366 424
                    }
367 425
                    newGp1 = new GeneralPathX();
368 426
                    initialPoint = new Point2D.Double(theData[0], theData[1]);
......
402 460
                theIterator.next();
403 461
            } // end while loop
404 462
            if (newGp1 != null) {
405
                Geometry geomLine = createCurve(newGp1);
406
                /*
407
                EditableFeature eFeature =
408
                    featureStore.createNewFeature(feature.getType(),
409
                        feature);
410
                eFeature.setGeometry(featureStore
411
                    .getDefaultFeatureType()
412
                    .getDefaultGeometryAttributeName(), geomLine);
413
                 */
414 463
                
415
                added_feat = insertGeometry(geomLine, feature);
416
                selectedRowAux.select(added_feat);
464
                Geometry new_part = null;
465
                
466
                if (allows_other) {
467
                    new_part = createCurve(newGp1);
468
                    added_feat = cloneFeatureWithGeometry(new_part, deleted_feature);
469
                    /*
470
                     * added_feat not added
471
                     * Now explode resulting line
472
                     */
473
                    exploitLine(added_feat, selectedRowAux);
474
                } else {
475
                    new_part = createSurface(newGp1);
476
                    added_feat = insertGeometry(new_part, deleted_feature);
477
                    selectedRowAux.select(added_feat);
478
                }
479
                
417 480
            }
418 481
        } catch (Throwable e) {
419 482
            logger.info("While doing exploit of polygon.", e);
......
455 518
    protected int[] getSupportedGeometryTypes() {
456 519
        return new int[] { CURVE, SURFACE };
457 520
    }
521
    
522
    /**
523
     * @return
524
     */
525
    private boolean askUserAllowOther() {
526
        
527
        int resp = 0;
528
        
529
        Object[] options = {
530
            Messages.getText("_Allow_lines"),
531
            Messages.getText("_Keep_as_polygons") };
532
        
533
        JPanel explanation_panel = getExplanationPanel();
534
        Component parent = ApplicationLocator.getManager().getRootComponent();
535
        
536
        resp = JOptionPane.showOptionDialog(
537
                    parent,
538
                    explanation_panel,
539
                    Messages.getText("exploit"),
540
                    JOptionPane.YES_NO_OPTION,
541
                    JOptionPane.QUESTION_MESSAGE, null, options,
542
                    options[1]);
543
        
544
        return (resp == JOptionPane.YES_OPTION);
545
    }
546
    
547
    
548
    private JPanel getExplanationPanel() {
549
        
550
        JPanel resp = new JPanel(new BorderLayout(10, 10));
551
        
552
        String msg = Messages.getText("_Polygons_will_exploded_in_polygon_only_layer_Decision_question");
553
        JLabel topLabel = new JLabel(msg);
554
        
555
        JPanel mainPanel = new JPanel(new GridBagLayout());
556
        GridBagConstraints cc = new GridBagConstraints();
557
        
558
        cc.gridx = 0;
559
        cc.gridy = 0;
560
        cc.anchor = GridBagConstraints.WEST;
561
        
562
        cc.insets = new Insets(3, 6, 3, 6);
563
        
564
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
458 565

  
566
        JLabel lbl = new JLabel(Messages.getText("_Allow_lines"));
567
        lbl.setFont(boldf);
568
        mainPanel.add(lbl, cc);
569
        cc.gridx = 1;
570
        mainPanel.add(new JLabel(Messages.getText("_Lines_will_exist_in_editing_mode_Must_remove_or_cancel")), cc);
571
        
572
        cc.gridx = 0;
573
        cc.gridy = 1;
574
        lbl = new JLabel(Messages.getText("_Keep_as_polygons"));
575
        lbl.setFont(boldf);
576
        mainPanel.add(lbl, cc);
577
        cc.gridx = 1;
578
        mainPanel.add(new JLabel(Messages.getText("_Multi_polygons_separated_Simple_polygons_kept")), cc);
579

  
580
        resp.add(mainPanel, BorderLayout.CENTER);
581
        resp.add(topLabel, BorderLayout.NORTH);
582
        return resp;
583
    }
584
    
585
    
586
    private Feature cloneFeatureWithGeometry(
587
        Geometry geometry, Feature feature) {
588
        
589
        VectorialLayerEdited vle = getVLE();
590

  
591
        try {
592
            FeatureStore featureStore =
593
                ((FLyrVect) vle.getLayer()).getFeatureStore();
594
            EditableFeature eFeature =
595
                featureStore.createNewFeature(
596
                    featureStore.getDefaultFeatureType(), feature);
597
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
598
                .getDefaultGeometryAttributeName(), geometry);
599
            return eFeature;
600
        } catch (ReadException e) {
601
            NotificationManager.addError(e.getMessage(), e);
602
            return null;
603
        } catch (DataException e) {
604
            NotificationManager.addError(e.getMessage(), e);
605
            return null;
606
        }
607
    }
608

  
459 609
}

Also available in: Unified diff