Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 27270

History | View | Annotate | Download (27.2 KB)

1 3831 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.cad;
42
43 3997 fjp
import java.awt.Color;
44 3883 caballero
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46 17984 vcaballero
import java.awt.event.MouseEvent;
47 3883 caballero
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Point2D;
49
import java.awt.image.BufferedImage;
50 4144 fjp
import java.util.ArrayList;
51 21668 vcaballero
import java.util.Iterator;
52 3883 caballero
53 5893 fjp
import org.apache.log4j.Logger;
54 24500 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.exception.ReadException;
56 24490 jmvivo
import org.gvsig.fmap.dal.feature.EditableFeature;
57
import org.gvsig.fmap.dal.feature.Feature;
58 25779 vcaballero
import org.gvsig.fmap.dal.feature.FeatureSelection;
59 24490 jmvivo
import org.gvsig.fmap.dal.feature.FeatureSet;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61 26921 jpiera
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
62 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
63 26329 jpiera
import org.gvsig.fmap.geom.GeometryLocator;
64 21668 vcaballero
import org.gvsig.fmap.geom.GeometryManager;
65 26921 jpiera
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
66
import org.gvsig.fmap.geom.Geometry.TYPES;
67 27024 jpiera
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
68 21668 vcaballero
import org.gvsig.fmap.geom.handler.Handler;
69 22025 jmvivo
import org.gvsig.fmap.geom.operation.Draw;
70
import org.gvsig.fmap.geom.operation.DrawOperationContext;
71 21668 vcaballero
import org.gvsig.fmap.geom.operation.GeometryOperationException;
72
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
73 26921 jpiera
import org.gvsig.fmap.geom.primitive.Arc;
74
import org.gvsig.fmap.geom.primitive.Circle;
75
import org.gvsig.fmap.geom.primitive.Curve;
76
import org.gvsig.fmap.geom.primitive.Ellipse;
77 22986 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
78 21668 vcaballero
import org.gvsig.fmap.geom.primitive.GeneralPathX;
79 26921 jpiera
import org.gvsig.fmap.geom.primitive.Point;
80 27024 jpiera
import org.gvsig.fmap.geom.primitive.Primitive;
81 26921 jpiera
import org.gvsig.fmap.geom.primitive.Spline;
82
import org.gvsig.fmap.geom.primitive.Surface;
83 27019 jpiera
import org.gvsig.fmap.geom.util.UtilFunctions;
84 21668 vcaballero
import org.gvsig.fmap.mapcontext.ViewPort;
85
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
86
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
87
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
88
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
89
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
90 5893 fjp
91 3883 caballero
import com.iver.andami.PluginServices;
92 12148 caballero
import com.iver.andami.messages.NotificationManager;
93 3883 caballero
import com.iver.cit.gvsig.CADExtension;
94 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
95 4465 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
96 7304 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
97 4892 caballero
import com.iver.utiles.console.JConsole;
98 3831 caballero
99
/**
100
 * DOCUMENT ME!
101 5985 caballero
 *
102 3831 caballero
 * @author Vicente Caballero Navarro
103
 */
104
public abstract class DefaultCADTool implements CADTool {
105 24429 vcaballero
        public static ISymbol selectionSymbol = SymbologyFactory
106
                        .createDefaultSymbolByShapeType(Geometry.TYPES.GEOMETRY, new Color(
107
                                        255, 0, 0, 100)); // Le ponemos una transparencia
108
        public static ISymbol axisReferencesSymbol = SymbologyFactory
109
                        .createDefaultSymbolByShapeType(Geometry.TYPES.GEOMETRY, new Color(
110
                                        100, 100, 100, 100));
111
        public static ISymbol geometrySelectSymbol = SymbologyFactory
112
                        .createDefaultSymbolByShapeType(Geometry.TYPES.GEOMETRY, Color.RED);
113
        public static ISymbol handlerSymbol = SymbologyFactory
114
                        .createDefaultSymbolByShapeType(Geometry.TYPES.GEOMETRY,
115
                                        Color.ORANGE);
116 26921 jpiera
        protected static Logger logger = Logger.getLogger(DefaultCADTool.class
117 5923 fjp
                        .getName());
118 4445 fjp
        private CADToolAdapter cadToolAdapter;
119 3831 caballero
120 4445 fjp
        private String question;
121 3831 caballero
122 4445 fjp
        private String[] currentdescriptions;
123 3831 caballero
124 4708 caballero
        private String tool = "selection";
125
126 9052 caballero
        private DefaultCADTool previousTool;
127
128 13561 caballero
        private ArrayList temporalCache = new ArrayList();
129 26921 jpiera
        protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
130 24429 vcaballero
131 21668 vcaballero
        public void addTemporalCache(Geometry geom) {
132 13561 caballero
                temporalCache.add(geom);
133
                insertSpatialCache(geom);
134
        }
135 24429 vcaballero
136 13561 caballero
        public void clearTemporalCache() {
137 24429 vcaballero
                Geometry[] geoms = (Geometry[]) temporalCache.toArray(new Geometry[0]);
138
                for (int i = 0; i < geoms.length; i++) {
139 13561 caballero
                        removeSpatialCache(geoms[i]);
140
                }
141
                temporalCache.clear();
142
        }
143 24429 vcaballero
144 21668 vcaballero
        private void insertSpatialCache(Geometry geom) {
145 13561 caballero
                VectorialLayerEdited vle = getVLE();
146 24429 vcaballero
                SpatialCache spatialCache = ((FLyrVect) vle.getLayer())
147
                                .getSpatialCache();
148
                Envelope r = geom.getEnvelope();
149
                if (geom.getType() == Geometry.TYPES.POINT) {
150 27019 jpiera
                        r = UtilFunctions.createEnvelope(r.getMinimum(0), r.getMinimum(1), r
151 24429 vcaballero
                                        .getMinimum(0) + 1, r.getMinimum(1) + 1);// Rectangle2D.Double(r.getX(),r.getY(),1,1);
152 13561 caballero
                }
153 24429 vcaballero
                spatialCache.insert(r, geom);
154 13561 caballero
155
        }
156 24429 vcaballero
157 21668 vcaballero
        private void removeSpatialCache(Geometry geom) {
158 13561 caballero
                VectorialLayerEdited vle = getVLE();
159 24429 vcaballero
                SpatialCache spatialCache = ((FLyrVect) vle.getLayer())
160
                                .getSpatialCache();
161
                Envelope r = geom.getEnvelope();
162
                if (geom.getType() == Geometry.TYPES.POINT) {
163 27019 jpiera
                        r = UtilFunctions.createEnvelope(r.getMinimum(0), r.getMinimum(1), r
164 24429 vcaballero
                                        .getMinimum(0) + 1, r.getMinimum(1) + 1);// Rectangle2D.Double(r.getX(),r.getY(),1,1);
165 13561 caballero
                }
166 24429 vcaballero
                spatialCache.remove(r, geom);
167 13561 caballero
168
        }
169 24429 vcaballero
170 4445 fjp
        /**
171
         * DOCUMENT ME!
172
         */
173 21668 vcaballero
        public void draw(Geometry geometry) {
174 4445 fjp
                if (geometry != null) {
175
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
176
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
177
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
178 24429 vcaballero
                        DrawOperationContext doc = new DrawOperationContext();
179 27269 jmvivo
                        doc.setGraphics(gImag);
180 21668 vcaballero
                        doc.setViewPort(vp);
181
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
182 24429 vcaballero
                        try {
183
                                geometry.cloneGeometry().invokeOperation(Draw.CODE, doc);
184 21668 vcaballero
                        } catch (GeometryOperationNotSupportedException e) {
185
                                e.printStackTrace();
186
                        } catch (GeometryOperationException e) {
187
                                e.printStackTrace();
188
                        }
189 24429 vcaballero
                        // geometry.draw(gImag, vp, DefaultCADTool.selectionSymbol);
190 4445 fjp
                }
191
        }
192 3831 caballero
193 4445 fjp
        /**
194
         * DOCUMENT ME!
195 5985 caballero
         *
196 4445 fjp
         * @param cta
197
         *            DOCUMENT ME!
198
         */
199
        public void setCadToolAdapter(CADToolAdapter cta) {
200
                cadToolAdapter = cta;
201
        }
202 3831 caballero
203 4445 fjp
        /**
204
         * DOCUMENT ME!
205 5985 caballero
         *
206 4445 fjp
         * @return DOCUMENT ME!
207
         */
208
        public CADToolAdapter getCadToolAdapter() {
209
                return cadToolAdapter;
210
        }
211 5923 fjp
212
        public VectorialLayerEdited getVLE() {
213
                return (VectorialLayerEdited) CADExtension.getEditionManager()
214
                                .getActiveLayerEdited();
215 4637 caballero
        }
216 3831 caballero
217 4445 fjp
        /**
218
         * DOCUMENT ME!
219 5985 caballero
         *
220 4445 fjp
         * @param g
221
         *            DOCUMENT ME!
222
         * @param firstPoint
223
         *            DOCUMENT ME!
224
         * @param endPoint
225
         *            DOCUMENT ME!
226
         */
227 24429 vcaballero
        public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint,
228
                        ISymbol symbol) {
229 4445 fjp
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
230
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
231
                elShape.lineTo(endPoint.getX(), endPoint.getY());
232 24429 vcaballero
                DrawOperationContext doc = new DrawOperationContext();
233 21668 vcaballero
                doc.setGraphics(g);
234
                doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
235
                doc.setSymbol(symbol);
236
                try {
237 26921 jpiera
                        Curve curve = (Curve)geomManager.create(Geometry.TYPES.CURVE, Geometry.SUBTYPES.GEOM2D);
238
                        curve.setGeneralPath(elShape);
239
                        curve.invokeOperation(Draw.CODE, doc);
240 21668 vcaballero
                } catch (GeometryOperationNotSupportedException e) {
241
                        e.printStackTrace();
242
                } catch (GeometryOperationException e) {
243
                        e.printStackTrace();
244 27269 jmvivo
                } catch (InstantiationException e) {
245 26921 jpiera
                        e.printStackTrace();
246 27269 jmvivo
                } catch (IllegalAccessException e) {
247 26921 jpiera
                        e.printStackTrace();
248 21668 vcaballero
                }
249 24429 vcaballero
                // draw(g,
250
                // getCadToolAdapter().getMapControl().getViewPort(),
251
                // symbol);
252 26921 jpiera
253 4445 fjp
        }
254
255
        /**
256
         * DOCUMENT ME!
257 5985 caballero
         *
258 4445 fjp
         * @param geometry
259
         *            DOCUMENT ME!
260
         */
261 24429 vcaballero
        public Feature insertGeometry(Geometry geometry, Feature feature) {
262
                VectorialLayerEdited vle = getVLE();
263 21668 vcaballero
264 4445 fjp
                try {
265 24429 vcaballero
                        FeatureStore featureStore = ((FLyrVect) vle.getLayer())
266
                                        .getFeatureStore();
267
                        EditableFeature eFeature = featureStore.createNewFeature(
268
                                        featureStore.getDefaultFeatureType(), feature);
269
                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
270
                                        .getDefaultGeometryAttributeName(), geometry);
271
                        featureStore.insert(eFeature);
272 27270 vcaballero
//                        drawToImage(featureStore, vle, eFeature);
273
                        insertSpatialCache(geometry);
274 24429 vcaballero
                        draw(geometry.cloneGeometry());
275
                        return eFeature;
276
                } catch (ReadException e) {
277
                        NotificationManager.addError(e.getMessage(), e);
278
                        return null;
279
                } catch (DataException e) {
280
                        NotificationManager.addError(e.getMessage(), e);
281
                        return null;
282
                }
283
        }
284 27270 vcaballero
        public Feature insertAndSelectGeometry(Geometry geometry){
285
                Feature feature=null;
286
                try{
287
                        FeatureStore featureStore = getVLE().getFeatureStore();
288
                        featureStore.beginEditingGroup(getName());
289
                        FeatureSelection newSelection = featureStore
290
                        .createFeatureSelection();
291
                        feature=insertGeometry(geometry);
292
                        newSelection.select(feature);
293
//                        clearSelection();
294
                        featureStore.setSelection(newSelection);
295
                        featureStore.endEditingGroup();
296
                }catch (DataException e) {
297
                        NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
298
                }
299
                return feature;
300
        }
301 24429 vcaballero
        public Feature insertGeometry(Geometry geometry) {
302
                VectorialLayerEdited vle = getVLE();
303 4637 caballero
304 24429 vcaballero
                try {
305
                        FeatureStore featureStore = ((FLyrVect) vle.getLayer())
306
                                        .getFeatureStore();
307
                        EditableFeature eFeature = featureStore.createNewFeature(true);
308
                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
309
                                        .getDefaultGeometryAttributeName(), geometry);
310
                        featureStore.insert(eFeature);
311 5985 caballero
312 27270 vcaballero
//                        drawToImage(featureStore, vle, eFeature);
313
                        insertSpatialCache(geometry);
314 24429 vcaballero
                        draw(geometry.cloneGeometry());
315
                        return eFeature;
316 21668 vcaballero
                } catch (ReadException e) {
317 24429 vcaballero
                        NotificationManager.addError(e.getMessage(), e);
318
                        return null;
319 21668 vcaballero
                } catch (DataException e) {
320 24429 vcaballero
                        NotificationManager.addError(e.getMessage(), e);
321
                        return null;
322 15637 jmvivo
                }
323 24429 vcaballero
324 4445 fjp
        }
325 25779 vcaballero
        private void drawToImage(FeatureStore featureStore,VectorialLayerEdited vle, Feature feature) throws DataException, GeometryOperationNotSupportedException, GeometryOperationException {
326 27269 jmvivo
                // clearSelection();
327
                // ViewPort vp = vle.getLayer().getMapContext().getViewPort();
328 26336 vcaballero
//                BufferedImage selectionImage = new BufferedImage(
329
//                                vp.getImageWidth(), vp.getImageHeight(),
330
//                                BufferedImage.TYPE_INT_ARGB);
331
//                Graphics2D gs = selectionImage.createGraphics();
332
//                BufferedImage handlersImage = new BufferedImage(
333
//                                vp.getImageWidth(), vp.getImageHeight(),
334
//                                BufferedImage.TYPE_INT_ARGB);
335
//                Graphics2D gh = handlersImage.createGraphics();
336
337 24429 vcaballero
                // int inversedIndex=vea.getInversedIndex(index);
338 27269 jmvivo
                // FeatureSelection selection = (FeatureSelection)
339
                // featureStore.getSelection();
340
                // selection.select(feature);
341 24429 vcaballero
                // vle.addSelectionCache(new DefaultRowEdited(df,
342
                // IRowEdited.STATUS_ADDED, inversedIndex ));
343
                // vea.getSelection().set(inversedIndex);
344
                // Geometry geom = (Geometry)feature.getDefaultGeometry();
345 26336 vcaballero
//                DrawOperationContext doc = new DrawOperationContext();
346
//                doc.setGraphics(gs);
347
//                doc.setViewPort(vp);
348
//                doc.setSymbol(DefaultCADTool.selectionSymbol);
349 25779 vcaballero
                Geometry geometry=feature.getDefaultGeometry();
350 26336 vcaballero
//                geometry.cloneGeometry().invokeOperation(Draw.CODE, doc);
351
//                // draw(gs, vp, DefaultCADTool.selectionSymbol);
352
//                vle.drawHandlers(geometry.cloneGeometry(), gh, vp);
353
//                vle.setHandlersImage(handlersImage);
354
//                vle.setSelectionImage(selectionImage);
355 24429 vcaballero
                insertSpatialCache(geometry);
356 4445 fjp
357 24429 vcaballero
        }
358 13561 caballero
359 4445 fjp
        /**
360
         * DOCUMENT ME!
361 5985 caballero
         *
362 4445 fjp
         * @param geometry
363
         *            DOCUMENT ME!
364
         */
365 24429 vcaballero
        // public void modifyFeature(Feature feature, Feature oldFeature) {
366
        // try {
367
        // CommandsRecord
368
        // cr=((FLyrVect)getVLE().getLayer()).getFeatureStore().getCommandsRecord();
369
        // cr.update(feature, oldFeature);
370
        // } catch (ReadException e) {
371
        // NotificationManager.addError(e.getMessage(),e);
372
        // }
373
        // draw(((Geometry)feature.getDefaultGeometry()).cloneGeometry());
374
        // }
375 4445 fjp
        /**
376
         * DOCUMENT ME!
377 5985 caballero
         *
378 4445 fjp
         * @param geometry
379
         *            DOCUMENT ME!
380
         * @param values
381
         *            DOCUMENT ME!
382
         */
383 24429 vcaballero
//        public Feature addGeometry(Geometry geometry, Object[] values) {
384
//                // int index = 0;
385 21668 vcaballero
//                try {
386 24429 vcaballero
//                        FeatureStore featureStore = ((FLyrVect) getVLE().getLayer())
387
//                                        .getFeatureStore();
388
//                        EditableFeature eFeature = featureStore.createNewFeature(true);
389
//                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
390
//                                        .getDefaultGeometryAttributeName(), geometry);
391
//                        for (int i = 0; i < values.length; i++) {
392
//                                eFeature.set(i, values[i]);
393
//                        }
394
//                        featureStore.insert(eFeature);
395
//                        return eFeature;
396
//                } catch (DataException e) {
397
//                        // TODO Auto-generated catch block
398
//                        e.printStackTrace();
399 21668 vcaballero
//                }
400 24429 vcaballero
//                // try {
401
//                // String newFID = vea.getNewFID();
402
//                // DefaultFeature df = new DefaultFeature(geometry, values, newFID);
403
//                // index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
404
//                // insertSpatialCache(geometry);
405
//                // } catch (ValidateRowException e) {
406
//                // NotificationManager.addError(e);
407
//                // } catch (ReadException e) {
408
//                // NotificationManager.addError(e);
409
//                // }
410
//                // return vea.getInversedIndex(index);
411
//                return null;
412
//        }
413 3831 caballero
414 4445 fjp
        /**
415
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
416 5985 caballero
         *
417 4445 fjp
         * @return Cadena para mostrar por consola.
418
         */
419
        public String getQuestion() {
420
                return question;
421
        }
422 3831 caballero
423 4445 fjp
        /**
424
         * Actualiza la cadena que corresponde al estado actual.
425 5985 caballero
         *
426 4445 fjp
         * @param s
427
         *            Cadena que aparecer? en consola.
428
         */
429
        public void setQuestion(String s) {
430
                question = s;
431 5923 fjp
                // ConsoleToken.addQuestion(s);
432 4445 fjp
        }
433 3831 caballero
434 4445 fjp
        /**
435 5923 fjp
         * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
436
         * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
437
         * edici?n y las que est?n por encima de ella en el TOC.
438 4445 fjp
         */
439
        public void refresh() {
440 5923 fjp
                // getCadToolAdapter().getMapControl().drawMap(false);
441
442
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
443 4445 fjp
        }
444
445 5923 fjp
        /*
446
         * public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
447
         * throws DriverIOException { for (int i = sel.nextSetBit(0); i >= 0; i =
448
         * sel.nextSetBit(i + 1)) { IGeometry ig =
449
         * getCadToolAdapter().getVectorialAdapter() .getShape(i).cloneGeometry();
450
         * if (ig == null) continue; Handler[] handlers =
451
         * ig.getHandlers(IGeometry.SELECTHANDLER);
452
         * FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers); } }
453
         */
454 4445 fjp
        public void drawHandlers(Graphics g, ArrayList selectedRows,
455
                        AffineTransform at) {
456 24429 vcaballero
                FeatureSet selection = null;
457 21668 vcaballero
                try {
458 24429 vcaballero
                        selection = (FeatureSet) ((FLyrVect) getVLE().getLayer())
459
                                        .getFeatureStore().getSelection();
460 24265 vcaballero
461 24429 vcaballero
                        Iterator iterator = selection.iterator();
462 24265 vcaballero
                        while (iterator.hasNext()) {
463
                                Feature feature = (Feature) iterator.next();
464
465 24429 vcaballero
                                // }
466
                                // for (int i = 0; i < selectedRows.size(); i++) {
467
                                // IRowEdited edRow = (IRowEdited) selectedRows.get(i);
468
                                // IFeature feat = (IFeature) edRow.getLinkedRow();
469 24265 vcaballero
                                // IFeature feat = (IFeature) selectedRows.get(i);
470 27269 jmvivo
                                Geometry ig = (feature.getDefaultGeometry())
471 24429 vcaballero
                                                .cloneGeometry();
472 27269 jmvivo
                                if (ig == null) {
473 24265 vcaballero
                                        continue;
474 27269 jmvivo
                                }
475 24265 vcaballero
                                Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
476 24429 vcaballero
                                FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers,
477
                                                DefaultCADTool.handlerSymbol);
478 24265 vcaballero
                        }
479 21668 vcaballero
                } catch (ReadException e) {
480
                        // TODO Auto-generated catch block
481
                        e.printStackTrace();
482 24265 vcaballero
                } catch (DataException e) {
483
                        // TODO Auto-generated catch block
484
                        e.printStackTrace();
485 21668 vcaballero
                }
486 4445 fjp
        }
487 3883 caballero
488
        public void setDescription(String[] currentdescriptions) {
489
                this.currentdescriptions = currentdescriptions;
490
        }
491 4445 fjp
492
        public String[] getDescriptions() {
493 3883 caballero
                return currentdescriptions;
494
        }
495
496 4445 fjp
        /*
497
         * (non-Javadoc)
498 5985 caballero
         *
499 4445 fjp
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
500
         */
501
        public void end() {
502 5923 fjp
                CADExtension.setCADTool("_selection", true);
503 5016 caballero
                PluginServices.getMainFrame().setSelectedTool("_selection");
504 24429 vcaballero
                CADTool cadtool = CADExtension.getCADTool();
505 9052 caballero
                cadtool.setPreviosTool(this);
506 4445 fjp
        }
507
508 3997 fjp
        public void init() {
509 24429 vcaballero
                // jaume, should not be necessary
510
                // CADTool.drawingSymbol.setOutlined(true);
511
                // CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
512 4024 caballero
513 3997 fjp
        }
514 5923 fjp
515 24429 vcaballero
        // protected ArrayList getSelectedRows() {
516
        // VectorialLayerEdited vle = getVLE();
517
        // ArrayList selectedRow = vle.getSelectedRow();
518
        // return selectedRow;
519
        // }
520 5923 fjp
521
        protected ArrayList getSelectedHandlers() {
522 4637 caballero
                VectorialLayerEdited vle = getVLE();
523 5923 fjp
                ArrayList selectedHandlers = vle.getSelectedHandler();
524
                return selectedHandlers;
525 4584 caballero
        }
526 5923 fjp
527 24429 vcaballero
        public void clearSelection() throws DataException {
528 4637 caballero
                VectorialLayerEdited vle = getVLE();
529 26336 vcaballero
                FeatureSelection selection = null;
530
                selection = (FeatureSelection) ((FLyrVect) vle.getLayer())
531 24429 vcaballero
                                        .getFeatureStore().getSelection();
532
                // ArrayList selectedRow = vle.getSelectedRow();
533 5923 fjp
                ArrayList selectedHandlers = vle.getSelectedHandler();
534 26336 vcaballero
                selection.deselectAll();
535 5923 fjp
                selectedHandlers.clear();
536 24429 vcaballero
                // VectorialEditableAdapter vea = vle.getVEA();
537
                // FBitSet selection = vea.getSelection();
538
                // selection.clear();
539 21668 vcaballero
                vle.setSelectionImage(null);
540
                vle.setHandlersImage(null);
541 4637 caballero
542 4584 caballero
        }
543 5923 fjp
544 4708 caballero
        public String getNextTool() {
545
                return tool;
546
        }
547
548
        public void setNextTool(String tool) {
549
                this.tool = tool;
550
        }
551 5923 fjp
552
        public boolean changeCommand(String name) throws CommandException {
553
                CADTool[] cadtools = CADExtension.getCADTools();
554
                for (int i = 0; i < cadtools.length; i++) {
555
                        CADTool ct = cadtools[i];
556
                        if (name.equalsIgnoreCase(ct.getName())
557
                                        || name.equalsIgnoreCase(ct.toString())) {
558 21668 vcaballero
                                int type = Geometry.TYPES.POINT;
559 5170 caballero
                                try {
560 5923 fjp
                                        type = ((FLyrVect) getVLE().getLayer()).getShapeType();
561 21668 vcaballero
                                } catch (ReadException e) {
562 12739 caballero
                                        throw new CommandException(e);
563 5170 caballero
                                }
564
                                if (ct.isApplicable(type)) {
565
                                        getCadToolAdapter().setCadTool(ct);
566
                                        ct.init();
567
                                        View vista = (View) PluginServices.getMDIManager()
568 6880 cesar
                                                        .getActiveWindow();
569 5170 caballero
                                        vista.getConsolePanel().addText("\n" + ct.getName(),
570
                                                        JConsole.COMMAND);
571 24429 vcaballero
                                        String question = ct.getQuestion();
572 9121 caballero
                                        vista.getConsolePanel().addText(
573
                                                        "\n" + "#" + question + " > ", JConsole.MESSAGE);
574 5170 caballero
                                        return true;
575
                                }
576 10626 caballero
                                throw new CommandException(name);
577 4892 caballero
                        }
578
                }
579
                return false;
580
        }
581 5923 fjp
582 5170 caballero
        public boolean isApplicable(int shapeType) {
583
                return true;
584
        }
585 5923 fjp
586 4892 caballero
        public abstract String toString();
587 5735 caballero
588 5923 fjp
        public void throwValueException(String s, double d) {
589 6880 cesar
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
590 5923 fjp
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
591 5735 caballero
        }
592 5923 fjp
593
        public void throwOptionException(String s, String o) {
594 6880 cesar
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
595 5923 fjp
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
596 5735 caballero
        }
597 5923 fjp
598
        public void throwPointException(String s, double x, double y) {
599 6880 cesar
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
600 5923 fjp
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
601
                                JConsole.ERROR);
602 5735 caballero
        }
603
604 9052 caballero
        public void setPreviosTool(DefaultCADTool tool) {
605 24429 vcaballero
                previousTool = tool;
606 9052 caballero
        }
607 24429 vcaballero
608 9052 caballero
        public void restorePreviousTool() {
609
                CADExtension.setCADTool(previousTool.toString(), true);
610
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
611
        }
612 24429 vcaballero
613 17984 vcaballero
        public void endTransition(double x, double y, MouseEvent e) {
614
                // TODO Auto-generated method stub
615 9052 caballero
616 17984 vcaballero
        }
617 27269 jmvivo
618 26921 jpiera
        /**
619
         * Create a curve from a GeneralPath. If there is an
620
         * error return <code>null</code> and add the error
621
         * to the log
622
         * @param gpx
623
         * The GeneralPath
624
         * @return
625
         * The Curve
626
         */
627
        protected Curve createCurve(GeneralPathX gpx){
628
                Curve curve = null;
629
                try {
630
                        curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
631 27269 jmvivo
                        curve.setGeneralPath(gpx);
632 26921 jpiera
                } catch (InstantiationException e) {
633
                        logger.error(new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D, e));
634
                } catch (IllegalAccessException e) {
635
                        logger.error(new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D, e));
636
                }
637
                return curve;
638
        }
639 27269 jmvivo
640 26921 jpiera
        /**
641
         * Create a surface from a GeneralPath. If there is an
642
         * error return <code>null</code> and add the error
643
         * to the log
644
         * @param gpx
645
         * The general path
646
         * @return
647
         * The Surface
648
         */
649
        protected Surface createSurface(GeneralPathX gpx){
650
                Surface surface = null;
651
                try {
652
                        surface = (Surface)geomManager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
653 27269 jmvivo
                        surface.setGeneralPath(gpx);
654 26921 jpiera
                } catch (InstantiationException e) {
655
                        logger.error(new CreateGeometryException(TYPES.SURFACE, SUBTYPES.GEOM2D, e));
656
                } catch (IllegalAccessException e) {
657
                        logger.error(new CreateGeometryException(TYPES.SURFACE, SUBTYPES.GEOM2D, e));
658
                }
659
                return surface;
660
        }
661 27269 jmvivo
662 26921 jpiera
        /**
663
         * Create a curve point. If there is an
664
         * error return <code>null</code> and add the error
665
         * to the log
666
         * @param p1
667
         * The AWT point
668
         * @return
669
         * The gvSIG point
670
         */
671
        protected Point createPoint(Point2D p1){
672
                return createPoint(p1.getX(), p1.getY());
673
        }
674 27269 jmvivo
675 26921 jpiera
        /**
676
         * Create point. If there is an
677
         * error return <code>null</code> and add the error
678
         * to the log
679
         * @param x
680
         * The X coordinate
681
         * @param y
682
         * The y coordinate
683
         * @return
684
         * The Point
685
         */
686
        protected Point createPoint(double x, double y){
687
                Point point = null;
688
                try {
689
                        point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
690
                        point.setX(x);
691
                        point.setY(y);
692
                } catch (InstantiationException e) {
693
                        logger.error(new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
694
                } catch (IllegalAccessException e) {
695
                        logger.error(new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
696
                }
697
                return point;
698
        }
699 27269 jmvivo
700 26921 jpiera
        /**
701
         * Create an Arc. If there is an
702
         * error return <code>null</code> and add the error
703
         * to the log
704
         * @param p1
705
         * @param p2
706
         * @param p3
707
         * @return
708
         * The arc
709
         */
710
        protected Arc createArc(Point2D p1, Point2D p2, Point2D p3){
711
                return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
712
        }
713 27269 jmvivo
714 26921 jpiera
        /**
715
         * Create an arc. If there is an
716
         * error return <code>null</code> and add the error
717
         * to the log
718
         * @param p1
719
         * @param p2
720
         * @param p3
721
         * @return
722
         * The arc
723
         */
724
        protected Arc createArc(Point p1, Point p2, Point p3){
725
                Arc arc = null;
726
                try {
727
                        arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
728
                        arc.setPoints(p1, p2, p3);
729
                } catch (InstantiationException e) {
730
                        logger.error(new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
731
                } catch (IllegalAccessException e) {
732
                        logger.error(new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
733
                }
734
                return arc;
735
        }
736 27269 jmvivo
737 26921 jpiera
        /**
738
         * Create a circle. If there is an
739
         * error return <code>null</code> and add the error
740
         * to the log
741
         * @param p1
742
         * @param p2
743
         * @param p3
744
         * @return
745
         * The Circle
746
         */
747
        protected Circle createCircle(Point p1, Point p2, Point p3){
748
                Circle circle = null;
749
                try {
750
                        circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
751
                        circle.setPoints(p1, p2, p3);
752
                } catch (InstantiationException e) {
753
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
754
                } catch (IllegalAccessException e) {
755
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
756
                }
757
                return circle;
758
        }
759 27269 jmvivo
760 26921 jpiera
        /**
761
         * Create a circle from a GeneralPath. If there is an
762
         * error return <code>null</code> and add the error
763
         * to the log
764
         * @param p1
765
         * @param p2
766
         * @return
767
         * The circle
768
         */
769
        protected Circle createCircle(Point2D p1, Point2D p2){
770
                return createCircle(createPoint(p1), createPoint(p2));
771
        }
772 27269 jmvivo
773 26921 jpiera
        /**
774
         * Create a circle. If there is an
775
         * error return <code>null</code> and add the error
776
         * to the log
777
         * @param p1
778
         * @param p2
779
         * @return
780
         * The circle
781
         */
782
        protected Circle createCircle(Point p1, Point p2){
783
                Circle circle = null;
784
                try {
785
                        circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
786
                        circle.setPoints(p1, p2);
787
                } catch (InstantiationException e) {
788
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
789
                } catch (IllegalAccessException e) {
790
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
791
                }
792
                return circle;
793
        }
794 27269 jmvivo
795 26921 jpiera
        /**
796
         * Create a circle. If there is an
797
         * error return <code>null</code> and add the error
798
         * to the log
799
         * @param p1
800
         * @param radious
801
         * @return
802
         * The Circle
803
         */
804
        protected Circle createCircle(Point2D p1, double radious){
805
                return createCircle(createPoint(p1), radious);
806
        }
807 27269 jmvivo
808 26921 jpiera
        /**
809
         * Create a circle. If there is an
810
         * error return <code>null</code> and add the error
811
         * to the log
812
         * @param p1
813
         * @param radious
814
         * @return
815
         * The Circle
816
         */
817
        protected Circle createCircle(Point p1, double radious){
818
                Circle circle = null;
819
                try {
820
                        circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
821
                        circle.setPoints(p1, radious);
822
                } catch (InstantiationException e) {
823
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
824
                } catch (IllegalAccessException e) {
825
                        logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
826
                }
827
                return circle;
828
        }
829 27269 jmvivo
830 26921 jpiera
        /**
831
         * Create an Ellipse. If there is an
832
         * error return <code>null</code> and add the error
833
         * to the log
834
         * @param p1
835
         * @param p2
836
         * @param d
837
         * @return
838
         * The Ellipse
839
         */
840
        protected Ellipse createEllipse(Point2D p1, Point2D p2, double d){
841
                return createEllipse(createPoint(p1), createPoint(p2), d);
842
        }
843 27269 jmvivo
844 26921 jpiera
        /**
845
         * Create an Ellipse. If there is an
846
         * error return <code>null</code> and add the error
847
         * to the log
848
         * @param p1
849
         * @param p2
850
         * @param d
851
         * @return
852
         * The Ellipse
853
         */
854
        protected Ellipse createEllipse(Point p1, Point p2, double d){
855
                Ellipse ellipse = null;
856
                try {
857
                        ellipse = (Ellipse)geomManager.create(TYPES.ELLIPSE, SUBTYPES.GEOM2D);
858
                        ellipse.setPoints(p1, p2, d);
859
                } catch (InstantiationException e) {
860
                        logger.error(new CreateGeometryException(TYPES.ELLIPSE, SUBTYPES.GEOM2D, e));
861
                } catch (IllegalAccessException e) {
862
                        logger.error(new CreateGeometryException(TYPES.ELLIPSE, SUBTYPES.GEOM2D, e));
863
                }
864
                return ellipse;
865
        }
866 27269 jmvivo
867 26921 jpiera
        /**
868
         * Create a Spline from a GeneralPath. If there is an
869
         * error return <code>null</code> and add the error
870
         * to the log
871
         * @param points
872
         * @return
873
         * The Spline
874
         */
875
        protected Spline createSpline(Point2D[] points){
876
                Spline spline = null;
877
                try {
878
                        spline = (Spline)geomManager.create(TYPES.SPLINE, SUBTYPES.GEOM2D);
879
                        for (int i=0 ; i<points.length ; i++){
880
                                spline.addVertex(createPoint(points[i]));
881 27269 jmvivo
                        }
882 26921 jpiera
                } catch (InstantiationException e) {
883
                        logger.error(new CreateGeometryException(TYPES.SPLINE, SUBTYPES.GEOM2D, e));
884
                } catch (IllegalAccessException e) {
885
                        logger.error(new CreateGeometryException(TYPES.SPLINE, SUBTYPES.GEOM2D, e));
886
                }
887
                return spline;
888
        }
889 27269 jmvivo
890 27024 jpiera
        /**
891
         * Create a MultiPrimitive. If there is an
892
         * error return <code>null</code> and add the error
893
         * to the log
894
         * @param geometries
895
         * @return
896
         */
897
        protected MultiPrimitive createMultiPrimitive(Geometry[] geometries){
898
                MultiPrimitive multiPrimitive = null;
899
                try {
900
                        multiPrimitive = (MultiPrimitive)geomManager.create(TYPES.AGGREGATE, SUBTYPES.GEOM2D);
901
                        for (int i=0 ; i<geometries.length ; i++){
902
                                multiPrimitive.addPrimitive((Primitive)geometries[i]);
903 27269 jmvivo
                        }
904 27024 jpiera
                } catch (InstantiationException e) {
905
                        logger.error(new CreateGeometryException(TYPES.SPLINE, SUBTYPES.GEOM2D, e));
906
                } catch (IllegalAccessException e) {
907
                        logger.error(new CreateGeometryException(TYPES.SPLINE, SUBTYPES.GEOM2D, e));
908
                }
909
                return multiPrimitive;
910
        }
911 3831 caballero
}