Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2057 / extensions / extEditing / src / org / gvsig / editing / gui / cad / DefaultCADTool.java @ 39154

History | View | Annotate | Download (37.8 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;
23

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

    
30
import org.cresques.cts.ICoordTrans;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.ui.mdiManager.IWindow;
37
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
38
import org.gvsig.editing.CADExtension;
39
import org.gvsig.editing.gui.cad.exception.CommandException;
40
import org.gvsig.editing.layers.VectorialLayerEdited;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.ReadException;
43
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
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.Geometry.TYPES;
52
import org.gvsig.fmap.geom.GeometryLocator;
53
import org.gvsig.fmap.geom.GeometryManager;
54
import org.gvsig.fmap.geom.aggregate.MultiCurve;
55
import org.gvsig.fmap.geom.aggregate.MultiPoint;
56
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
57
import org.gvsig.fmap.geom.aggregate.MultiSurface;
58
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
59
import org.gvsig.fmap.geom.handler.Handler;
60
import org.gvsig.fmap.geom.primitive.Arc;
61
import org.gvsig.fmap.geom.primitive.Circle;
62
import org.gvsig.fmap.geom.primitive.Curve;
63
import org.gvsig.fmap.geom.primitive.Ellipse;
64
import org.gvsig.fmap.geom.primitive.Envelope;
65
import org.gvsig.fmap.geom.primitive.GeneralPathX;
66
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
67
import org.gvsig.fmap.geom.primitive.Point;
68
import org.gvsig.fmap.geom.primitive.Primitive;
69
import org.gvsig.fmap.geom.primitive.Spline;
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;
74
import org.gvsig.fmap.geom.util.UtilFunctions;
75
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
76
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
77
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
78
import org.gvsig.fmap.mapcontrol.MapControlLocator;
79
import org.gvsig.fmap.mapcontrol.MapControlManager;
80
import org.gvsig.tools.dispose.DisposableIterator;
81
import org.gvsig.utils.console.JConsole;
82

    
83
/**
84
 * 
85
 * @author Vicente Caballero Navarro
86
 */
87
public abstract class DefaultCADTool implements CADTool, Geometry.TYPES,
88
    Geometry.SUBTYPES {
89

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

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

    
102
    private CADToolAdapter cadToolAdapter;
103

    
104
    private String question;
105

    
106
    private String[] currentdescriptions;
107

    
108
    private String tool = "selection";
109

    
110
    private DefaultCADTool previousTool;
111

    
112
    private List<Geometry> temporalCache = new ArrayList<Geometry>();
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

    
136
    public void addTemporalCache(Geometry geom) {
137
        
138
        if (geom == null) {
139
            try {
140
                throw new IllegalArgumentException("Null geometry");
141
            } catch (Exception ex) {
142
                LOG.info("Tried to add null geometry to temporal cache", ex);
143
            }
144
            return;
145
        }
146
        
147
        temporalCache.add(geom);
148
        try {
149
            insertSpatialCache(geom);
150
        } catch (CreateEnvelopeException e) {
151
            LOG.error("Error adding the spatial cache", e);
152
        }
153
    }
154

    
155
    public void clearTemporalCache() {
156
        Geometry[] geoms =
157
            temporalCache.toArray(new Geometry[temporalCache.size()]);
158
        for (int i = 0; i < geoms.length; i++) {
159
            try {
160
                removeSpatialCache(geoms[i]);
161
            } catch (CreateEnvelopeException e) {
162
                LOG.error("Error removing the temporal cache", e);
163
            }
164
        }
165
        temporalCache.clear();
166
    }
167

    
168
    private void insertSpatialCache(Geometry geom)
169
        throws CreateEnvelopeException {
170
        VectorialLayerEdited vle = getVLE();
171
        SpatialCache spatialCache =
172
            ((FLyrVect) vle.getLayer()).getSpatialCache();
173
        Envelope r = geom.getEnvelope();
174
        if (geom.getType() == Geometry.TYPES.POINT) {
175
            r =
176
                geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
177
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
178
        }
179
        spatialCache.insert(r, geom);
180

    
181
    }
182

    
183
    private void removeSpatialCache(Geometry geom)
184
        throws CreateEnvelopeException {
185
        VectorialLayerEdited vle = getVLE();
186
        SpatialCache spatialCache =
187
            ((FLyrVect) vle.getLayer()).getSpatialCache();
188
        Envelope r = geom.getEnvelope();
189
        if (geom.getType() == Geometry.TYPES.POINT) {
190
            r =
191
                geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
192
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);// Rectangle2D.Double(r.getX(),r.getY(),1,1);
193
        }
194
        spatialCache.remove(r, geom);
195

    
196
    }
197

    
198
    /**
199
     * DOCUMENT ME!
200
     * 
201
     * @param feature
202
     */
203
    public void draw(Geometry geometry, Feature feature) {
204
        if (geometry != null) {
205
            getCadToolAdapter().getMapControl().getMapControlDrawer()
206
                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
207
        }
208
    }
209

    
210
    /**
211
     * It draw the selected geometries from the initial point until a new
212
     * point.
213
     * 
214
     * @param mapControlDrawer
215
     *            The drawer.
216
     * @param firstPoint
217
     *            The initial point.
218
     * @param x
219
     *            The X coordinate of the mouse.
220
     * @param y
221
     *            The Y coordinate of the mouse.
222
     */
223
    protected void drawSelectedGeometries(MapControlDrawer mapControlDrawer,
224
        Point2D firstPoint, double x, double y) {
225
        FeatureStore featureStore = null;
226
        DisposableIterator iterator = null;
227
        ICoordTrans ct = getVLE().getLayer().getCoordTrans();
228

    
229
        try {
230
            featureStore = getVLE().getFeatureStore();
231
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
232

    
233
            iterator = selection.fastIterator();
234
            while (iterator.hasNext()) {
235
                Feature feature = (Feature) iterator.next();
236
                Geometry geometry =
237
                    (feature.getDefaultGeometry()).cloneGeometry();
238

    
239
                if (geometry == null) {
240
                    continue;
241
                }
242

    
243
                Point2D currentPoint = new Point2D.Double(x, y);
244
                if (ct != null) {
245
                    currentPoint = ct.getInverted().convert(currentPoint, null);
246
                }
247

    
248
                geometry.move(currentPoint.getX() - firstPoint.getX(),
249
                    currentPoint.getY() - firstPoint.getY());
250

    
251
                mapControlDrawer.draw(geometry,
252
                    mapControlManager.getGeometrySelectionSymbol());
253
            }
254
        } catch (Exception e) {
255
            LOG.error("Retrieving the selection", e);
256
        } finally {
257
            if (iterator != null) {
258
                iterator.dispose();
259
            }
260
        }
261
    }
262

    
263
    /**
264
     * It draw the selected geometries from the initial point until a new
265
     * point.
266
     * 
267
     * @param mapControlDrawer
268
     *            The drawer.
269
     * @param firstPoint
270
     *            The initial point.
271
     * @param x
272
     *            The X coordinate of the mouse.
273
     * @param y
274
     *            The Y coordinate of the mouse.
275
     * @param affineTransform
276
     *            The transformation to apply
277
     */
278
    protected void drawAndRotateSelectedGeometries(
279
        MapControlDrawer mapControlDrawer, Point2D firstPoint, double x,
280
        double y) {
281
        FeatureStore featureStore = null;
282
        DisposableIterator iterator = null;
283

    
284
        try {
285
            Point2D lastPoint = new Point2D.Double(x, y);
286

    
287
            double w = lastPoint.getX() - firstPoint.getX();
288
            double h = lastPoint.getY() - firstPoint.getY();
289

    
290
            featureStore = getVLE().getFeatureStore();
291
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
292

    
293
            iterator = selection.fastIterator();
294
            while (iterator.hasNext()) {
295
                Feature feature = (Feature) iterator.next();
296
                Geometry geometry =
297
                    (feature.getDefaultGeometry()).cloneGeometry();
298

    
299
                if (geometry == null) {
300
                    continue;
301
                }
302

    
303
                geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2),
304
                    firstPoint.getX(), firstPoint.getY());
305

    
306
                mapControlDrawer.draw(geometry,
307
                    mapControlManager.getGeometrySelectionSymbol());
308
            }
309
        } catch (Exception e) {
310
            LOG.error("Retrieving the selection", e);
311
        } finally {
312
            if (iterator != null) {
313
                iterator.dispose();
314
            }
315
        }
316
    }
317

    
318
    public void setCadToolAdapter(CADToolAdapter cta) {
319
        cadToolAdapter = cta;
320
    }
321

    
322
    public CADToolAdapter getCadToolAdapter() {
323
        return cadToolAdapter;
324
    }
325

    
326
    public VectorialLayerEdited getVLE() {
327
        return (VectorialLayerEdited) CADExtension.getEditionManager()
328
            .getActiveLayerEdited();
329
    }
330

    
331
    public Feature insertGeometry(Geometry geometry, Feature feature) {
332
        VectorialLayerEdited vle = getVLE();
333

    
334
        try {
335
            FeatureStore featureStore =
336
                ((FLyrVect) vle.getLayer()).getFeatureStore();
337
            EditableFeature eFeature =
338
                featureStore.createNewFeature(
339
                    featureStore.getDefaultFeatureType(), feature);
340
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
341
                .getDefaultGeometryAttributeName(), geometry);
342
            featureStore.insert(eFeature);
343
            // drawToImage(featureStore, vle, eFeature);
344
            insertSpatialCache(geometry);
345

    
346
            getCadToolAdapter().getMapControl().getMapControlDrawer()
347
                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
348

    
349
            return eFeature;
350
        } catch (ReadException e) {
351
            NotificationManager.addError(e.getMessage(), e);
352
            return null;
353
        } catch (DataException e) {
354
            NotificationManager.addError(e.getMessage(), e);
355
            return null;
356
        } catch (CreateEnvelopeException e) {
357
            NotificationManager.addError(e.getMessage(), e);
358
            return null;
359
        }
360
    }
361

    
362
    public Feature insertAndSelectGeometry(Geometry geometry) {
363
        Feature feature = null;
364
        try {
365
            FeatureStore featureStore = getVLE().getFeatureStore();
366
            featureStore.beginComplexNotification();
367
            featureStore.beginEditingGroup(getName());
368
            FeatureSelection newSelection =
369
                featureStore.createFeatureSelection();
370
            feature = insertGeometry(geometry);
371
            newSelection.select(feature);
372
            featureStore.setSelection(newSelection);
373
            featureStore.endEditingGroup();
374
            featureStore.endComplexNotification();
375
        } catch (DataException e) {
376
            NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
377
        }
378
        return feature;
379
    }
380

    
381
    public Feature insertGeometry(Geometry geometry) {
382
        VectorialLayerEdited vle = getVLE();
383

    
384
        try {
385
            FeatureStore featureStore =
386
                ((FLyrVect) vle.getLayer()).getFeatureStore();
387
            EditableFeature eFeature = featureStore.createNewFeature(true);
388
           
389
            //Reproject the geometry
390
            Geometry insertedGeometry = geometry;
391
            if (getVLE().getLayer().getCoordTrans() != null){
392
                insertedGeometry = insertedGeometry.cloneGeometry();
393
                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
394
            }
395
            
396
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
397
                .getDefaultGeometryAttributeName(), insertedGeometry);
398
            featureStore.insert(eFeature);
399

    
400
            insertSpatialCache(insertedGeometry);
401

    
402
            draw(insertedGeometry, eFeature);
403
            return eFeature;
404
        } catch (ReadException e) {
405
            NotificationManager.addError(e.getMessage(), e);
406
            return null;
407
        } catch (DataException e) {
408
            NotificationManager.addError(e.getMessage(), e);
409
            return null;
410
        } catch (CreateEnvelopeException e) {
411
            NotificationManager.addError(e.getMessage(), e);
412
            return null;
413
        }
414

    
415
    }
416

    
417
    /**
418
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
419
     * 
420
     * @return Cadena para mostrar por consola.
421
     */
422
    public String getQuestion() {
423
        return question;
424
    }
425

    
426
    /**
427
     * Actualiza la cadena que corresponde al estado actual.
428
     * 
429
     * @param s
430
     *            Cadena que aparecer� en consola.
431
     */
432
    public void setQuestion(String s) {
433
        question = s;
434
    }
435

    
436
    /**
437
     * Provoca un repintado "soft" de la capa activa en edici�n. Las capas por
438
     * debajo de ella no se dibujan de verdad, solo se dibuja la que est� en
439
     * edici�n y las que est�n por encima de ella en el TOC.
440
     */
441
    public void refresh() {
442
        getCadToolAdapter().getMapControl().rePaintDirtyLayers();
443
    }
444

    
445
    public void drawHandlers(MapControlDrawer renderer, ArrayList selectedRows,
446
        AffineTransform at) {
447
        FeatureSet selection = null;
448
        DisposableIterator iterator = null;
449
        try {
450
            selection =
451
                (FeatureSet) ((FLyrVect) getVLE().getLayer()).getFeatureStore()
452
                    .getSelection();
453

    
454
            iterator = selection.fastIterator();
455
            while (iterator.hasNext()) {
456
                Feature feature = (Feature) iterator.next();
457

    
458
                Geometry ig = (feature.getDefaultGeometry()).cloneGeometry();
459
                if (ig == null) {
460
                    continue;
461
                }
462
                Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
463
                renderer.drawHandlers(handlers, at,
464
                    mapControlManager.getGeometrySelectionSymbol());
465
            }
466

    
467
        } catch (ReadException e) {
468
            NotificationManager.addError(e.getMessage(), e);
469
        } catch (DataException e) {
470
            NotificationManager.addError(e.getMessage(), e);
471
        } finally {
472
            if (iterator != null) {
473
                iterator.dispose();
474
            }
475
        }
476

    
477
    }
478

    
479
    public void setDescription(String[] currentdescriptions) {
480
        this.currentdescriptions = currentdescriptions;
481
    }
482

    
483
    public String[] getDescriptions() {
484
        return currentdescriptions;
485
    }
486

    
487
    public void end() {
488
        CADExtension.setCADTool("_selection", true);
489
        PluginServices.getMainFrame().setSelectedTool("_selection");
490
        CADTool cadtool = CADExtension.getCADTool();
491
        cadtool.setPreviosTool(this);
492
        cadtool.getVLE().getLayer().getMapContext().invalidate();
493
    }
494

    
495
    public void init() {
496
        // Nothing to do
497
    }
498

    
499
    protected ArrayList getSelectedHandlers() {
500
        VectorialLayerEdited vle = getVLE();
501
        ArrayList selectedHandlers = vle.getSelectedHandler();
502
        return selectedHandlers;
503
    }
504

    
505
    public void clearSelection() throws DataException {
506
        VectorialLayerEdited vle = getVLE();
507
        FeatureSelection selection = null;
508
        selection =
509
            (FeatureSelection) ((FLyrVect) vle.getLayer()).getFeatureStore()
510
                .getSelection();
511
        // ArrayList selectedRow = vle.getSelectedRow();
512
        ArrayList selectedHandlers = vle.getSelectedHandler();
513
        selection.deselectAll();
514
        selectedHandlers.clear();
515
        // VectorialEditableAdapter vea = vle.getVEA();
516
        // FBitSet selection = vea.getSelection();
517
        // selection.clear();
518
        vle.setSelectionImage(null);
519
        vle.setHandlersImage(null);
520

    
521
    }
522

    
523
    public String getNextTool() {
524
        return tool;
525
    }
526

    
527
    public void setNextTool(String tool) {
528
        this.tool = tool;
529
    }
530

    
531
    public boolean changeCommand(String name) throws CommandException {
532
        CADTool[] cadtools = CADExtension.getCADTools();
533
        for (int i = 0; i < cadtools.length; i++) {
534
            CADTool ct = cadtools[i];
535
            if (name.equalsIgnoreCase(ct.getName())
536
                || name.equalsIgnoreCase(ct.toString())) {
537
                int type = Geometry.TYPES.POINT;
538
                try {
539
                    type = ((FLyrVect) getVLE().getLayer()).getShapeType();
540
                } catch (ReadException e) {
541
                    throw new CommandException(e);
542
                }
543
                if (ct.isApplicable(type)) {
544
                    getCadToolAdapter().setCadTool(ct);
545
                    ct.init();
546
                    DefaultViewPanel vista =
547
                        (DefaultViewPanel) PluginServices.getMDIManager()
548
                            .getActiveWindow();
549
                    vista.getConsolePanel().addText("\n" + ct.getName(),
550
                        JConsole.COMMAND);
551
                    String question = ct.getQuestion();
552
                    vista.getConsolePanel().addText(
553
                        "\n" + "#" + question + " > ", JConsole.MESSAGE);
554
                    return true;
555
                }
556
                throw new CommandException(name);
557
            }
558
        }
559
        return false;
560
    }
561

    
562
    public boolean isApplicable(int shapeType) {
563
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
564
        return isApplicable(type);
565
    }
566

    
567
    public boolean isApplicable(GeometryType geometryType) {
568
        if (supportedTypes != null) {
569
            for (int i = 0; i < supportedTypes.length; i++) {
570
                if (supportedTypes[i].isTypeOf(geometryType)) {
571
                    return true;
572
                }
573
            }
574
        }
575
        return false;
576
    }
577

    
578
    /**
579
     * Returns the geometry types supported by the tool.
580
     * 
581
     * @return the supported geometry types
582
     */
583
    protected GeometryType[] getSupportedTypes() {
584
        return supportedTypes;
585
    }
586

    
587
    /**
588
     * Returns the list of Geometry types supported by this tool. If any
589
     * Geometry type is supported, just return null.
590
     * 
591
     * @return the list of Geometry types supported by this tool
592
     */
593
    protected int[] getSupportedGeometryTypes() {
594
        return null;
595
    }
596

    
597
    protected GeometryType loadGeometryType(int type, int subtype) {
598
        try {
599
            return GeometryLocator.getGeometryManager().getGeometryType(type,
600
                subtype);
601
        } catch (GeometryTypeNotSupportedException e) {
602
            throw new RuntimeException(
603
                "Error getting the Geometry type with type = " + type
604
                    + ", subtype = " + subtype, e);
605
        } catch (GeometryTypeNotValidException e) {
606
            throw new RuntimeException(
607
                "Error getting the Geometry type with type = " + type
608
                    + ", subtype = " + subtype, e);
609
        }
610
    }
611

    
612
    public abstract String toString();
613

    
614
    public void throwValueException(String s, double d) {
615
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
616
        if (window instanceof DefaultViewPanel) {
617
            ((DefaultViewPanel) window).getConsolePanel().addText(
618
                s + " : " + d, JConsole.ERROR);
619
        }
620
    }
621

    
622
    public void throwOptionException(String s, String o) {
623
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
624
        if (window instanceof DefaultViewPanel) {
625
            ((DefaultViewPanel) window).getConsolePanel().addText(
626
                s + " : " + o, JConsole.ERROR);
627
        }
628
    }
629

    
630
    public void throwPointException(String s, double x, double y) {
631
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
632
        if (window instanceof DefaultViewPanel) {
633
            ((DefaultViewPanel) window).getConsolePanel().addText(
634
                s + " : " + " X = " + x + ", Y = " + y, JConsole.ERROR);
635
        }
636
    }
637

    
638
    public void setPreviosTool(DefaultCADTool tool) {
639
        previousTool = tool;
640
    }
641

    
642
    public void restorePreviousTool() {
643
        CADExtension.setCADTool(previousTool.toString(), true);
644
        PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
645
    }
646

    
647
    public void endTransition(double x, double y, MouseEvent e) {
648
        // Nothing to do
649
    }
650

    
651
    /**
652
     * Create a curve. If there is an
653
     * error return <code>null</code> and add the error
654
     * to the log
655
     * 
656
     * @return
657
     *         The Curve
658
     */
659
    protected Curve createCurve() {
660
        try {
661
            return (Curve) geomManager.create(TYPES.CURVE, getSubType());
662
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
663
            LOG.error("Error creating curve", new CreateGeometryException(
664
                TYPES.CURVE, getSubType(), e));
665
        }
666
        return null;
667
    }
668

    
669
    /**
670
     * Create a multicurve. If there is an
671
     * error return <code>null</code> and add the error
672
     * to the log
673
     * 
674
     * @return
675
     *         The MultiCurve
676
     */
677
    protected MultiCurve createMultiCurve() {
678
        try {
679
            return (MultiCurve) geomManager.create(TYPES.MULTICURVE,
680
                getSubType());
681
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
682
            LOG.error("Error creating multicurve", new CreateGeometryException(
683
                TYPES.MULTICURVE, getSubType(), e));
684
        }
685
        return null;
686
    }
687

    
688
    /**
689
     * Create an {@link OrientablePrimitive}. If there is an
690
     * error return <code>null</code> and add the error
691
     * to the log
692
     * 
693
     * @param geometryType
694
     *            a type of a OrientablePrimitive.
695
     * @return
696
     *         The {@link OrientablePrimitive}
697
     */
698
    protected OrientablePrimitive createOrientablePrimitive(int geometryType) {
699
        try {
700
            return (OrientablePrimitive) geomManager.create(geometryType,
701
                getSubType());
702
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
703
            LOG.error("Error creating curve", new CreateGeometryException(
704
                TYPES.CURVE, getSubType(), e));
705
        }
706
        return null;
707
    }
708

    
709
    /**
710
     * Create an envelope like a curve from two points.
711
     * If there is an error return <code>null</code> and add the error to the
712
     * log.
713
     * 
714
     * @param firstPoint
715
     *            first point
716
     * @param secondPoint
717
     *            second point
718
     * @return
719
     *         the curve
720
     */
721
    protected Curve createEnvelopeLikeCurve(Point2D firstPoint,
722
        Point2D secondPoint) {
723
        try {
724
            Curve curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
725
            curve.addMoveToVertex(createPoint(firstPoint.getX(),
726
                firstPoint.getY()));
727
            curve.addVertex(createPoint(secondPoint.getX(), firstPoint.getY()));
728
            curve
729
                .addVertex(createPoint(secondPoint.getX(), secondPoint.getY()));
730
            curve.addVertex(createPoint(firstPoint.getX(), secondPoint.getY()));
731
            curve.addVertex(createPoint(firstPoint.getX(), firstPoint.getY()));
732
            return curve;
733
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
734
            LOG.error("Error creating curve", new CreateGeometryException(
735
                TYPES.CURVE, getSubType(), e));
736
        }
737
        return null;
738
    }
739

    
740
    /**
741
     * Create a curve from a GeneralPath. If there is an
742
     * error return <code>null</code> and add the error
743
     * to the log
744
     * 
745
     * @param gpx
746
     *            The GeneralPath
747
     * @return
748
     *         The Curve
749
     */
750
    protected Curve createCurve(GeneralPathX gpx) {
751
        Curve curve = null;
752
        try {
753
            curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
754
            curve.setGeneralPath(gpx);
755
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
756
            LOG.error("Error creating curve", new CreateGeometryException(
757
                TYPES.CURVE, getSubType(), e));
758
        }
759
        return curve;
760
    }
761

    
762
    /**
763
     * Create a surface. If there is an
764
     * error return <code>null</code> and add the error
765
     * to the log
766
     * 
767
     * @return
768
     *         The Surface
769
     */
770
    protected Surface createSurface() {
771
        try {
772
            return (Surface) geomManager.create(TYPES.SURFACE, getSubType());
773

    
774
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
775
            LOG.error("Error creating surface", new CreateGeometryException(
776
                TYPES.SURFACE, getSubType(), e));
777
        }
778
        return null;
779
    }
780

    
781
    /**
782
     * Create a multisurface. If there is an
783
     * error return <code>null</code> and add the error
784
     * to the log
785
     * 
786
     * @return
787
     *         The MultiSurface
788
     */
789
    protected MultiSurface createMultiSurface() {
790
        try {
791
            return (MultiSurface) geomManager.create(TYPES.MULTISURFACE,
792
                getSubType());
793

    
794
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
795
            LOG.error(
796
                "Error creating multisurface",
797
                new CreateGeometryException(TYPES.MULTISURFACE, getSubType(), e));
798
        }
799
        return null;
800
    }
801

    
802
    /**
803
     * Create a surface from a GeneralPath. If there is an
804
     * error return <code>null</code> and add the error
805
     * to the log
806
     * 
807
     * @param gpx
808
     *            The general path
809
     * @return
810
     *         The Surface
811
     */
812
    protected Surface createSurface(GeneralPathX gpx) {
813
        Surface surface = null;
814
        try {
815
            surface = (Surface) geomManager.create(TYPES.SURFACE, getSubType());
816
            surface.setGeneralPath(gpx);
817
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
818
            LOG.error("Error creating surface", new CreateGeometryException(
819
                TYPES.SURFACE, getSubType(), e));
820
        }
821
        return surface;
822
    }
823

    
824
    /**
825
     * Create a curve point. If there is an
826
     * error return <code>null</code> and add the error
827
     * to the log
828
     * 
829
     * @param p1
830
     *            The AWT point
831
     * @return
832
     *         The gvSIG point
833
     */
834
    protected Point createPoint(Point2D p1) {
835
        return createPoint(p1.getX(), p1.getY());
836
    }
837

    
838
    /**
839
     * Create point. If there is an
840
     * error return <code>null</code> and add the error
841
     * to the log
842
     * 
843
     * @param x
844
     *            The X coordinate
845
     * @param y
846
     *            The y coordinate
847
     * @return
848
     *         The Point
849
     */
850
    protected Point createPoint(double x, double y) {
851
        Point point = null;
852
        try {
853
            point = (Point) geomManager.create(TYPES.POINT, getSubType());
854
            point.setX(x);
855
            point.setY(y);
856
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
857
            LOG.error("Error creating point with x=" + x + ", y=" + y,
858
                new CreateGeometryException(TYPES.POINT, getSubType(), e));
859
        }
860
        return point;
861
    }
862

    
863
    /**
864
     * Create a multipoint. If there is an
865
     * error return <code>null</code> and add the error
866
     * to the log
867
     * 
868
     * @param x
869
     *            The X coordinate
870
     * @param y
871
     *            The y coordinate
872
     * @return
873
     *         The MultiPoint
874
     */
875
    protected MultiPoint createMultiPoint() {
876
        try {
877
            return (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
878
                getSubType());
879
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
880
            LOG.error("Error creating multipoint", new CreateGeometryException(
881
                TYPES.MULTIPOINT, getSubType(), e));
882
            return null;
883
        }
884
    }
885

    
886
    /**
887
     * Create an Arc. If there is an
888
     * error return <code>null</code> and add the error
889
     * to the log
890
     * 
891
     * @param p1
892
     * @param p2
893
     * @param p3
894
     * @return
895
     *         The arc
896
     */
897
    protected Arc createArc(Point2D p1, Point2D p2, Point2D p3) {
898
        return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
899
    }
900

    
901
    /**
902
     * Create an arc. If there is an
903
     * error return <code>null</code> and add the error
904
     * to the log
905
     * 
906
     * @param p1
907
     * @param p2
908
     * @param p3
909
     * @return
910
     *         The arc
911
     */
912
    protected Arc createArc(Point p1, Point p2, Point p3) {
913
        Arc arc = null;
914
        if (p1.equals(p2) || p2.equals(p3) || p1.equals(p3)) {
915
            return null;
916
        }
917
        try {
918
            arc = (Arc) geomManager.create(TYPES.ARC, getSubType());
919
            arc.setPoints(p1, p2, p3);
920
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
921
            LOG.error("Error creating arc with p1=" + p1, ", p2=" + p2
922
                + ", p3=" + p3, new CreateGeometryException(TYPES.ARC,
923
                getSubType(), e));
924
        } catch (IllegalArgumentException ex) {
925
            LOG.info("Warning: unable to create arc from points: "
926
                + p1.getX() + " " + p1.getY() + " :: "
927
                + p2.getX() + " " + p2.getY() + " :: "
928
                + p3.getX() + " " + p3.getY());
929
            arc = null;
930
        }
931
        return arc;
932
    }
933

    
934
    /**
935
     * Create a circle. If there is an
936
     * error return <code>null</code> and add the error
937
     * to the log
938
     * 
939
     * @param p1
940
     * @param p2
941
     * @param p3
942
     * @return
943
     *         The Circle
944
     */
945
    protected Circle createCircle(Point p1, Point p2, Point p3) {
946
        Circle circle = null;
947
        try {
948
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
949
            circle.setPoints(p1, p2, p3);
950
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
951
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2
952
                + ", p3=" + p3, new CreateGeometryException(TYPES.CIRCLE,
953
                getSubType(), e));
954
        }
955
        return circle;
956
    }
957

    
958
    /**
959
     * Create a circle from a GeneralPath. If there is an
960
     * error return <code>null</code> and add the error
961
     * to the log
962
     * 
963
     * @param p1
964
     * @param p2
965
     * @return
966
     *         The circle
967
     */
968
    protected Circle createCircle(Point2D p1, Point2D p2) {
969
        return createCircle(createPoint(p1), createPoint(p2));
970
    }
971

    
972
    /**
973
     * Create a circle. If there is an
974
     * error return <code>null</code> and add the error
975
     * to the log
976
     * 
977
     * @param p1
978
     * @param p2
979
     * @return
980
     *         The circle
981
     */
982
    protected Circle createCircle(Point p1, Point p2) {
983
        Circle circle = null;
984
        try {
985
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
986
            circle.setPoints(p1, p2);
987
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
988
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2,
989
                new CreateGeometryException(TYPES.CIRCLE, getSubType(), e));
990
        }
991
        return circle;
992
    }
993

    
994
    /**
995
     * Create a circle. If there is an
996
     * error return <code>null</code> and add the error
997
     * to the log
998
     * 
999
     * @param p1
1000
     * @param radious
1001
     * @return
1002
     *         The Circle
1003
     */
1004
    protected Circle createCircle(Point2D p1, double radious) {
1005
        return createCircle(createPoint(p1), radious);
1006
    }
1007

    
1008
    /**
1009
     * Create a circle. If there is an
1010
     * error return <code>null</code> and add the error
1011
     * to the log
1012
     * 
1013
     * @param p1
1014
     * @param radious
1015
     * @return
1016
     *         The Circle
1017
     */
1018
    protected Circle createCircle(Point p1, double radious) {
1019
        Circle circle = null;
1020
        try {
1021
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
1022
            circle.setPoints(p1, radious);
1023
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1024
            LOG.error("Error creating circle with p1=" + p1 + " radious="
1025
                + radious, new CreateGeometryException(TYPES.CIRCLE,
1026
                getSubType(), e));
1027
        }
1028
        return circle;
1029
    }
1030

    
1031
    /**
1032
     * Create an Ellipse. If there is an
1033
     * error return <code>null</code> and add the error
1034
     * to the log
1035
     * 
1036
     * @param p1
1037
     * @param p2
1038
     * @param d
1039
     * @return
1040
     *         The Ellipse
1041
     */
1042
    protected Ellipse createEllipse(Point2D p1, Point2D p2, double d) {
1043
        return createEllipse(createPoint(p1), createPoint(p2), d);
1044
    }
1045

    
1046
    /**
1047
     * Create an Ellipse. If there is an
1048
     * error return <code>null</code> and add the error
1049
     * to the log
1050
     * 
1051
     * @param p1
1052
     * @param p2
1053
     * @param d
1054
     * @return
1055
     *         The Ellipse
1056
     */
1057
    protected Ellipse createEllipse(Point p1, Point p2, double d) {
1058
        Ellipse ellipse = null;
1059
        try {
1060
            ellipse = (Ellipse) geomManager.create(TYPES.ELLIPSE, getSubType());
1061
            ellipse.setPoints(p1, p2, d);
1062
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1063
            LOG.error("Error creating ellipse with p1=" + p1 + " p2=" + p2
1064
                + ", d=" + d, new CreateGeometryException(TYPES.ELLIPSE,
1065
                getSubType(), e));
1066
        }
1067
        return ellipse;
1068
    }
1069

    
1070
    /**
1071
     * Create a Spline from a GeneralPath. If there is an
1072
     * error return <code>null</code> and add the error
1073
     * to the log
1074
     * 
1075
     * @param points
1076
     * @return
1077
     *         The Spline
1078
     */
1079
    protected Spline createSpline(Point2D[] points) {
1080
        Spline spline = null;
1081
        try {
1082
            spline = (Spline) geomManager.create(TYPES.SPLINE, getSubType());
1083
            for (int i = 0; i < points.length; i++) {
1084
                spline.addVertex(createPoint(points[i]));
1085
            }
1086
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1087
            LOG.error("Error creating spline", new CreateGeometryException(
1088
                TYPES.SPLINE, getSubType(), e));
1089
        }
1090
        return spline;
1091
    }
1092

    
1093
    /**
1094
     * Create a MultiPrimitive. If there is an
1095
     * error return <code>null</code> and add the error
1096
     * to the log
1097
     * 
1098
     * @return
1099
     */
1100
    protected MultiPrimitive createMultiPrimitive() {
1101
        try {
1102
            return (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1103
                getSubType());
1104
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1105
            LOG.error("Error creating MultiPrimitive",
1106
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1107
        }
1108
        return null;
1109
    }
1110

    
1111
    /**
1112
     * Create a MultiPrimitive. If there is an
1113
     * error return <code>null</code> and add the error
1114
     * to the log
1115
     * 
1116
     * @param geometries
1117
     * @return
1118
     */
1119
    protected MultiPrimitive createMultiPrimitive(Geometry[] geometries) {
1120
        MultiPrimitive multiPrimitive = null;
1121
        try {
1122
            multiPrimitive =
1123
                (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1124
                    getSubType());
1125
            for (int i = 0; i < geometries.length; i++) {
1126
                multiPrimitive.addPrimitive((Primitive) geometries[i]);
1127
            }
1128
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1129
            LOG.error("Error creating MultiPrimitive",
1130
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1131
        }
1132
        return multiPrimitive;
1133
    }
1134

    
1135
    /**
1136
     * @return the subtype of the default geometry.
1137
     */
1138
    protected int getSubType() {
1139
        FeatureStore featureStore =
1140
            ((FLyrVect) getVLE().getLayer()).getFeatureStore();
1141
        try {
1142
            return featureStore.getDefaultFeatureType()
1143
                .getDefaultGeometryAttribute().getGeometrySubType();
1144
        } catch (DataException e) {
1145
            LOG.error(
1146
                "Error getting subtype of the default feature type of the store: "
1147
                    + featureStore, e);
1148
            return SUBTYPES.GEOM3D;
1149
        }
1150
    }
1151

    
1152
    /**
1153
     * Returns the type of the geometries to create.
1154
     * 
1155
     * @return the type of the geometries to create
1156
     */
1157
    protected GeometryType getGeometryType() {
1158
        return getCadToolAdapter().getActiveLayerGeometryType();
1159
    }
1160

    
1161
}