Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / DefaultCADTool.java @ 38564

History | View | Annotate | Download (37.2 KB)

1 37138 cordinyana
/* gvSIG. Geographic Information System of the Valencian Government
2 29685 jpiera
 *
3 37138 cordinyana
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6 29685 jpiera
 * 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 37138 cordinyana
 *
11 29685 jpiera
 * 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 37138 cordinyana
 *
16 29685 jpiera
 * 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 37138 cordinyana
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21 29685 jpiera
 */
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 37328 cordinyana
import java.util.List;
29 29685 jpiera
30 30788 jpiera
import org.cresques.cts.ICoordTrans;
31 34696 cordinyana
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33
34 29685 jpiera
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36 30203 vcaballero
import org.gvsig.andami.ui.mdiManager.IWindow;
37 31496 jjdelcerro
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
38 29685 jpiera
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 34696 cordinyana
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.Geometry.TYPES;
52 29685 jpiera
import org.gvsig.fmap.geom.GeometryLocator;
53
import org.gvsig.fmap.geom.GeometryManager;
54 37328 cordinyana
import org.gvsig.fmap.geom.aggregate.MultiCurve;
55
import org.gvsig.fmap.geom.aggregate.MultiPoint;
56 29685 jpiera
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
57 37328 cordinyana
import org.gvsig.fmap.geom.aggregate.MultiSurface;
58 29685 jpiera
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 35941 jpiera
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
67 29685 jpiera
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 37328 cordinyana
import org.gvsig.fmap.geom.type.GeometryType;
72
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
73
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
74 30788 jpiera
import org.gvsig.fmap.geom.util.UtilFunctions;
75 29685 jpiera
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
76
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
77 30335 jpiera
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
78
import org.gvsig.fmap.mapcontrol.MapControlLocator;
79
import org.gvsig.fmap.mapcontrol.MapControlManager;
80 34696 cordinyana
import org.gvsig.tools.dispose.DisposableIterator;
81 29685 jpiera
import org.gvsig.utils.console.JConsole;
82
83
/**
84 37138 cordinyana
 *
85 29685 jpiera
 * @author Vicente Caballero Navarro
86
 */
87 37328 cordinyana
public abstract class DefaultCADTool implements CADTool, Geometry.TYPES,
88
    Geometry.SUBTYPES {
89 30335 jpiera
90 36480 cordinyana
    private static final Logger LOG = LoggerFactory
91 34696 cordinyana
        .getLogger(DefaultCADTool.class);
92
93 37138 cordinyana
    /**
94
     * Reference to the MapControl library manager, used to manage
95
     * tools.
96
     */
97 37328 cordinyana
    protected MapControlManager mapControlManager = MapControlLocator
98 37138 cordinyana
        .getMapControlManager();
99 37328 cordinyana
    protected GeometryManager geomManager = GeometryLocator
100
        .getGeometryManager();
101 30335 jpiera
102 37138 cordinyana
    private CADToolAdapter cadToolAdapter;
103 29685 jpiera
104 37138 cordinyana
    private String question;
105 29685 jpiera
106 37138 cordinyana
    private String[] currentdescriptions;
107 29685 jpiera
108 37138 cordinyana
    private String tool = "selection";
109 29685 jpiera
110 37138 cordinyana
    private DefaultCADTool previousTool;
111 29685 jpiera
112 37328 cordinyana
    private List<Geometry> temporalCache = new ArrayList<Geometry>();
113 29685 jpiera
114 37328 cordinyana
    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 37138 cordinyana
    public void addTemporalCache(Geometry geom) {
137
        temporalCache.add(geom);
138
        try {
139
            insertSpatialCache(geom);
140
        } catch (CreateEnvelopeException e) {
141 34696 cordinyana
            LOG.error("Error adding the spatial cache", e);
142 37138 cordinyana
        }
143
    }
144 29685 jpiera
145 37138 cordinyana
    public void clearTemporalCache() {
146 37328 cordinyana
        Geometry[] geoms =
147
            temporalCache.toArray(new Geometry[temporalCache.size()]);
148 37138 cordinyana
        for (int i = 0; i < geoms.length; i++) {
149
            try {
150
                removeSpatialCache(geoms[i]);
151
            } catch (CreateEnvelopeException e) {
152 34696 cordinyana
                LOG.error("Error removing the temporal cache", e);
153 37138 cordinyana
            }
154
        }
155
        temporalCache.clear();
156
    }
157 29685 jpiera
158 37138 cordinyana
    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 37328 cordinyana
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
168 37138 cordinyana
        }
169
        spatialCache.insert(r, geom);
170 29685 jpiera
171 37138 cordinyana
    }
172 29685 jpiera
173 37138 cordinyana
    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 29685 jpiera
186 37138 cordinyana
    }
187 29685 jpiera
188 37138 cordinyana
    /**
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 29685 jpiera
200 37138 cordinyana
    /**
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 30788 jpiera
219 37138 cordinyana
        try {
220
            featureStore = getVLE().getFeatureStore();
221
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
222
223 37328 cordinyana
            iterator = selection.fastIterator();
224 37138 cordinyana
            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
                UtilFunctions.moveGeom(geometry, currentPoint.getX()
239
                    - firstPoint.getX(),
240
                    currentPoint.getY() - firstPoint.getY());
241
242
                mapControlDrawer.draw(geometry,
243
                    mapControlManager.getGeometrySelectionSymbol());
244
            }
245
        } catch (Exception e) {
246 34696 cordinyana
            LOG.error("Retrieving the selection", e);
247 37138 cordinyana
        } finally {
248
            if (iterator != null) {
249
                iterator.dispose();
250
            }
251
        }
252
    }
253 30788 jpiera
254 37138 cordinyana
    /**
255
     * It draw the selected geometries from the initial point until a new
256
     * point.
257
     *
258
     * @param mapControlDrawer
259
     *            The drawer.
260
     * @param firstPoint
261
     *            The initial point.
262
     * @param x
263
     *            The X coordinate of the mouse.
264
     * @param y
265
     *            The Y coordinate of the mouse.
266
     * @param affineTransform
267
     *            The transformation to apply
268
     */
269
    protected void drawAndRotateSelectedGeometries(
270
        MapControlDrawer mapControlDrawer, Point2D firstPoint, double x,
271
        double y) {
272
        FeatureStore featureStore = null;
273
        DisposableIterator iterator = null;
274 30788 jpiera
275 37138 cordinyana
        try {
276
            Point2D lastPoint = new Point2D.Double(x, y);
277 30788 jpiera
278 37138 cordinyana
            double w = lastPoint.getX() - firstPoint.getX();
279
            double h = lastPoint.getY() - firstPoint.getY();
280
281
            featureStore = getVLE().getFeatureStore();
282
            FeatureSet selection = (FeatureSet) featureStore.getSelection();
283
284 37328 cordinyana
            iterator = selection.fastIterator();
285 37138 cordinyana
            while (iterator.hasNext()) {
286
                Feature feature = (Feature) iterator.next();
287
                Geometry geometry =
288
                    (feature.getDefaultGeometry()).cloneGeometry();
289
290
                if (geometry == null) {
291
                    continue;
292
                }
293
294
                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h)
295
                    + (Math.PI / 2), firstPoint.getX(), firstPoint.getY());
296
297
                mapControlDrawer.draw(geometry,
298
                    mapControlManager.getGeometrySelectionSymbol());
299
            }
300
        } catch (Exception e) {
301 34696 cordinyana
            LOG.error("Retrieving the selection", e);
302 37138 cordinyana
        } finally {
303
            if (iterator != null) {
304
                iterator.dispose();
305
            }
306
        }
307
    }
308 30788 jpiera
309 37138 cordinyana
    public void setCadToolAdapter(CADToolAdapter cta) {
310
        cadToolAdapter = cta;
311
    }
312 29685 jpiera
313 37138 cordinyana
    public CADToolAdapter getCadToolAdapter() {
314
        return cadToolAdapter;
315
    }
316 29685 jpiera
317 37138 cordinyana
    public VectorialLayerEdited getVLE() {
318
        return (VectorialLayerEdited) CADExtension.getEditionManager()
319
            .getActiveLayerEdited();
320
    }
321 29685 jpiera
322 37138 cordinyana
    public Feature insertGeometry(Geometry geometry, Feature feature) {
323
        VectorialLayerEdited vle = getVLE();
324 29685 jpiera
325 37138 cordinyana
        try {
326
            FeatureStore featureStore =
327
                ((FLyrVect) vle.getLayer()).getFeatureStore();
328
            EditableFeature eFeature =
329
                featureStore.createNewFeature(
330
                    featureStore.getDefaultFeatureType(), feature);
331
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
332
                .getDefaultGeometryAttributeName(), geometry);
333
            featureStore.insert(eFeature);
334
            // drawToImage(featureStore, vle, eFeature);
335
            insertSpatialCache(geometry);
336 30335 jpiera
337 37138 cordinyana
            getCadToolAdapter().getMapControl().getMapControlDrawer()
338
                .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
339 30335 jpiera
340 37138 cordinyana
            return eFeature;
341
        } catch (ReadException e) {
342
            NotificationManager.addError(e.getMessage(), e);
343
            return null;
344
        } catch (DataException e) {
345
            NotificationManager.addError(e.getMessage(), e);
346
            return null;
347
        } catch (CreateEnvelopeException e) {
348
            NotificationManager.addError(e.getMessage(), e);
349
            return null;
350
        }
351
    }
352 29685 jpiera
353 37138 cordinyana
    public Feature insertAndSelectGeometry(Geometry geometry) {
354
        Feature feature = null;
355
        try {
356
            FeatureStore featureStore = getVLE().getFeatureStore();
357
            featureStore.beginComplexNotification();
358
            featureStore.beginEditingGroup(getName());
359
            FeatureSelection newSelection =
360
                featureStore.createFeatureSelection();
361
            feature = insertGeometry(geometry);
362
            newSelection.select(feature);
363
            featureStore.setSelection(newSelection);
364
            featureStore.endEditingGroup();
365
            featureStore.endComplexNotification();
366
        } catch (DataException e) {
367
            NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
368
        }
369
        return feature;
370
    }
371 29685 jpiera
372 37138 cordinyana
    public Feature insertGeometry(Geometry geometry) {
373
        VectorialLayerEdited vle = getVLE();
374 30335 jpiera
375 37138 cordinyana
        try {
376
            FeatureStore featureStore =
377
                ((FLyrVect) vle.getLayer()).getFeatureStore();
378
            EditableFeature eFeature = featureStore.createNewFeature(true);
379 37495 jpiera
380
            //Reproject the geometry
381
            Geometry insertedGeometry = geometry;
382
            if (getVLE().getLayer().getCoordTrans() != null){
383
                insertedGeometry = insertedGeometry.cloneGeometry();
384
                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
385
            }
386
387 37138 cordinyana
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
388 37495 jpiera
                .getDefaultGeometryAttributeName(), insertedGeometry);
389 37138 cordinyana
            featureStore.insert(eFeature);
390 30335 jpiera
391 37495 jpiera
            insertSpatialCache(insertedGeometry);
392 29685 jpiera
393 37495 jpiera
            draw(insertedGeometry, eFeature);
394 37138 cordinyana
            return eFeature;
395
        } catch (ReadException e) {
396
            NotificationManager.addError(e.getMessage(), e);
397
            return null;
398
        } catch (DataException e) {
399
            NotificationManager.addError(e.getMessage(), e);
400
            return null;
401
        } catch (CreateEnvelopeException e) {
402
            NotificationManager.addError(e.getMessage(), e);
403
            return null;
404
        }
405
406
    }
407
408
    /**
409
     * Devuelve la cadena que corresponde al estado en el que nos encontramos.
410
     *
411
     * @return Cadena para mostrar por consola.
412
     */
413
    public String getQuestion() {
414
        return question;
415
    }
416 29685 jpiera
417 37138 cordinyana
    /**
418
     * Actualiza la cadena que corresponde al estado actual.
419
     *
420
     * @param s
421
     *            Cadena que aparecer� en consola.
422
     */
423
    public void setQuestion(String s) {
424
        question = s;
425
    }
426 29685 jpiera
427 37138 cordinyana
    /**
428
     * Provoca un repintado "soft" de la capa activa en edici�n. Las capas por
429
     * debajo de ella no se dibujan de verdad, solo se dibuja la que est� en
430
     * edici�n y las que est�n por encima de ella en el TOC.
431
     */
432
    public void refresh() {
433
        getCadToolAdapter().getMapControl().rePaintDirtyLayers();
434
    }
435 29685 jpiera
436 37138 cordinyana
    public void drawHandlers(MapControlDrawer renderer, ArrayList selectedRows,
437
        AffineTransform at) {
438
        FeatureSet selection = null;
439
        DisposableIterator iterator = null;
440
        try {
441
            selection =
442
                (FeatureSet) ((FLyrVect) getVLE().getLayer()).getFeatureStore()
443
                    .getSelection();
444 29685 jpiera
445 37328 cordinyana
            iterator = selection.fastIterator();
446 37138 cordinyana
            while (iterator.hasNext()) {
447
                Feature feature = (Feature) iterator.next();
448 29685 jpiera
449 37138 cordinyana
                Geometry ig = (feature.getDefaultGeometry()).cloneGeometry();
450
                if (ig == null) {
451
                    continue;
452
                }
453
                Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
454
                renderer.drawHandlers(handlers, at,
455
                    mapControlManager.getGeometrySelectionSymbol());
456
            }
457 29685 jpiera
458 37138 cordinyana
        } catch (ReadException e) {
459 37328 cordinyana
            NotificationManager.addError(e.getMessage(), e);
460 37138 cordinyana
        } catch (DataException e) {
461 37328 cordinyana
            NotificationManager.addError(e.getMessage(), e);
462 37138 cordinyana
        } finally {
463
            if (iterator != null) {
464
                iterator.dispose();
465
            }
466
        }
467 29685 jpiera
468 37138 cordinyana
    }
469 29685 jpiera
470 37138 cordinyana
    public void setDescription(String[] currentdescriptions) {
471
        this.currentdescriptions = currentdescriptions;
472
    }
473 29685 jpiera
474 37138 cordinyana
    public String[] getDescriptions() {
475
        return currentdescriptions;
476
    }
477 29685 jpiera
478 37138 cordinyana
    public void end() {
479
        CADExtension.setCADTool("_selection", true);
480
        PluginServices.getMainFrame().setSelectedTool("_selection");
481
        CADTool cadtool = CADExtension.getCADTool();
482
        cadtool.setPreviosTool(this);
483
    }
484 29685 jpiera
485 37138 cordinyana
    public void init() {
486 37328 cordinyana
        // Nothing to do
487 37138 cordinyana
    }
488 29685 jpiera
489 37138 cordinyana
    protected ArrayList getSelectedHandlers() {
490
        VectorialLayerEdited vle = getVLE();
491
        ArrayList selectedHandlers = vle.getSelectedHandler();
492
        return selectedHandlers;
493
    }
494 29685 jpiera
495 37138 cordinyana
    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 29685 jpiera
511 37138 cordinyana
    }
512 29685 jpiera
513 37138 cordinyana
    public String getNextTool() {
514
        return tool;
515
    }
516 29685 jpiera
517 37138 cordinyana
    public void setNextTool(String tool) {
518
        this.tool = tool;
519
    }
520 29685 jpiera
521 37138 cordinyana
    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 29685 jpiera
552 37138 cordinyana
    public boolean isApplicable(int shapeType) {
553 37328 cordinyana
        GeometryType type = loadGeometryType(shapeType, GEOM2D);
554
        return isApplicable(type);
555 37138 cordinyana
    }
556 29685 jpiera
557 37328 cordinyana
    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 37138 cordinyana
    public abstract String toString();
603 29685 jpiera
604 37138 cordinyana
    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 29685 jpiera
612 37138 cordinyana
    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 29685 jpiera
620 37138 cordinyana
    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 29685 jpiera
628 37138 cordinyana
    public void setPreviosTool(DefaultCADTool tool) {
629
        previousTool = tool;
630
    }
631 29685 jpiera
632 37138 cordinyana
    public void restorePreviousTool() {
633
        CADExtension.setCADTool(previousTool.toString(), true);
634
        PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
635
    }
636 29685 jpiera
637 37138 cordinyana
    public void endTransition(double x, double y, MouseEvent e) {
638 37328 cordinyana
        // Nothing to do
639 37138 cordinyana
    }
640
641
    /**
642 35619 jpiera
     * Create a curve. If there is an
643
     * error return <code>null</code> and add the error
644 37138 cordinyana
     * to the log
645
     *
646 35619 jpiera
     * @return
647 37138 cordinyana
     *         The Curve
648 35619 jpiera
     */
649 37138 cordinyana
    protected Curve createCurve() {
650 35619 jpiera
        try {
651 37138 cordinyana
            return (Curve) geomManager.create(TYPES.CURVE, getSubType());
652 35619 jpiera
        } 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 37138 cordinyana
659 35941 jpiera
    /**
660 37328 cordinyana
     * 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 35941 jpiera
     * Create an {@link OrientablePrimitive}. If there is an
680
     * error return <code>null</code> and add the error
681 37138 cordinyana
     * to the log
682
     *
683 35941 jpiera
     * @param geometryType
684 37138 cordinyana
     *            a type of a OrientablePrimitive.
685 35941 jpiera
     * @return
686 37138 cordinyana
     *         The {@link OrientablePrimitive}
687 35941 jpiera
     */
688 37138 cordinyana
    protected OrientablePrimitive createOrientablePrimitive(int geometryType) {
689 35941 jpiera
        try {
690 37138 cordinyana
            return (OrientablePrimitive) geomManager.create(geometryType,
691
                getSubType());
692 35941 jpiera
        } 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 37138 cordinyana
699 35941 jpiera
    /**
700
     * Create an envelope like a curve from two points.
701 37138 cordinyana
     * If there is an error return <code>null</code> and add the error to the
702
     * log.
703
     *
704 35941 jpiera
     * @param firstPoint
705 37138 cordinyana
     *            first point
706 35941 jpiera
     * @param secondPoint
707 37138 cordinyana
     *            second point
708 35941 jpiera
     * @return
709 37138 cordinyana
     *         the curve
710 35941 jpiera
     */
711 37138 cordinyana
    protected Curve createEnvelopeLikeCurve(Point2D firstPoint,
712
        Point2D secondPoint) {
713 35941 jpiera
        try {
714 37138 cordinyana
            Curve curve = (Curve) geomManager.create(TYPES.CURVE, getSubType());
715
            curve.addMoveToVertex(createPoint(firstPoint.getX(),
716
                firstPoint.getY()));
717 35941 jpiera
            curve.addVertex(createPoint(secondPoint.getX(), firstPoint.getY()));
718 37138 cordinyana
            curve
719
                .addVertex(createPoint(secondPoint.getX(), secondPoint.getY()));
720 35941 jpiera
            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 29685 jpiera
730 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating curve", new CreateGeometryException(
747
                TYPES.CURVE, getSubType(), e));
748 37138 cordinyana
        }
749
        return curve;
750
    }
751 29685 jpiera
752 37138 cordinyana
    /**
753 35619 jpiera
     * Create a surface. If there is an
754
     * error return <code>null</code> and add the error
755 37138 cordinyana
     * to the log
756
     *
757 35619 jpiera
     * @return
758 37138 cordinyana
     *         The Surface
759 35619 jpiera
     */
760 37138 cordinyana
    protected Surface createSurface() {
761 35619 jpiera
        try {
762 37138 cordinyana
            return (Surface) geomManager.create(TYPES.SURFACE, getSubType());
763
764 35619 jpiera
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
765
            LOG.error("Error creating surface", new CreateGeometryException(
766 37138 cordinyana
                TYPES.SURFACE, getSubType(), e));
767 35619 jpiera
        }
768
        return null;
769
    }
770 37138 cordinyana
771
    /**
772 37328 cordinyana
     * 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 37138 cordinyana
     * 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 34696 cordinyana
            LOG.error("Error creating surface", new CreateGeometryException(
809 37138 cordinyana
                TYPES.SURFACE, getSubType(), e));
810
        }
811
        return surface;
812
    }
813 29685 jpiera
814 37138 cordinyana
    /**
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 29685 jpiera
828 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating point with x=" + x + ", y=" + y,
848
                new CreateGeometryException(TYPES.POINT, getSubType(), e));
849 37138 cordinyana
        }
850
        return point;
851
    }
852 29685 jpiera
853 37138 cordinyana
    /**
854 37328 cordinyana
     * 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 37138 cordinyana
     * 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 29685 jpiera
891 37138 cordinyana
    /**
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 35615 jpiera
            return null;
906
        }
907 37138 cordinyana
        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 34696 cordinyana
            LOG.error("Error creating arc with p1=" + p1, ", p2=" + p2
912
                + ", p3=" + p3, new CreateGeometryException(TYPES.ARC,
913
                getSubType(), e));
914 37138 cordinyana
        }
915
        return arc;
916
    }
917 29685 jpiera
918 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2
936
                + ", p3=" + p3, new CreateGeometryException(TYPES.CIRCLE,
937
                getSubType(), e));
938 37138 cordinyana
        }
939
        return circle;
940
    }
941 29685 jpiera
942 37138 cordinyana
    /**
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 29685 jpiera
956 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating circle with p1=" + p1 + " p2=" + p2,
973
                new CreateGeometryException(TYPES.CIRCLE, getSubType(), e));
974 37138 cordinyana
        }
975
        return circle;
976
    }
977 29685 jpiera
978 37138 cordinyana
    /**
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 29685 jpiera
992 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating circle with p1=" + p1 + " radious="
1009
                + radious, new CreateGeometryException(TYPES.CIRCLE,
1010
                getSubType(), e));
1011 37138 cordinyana
        }
1012
        return circle;
1013
    }
1014 29685 jpiera
1015 37138 cordinyana
    /**
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 29685 jpiera
1030 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating ellipse with p1=" + p1 + " p2=" + p2
1048
                + ", d=" + d, new CreateGeometryException(TYPES.ELLIPSE,
1049 37138 cordinyana
                getSubType(), e));
1050
        }
1051
        return ellipse;
1052
    }
1053 29685 jpiera
1054 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error("Error creating spline", new CreateGeometryException(
1072
                TYPES.SPLINE, getSubType(), e));
1073 37138 cordinyana
        }
1074
        return spline;
1075
    }
1076 29685 jpiera
1077 37138 cordinyana
    /**
1078 35620 jpiera
     * Create a MultiPrimitive. If there is an
1079
     * error return <code>null</code> and add the error
1080 37138 cordinyana
     * to the log
1081
     *
1082 35620 jpiera
     * @return
1083
     */
1084 37138 cordinyana
    protected MultiPrimitive createMultiPrimitive() {
1085 35620 jpiera
        try {
1086 37138 cordinyana
            return (MultiPrimitive) geomManager.create(TYPES.AGGREGATE,
1087
                getSubType());
1088 35620 jpiera
        } 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 37138 cordinyana
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 34696 cordinyana
            LOG.error("Error creating MultiPrimitive",
1114
                new CreateGeometryException(TYPES.SPLINE, getSubType(), e));
1115 37138 cordinyana
        }
1116
        return multiPrimitive;
1117
    }
1118 30335 jpiera
1119 37138 cordinyana
    /**
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 34696 cordinyana
            LOG.error(
1130
                "Error getting subtype of the default feature type of the store: "
1131
                    + featureStore, e);
1132 37138 cordinyana
            return SUBTYPES.GEOM3D;
1133
        }
1134
    }
1135 37328 cordinyana
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 29685 jpiera
}