Revision 37328 branches/v2_0_0_prep/extensions/extEditing/src/org/gvsig/editing/gui/cad/DefaultCADTool.java

View differences:

DefaultCADTool.java
25 25
import java.awt.geom.AffineTransform;
26 26
import java.awt.geom.Point2D;
27 27
import java.util.ArrayList;
28
import java.util.List;
28 29

  
29 30
import org.cresques.cts.ICoordTrans;
30 31
import org.slf4j.Logger;
......
50 51
import org.gvsig.fmap.geom.Geometry.TYPES;
51 52
import org.gvsig.fmap.geom.GeometryLocator;
52 53
import org.gvsig.fmap.geom.GeometryManager;
54
import org.gvsig.fmap.geom.aggregate.MultiCurve;
55
import org.gvsig.fmap.geom.aggregate.MultiPoint;
53 56
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
57
import org.gvsig.fmap.geom.aggregate.MultiSurface;
54 58
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
55 59
import org.gvsig.fmap.geom.handler.Handler;
56 60
import org.gvsig.fmap.geom.primitive.Arc;
......
64 68
import org.gvsig.fmap.geom.primitive.Primitive;
65 69
import org.gvsig.fmap.geom.primitive.Spline;
66 70
import org.gvsig.fmap.geom.primitive.Surface;
71
import org.gvsig.fmap.geom.type.GeometryType;
72
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
73
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
67 74
import org.gvsig.fmap.geom.util.UtilFunctions;
68
import org.gvsig.fmap.mapcontext.MapContextLocator;
69
import org.gvsig.fmap.mapcontext.MapContextManager;
70 75
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
71 76
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72 77
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
......
76 81
import org.gvsig.utils.console.JConsole;
77 82

  
78 83
/**
79
 * DOCUMENT ME!
80 84
 * 
81 85
 * @author Vicente Caballero Navarro
82 86
 */
83
public abstract class DefaultCADTool implements CADTool {
87
public abstract class DefaultCADTool implements CADTool, Geometry.TYPES,
88
    Geometry.SUBTYPES {
84 89

  
85 90
    private static final Logger LOG = LoggerFactory
86 91
        .getLogger(DefaultCADTool.class);
87 92

  
88 93
    /**
89
     * Reference to the MapContext library manager, used to create symbols
90
     * and legends.
91
     */
92
    private static MapContextManager mapContextManager = MapContextLocator
93
        .getMapContextManager();
94

  
95
    /**
96 94
     * Reference to the MapControl library manager, used to manage
97 95
     * tools.
98 96
     */
99
    protected static MapControlManager mapControlManager = MapControlLocator
97
    protected MapControlManager mapControlManager = MapControlLocator
100 98
        .getMapControlManager();
99
    protected GeometryManager geomManager = GeometryLocator
100
        .getGeometryManager();
101 101

  
102 102
    private CADToolAdapter cadToolAdapter;
103 103

  
......
109 109

  
110 110
    private DefaultCADTool previousTool;
111 111

  
112
    private ArrayList temporalCache = new ArrayList();
113
    protected GeometryManager geomManager = GeometryLocator
114
        .getGeometryManager();
112
    private List<Geometry> temporalCache = new ArrayList<Geometry>();
115 113

  
114
    private GeometryType[] supportedTypes;
115

  
116
    /**
117
     * Crea un nuevo DefaultCADTool.
118
     */
119
    public DefaultCADTool() {
120
        loadGeometryTypes();
121
    }
122

  
123
    protected void loadGeometryTypes() {
124
        int[] types = getSupportedGeometryTypes();
125
        if (types == null) {
126
            supportedTypes = new GeometryType[0];
127
        }
128
        else {
129
            supportedTypes = new GeometryType[types.length];
130
            for (int i = 0; i < types.length; i++) {
131
                supportedTypes[i] = loadGeometryType(types[i], GEOM2D);
132
            }
133
        }
134
    }
135

  
116 136
    public void addTemporalCache(Geometry geom) {
117 137
        temporalCache.add(geom);
118 138
        try {
......
123 143
    }
124 144

  
125 145
    public void clearTemporalCache() {
126
        Geometry[] geoms = (Geometry[]) temporalCache.toArray(new Geometry[0]);
146
        Geometry[] geoms =
147
            temporalCache.toArray(new Geometry[temporalCache.size()]);
127 148
        for (int i = 0; i < geoms.length; i++) {
128 149
            try {
129 150
                removeSpatialCache(geoms[i]);
......
143 164
        if (geom.getType() == Geometry.TYPES.POINT) {
144 165
            r =
145 166
                geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
146
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);// Rectangle2D.Double(r.getX(),r.getY(),1,1);
167
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
147 168
        }
148 169
        spatialCache.insert(r, geom);
149 170

  
......
199 220
            featureStore = getVLE().getFeatureStore();
200 221
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
201 222

  
202
            iterator = selection.iterator();
223
            iterator = selection.fastIterator();
203 224
            while (iterator.hasNext()) {
204 225
                Feature feature = (Feature) iterator.next();
205 226
                Geometry geometry =
......
250 271
        double y) {
251 272
        FeatureStore featureStore = null;
252 273
        DisposableIterator iterator = null;
253
        ICoordTrans ct = getVLE().getLayer().getCoordTrans();
254 274

  
255 275
        try {
256 276
            Point2D lastPoint = new Point2D.Double(x, y);
......
261 281
            featureStore = getVLE().getFeatureStore();
262 282
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
263 283

  
264
            iterator = selection.iterator();
284
            iterator = selection.fastIterator();
265 285
            while (iterator.hasNext()) {
266 286
                Feature feature = (Feature) iterator.next();
267 287
                Geometry geometry =
......
286 306
        }
287 307
    }
288 308

  
289
    /**
290
     * 
291
     * @param cta
292
     *            DOCUMENT ME!
293
     */
294 309
    public void setCadToolAdapter(CADToolAdapter cta) {
295 310
        cadToolAdapter = cta;
296 311
    }
297 312

  
298
    /**
299
     * DOCUMENT ME!
300
     * 
301
     * @return DOCUMENT ME!
302
     */
303 313
    public CADToolAdapter getCadToolAdapter() {
304 314
        return cadToolAdapter;
305 315
    }
......
309 319
            .getActiveLayerEdited();
310 320
    }
311 321

  
312
    /**
313
     * DOCUMENT ME!
314
     * 
315
     * @param geometry
316
     *            DOCUMENT ME!
317
     */
318 322
    public Feature insertGeometry(Geometry geometry, Feature feature) {
319 323
        VectorialLayerEdited vle = getVLE();
320 324

  
......
356 360
                featureStore.createFeatureSelection();
357 361
            feature = insertGeometry(geometry);
358 362
            newSelection.select(feature);
359
            // clearSelection();
360 363
            featureStore.setSelection(newSelection);
361 364
            featureStore.endEditingGroup();
362 365
            featureStore.endComplexNotification();
......
377 380
                .getDefaultGeometryAttributeName(), geometry);
378 381
            featureStore.insert(eFeature);
379 382

  
380
            // drawToImage(featureStore, vle, eFeature);
381 383
            insertSpatialCache(geometry);
382 384

  
383 385
            getCadToolAdapter().getMapControl().getMapControlDrawer()
......
398 400

  
399 401
    }
400 402

  
401
    // private void drawToImage(FeatureStore featureStore,VectorialLayerEdited
402
    // vle, Feature feature) throws DataException,
403
    // GeometryOperationNotSupportedException, GeometryOperationException {
404
    // // clearSelection();
405
    // // ViewPort vp = vle.getLayer().getMapContext().getViewPort();
406
    // // BufferedImage selectionImage = new BufferedImage(
407
    // // vp.getImageWidth(), vp.getImageHeight(),
408
    // // BufferedImage.TYPE_INT_ARGB);
409
    // // Graphics2D gs = selectionImage.createGraphics();
410
    // // BufferedImage handlersImage = new BufferedImage(
411
    // // vp.getImageWidth(), vp.getImageHeight(),
412
    // // BufferedImage.TYPE_INT_ARGB);
413
    // // Graphics2D gh = handlersImage.createGraphics();
414
    //
415
    // // int inversedIndex=vea.getInversedIndex(index);
416
    // // FeatureSelection selection = (FeatureSelection)
417
    // // featureStore.getSelection();
418
    // // selection.select(feature);
419
    // // vle.addSelectionCache(new DefaultRowEdited(df,
420
    // // IRowEdited.STATUS_ADDED, inversedIndex ));
421
    // // vea.getSelection().set(inversedIndex);
422
    // // Geometry geom = (Geometry)feature.getDefaultGeometry();
423
    // // DrawOperationContext doc = new DrawOperationContext();
424
    // // doc.setGraphics(gs);
425
    // // doc.setViewPort(vp);
426
    // // doc.setSymbol(DefaultCADTool.selectionSymbol);
427
    // Geometry geometry=feature.getDefaultGeometry();
428
    // // geometry.cloneGeometry().invokeOperation(Draw.CODE, doc);
429
    // // // draw(gs, vp, DefaultCADTool.selectionSymbol);
430
    // // vle.drawHandlers(geometry.cloneGeometry(), gh, vp);
431
    // // vle.setHandlersImage(handlersImage);
432
    // // vle.setSelectionImage(selectionImage);
433
    // try {
434
    // insertSpatialCache(geometry);
435
    // } catch (CreateEnvelopeException e) {
436
    // LOG.error("Error creating the envelope", e);
437
    // }
438
    //
439
    // }
440

  
441 403
    /**
442
     * DOCUMENT ME!
443
     * 
444
     * @param geometry
445
     *            DOCUMENT ME!
446
     */
447
    // public void modifyFeature(Feature feature, Feature oldFeature) {
448
    // try {
449
    // CommandsRecord
450
    // cr=((FLyrVect)getVLE().getLayer()).getFeatureStore().getCommandsRecord();
451
    // cr.update(feature, oldFeature);
452
    // } catch (ReadException e) {
453
    // NotificationManager.addError(e.getMessage(),e);
454
    // }
455
    // draw(((Geometry)feature.getDefaultGeometry()).cloneGeometry());
456
    // }
457
    /**
458
     * DOCUMENT ME!
459
     * 
460
     * @param geometry
461
     *            DOCUMENT ME!
462
     * @param values
463
     *            DOCUMENT ME!
464
     */
465
    // public Feature addGeometry(Geometry geometry, Object[] values) {
466
    // // int index = 0;
467
    // try {
468
    // FeatureStore featureStore = ((FLyrVect) getVLE().getLayer())
469
    // .getFeatureStore();
470
    // EditableFeature eFeature = featureStore.createNewFeature(true);
471
    // eFeature.setGeometry(featureStore.getDefaultFeatureType()
472
    // .getDefaultGeometryAttributeName(), geometry);
473
    // for (int i = 0; i < values.length; i++) {
474
    // eFeature.set(i, values[i]);
475
    // }
476
    // featureStore.insert(eFeature);
477
    // return eFeature;
478
    // } catch (DataException e) {
479
    // // TODO Auto-generated catch block
480
    // e.printStackTrace();
481
    // }
482
    // // try {
483
    // // String newFID = vea.getNewFID();
484
    // // DefaultFeature df = new DefaultFeature(geometry, values, newFID);
485
    // // index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
486
    // // insertSpatialCache(geometry);
487
    // // } catch (ValidateRowException e) {
488
    // // NotificationManager.addError(e);
489
    // // } catch (ReadException e) {
490
    // // NotificationManager.addError(e);
491
    // // }
492
    // // return vea.getInversedIndex(index);
493
    // return null;
494
    // }
495

  
496
    /**
497 404
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
498 405
     * 
499 406
     * @return Cadena para mostrar por consola.
......
510 417
     */
511 418
    public void setQuestion(String s) {
512 419
        question = s;
513
        // ConsoleToken.addQuestion(s);
514 420
    }
515 421

  
516 422
    /**
......
519 425
     * edici�n y las que est�n por encima de ella en el TOC.
520 426
     */
521 427
    public void refresh() {
522
        // getCadToolAdapter().getMapControl().drawMap(false);
523

  
524 428
        getCadToolAdapter().getMapControl().rePaintDirtyLayers();
525 429
    }
526 430

  
527
    /*
528
     * public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
529
     * throws DriverIOException { for (int i = sel.nextSetBit(0); i >= 0; i =
530
     * sel.nextSetBit(i + 1)) { IGeometry ig =
531
     * getCadToolAdapter().getVectorialAdapter() .getShape(i).cloneGeometry();
532
     * if (ig == null) continue; Handler[] handlers =
533
     * ig.getHandlers(IGeometry.SELECTHANDLER);
534
     * FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers); } }
535
     */
536 431
    public void drawHandlers(MapControlDrawer renderer, ArrayList selectedRows,
537 432
        AffineTransform at) {
538 433
        FeatureSet selection = null;
......
542 437
                (FeatureSet) ((FLyrVect) getVLE().getLayer()).getFeatureStore()
543 438
                    .getSelection();
544 439

  
545
            iterator = selection.iterator();
440
            iterator = selection.fastIterator();
546 441
            while (iterator.hasNext()) {
547 442
                Feature feature = (Feature) iterator.next();
548 443

  
549
                // }
550
                // for (int i = 0; i < selectedRows.size(); i++) {
551
                // IRowEdited edRow = (IRowEdited) selectedRows.get(i);
552
                // IFeature feat = (IFeature) edRow.getLinkedRow();
553
                // IFeature feat = (IFeature) selectedRows.get(i);
554 444
                Geometry ig = (feature.getDefaultGeometry()).cloneGeometry();
555 445
                if (ig == null) {
556 446
                    continue;
......
561 451
            }
562 452

  
563 453
        } catch (ReadException e) {
564
            // TODO Auto-generated catch block
565
            e.printStackTrace();
454
            NotificationManager.addError(e.getMessage(), e);
566 455
        } catch (DataException e) {
567
            // TODO Auto-generated catch block
568
            e.printStackTrace();
456
            NotificationManager.addError(e.getMessage(), e);
569 457
        } finally {
570 458
            if (iterator != null) {
571 459
                iterator.dispose();
......
582 470
        return currentdescriptions;
583 471
    }
584 472

  
585
    /*
586
     * (non-Javadoc)
587
     * 
588
     * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
589
     */
590 473
    public void end() {
591 474
        CADExtension.setCADTool("_selection", true);
592 475
        PluginServices.getMainFrame().setSelectedTool("_selection");
......
595 478
    }
596 479

  
597 480
    public void init() {
598
        // jaume, should not be necessary
599
        // CADTool.drawingSymbol.setOutlined(true);
600
        // CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
601

  
481
        // Nothing to do
602 482
    }
603 483

  
604
    // protected ArrayList getSelectedRows() {
605
    // VectorialLayerEdited vle = getVLE();
606
    // ArrayList selectedRow = vle.getSelectedRow();
607
    // return selectedRow;
608
    // }
609

  
610 484
    protected ArrayList getSelectedHandlers() {
611 485
        VectorialLayerEdited vle = getVLE();
612 486
        ArrayList selectedHandlers = vle.getSelectedHandler();
......
671 545
    }
672 546

  
673 547
    public boolean isApplicable(int shapeType) {
674
        return true;
548
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
549
        return isApplicable(type);
675 550
    }
676 551

  
552
    public boolean isApplicable(GeometryType geometryType) {
553
        if (supportedTypes != null) {
554
            for (int i = 0; i < supportedTypes.length; i++) {
555
                if (supportedTypes[i].isTypeOf(geometryType)) {
556
                    return true;
557
                }
558
            }
559
        }
560
        return false;
561
    }
562

  
563
    /**
564
     * Returns the geometry types supported by the tool.
565
     * 
566
     * @return the supported geometry types
567
     */
568
    protected GeometryType[] getSupportedTypes() {
569
        return supportedTypes;
570
    }
571

  
572
    /**
573
     * Returns the list of Geometry types supported by this tool. If any
574
     * Geometry type is supported, just return null.
575
     * 
576
     * @return the list of Geometry types supported by this tool
577
     */
578
    protected int[] getSupportedGeometryTypes() {
579
        return null;
580
    }
581

  
582
    protected GeometryType loadGeometryType(int type, int subtype) {
583
        try {
584
            return GeometryLocator.getGeometryManager().getGeometryType(type,
585
                subtype);
586
        } catch (GeometryTypeNotSupportedException e) {
587
            throw new RuntimeException(
588
                "Error getting the Geometry type with type = " + type
589
                    + ", subtype = " + subtype, e);
590
        } catch (GeometryTypeNotValidException e) {
591
            throw new RuntimeException(
592
                "Error getting the Geometry type with type = " + type
593
                    + ", subtype = " + subtype, e);
594
        }
595
    }
596

  
677 597
    public abstract String toString();
678 598

  
679 599
    public void throwValueException(String s, double d) {
......
710 630
    }
711 631

  
712 632
    public void endTransition(double x, double y, MouseEvent e) {
713
        // TODO Auto-generated method stub
714

  
633
        // Nothing to do
715 634
    }
716 635

  
717 636
    /**
......
733 652
    }
734 653

  
735 654
    /**
655
     * Create a multicurve. If there is an
656
     * error return <code>null</code> and add the error
657
     * to the log
658
     * 
659
     * @return
660
     *         The MultiCurve
661
     */
662
    protected MultiCurve createMultiCurve() {
663
        try {
664
            return (MultiCurve) geomManager.create(TYPES.MULTICURVE,
665
                getSubType());
666
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
667
            LOG.error("Error creating multicurve", new CreateGeometryException(
668
                TYPES.MULTICURVE, getSubType(), e));
669
        }
670
        return null;
671
    }
672

  
673
    /**
736 674
     * Create an {@link OrientablePrimitive}. If there is an
737 675
     * error return <code>null</code> and add the error
738 676
     * to the log
......
826 764
    }
827 765

  
828 766
    /**
767
     * Create a multisurface. If there is an
768
     * error return <code>null</code> and add the error
769
     * to the log
770
     * 
771
     * @return
772
     *         The MultiSurface
773
     */
774
    protected MultiSurface createMultiSurface() {
775
        try {
776
            return (MultiSurface) geomManager.create(TYPES.MULTISURFACE,
777
                getSubType());
778

  
779
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
780
            LOG.error(
781
                "Error creating multisurface",
782
                new CreateGeometryException(TYPES.MULTISURFACE, getSubType(), e));
783
        }
784
        return null;
785
    }
786

  
787
    /**
829 788
     * Create a surface from a GeneralPath. If there is an
830 789
     * error return <code>null</code> and add the error
831 790
     * to the log
......
887 846
    }
888 847

  
889 848
    /**
849
     * Create a multipoint. If there is an
850
     * error return <code>null</code> and add the error
851
     * to the log
852
     * 
853
     * @param x
854
     *            The X coordinate
855
     * @param y
856
     *            The y coordinate
857
     * @return
858
     *         The MultiPoint
859
     */
860
    protected MultiPoint createMultiPoint() {
861
        try {
862
            return (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
863
                getSubType());
864
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
865
            LOG.error("Error creating multipoint", new CreateGeometryException(
866
                TYPES.MULTIPOINT, getSubType(), e));
867
            return null;
868
        }
869
    }
870

  
871
    /**
890 872
     * Create an Arc. If there is an
891 873
     * error return <code>null</code> and add the error
892 874
     * to the log
......
1145 1127
            return SUBTYPES.GEOM3D;
1146 1128
        }
1147 1129
    }
1130

  
1131
    /**
1132
     * Returns the type of the geometries to create.
1133
     * 
1134
     * @return the type of the geometries to create
1135
     */
1136
    protected GeometryType getGeometryType() {
1137
        return getCadToolAdapter().getActiveLayerGeometryType();
1138
    }
1139

  
1148 1140
}

Also available in: Unified diff