Statistics
| Revision:

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

History | View | Annotate | Download (38.6 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.gvsig.andami.PluginServices;
32
import org.gvsig.andami.messages.NotificationManager;
33
import org.gvsig.andami.ui.mdiManager.IWindow;
34
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
35
import org.gvsig.editing.CADExtension;
36
import org.gvsig.editing.IEditionManager;
37
import org.gvsig.editing.gui.cad.exception.CommandException;
38
import org.gvsig.editing.layers.VectorialLayerEdited;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.feature.EditableFeature;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureSelection;
44
import org.gvsig.fmap.dal.feature.FeatureSet;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
49
import org.gvsig.fmap.geom.Geometry.TYPES;
50
import org.gvsig.fmap.geom.GeometryLocator;
51
import org.gvsig.fmap.geom.GeometryManager;
52
import org.gvsig.fmap.geom.aggregate.MultiCurve;
53
import org.gvsig.fmap.geom.aggregate.MultiPoint;
54
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
55
import org.gvsig.fmap.geom.aggregate.MultiSurface;
56
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
57
import org.gvsig.fmap.geom.handler.Handler;
58
import org.gvsig.fmap.geom.primitive.Arc;
59
import org.gvsig.fmap.geom.primitive.Circle;
60
import org.gvsig.fmap.geom.primitive.Curve;
61
import org.gvsig.fmap.geom.primitive.Ellipse;
62
import org.gvsig.fmap.geom.primitive.Envelope;
63
import org.gvsig.fmap.geom.primitive.GeneralPathX;
64
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
65
import org.gvsig.fmap.geom.primitive.Point;
66
import org.gvsig.fmap.geom.primitive.Primitive;
67
import org.gvsig.fmap.geom.primitive.Spline;
68
import org.gvsig.fmap.geom.primitive.Surface;
69
import org.gvsig.fmap.geom.type.GeometryType;
70
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
71
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
72
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
73
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
74
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
75
import org.gvsig.fmap.mapcontrol.MapControlLocator;
76
import org.gvsig.fmap.mapcontrol.MapControlManager;
77
import org.gvsig.tools.dispose.DisposableIterator;
78
import org.gvsig.utils.console.JConsole;
79
import org.slf4j.Logger;
80
import org.slf4j.LoggerFactory;
81

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

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

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

    
101
    private CADToolAdapter cadToolAdapter;
102

    
103
    private String question;
104

    
105
    private String[] currentdescriptions;
106

    
107
    private String tool = "selection";
108

    
109
    private DefaultCADTool previousTool;
110

    
111
    private List<Geometry> temporalCache = new ArrayList<Geometry>();
112

    
113
    private GeometryType[] supportedTypes;
114

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

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

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

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

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

    
180
    }
181

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

    
195
    }
196

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
330
    public Feature insertGeometry(Geometry geometry, Feature feature) {
331
            return this.getEditionManager().insertGeometry(geometry,feature);
332
//
333
//            VectorialLayerEdited vle = getVLE();
334
//
335
//        try {
336
//            FeatureStore featureStore =
337
//                ((FLyrVect) vle.getLayer()).getFeatureStore();
338
//            EditableFeature eFeature =
339
//                featureStore.createNewFeature(
340
//                    featureStore.getDefaultFeatureType(), feature);
341
//            eFeature.setGeometry(featureStore.getDefaultFeatureType()
342
//                .getDefaultGeometryAttributeName(), geometry);
343
//            featureStore.insert(eFeature);
344
//            // drawToImage(featureStore, vle, eFeature);
345
//            insertSpatialCache(geometry);
346
//
347
//            getCadToolAdapter().getMapControl().getMapControlDrawer()
348
//                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
349
//
350
//            return eFeature;
351
//        } catch (ReadException e) {
352
//            NotificationManager.addError(e.getMessage(), e);
353
//            return null;
354
//        } catch (DataException e) {
355
//            NotificationManager.addError(e.getMessage(), e);
356
//            return null;
357
//        } catch (CreateEnvelopeException e) {
358
//            NotificationManager.addError(e.getMessage(), e);
359
//            return null;
360
//        }
361
    }
362

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

    
388
    public void updateGeometry(FeatureStore store, Feature feature, Geometry geometry) {
389
            this.getEditionManager().updateGeometry(store, feature, geometry);
390
    }
391
    
392
    protected IEditionManager getEditionManager() {
393
            return this.getCadToolAdapter().getEditionManager();
394
    }
395
    
396
    public Feature insertGeometry(Geometry geometry) {
397
            return this.getEditionManager().insertGeometry(geometry);
398
//            
399
//        VectorialLayerEdited vle = getVLE();
400
//
401
//        try {
402
//            FeatureStore featureStore =
403
//                ((FLyrVect) vle.getLayer()).getFeatureStore();
404
//            EditableFeature eFeature = featureStore.createNewFeature(true);
405
//           
406
//            //Reproject the geometry
407
//            Geometry insertedGeometry = geometry;
408
//            if (getVLE().getLayer().getCoordTrans() != null){
409
//                insertedGeometry = insertedGeometry.cloneGeometry();
410
//                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
411
//            }
412
//            
413
//            eFeature.setGeometry(featureStore.getDefaultFeatureType()
414
//                .getDefaultGeometryAttributeName(), insertedGeometry);
415
//            featureStore.insert(eFeature);
416
//
417
//            insertSpatialCache(insertedGeometry);
418
//
419
//            draw(insertedGeometry, eFeature);
420
//            return eFeature;
421
//        } catch (ReadException e) {
422
//            NotificationManager.addError(e.getMessage(), e);
423
//            return null;
424
//        } catch (DataException e) {
425
//            NotificationManager.addError(e.getMessage(), e);
426
//            return null;
427
//        } catch (CreateEnvelopeException e) {
428
//            NotificationManager.addError(e.getMessage(), e);
429
//            return null;
430
//        }
431

    
432
    }
433

    
434
    /**
435
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
436
     * 
437
     * @return Cadena para mostrar por consola.
438
     */
439
    public String getQuestion() {
440
        return question;
441
    }
442

    
443
    /**
444
     * Actualiza la cadena que corresponde al estado actual.
445
     * 
446
     * @param s
447
     *            Cadena que aparecer� en consola.
448
     */
449
    public void setQuestion(String s) {
450
        question = s;
451
    }
452

    
453
    /**
454
     * Provoca un repintado "soft" de la capa activa en edici�n. Las capas por
455
     * debajo de ella no se dibujan de verdad, solo se dibuja la que est� en
456
     * edici�n y las que est�n por encima de ella en el TOC.
457
     */
458
    public void refresh() {
459
        getCadToolAdapter().getMapControl().rePaintDirtyLayers();
460
    }
461

    
462
    public void drawHandlers(MapControlDrawer renderer, ArrayList selectedRows,
463
        AffineTransform at) {
464
        FeatureSet selection = null;
465
        DisposableIterator iterator = null;
466
        try {
467
            selection =
468
                (FeatureSet) ((FLyrVect) getVLE().getLayer()).getFeatureStore()
469
                    .getSelection();
470

    
471
            iterator = selection.fastIterator();
472
            while (iterator.hasNext()) {
473
                Feature feature = (Feature) iterator.next();
474

    
475
                Geometry ig = (feature.getDefaultGeometry()).cloneGeometry();
476
                if (ig == null) {
477
                    continue;
478
                }
479
                Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
480
                renderer.drawHandlers(handlers, at,
481
                    mapControlManager.getGeometrySelectionSymbol());
482
            }
483

    
484
        } catch (ReadException e) {
485
            NotificationManager.addError(e.getMessage(), e);
486
        } catch (DataException e) {
487
            NotificationManager.addError(e.getMessage(), e);
488
        } finally {
489
            if (iterator != null) {
490
                iterator.dispose();
491
            }
492
        }
493

    
494
    }
495

    
496
    public void setDescription(String[] currentdescriptions) {
497
        this.currentdescriptions = currentdescriptions;
498
    }
499

    
500
    public String[] getDescriptions() {
501
        return currentdescriptions;
502
    }
503

    
504
    public void end() {
505
        CADExtension.setCADTool("_selection", true);
506
        PluginServices.getMainFrame().setSelectedTool("_selection");
507
        CADTool cadtool = CADExtension.getCADTool();
508
        cadtool.setPreviosTool(this);
509
        cadtool.getVLE().getLayer().getMapContext().invalidate();
510
    }
511

    
512
    public void init() {
513
        // Nothing to do
514
    }
515

    
516
    protected ArrayList getSelectedHandlers() {
517
        VectorialLayerEdited vle = getVLE();
518
        ArrayList selectedHandlers = vle.getSelectedHandler();
519
        return selectedHandlers;
520
    }
521

    
522
    public void clearSelection() throws DataException {
523
        VectorialLayerEdited vle = getVLE();
524
        FeatureSelection selection = null;
525
        selection =
526
            (FeatureSelection) ((FLyrVect) vle.getLayer()).getFeatureStore()
527
                .getSelection();
528
        // ArrayList selectedRow = vle.getSelectedRow();
529
        ArrayList selectedHandlers = vle.getSelectedHandler();
530
        selection.deselectAll();
531
        selectedHandlers.clear();
532
        // VectorialEditableAdapter vea = vle.getVEA();
533
        // FBitSet selection = vea.getSelection();
534
        // selection.clear();
535
        vle.setSelectionImage(null);
536
        vle.setHandlersImage(null);
537

    
538
    }
539

    
540
    public String getNextTool() {
541
        return tool;
542
    }
543

    
544
    public void setNextTool(String tool) {
545
        this.tool = tool;
546
    }
547

    
548
    public boolean changeCommand(String name) throws CommandException {
549
        CADTool[] cadtools = CADExtension.getCADTools();
550
        for (int i = 0; i < cadtools.length; i++) {
551
            CADTool ct = cadtools[i];
552
            if (name.equalsIgnoreCase(ct.getName())
553
                || name.equalsIgnoreCase(ct.toString())) {
554
                int type = Geometry.TYPES.POINT;
555
                try {
556
                    type = ((FLyrVect) getVLE().getLayer()).getShapeType();
557
                } catch (ReadException e) {
558
                    throw new CommandException(e);
559
                }
560
                if (ct.isApplicable(type)) {
561
                    getCadToolAdapter().setCadTool(ct);
562
                    ct.init();
563
                    DefaultViewPanel vista =
564
                        (DefaultViewPanel) PluginServices.getMDIManager()
565
                            .getActiveWindow();
566
                    vista.getConsolePanel().addText("\n" + ct.getName(),
567
                        JConsole.COMMAND);
568
                    String question = ct.getQuestion();
569
                    vista.getConsolePanel().addText(
570
                        "\n" + "#" + question + " > ", JConsole.MESSAGE);
571
                    return true;
572
                }
573
                throw new CommandException(name);
574
            }
575
        }
576
        return false;
577
    }
578

    
579
    public boolean isApplicable(int shapeType) {
580
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
581
        return isApplicable(type);
582
    }
583

    
584
    public boolean isApplicable(GeometryType geometryType) {
585
        if (supportedTypes != null) {
586
            for (int i = 0; i < supportedTypes.length; i++) {
587
                if (supportedTypes[i].isTypeOf(geometryType)) {
588
                    return true;
589
                }
590
            }
591
        }
592
        return false;
593
    }
594

    
595
    /**
596
     * Returns the geometry types supported by the tool.
597
     * 
598
     * @return the supported geometry types
599
     */
600
    protected GeometryType[] getSupportedTypes() {
601
        return supportedTypes;
602
    }
603

    
604
    /**
605
     * Returns the list of Geometry types supported by this tool. If any
606
     * Geometry type is supported, just return null.
607
     * 
608
     * @return the list of Geometry types supported by this tool
609
     */
610
    protected int[] getSupportedGeometryTypes() {
611
        return null;
612
    }
613

    
614
    protected GeometryType loadGeometryType(int type, int subtype) {
615
        try {
616
            return GeometryLocator.getGeometryManager().getGeometryType(type,
617
                subtype);
618
        } catch (GeometryTypeNotSupportedException e) {
619
            throw new RuntimeException(
620
                "Error getting the Geometry type with type = " + type
621
                    + ", subtype = " + subtype, e);
622
        } catch (GeometryTypeNotValidException e) {
623
            throw new RuntimeException(
624
                "Error getting the Geometry type with type = " + type
625
                    + ", subtype = " + subtype, e);
626
        }
627
    }
628

    
629
    public abstract String toString();
630

    
631
    public void throwValueException(String s, double d) {
632
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
633
        if (window instanceof DefaultViewPanel) {
634
            ((DefaultViewPanel) window).getConsolePanel().addText(
635
                s + " : " + d, JConsole.ERROR);
636
        }
637
    }
638

    
639
    public void throwOptionException(String s, String o) {
640
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
641
        if (window instanceof DefaultViewPanel) {
642
            ((DefaultViewPanel) window).getConsolePanel().addText(
643
                s + " : " + o, JConsole.ERROR);
644
        }
645
    }
646

    
647
    public void throwPointException(String s, double x, double y) {
648
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
649
        if (window instanceof DefaultViewPanel) {
650
            ((DefaultViewPanel) window).getConsolePanel().addText(
651
                s + " : " + " X = " + x + ", Y = " + y, JConsole.ERROR);
652
        }
653
    }
654

    
655
    public void setPreviosTool(DefaultCADTool tool) {
656
        previousTool = tool;
657
    }
658

    
659
    public void restorePreviousTool() {
660
        CADExtension.setCADTool(previousTool.toString(), true);
661
        PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
662
    }
663

    
664
    public void endTransition(double x, double y, MouseEvent e) {
665
        // Nothing to do
666
    }
667

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

    
686
    /**
687
     * Create a multicurve. If there is an
688
     * error return <code>null</code> and add the error
689
     * to the log
690
     * 
691
     * @return
692
     *         The MultiCurve
693
     */
694
    protected MultiCurve createMultiCurve() {
695
        try {
696
            return (MultiCurve) geomManager.create(TYPES.MULTICURVE,
697
                getSubType());
698
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
699
            LOG.error("Error creating multicurve", new CreateGeometryException(
700
                TYPES.MULTICURVE, getSubType(), e));
701
        }
702
        return null;
703
    }
704

    
705
    /**
706
     * Create an {@link OrientablePrimitive}. If there is an
707
     * error return <code>null</code> and add the error
708
     * to the log
709
     * 
710
     * @param geometryType
711
     *            a type of a OrientablePrimitive.
712
     * @return
713
     *         The {@link OrientablePrimitive}
714
     */
715
    protected OrientablePrimitive createOrientablePrimitive(int geometryType) {
716
        try {
717
            return (OrientablePrimitive) geomManager.create(geometryType,
718
                getSubType());
719
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
720
            LOG.error("Error creating curve", new CreateGeometryException(
721
                TYPES.CURVE, getSubType(), e));
722
        }
723
        return null;
724
    }
725

    
726
    /**
727
     * Create an envelope like a curve from two points.
728
     * If there is an error return <code>null</code> and add the error to the
729
     * log.
730
     * 
731
     * @param firstPoint
732
     *            first point
733
     * @param secondPoint
734
     *            second point
735
     * @return
736
     *         the curve
737
     */
738
    protected Curve createEnvelopeLikeCurve(Point2D firstPoint,
739
        Point2D secondPoint) {
740
        try {
741
            Curve curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
742
            curve.addMoveToVertex(createPoint(firstPoint.getX(),
743
                firstPoint.getY()));
744
            curve.addVertex(createPoint(secondPoint.getX(), firstPoint.getY()));
745
            curve
746
                .addVertex(createPoint(secondPoint.getX(), secondPoint.getY()));
747
            curve.addVertex(createPoint(firstPoint.getX(), secondPoint.getY()));
748
            curve.addVertex(createPoint(firstPoint.getX(), firstPoint.getY()));
749
            return curve;
750
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
751
            LOG.error("Error creating curve", new CreateGeometryException(
752
                TYPES.CURVE, getSubType(), e));
753
        }
754
        return null;
755
    }
756

    
757
    /**
758
     * Create a curve from a GeneralPath. If there is an
759
     * error return <code>null</code> and add the error
760
     * to the log
761
     * 
762
     * @param gpx
763
     *            The GeneralPath
764
     * @return
765
     *         The Curve
766
     */
767
    protected Curve createCurve(GeneralPathX gpx) {
768
        Curve curve = null;
769
        try {
770
            curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
771
            curve.setGeneralPath(gpx);
772
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
773
            LOG.error("Error creating curve", new CreateGeometryException(
774
                TYPES.CURVE, getSubType(), e));
775
        }
776
        return curve;
777
    }
778

    
779
    /**
780
     * Create a surface. If there is an
781
     * error return <code>null</code> and add the error
782
     * to the log
783
     * 
784
     * @return
785
     *         The Surface
786
     */
787
    protected Surface createSurface() {
788
        try {
789
            return (Surface) geomManager.create(TYPES.SURFACE, getSubType());
790

    
791
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
792
            LOG.error("Error creating surface", new CreateGeometryException(
793
                TYPES.SURFACE, getSubType(), e));
794
        }
795
        return null;
796
    }
797

    
798
    /**
799
     * Create a multisurface. If there is an
800
     * error return <code>null</code> and add the error
801
     * to the log
802
     * 
803
     * @return
804
     *         The MultiSurface
805
     */
806
    protected MultiSurface createMultiSurface() {
807
        try {
808
            return (MultiSurface) geomManager.create(TYPES.MULTISURFACE,
809
                getSubType());
810

    
811
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
812
            LOG.error(
813
                "Error creating multisurface",
814
                new CreateGeometryException(TYPES.MULTISURFACE, getSubType(), e));
815
        }
816
        return null;
817
    }
818

    
819
    /**
820
     * Create a surface from a GeneralPath. If there is an
821
     * error return <code>null</code> and add the error
822
     * to the log
823
     * 
824
     * @param gpx
825
     *            The general path
826
     * @return
827
     *         The Surface
828
     */
829
    protected Surface createSurface(GeneralPathX gpx) {
830
        Surface surface = null;
831
        try {
832
            surface = (Surface) geomManager.create(TYPES.SURFACE, getSubType());
833
            surface.setGeneralPath(gpx);
834
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
835
            LOG.error("Error creating surface", new CreateGeometryException(
836
                TYPES.SURFACE, getSubType(), e));
837
        }
838
        return surface;
839
    }
840

    
841
    /**
842
     * Create a curve point. If there is an
843
     * error return <code>null</code> and add the error
844
     * to the log
845
     * 
846
     * @param p1
847
     *            The AWT point
848
     * @return
849
     *         The gvSIG point
850
     */
851
    protected Point createPoint(Point2D p1) {
852
        return createPoint(p1.getX(), p1.getY());
853
    }
854

    
855
    /**
856
     * Create point. If there is an
857
     * error return <code>null</code> and add the error
858
     * to the log
859
     * 
860
     * @param x
861
     *            The X coordinate
862
     * @param y
863
     *            The y coordinate
864
     * @return
865
     *         The Point
866
     */
867
    protected Point createPoint(double x, double y) {
868
        Point point = null;
869
        try {
870
            point = (Point) geomManager.create(TYPES.POINT, getSubType());
871
            point.setX(x);
872
            point.setY(y);
873
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
874
            LOG.error("Error creating point with x=" + x + ", y=" + y,
875
                new CreateGeometryException(TYPES.POINT, getSubType(), e));
876
        }
877
        return point;
878
    }
879

    
880
    /**
881
     * Create a multipoint. If there is an
882
     * error return <code>null</code> and add the error
883
     * to the log
884
     * 
885
     * @param x
886
     *            The X coordinate
887
     * @param y
888
     *            The y coordinate
889
     * @return
890
     *         The MultiPoint
891
     */
892
    protected MultiPoint createMultiPoint() {
893
        try {
894
            return (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
895
                getSubType());
896
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
897
            LOG.error("Error creating multipoint", new CreateGeometryException(
898
                TYPES.MULTIPOINT, getSubType(), e));
899
            return null;
900
        }
901
    }
902

    
903
    /**
904
     * Create an Arc. If there is an
905
     * error return <code>null</code> and add the error
906
     * to the log
907
     * 
908
     * @param p1
909
     * @param p2
910
     * @param p3
911
     * @return
912
     *         The arc
913
     */
914
    protected Arc createArc(Point2D p1, Point2D p2, Point2D p3) {
915
        return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
916
    }
917

    
918
    /**
919
     * Create an arc. 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 arc
928
     */
929
    protected Arc createArc(Point p1, Point p2, Point p3) {
930
        Arc arc = null;
931
        if (p1.equals(p2) || p2.equals(p3) || p1.equals(p3)) {
932
            return null;
933
        }
934
        try {
935
            arc = (Arc) geomManager.create(TYPES.ARC, getSubType());
936
            arc.setPoints(p1, p2, p3);
937
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
938
            LOG.error("Error creating arc with p1=" + p1, ", p2=" + p2
939
                + ", p3=" + p3, new CreateGeometryException(TYPES.ARC,
940
                getSubType(), e));
941
        } catch (IllegalArgumentException ex) {
942
            LOG.info("Warning: unable to create arc from points: "
943
                + p1.getX() + " " + p1.getY() + " :: "
944
                + p2.getX() + " " + p2.getY() + " :: "
945
                + p3.getX() + " " + p3.getY());
946
            arc = null;
947
        }
948
        return arc;
949
    }
950

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

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

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

    
1011
    /**
1012
     * Create a circle. If there is an
1013
     * error return <code>null</code> and add the error
1014
     * to the log
1015
     * 
1016
     * @param p1
1017
     * @param radious
1018
     * @return
1019
     *         The Circle
1020
     */
1021
    protected Circle createCircle(Point2D p1, double radious) {
1022
        return createCircle(createPoint(p1), radious);
1023
    }
1024

    
1025
    /**
1026
     * Create a circle. If there is an
1027
     * error return <code>null</code> and add the error
1028
     * to the log
1029
     * 
1030
     * @param p1
1031
     * @param radious
1032
     * @return
1033
     *         The Circle
1034
     */
1035
    protected Circle createCircle(Point p1, double radious) {
1036
        Circle circle = null;
1037
        try {
1038
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
1039
            circle.setPoints(p1, radious);
1040
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1041
            LOG.error("Error creating circle with p1=" + p1 + " radious="
1042
                + radious, new CreateGeometryException(TYPES.CIRCLE,
1043
                getSubType(), e));
1044
        }
1045
        return circle;
1046
    }
1047

    
1048
    /**
1049
     * Create an Ellipse. If there is an
1050
     * error return <code>null</code> and add the error
1051
     * to the log
1052
     * 
1053
     * @param p1
1054
     * @param p2
1055
     * @param d
1056
     * @return
1057
     *         The Ellipse
1058
     */
1059
    protected Ellipse createEllipse(Point2D p1, Point2D p2, double d) {
1060
        return createEllipse(createPoint(p1), createPoint(p2), d);
1061
    }
1062

    
1063
    /**
1064
     * Create an Ellipse. If there is an
1065
     * error return <code>null</code> and add the error
1066
     * to the log
1067
     * 
1068
     * @param p1
1069
     * @param p2
1070
     * @param d
1071
     * @return
1072
     *         The Ellipse
1073
     */
1074
    protected Ellipse createEllipse(Point p1, Point p2, double d) {
1075
        Ellipse ellipse = null;
1076
        try {
1077
            ellipse = (Ellipse) geomManager.create(TYPES.ELLIPSE, getSubType());
1078
            ellipse.setPoints(p1, p2, d);
1079
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1080
            LOG.error("Error creating ellipse with p1=" + p1 + " p2=" + p2
1081
                + ", d=" + d, new CreateGeometryException(TYPES.ELLIPSE,
1082
                getSubType(), e));
1083
        }
1084
        return ellipse;
1085
    }
1086

    
1087
    /**
1088
     * Create a Spline from a GeneralPath. If there is an
1089
     * error return <code>null</code> and add the error
1090
     * to the log
1091
     * 
1092
     * @param points
1093
     * @return
1094
     *         The Spline
1095
     */
1096
    protected Spline createSpline(Point2D[] points) {
1097
        Spline spline = null;
1098
        try {
1099
            spline = (Spline) geomManager.create(TYPES.SPLINE, getSubType());
1100
            for (int i = 0; i < points.length; i++) {
1101
                spline.addVertex(createPoint(points[i]));
1102
            }
1103
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1104
            LOG.error("Error creating spline", new CreateGeometryException(
1105
                TYPES.SPLINE, getSubType(), e));
1106
        }
1107
        return spline;
1108
    }
1109

    
1110
    /**
1111
     * Create a MultiPrimitive. If there is an
1112
     * error return <code>null</code> and add the error
1113
     * to the log
1114
     * 
1115
     * @return
1116
     */
1117
    protected MultiPrimitive createMultiPrimitive() {
1118
        try {
1119
            return (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1120
                getSubType());
1121
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1122
            LOG.error("Error creating MultiPrimitive",
1123
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1124
        }
1125
        return null;
1126
    }
1127

    
1128
    /**
1129
     * Create a MultiPrimitive. If there is an
1130
     * error return <code>null</code> and add the error
1131
     * to the log
1132
     * 
1133
     * @param geometries
1134
     * @return
1135
     */
1136
    protected MultiPrimitive createMultiPrimitive(Geometry[] geometries) {
1137
        MultiPrimitive multiPrimitive = null;
1138
        try {
1139
            multiPrimitive =
1140
                (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1141
                    getSubType());
1142
            for (int i = 0; i < geometries.length; i++) {
1143
                multiPrimitive.addPrimitive((Primitive) geometries[i]);
1144
            }
1145
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1146
            LOG.error("Error creating MultiPrimitive",
1147
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1148
        }
1149
        return multiPrimitive;
1150
    }
1151

    
1152
    /**
1153
     * @return the subtype of the default geometry.
1154
     */
1155
    protected int getSubType() {
1156
        FeatureStore featureStore =
1157
            ((FLyrVect) getVLE().getLayer()).getFeatureStore();
1158
        try {
1159
            return featureStore.getDefaultFeatureType()
1160
                .getDefaultGeometryAttribute().getGeometrySubType();
1161
        } catch (DataException e) {
1162
            LOG.error(
1163
                "Error getting subtype of the default feature type of the store: "
1164
                    + featureStore, e);
1165
            return SUBTYPES.GEOM3D;
1166
        }
1167
    }
1168

    
1169
    /**
1170
     * Returns the type of the geometries to create.
1171
     * 
1172
     * @return the type of the geometries to create
1173
     */
1174
    protected GeometryType getGeometryType() {
1175
        return getCadToolAdapter().getActiveLayerGeometryType();
1176
    }
1177

    
1178
}