Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / DefaultCADTool.java @ 40557

History | View | Annotate | Download (38.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad;
25

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

    
32
import org.cresques.cts.ICoordTrans;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.messages.NotificationManager;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
37
import org.gvsig.editing.CADExtension;
38
import org.gvsig.editing.EditionLocator;
39
import org.gvsig.editing.IEditionManager;
40
import org.gvsig.editing.gui.cad.exception.CommandException;
41
import org.gvsig.editing.layers.VectorialLayerEdited;
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.exception.ReadException;
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.mapcontext.layers.SpatialCache;
75
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
76
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
77
import org.gvsig.fmap.mapcontrol.MapControlLocator;
78
import org.gvsig.fmap.mapcontrol.MapControlManager;
79
import org.gvsig.tools.dispose.DisposableIterator;
80
import org.gvsig.utils.console.JConsole;
81
import org.slf4j.Logger;
82
import org.slf4j.LoggerFactory;
83

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

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

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

    
103
    private CADToolAdapter cadToolAdapter;
104

    
105
    private String question;
106

    
107
    private String[] currentdescriptions;
108

    
109
    private String tool = "selection";
110

    
111
    private DefaultCADTool previousTool;
112

    
113
    private List<Geometry> temporalCache = new ArrayList<Geometry>();
114

    
115
    private GeometryType[] supportedTypes;
116

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

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

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

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

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

    
182
    }
183

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

    
197
    }
198

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
327
    public VectorialLayerEdited getVLE() {
328
        
329
        return (VectorialLayerEdited) EditionLocator.getEditionManager()
330
            .getActiveLayerEdited();
331
    }
332

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

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

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

    
435
    }
436

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

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

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

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

    
474
            iterator = selection.fastIterator();
475
            while (iterator.hasNext()) {
476
                Feature feature = (Feature) iterator.next();
477

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

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

    
497
    }
498

    
499
    public void setDescription(String[] currentdescriptions) {
500
        this.currentdescriptions = currentdescriptions;
501
    }
502

    
503
    public String[] getDescriptions() {
504
        return currentdescriptions;
505
    }
506

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

    
515
    public void init() {
516
        // Nothing to do
517
    }
518

    
519
    protected ArrayList getSelectedHandlers() {
520
        VectorialLayerEdited vle = getVLE();
521
        ArrayList selectedHandlers = vle.getSelectedHandler();
522
        return selectedHandlers;
523
    }
524

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

    
541
    }
542

    
543
    public String getNextTool() {
544
        return tool;
545
    }
546

    
547
    public void setNextTool(String tool) {
548
        this.tool = tool;
549
    }
550

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

    
582
    public boolean isApplicable(int shapeType) {
583
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
584
        return isApplicable(type);
585
    }
586

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

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

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

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

    
632
    public abstract String toString();
633

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

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

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

    
658
    public void setPreviosTool(DefaultCADTool tool) {
659
        previousTool = tool;
660
    }
661

    
662
    public void restorePreviousTool() {
663
        CADExtension.setCADTool(previousTool.toString(), true);
664
        PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
665
    }
666

    
667
    public void endTransition(double x, double y, MouseEvent e) {
668
        // Nothing to do
669
    }
670

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
921
    /**
922
     * Create an arc. If there is an
923
     * error return <code>null</code> and add the error
924
     * to the log
925
     * 
926
     * @param p1
927
     * @param p2
928
     * @param p3
929
     * @return
930
     *         The arc
931
     */
932
    protected Arc createArc(Point p1, Point p2, Point p3) {
933
        Arc arc = null;
934
        if (p1.equals(p2) || p2.equals(p3) || p1.equals(p3)) {
935
            return null;
936
        }
937
        try {
938
            arc = (Arc) geomManager.create(TYPES.ARC, getSubType());
939
            arc.setPoints(p1, p2, p3);
940
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
941
            LOG.error("Error creating arc with p1=" + p1, ", p2=" + p2
942
                + ", p3=" + p3, new CreateGeometryException(TYPES.ARC,
943
                getSubType(), e));
944
        } catch (IllegalArgumentException ex) {
945
            LOG.info("Warning: unable to create arc from points: "
946
                + p1.getX() + " " + p1.getY() + " :: "
947
                + p2.getX() + " " + p2.getY() + " :: "
948
                + p3.getX() + " " + p3.getY());
949
            arc = null;
950
        }
951
        return arc;
952
    }
953

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

    
978
    /**
979
     * Create a circle from a GeneralPath. If there is an
980
     * error return <code>null</code> and add the error
981
     * to the log
982
     * 
983
     * @param p1
984
     * @param p2
985
     * @return
986
     *         The circle
987
     */
988
    protected Circle createCircle(Point2D p1, Point2D p2) {
989
        return createCircle(createPoint(p1), createPoint(p2));
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 p2
999
     * @return
1000
     *         The circle
1001
     */
1002
    protected Circle createCircle(Point p1, Point p2) {
1003
        Circle circle = null;
1004
        try {
1005
            circle = (Circle) geomManager.create(TYPES.CIRCLE, getSubType());
1006
            circle.setPoints(p1, p2);
1007
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
1008
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2,
1009
                new CreateGeometryException(TYPES.CIRCLE, getSubType(), e));
1010
        }
1011
        return circle;
1012
    }
1013

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

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

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

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

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

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

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

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

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

    
1181
}