Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / DefaultCADTool.java @ 39055

History | View | Annotate | Download (37.2 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
        temporalCache.add(geom);
138
        try {
139
            insertSpatialCache(geom);
140
        } catch (CreateEnvelopeException e) {
141
            LOG.error("Error adding the spatial cache", e);
142
        }
143
    }
144

    
145
    public void clearTemporalCache() {
146
        Geometry[] geoms =
147
            temporalCache.toArray(new Geometry[temporalCache.size()]);
148
        for (int i = 0; i < geoms.length; i++) {
149
            try {
150
                removeSpatialCache(geoms[i]);
151
            } catch (CreateEnvelopeException e) {
152
                LOG.error("Error removing the temporal cache", e);
153
            }
154
        }
155
        temporalCache.clear();
156
    }
157

    
158
    private void insertSpatialCache(Geometry geom)
159
        throws CreateEnvelopeException {
160
        VectorialLayerEdited vle = getVLE();
161
        SpatialCache spatialCache =
162
            ((FLyrVect) vle.getLayer()).getSpatialCache();
163
        Envelope r = geom.getEnvelope();
164
        if (geom.getType() == Geometry.TYPES.POINT) {
165
            r =
166
                geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
167
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
168
        }
169
        spatialCache.insert(r, geom);
170

    
171
    }
172

    
173
    private void removeSpatialCache(Geometry geom)
174
        throws CreateEnvelopeException {
175
        VectorialLayerEdited vle = getVLE();
176
        SpatialCache spatialCache =
177
            ((FLyrVect) vle.getLayer()).getSpatialCache();
178
        Envelope r = geom.getEnvelope();
179
        if (geom.getType() == Geometry.TYPES.POINT) {
180
            r =
181
                geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
182
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);// Rectangle2D.Double(r.getX(),r.getY(),1,1);
183
        }
184
        spatialCache.remove(r, geom);
185

    
186
    }
187

    
188
    /**
189
     * DOCUMENT ME!
190
     * 
191
     * @param feature
192
     */
193
    public void draw(Geometry geometry, Feature feature) {
194
        if (geometry != null) {
195
            getCadToolAdapter().getMapControl().getMapControlDrawer()
196
                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
197
        }
198
    }
199

    
200
    /**
201
     * It draw the selected geometries from the initial point until a new
202
     * point.
203
     * 
204
     * @param mapControlDrawer
205
     *            The drawer.
206
     * @param firstPoint
207
     *            The initial point.
208
     * @param x
209
     *            The X coordinate of the mouse.
210
     * @param y
211
     *            The Y coordinate of the mouse.
212
     */
213
    protected void drawSelectedGeometries(MapControlDrawer mapControlDrawer,
214
        Point2D firstPoint, double x, double y) {
215
        FeatureStore featureStore = null;
216
        DisposableIterator iterator = null;
217
        ICoordTrans ct = getVLE().getLayer().getCoordTrans();
218

    
219
        try {
220
            featureStore = getVLE().getFeatureStore();
221
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
222

    
223
            iterator = selection.fastIterator();
224
            while (iterator.hasNext()) {
225
                Feature feature = (Feature) iterator.next();
226
                Geometry geometry =
227
                    (feature.getDefaultGeometry()).cloneGeometry();
228

    
229
                if (geometry == null) {
230
                    continue;
231
                }
232

    
233
                Point2D currentPoint = new Point2D.Double(x, y);
234
                if (ct != null) {
235
                    currentPoint = ct.getInverted().convert(currentPoint, null);
236
                }
237

    
238
                geometry.move(currentPoint.getX() - firstPoint.getX(),
239
                    currentPoint.getY() - firstPoint.getY());
240

    
241
                mapControlDrawer.draw(geometry,
242
                    mapControlManager.getGeometrySelectionSymbol());
243
            }
244
        } catch (Exception e) {
245
            LOG.error("Retrieving the selection", e);
246
        } finally {
247
            if (iterator != null) {
248
                iterator.dispose();
249
            }
250
        }
251
    }
252

    
253
    /**
254
     * It draw the selected geometries from the initial point until a new
255
     * point.
256
     * 
257
     * @param mapControlDrawer
258
     *            The drawer.
259
     * @param firstPoint
260
     *            The initial point.
261
     * @param x
262
     *            The X coordinate of the mouse.
263
     * @param y
264
     *            The Y coordinate of the mouse.
265
     * @param affineTransform
266
     *            The transformation to apply
267
     */
268
    protected void drawAndRotateSelectedGeometries(
269
        MapControlDrawer mapControlDrawer, Point2D firstPoint, double x,
270
        double y) {
271
        FeatureStore featureStore = null;
272
        DisposableIterator iterator = null;
273

    
274
        try {
275
            Point2D lastPoint = new Point2D.Double(x, y);
276

    
277
            double w = lastPoint.getX() - firstPoint.getX();
278
            double h = lastPoint.getY() - firstPoint.getY();
279

    
280
            featureStore = getVLE().getFeatureStore();
281
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
282

    
283
            iterator = selection.fastIterator();
284
            while (iterator.hasNext()) {
285
                Feature feature = (Feature) iterator.next();
286
                Geometry geometry =
287
                    (feature.getDefaultGeometry()).cloneGeometry();
288

    
289
                if (geometry == null) {
290
                    continue;
291
                }
292

    
293
                geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2),
294
                    firstPoint.getX(), firstPoint.getY());
295

    
296
                mapControlDrawer.draw(geometry,
297
                    mapControlManager.getGeometrySelectionSymbol());
298
            }
299
        } catch (Exception e) {
300
            LOG.error("Retrieving the selection", e);
301
        } finally {
302
            if (iterator != null) {
303
                iterator.dispose();
304
            }
305
        }
306
    }
307

    
308
    public void setCadToolAdapter(CADToolAdapter cta) {
309
        cadToolAdapter = cta;
310
    }
311

    
312
    public CADToolAdapter getCadToolAdapter() {
313
        return cadToolAdapter;
314
    }
315

    
316
    public VectorialLayerEdited getVLE() {
317
        return (VectorialLayerEdited) CADExtension.getEditionManager()
318
            .getActiveLayerEdited();
319
    }
320

    
321
    public Feature insertGeometry(Geometry geometry, Feature feature) {
322
        VectorialLayerEdited vle = getVLE();
323

    
324
        try {
325
            FeatureStore featureStore =
326
                ((FLyrVect) vle.getLayer()).getFeatureStore();
327
            EditableFeature eFeature =
328
                featureStore.createNewFeature(
329
                    featureStore.getDefaultFeatureType(), feature);
330
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
331
                .getDefaultGeometryAttributeName(), geometry);
332
            featureStore.insert(eFeature);
333
            // drawToImage(featureStore, vle, eFeature);
334
            insertSpatialCache(geometry);
335

    
336
            getCadToolAdapter().getMapControl().getMapControlDrawer()
337
                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
338

    
339
            return eFeature;
340
        } catch (ReadException e) {
341
            NotificationManager.addError(e.getMessage(), e);
342
            return null;
343
        } catch (DataException e) {
344
            NotificationManager.addError(e.getMessage(), e);
345
            return null;
346
        } catch (CreateEnvelopeException e) {
347
            NotificationManager.addError(e.getMessage(), e);
348
            return null;
349
        }
350
    }
351

    
352
    public Feature insertAndSelectGeometry(Geometry geometry) {
353
        Feature feature = null;
354
        try {
355
            FeatureStore featureStore = getVLE().getFeatureStore();
356
            featureStore.beginComplexNotification();
357
            featureStore.beginEditingGroup(getName());
358
            FeatureSelection newSelection =
359
                featureStore.createFeatureSelection();
360
            feature = insertGeometry(geometry);
361
            newSelection.select(feature);
362
            featureStore.setSelection(newSelection);
363
            featureStore.endEditingGroup();
364
            featureStore.endComplexNotification();
365
        } catch (DataException e) {
366
            NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
367
        }
368
        return feature;
369
    }
370

    
371
    public Feature insertGeometry(Geometry geometry) {
372
        VectorialLayerEdited vle = getVLE();
373

    
374
        try {
375
            FeatureStore featureStore =
376
                ((FLyrVect) vle.getLayer()).getFeatureStore();
377
            EditableFeature eFeature = featureStore.createNewFeature(true);
378
           
379
            //Reproject the geometry
380
            Geometry insertedGeometry = geometry;
381
            if (getVLE().getLayer().getCoordTrans() != null){
382
                insertedGeometry = insertedGeometry.cloneGeometry();
383
                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
384
            }
385
            
386
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
387
                .getDefaultGeometryAttributeName(), insertedGeometry);
388
            featureStore.insert(eFeature);
389

    
390
            insertSpatialCache(insertedGeometry);
391

    
392
            draw(insertedGeometry, eFeature);
393
            return eFeature;
394
        } catch (ReadException e) {
395
            NotificationManager.addError(e.getMessage(), e);
396
            return null;
397
        } catch (DataException e) {
398
            NotificationManager.addError(e.getMessage(), e);
399
            return null;
400
        } catch (CreateEnvelopeException e) {
401
            NotificationManager.addError(e.getMessage(), e);
402
            return null;
403
        }
404

    
405
    }
406

    
407
    /**
408
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
409
     * 
410
     * @return Cadena para mostrar por consola.
411
     */
412
    public String getQuestion() {
413
        return question;
414
    }
415

    
416
    /**
417
     * Actualiza la cadena que corresponde al estado actual.
418
     * 
419
     * @param s
420
     *            Cadena que aparecer� en consola.
421
     */
422
    public void setQuestion(String s) {
423
        question = s;
424
    }
425

    
426
    /**
427
     * Provoca un repintado "soft" de la capa activa en edici�n. Las capas por
428
     * debajo de ella no se dibujan de verdad, solo se dibuja la que est� en
429
     * edici�n y las que est�n por encima de ella en el TOC.
430
     */
431
    public void refresh() {
432
        getCadToolAdapter().getMapControl().rePaintDirtyLayers();
433
    }
434

    
435
    public void drawHandlers(MapControlDrawer renderer, ArrayList selectedRows,
436
        AffineTransform at) {
437
        FeatureSet selection = null;
438
        DisposableIterator iterator = null;
439
        try {
440
            selection =
441
                (FeatureSet) ((FLyrVect) getVLE().getLayer()).getFeatureStore()
442
                    .getSelection();
443

    
444
            iterator = selection.fastIterator();
445
            while (iterator.hasNext()) {
446
                Feature feature = (Feature) iterator.next();
447

    
448
                Geometry ig = (feature.getDefaultGeometry()).cloneGeometry();
449
                if (ig == null) {
450
                    continue;
451
                }
452
                Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
453
                renderer.drawHandlers(handlers, at,
454
                    mapControlManager.getGeometrySelectionSymbol());
455
            }
456

    
457
        } catch (ReadException e) {
458
            NotificationManager.addError(e.getMessage(), e);
459
        } catch (DataException e) {
460
            NotificationManager.addError(e.getMessage(), e);
461
        } finally {
462
            if (iterator != null) {
463
                iterator.dispose();
464
            }
465
        }
466

    
467
    }
468

    
469
    public void setDescription(String[] currentdescriptions) {
470
        this.currentdescriptions = currentdescriptions;
471
    }
472

    
473
    public String[] getDescriptions() {
474
        return currentdescriptions;
475
    }
476

    
477
    public void end() {
478
        CADExtension.setCADTool("_selection", true);
479
        PluginServices.getMainFrame().setSelectedTool("_selection");
480
        CADTool cadtool = CADExtension.getCADTool();
481
        cadtool.setPreviosTool(this);
482
        cadtool.getVLE().getLayer().getMapContext().invalidate();
483
    }
484

    
485
    public void init() {
486
        // Nothing to do
487
    }
488

    
489
    protected ArrayList getSelectedHandlers() {
490
        VectorialLayerEdited vle = getVLE();
491
        ArrayList selectedHandlers = vle.getSelectedHandler();
492
        return selectedHandlers;
493
    }
494

    
495
    public void clearSelection() throws DataException {
496
        VectorialLayerEdited vle = getVLE();
497
        FeatureSelection selection = null;
498
        selection =
499
            (FeatureSelection) ((FLyrVect) vle.getLayer()).getFeatureStore()
500
                .getSelection();
501
        // ArrayList selectedRow = vle.getSelectedRow();
502
        ArrayList selectedHandlers = vle.getSelectedHandler();
503
        selection.deselectAll();
504
        selectedHandlers.clear();
505
        // VectorialEditableAdapter vea = vle.getVEA();
506
        // FBitSet selection = vea.getSelection();
507
        // selection.clear();
508
        vle.setSelectionImage(null);
509
        vle.setHandlersImage(null);
510

    
511
    }
512

    
513
    public String getNextTool() {
514
        return tool;
515
    }
516

    
517
    public void setNextTool(String tool) {
518
        this.tool = tool;
519
    }
520

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

    
552
    public boolean isApplicable(int shapeType) {
553
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
554
        return isApplicable(type);
555
    }
556

    
557
    public boolean isApplicable(GeometryType geometryType) {
558
        if (supportedTypes != null) {
559
            for (int i = 0; i < supportedTypes.length; i++) {
560
                if (supportedTypes[i].isTypeOf(geometryType)) {
561
                    return true;
562
                }
563
            }
564
        }
565
        return false;
566
    }
567

    
568
    /**
569
     * Returns the geometry types supported by the tool.
570
     * 
571
     * @return the supported geometry types
572
     */
573
    protected GeometryType[] getSupportedTypes() {
574
        return supportedTypes;
575
    }
576

    
577
    /**
578
     * Returns the list of Geometry types supported by this tool. If any
579
     * Geometry type is supported, just return null.
580
     * 
581
     * @return the list of Geometry types supported by this tool
582
     */
583
    protected int[] getSupportedGeometryTypes() {
584
        return null;
585
    }
586

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

    
602
    public abstract String toString();
603

    
604
    public void throwValueException(String s, double d) {
605
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
606
        if (window instanceof DefaultViewPanel) {
607
            ((DefaultViewPanel) window).getConsolePanel().addText(
608
                s + " : " + d, JConsole.ERROR);
609
        }
610
    }
611

    
612
    public void throwOptionException(String s, String o) {
613
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
614
        if (window instanceof DefaultViewPanel) {
615
            ((DefaultViewPanel) window).getConsolePanel().addText(
616
                s + " : " + o, JConsole.ERROR);
617
        }
618
    }
619

    
620
    public void throwPointException(String s, double x, double y) {
621
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
622
        if (window instanceof DefaultViewPanel) {
623
            ((DefaultViewPanel) window).getConsolePanel().addText(
624
                s + " : " + " X = " + x + ", Y = " + y, JConsole.ERROR);
625
        }
626
    }
627

    
628
    public void setPreviosTool(DefaultCADTool tool) {
629
        previousTool = tool;
630
    }
631

    
632
    public void restorePreviousTool() {
633
        CADExtension.setCADTool(previousTool.toString(), true);
634
        PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
635
    }
636

    
637
    public void endTransition(double x, double y, MouseEvent e) {
638
        // Nothing to do
639
    }
640

    
641
    /**
642
     * Create a curve. If there is an
643
     * error return <code>null</code> and add the error
644
     * to the log
645
     * 
646
     * @return
647
     *         The Curve
648
     */
649
    protected Curve createCurve() {
650
        try {
651
            return (Curve) geomManager.create(TYPES.CURVE, getSubType());
652
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
653
            LOG.error("Error creating curve", new CreateGeometryException(
654
                TYPES.CURVE, getSubType(), e));
655
        }
656
        return null;
657
    }
658

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

    
678
    /**
679
     * Create an {@link OrientablePrimitive}. If there is an
680
     * error return <code>null</code> and add the error
681
     * to the log
682
     * 
683
     * @param geometryType
684
     *            a type of a OrientablePrimitive.
685
     * @return
686
     *         The {@link OrientablePrimitive}
687
     */
688
    protected OrientablePrimitive createOrientablePrimitive(int geometryType) {
689
        try {
690
            return (OrientablePrimitive) geomManager.create(geometryType,
691
                getSubType());
692
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
693
            LOG.error("Error creating curve", new CreateGeometryException(
694
                TYPES.CURVE, getSubType(), e));
695
        }
696
        return null;
697
    }
698

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

    
730
    /**
731
     * Create a curve from a GeneralPath. If there is an
732
     * error return <code>null</code> and add the error
733
     * to the log
734
     * 
735
     * @param gpx
736
     *            The GeneralPath
737
     * @return
738
     *         The Curve
739
     */
740
    protected Curve createCurve(GeneralPathX gpx) {
741
        Curve curve = null;
742
        try {
743
            curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
744
            curve.setGeneralPath(gpx);
745
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
746
            LOG.error("Error creating curve", new CreateGeometryException(
747
                TYPES.CURVE, getSubType(), e));
748
        }
749
        return curve;
750
    }
751

    
752
    /**
753
     * Create a surface. If there is an
754
     * error return <code>null</code> and add the error
755
     * to the log
756
     * 
757
     * @return
758
     *         The Surface
759
     */
760
    protected Surface createSurface() {
761
        try {
762
            return (Surface) geomManager.create(TYPES.SURFACE, getSubType());
763

    
764
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
765
            LOG.error("Error creating surface", new CreateGeometryException(
766
                TYPES.SURFACE, getSubType(), e));
767
        }
768
        return null;
769
    }
770

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

    
784
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
785
            LOG.error(
786
                "Error creating multisurface",
787
                new CreateGeometryException(TYPES.MULTISURFACE, getSubType(), e));
788
        }
789
        return null;
790
    }
791

    
792
    /**
793
     * Create a surface from a GeneralPath. If there is an
794
     * error return <code>null</code> and add the error
795
     * to the log
796
     * 
797
     * @param gpx
798
     *            The general path
799
     * @return
800
     *         The Surface
801
     */
802
    protected Surface createSurface(GeneralPathX gpx) {
803
        Surface surface = null;
804
        try {
805
            surface = (Surface) geomManager.create(TYPES.SURFACE, getSubType());
806
            surface.setGeneralPath(gpx);
807
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
808
            LOG.error("Error creating surface", new CreateGeometryException(
809
                TYPES.SURFACE, getSubType(), e));
810
        }
811
        return surface;
812
    }
813

    
814
    /**
815
     * Create a curve point. If there is an
816
     * error return <code>null</code> and add the error
817
     * to the log
818
     * 
819
     * @param p1
820
     *            The AWT point
821
     * @return
822
     *         The gvSIG point
823
     */
824
    protected Point createPoint(Point2D p1) {
825
        return createPoint(p1.getX(), p1.getY());
826
    }
827

    
828
    /**
829
     * Create point. If there is an
830
     * error return <code>null</code> and add the error
831
     * to the log
832
     * 
833
     * @param x
834
     *            The X coordinate
835
     * @param y
836
     *            The y coordinate
837
     * @return
838
     *         The Point
839
     */
840
    protected Point createPoint(double x, double y) {
841
        Point point = null;
842
        try {
843
            point = (Point) geomManager.create(TYPES.POINT, getSubType());
844
            point.setX(x);
845
            point.setY(y);
846
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
847
            LOG.error("Error creating point with x=" + x + ", y=" + y,
848
                new CreateGeometryException(TYPES.POINT, getSubType(), e));
849
        }
850
        return point;
851
    }
852

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

    
876
    /**
877
     * Create an Arc. If there is an
878
     * error return <code>null</code> and add the error
879
     * to the log
880
     * 
881
     * @param p1
882
     * @param p2
883
     * @param p3
884
     * @return
885
     *         The arc
886
     */
887
    protected Arc createArc(Point2D p1, Point2D p2, Point2D p3) {
888
        return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
889
    }
890

    
891
    /**
892
     * Create an arc. If there is an
893
     * error return <code>null</code> and add the error
894
     * to the log
895
     * 
896
     * @param p1
897
     * @param p2
898
     * @param p3
899
     * @return
900
     *         The arc
901
     */
902
    protected Arc createArc(Point p1, Point p2, Point p3) {
903
        Arc arc = null;
904
        if (p1.equals(p2) || p2.equals(p3) || p1.equals(p3)) {
905
            return null;
906
        }
907
        try {
908
            arc = (Arc) geomManager.create(TYPES.ARC, getSubType());
909
            arc.setPoints(p1, p2, p3);
910
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
911
            LOG.error("Error creating arc with p1=" + p1, ", p2=" + p2
912
                + ", p3=" + p3, new CreateGeometryException(TYPES.ARC,
913
                getSubType(), e));
914
        }
915
        return arc;
916
    }
917

    
918
    /**
919
     * Create a circle. If there is an
920
     * error return <code>null</code> and add the error
921
     * to the log
922
     * 
923
     * @param p1
924
     * @param p2
925
     * @param p3
926
     * @return
927
     *         The Circle
928
     */
929
    protected Circle createCircle(Point p1, Point p2, Point p3) {
930
        Circle circle = null;
931
        try {
932
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
933
            circle.setPoints(p1, p2, p3);
934
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
935
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2
936
                + ", p3=" + p3, new CreateGeometryException(TYPES.CIRCLE,
937
                getSubType(), e));
938
        }
939
        return circle;
940
    }
941

    
942
    /**
943
     * Create a circle from a GeneralPath. If there is an
944
     * error return <code>null</code> and add the error
945
     * to the log
946
     * 
947
     * @param p1
948
     * @param p2
949
     * @return
950
     *         The circle
951
     */
952
    protected Circle createCircle(Point2D p1, Point2D p2) {
953
        return createCircle(createPoint(p1), createPoint(p2));
954
    }
955

    
956
    /**
957
     * Create a circle. If there is an
958
     * error return <code>null</code> and add the error
959
     * to the log
960
     * 
961
     * @param p1
962
     * @param p2
963
     * @return
964
     *         The circle
965
     */
966
    protected Circle createCircle(Point p1, Point p2) {
967
        Circle circle = null;
968
        try {
969
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
970
            circle.setPoints(p1, p2);
971
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
972
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2,
973
                new CreateGeometryException(TYPES.CIRCLE, getSubType(), e));
974
        }
975
        return circle;
976
    }
977

    
978
    /**
979
     * Create a circle. If there is an
980
     * error return <code>null</code> and add the error
981
     * to the log
982
     * 
983
     * @param p1
984
     * @param radious
985
     * @return
986
     *         The Circle
987
     */
988
    protected Circle createCircle(Point2D p1, double radious) {
989
        return createCircle(createPoint(p1), radious);
990
    }
991

    
992
    /**
993
     * Create a circle. If there is an
994
     * error return <code>null</code> and add the error
995
     * to the log
996
     * 
997
     * @param p1
998
     * @param radious
999
     * @return
1000
     *         The Circle
1001
     */
1002
    protected Circle createCircle(Point p1, double radious) {
1003
        Circle circle = null;
1004
        try {
1005
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
1006
            circle.setPoints(p1, radious);
1007
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1008
            LOG.error("Error creating circle with p1=" + p1 + " radious="
1009
                + radious, new CreateGeometryException(TYPES.CIRCLE,
1010
                getSubType(), e));
1011
        }
1012
        return circle;
1013
    }
1014

    
1015
    /**
1016
     * Create an Ellipse. If there is an
1017
     * error return <code>null</code> and add the error
1018
     * to the log
1019
     * 
1020
     * @param p1
1021
     * @param p2
1022
     * @param d
1023
     * @return
1024
     *         The Ellipse
1025
     */
1026
    protected Ellipse createEllipse(Point2D p1, Point2D p2, double d) {
1027
        return createEllipse(createPoint(p1), createPoint(p2), d);
1028
    }
1029

    
1030
    /**
1031
     * Create an Ellipse. If there is an
1032
     * error return <code>null</code> and add the error
1033
     * to the log
1034
     * 
1035
     * @param p1
1036
     * @param p2
1037
     * @param d
1038
     * @return
1039
     *         The Ellipse
1040
     */
1041
    protected Ellipse createEllipse(Point p1, Point p2, double d) {
1042
        Ellipse ellipse = null;
1043
        try {
1044
            ellipse = (Ellipse) geomManager.create(TYPES.ELLIPSE, getSubType());
1045
            ellipse.setPoints(p1, p2, d);
1046
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1047
            LOG.error("Error creating ellipse with p1=" + p1 + " p2=" + p2
1048
                + ", d=" + d, new CreateGeometryException(TYPES.ELLIPSE,
1049
                getSubType(), e));
1050
        }
1051
        return ellipse;
1052
    }
1053

    
1054
    /**
1055
     * Create a Spline from a GeneralPath. If there is an
1056
     * error return <code>null</code> and add the error
1057
     * to the log
1058
     * 
1059
     * @param points
1060
     * @return
1061
     *         The Spline
1062
     */
1063
    protected Spline createSpline(Point2D[] points) {
1064
        Spline spline = null;
1065
        try {
1066
            spline = (Spline) geomManager.create(TYPES.SPLINE, getSubType());
1067
            for (int i = 0; i < points.length; i++) {
1068
                spline.addVertex(createPoint(points[i]));
1069
            }
1070
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1071
            LOG.error("Error creating spline", new CreateGeometryException(
1072
                TYPES.SPLINE, getSubType(), e));
1073
        }
1074
        return spline;
1075
    }
1076

    
1077
    /**
1078
     * Create a MultiPrimitive. If there is an
1079
     * error return <code>null</code> and add the error
1080
     * to the log
1081
     * 
1082
     * @return
1083
     */
1084
    protected MultiPrimitive createMultiPrimitive() {
1085
        try {
1086
            return (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1087
                getSubType());
1088
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1089
            LOG.error("Error creating MultiPrimitive",
1090
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1091
        }
1092
        return null;
1093
    }
1094

    
1095
    /**
1096
     * Create a MultiPrimitive. If there is an
1097
     * error return <code>null</code> and add the error
1098
     * to the log
1099
     * 
1100
     * @param geometries
1101
     * @return
1102
     */
1103
    protected MultiPrimitive createMultiPrimitive(Geometry[] geometries) {
1104
        MultiPrimitive multiPrimitive = null;
1105
        try {
1106
            multiPrimitive =
1107
                (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1108
                    getSubType());
1109
            for (int i = 0; i < geometries.length; i++) {
1110
                multiPrimitive.addPrimitive((Primitive) geometries[i]);
1111
            }
1112
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1113
            LOG.error("Error creating MultiPrimitive",
1114
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1115
        }
1116
        return multiPrimitive;
1117
    }
1118

    
1119
    /**
1120
     * @return the subtype of the default geometry.
1121
     */
1122
    protected int getSubType() {
1123
        FeatureStore featureStore =
1124
            ((FLyrVect) getVLE().getLayer()).getFeatureStore();
1125
        try {
1126
            return featureStore.getDefaultFeatureType()
1127
                .getDefaultGeometryAttribute().getGeometrySubType();
1128
        } catch (DataException e) {
1129
            LOG.error(
1130
                "Error getting subtype of the default feature type of the store: "
1131
                    + featureStore, e);
1132
            return SUBTYPES.GEOM3D;
1133
        }
1134
    }
1135

    
1136
    /**
1137
     * Returns the type of the geometries to create.
1138
     * 
1139
     * @return the type of the geometries to create
1140
     */
1141
    protected GeometryType getGeometryType() {
1142
        return getCadToolAdapter().getActiveLayerGeometryType();
1143
    }
1144

    
1145
}