Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ComplexSelectionCADTool.java @ 24363

History | View | Annotate | Download (22.7 KB)

1
/* 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.tools;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.Image;
47
import java.awt.event.InputEvent;
48
import java.awt.event.MouseEvent;
49
import java.awt.geom.Point2D;
50
import java.util.ArrayList;
51
import java.util.Iterator;
52
import java.util.List;
53

    
54
import org.gvsig.fmap.data.exceptions.DataException;
55
import org.gvsig.fmap.data.exceptions.InitializeException;
56
import org.gvsig.fmap.data.exceptions.ReadException;
57
import org.gvsig.fmap.data.feature.EditableFeature;
58
import org.gvsig.fmap.data.feature.Feature;
59
import org.gvsig.fmap.data.feature.FeatureSelection;
60
import org.gvsig.fmap.data.feature.FeatureStore;
61
import org.gvsig.fmap.data.feature.FeatureType;
62
import org.gvsig.fmap.geom.Geometry;
63
import org.gvsig.fmap.geom.handler.Handler;
64
import org.gvsig.fmap.geom.operation.Draw;
65
import org.gvsig.fmap.geom.operation.DrawOperationContext;
66
import org.gvsig.fmap.geom.operation.GeometryOperationException;
67
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
68
import org.gvsig.fmap.geom.primitive.GeneralPathX;
69
import org.gvsig.fmap.mapcontext.ViewPort;
70
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
71
import org.gvsig.fmap.mapcontrol.MapControl;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.messages.NotificationManager;
75
import com.iver.cit.gvsig.CADExtension;
76
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
77
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
78
import com.iver.cit.gvsig.gui.cad.tools.smc.ComplexSelectionCADToolContext;
79
import com.iver.cit.gvsig.gui.cad.tools.smc.ComplexSelectionCADToolContext.ComplexSelectionCADToolState;
80
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
81
import com.iver.cit.gvsig.project.documents.table.gui.Table;
82

    
83
/**
84
 * DOCUMENT ME!
85
 *
86
 * @author Vicente Caballero Navarro
87
 */
88
public class ComplexSelectionCADTool extends SelectionCADTool {
89
        private ComplexSelectionCADToolContext _fsm;
90
        private List pointsPolygon = new ArrayList();
91

    
92
        /**
93
         * Crea un nuevo ComplexSelectionCADTool.
94
         */
95
        public ComplexSelectionCADTool() {
96
                type = PluginServices.getText(this, "inside_circle");
97
        }
98

    
99
        /**
100
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
101
         * carga previa a la utilizaci?n de la herramienta.
102
         */
103
        public void init() {
104
                _fsm = new ComplexSelectionCADToolContext(this);
105
                setNextTool("complex_selection");
106

    
107
                setType(PluginServices.getText(this, "inside_circle"));
108
        }
109

    
110
        /**
111
         * Equivale al transition del prototipo pero sin pasarle como par? metro el
112
         * editableFeatureSource que ya estar? creado.
113
         *
114
         * @param selection
115
         *            Bitset con las geometr?as que est?n seleccionadas.
116
         * @param x
117
         *            par?metro x del punto que se pase en esta transici?n.
118
         * @param y
119
         *            par?metro y del punto que se pase en esta transici?n.
120
         */
121
        public void addPoint(double x, double y, InputEvent event) {
122
                if (event != null && ((MouseEvent) event).getClickCount() == 2) {
123
                        try {
124
                                pointDoubleClick(((MapControl) event.getComponent())
125
                                                .getMapContext());
126
                        } catch (ReadException e) {
127
                                NotificationManager.addError(e.getMessage(), e);
128
                        }
129
                        return;
130
                }
131
                ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
132
                                .getPreviousState();
133
                String status = actualState.getName();
134
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
135
                // _fsm.getState());
136
                VectorialLayerEdited vle = getVLE();
137
                FeatureStore featureStore = null;
138
                try {
139
                        featureStore = vle.getFeatureStore();
140
                } catch (ReadException e) {
141
                        // TODO Auto-generated catch block
142
                        e.printStackTrace();
143
                }
144
                ArrayList selectedHandler = vle.getSelectedHandler();
145
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
146
                if (status.equals("Selection.FirstPoint")) {
147
                        firstPoint = new Point2D.Double(x, y);
148
                        pointsPolygon.add(firstPoint);
149
                } else if (status.equals("Selection.SecondPoint")) {
150
                } else if (status.equals("Selection.WithFeatures")) {
151
                } else if (status.equals("Selection.WithHandlers")) {
152
                        String description = PluginServices.getText(this, "move_handlers");
153
                        try {
154
                                featureStore.beginEditingGroup(description);
155
                                ArrayList selectedRowsAux = new ArrayList();
156
                                Iterator iterator;
157
                                iterator = ((FeatureSelection) featureStore.getSelection())
158
                                                .iterator();
159

    
160
                                while (iterator.hasNext()) {
161
                                        Feature feature = (Feature) iterator.next();
162

    
163
                                        // }
164
                                        Geometry ig = ((Geometry) feature.getDefaultGeometry())
165
                                                        .cloneGeometry();
166
                                        // Movemos los handlers que hemos seleccionado
167
                                        // previamente dentro del m?todo select()
168
                                        Handler[] handlers = ig.getHandlers(Geometry.SELECTHANDLER);
169
                                        for (int k = 0; k < selectedHandler.size(); k++) {
170
                                                Handler h = (Handler) selectedHandler.get(k);
171
                                                for (int j = 0; j < handlers.length; j++) {
172
                                                        if (h.getPoint().equals(handlers[j].getPoint()))
173
                                                                handlers[j].set(x, y);
174
                                                }
175
                                        }
176
                                        EditableFeature eFeature = feature.getEditable();
177
                                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
178
                                                        .getDefaultGeometryAttributeName(), ig);
179
                                        featureStore.update(eFeature);
180
                                        selectedRowsAux.add(eFeature);
181
                                }
182

    
183
                                firstPoint = new Point2D.Double(x, y);
184
                                // vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS,
185
                                // selectedRowsAux);
186

    
187
                                featureStore.endEditingGroup();
188
                        } catch (DataException e1) {
189
                                // TODO Auto-generated catch block
190
                                e1.printStackTrace();
191
                        }
192
                } else if (status.equals("Selection.NextPointPolygon")) {
193
                        pointsPolygon.add(new Point2D.Double(x, y));
194
                }
195
        }
196

    
197
        /**
198
         * Receives second point
199
         *
200
         * @param x
201
         * @param y
202
         * @return numFeatures selected
203
         */
204
        public long selectWithSecondPointOutRectangle(double x, double y,
205
                        InputEvent event) {
206
                Point2D lastPoint = new Point2D.Double(x, y);
207
                GeneralPathX gpx = new GeneralPathX();
208
                gpx.moveTo(firstPoint.getX(), firstPoint.getY());
209
                gpx.lineTo(lastPoint.getX(), firstPoint.getY());
210
                gpx.lineTo(lastPoint.getX(), lastPoint.getY());
211
                gpx.lineTo(firstPoint.getX(), lastPoint.getY());
212
                gpx.closePath();
213
                Geometry rectangle = geomFactory.createPolygon2D(gpx);
214
                return selectWithPolygon(rectangle);
215
        }
216

    
217
        /**
218
         * Receives second point
219
         *
220
         * @param x
221
         * @param y
222
         * @return numFeatures selected
223
         */
224
        public long selectWithCircle(double x, double y, InputEvent event) {
225
                Geometry circle = geomFactory.createCircle(firstPoint,
226
                                new Point2D.Double(x, y));
227
                return selectWithPolygon(circle);
228
        }
229

    
230
        public long selectWithPolygon(Geometry polygon) {
231
                VectorialLayerEdited vle = getVLE();
232
                PluginServices.getMDIManager().setWaitCursor();
233

    
234
                if (getType().equals(PluginServices.getText(this, "inside_circle"))
235
                                || getType().equals(
236
                                                PluginServices.getText(this, "inside_polygon"))) {
237
                        vle.selectInsidePolygon(polygon);
238
                } else if (getType().equals(
239
                                PluginServices.getText(this, "cross_circle"))
240
                                || getType().equals(
241
                                                PluginServices.getText(this, "cross_polygon"))) {
242
                        vle.selectCrossPolygon(polygon);
243
                } else if (getType().equals(PluginServices.getText(this, "out_circle"))
244
                                || getType()
245
                                                .equals(PluginServices.getText(this, "out_polygon"))
246
                                || getType().equals(
247
                                                PluginServices.getText(this, "out_rectangle"))) {
248
                        vle.selectOutPolygon(polygon);
249
                }
250
                long countSelection = 0;
251
                try {
252
                        countSelection = ((FeatureSelection) vle.getFeatureStore()
253
                                        .getSelection()).getSize();
254
                } catch (ReadException e) {
255
                        // TODO Auto-generated catch block
256
                        e.printStackTrace();
257
                } catch (DataException e) {
258
                        // TODO Auto-generated catch block
259
                        e.printStackTrace();
260
                }
261
                PluginServices.getMDIManager().restoreCursor();
262
                if (countSelection > 0) {
263
                        nextState = "Selection.WithSelectedFeatures";
264
                        end();
265
                } else
266
                        nextState = "Selection.FirstPoint";
267
                return countSelection;
268
        }
269

    
270
        /**
271
         * M?todo para dibujar la lo necesario para el estado en el que nos
272
         * encontremos.
273
         *
274
         * @param g
275
         *            Graphics sobre el que dibujar.
276
         * @param selectedGeometries
277
         *            BitSet con las geometr?as seleccionadas.
278
         * @param x
279
         *            par?metro x del punto que se pase para dibujar.
280
         * @param y
281
         *            par?metro x del punto que se pase para dibujar.
282
         */
283
        public void drawOperation(Graphics g, double x, double y) {
284
                ComplexSelectionCADToolState actualState = _fsm.getState();
285
                String status = actualState.getName();
286
                VectorialLayerEdited vle = getVLE();
287
                ArrayList selectedHandler = vle.getSelectedHandler();
288
                ViewPort vp = vle.getLayer().getMapContext().getViewPort();
289
                if (status.equals("Selection.SecondPoint")
290
                                || status.equals("Selection.SecondPointOutRectangle")) {
291
                        // Dibuja el rect?ngulo de selecci?n
292
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
293
                                        4);
294
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
295
                        elShape.lineTo(x, firstPoint.getY());
296
                        elShape.lineTo(x, y);
297
                        elShape.lineTo(firstPoint.getX(), y);
298
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
299

    
300
                        DrawOperationContext doc = new DrawOperationContext();
301
                        doc.setGraphics((Graphics2D) g);
302
                        doc.setViewPort(vp);
303
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
304
                        try {
305
                                geomFactory.createPolyline2D(elShape).invokeOperation(
306
                                                Draw.CODE, doc);
307
                        } catch (GeometryOperationNotSupportedException e) {
308
                                e.printStackTrace();
309
                        } catch (GeometryOperationException e) {
310
                                e.printStackTrace();
311
                        }
312
                        Image img = vle.getSelectionImage();
313
                        g.drawImage(img, 0, 0, null);
314
                        return;
315
                }
316
                if (status.equals("Selection.SecondPointCircle")) {
317
                        Geometry circle = geomFactory.createCircle(firstPoint,
318
                                        new Point2D.Double(x, y));
319
                        GeneralPathX gpx = new GeneralPathX();
320
                        gpx.append(circle.getInternalShape(), true);
321
                        Geometry circleSel = geomFactory.createPolyline2D(gpx);
322
                        // Dibuja el c?rculo de selecci?n
323
                        DrawOperationContext doc = new DrawOperationContext();
324
                        doc.setGraphics((Graphics2D) g);
325
                        doc.setViewPort(vp);
326
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
327
                        try {
328
                                circleSel.invokeOperation(Draw.CODE, doc);
329
                        } catch (GeometryOperationNotSupportedException e) {
330
                                e.printStackTrace();
331
                        } catch (GeometryOperationException e) {
332
                                e.printStackTrace();
333
                        }
334
                        Image img = vle.getSelectionImage();
335
                        g.drawImage(img, 0, 0, null);
336
                        return;
337
                } else if (status.equals("Selection.NextPointPolygon")) {
338
                        // Dibuja el pol?gono de selecci?n
339
                        Geometry polygon = getGeometryPolygon(new Point2D.Double(x, y));
340
                        DrawOperationContext doc = new DrawOperationContext();
341
                        doc.setGraphics((Graphics2D) g);
342
                        doc.setViewPort(vp);
343
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
344
                        try {
345
                                polygon.invokeOperation(Draw.CODE, doc);
346
                        } catch (GeometryOperationNotSupportedException e) {
347
                                e.printStackTrace();
348
                        } catch (GeometryOperationException e) {
349
                                e.printStackTrace();
350
                        }
351
                        Image img = vle.getSelectionImage();
352
                        g.drawImage(img, 0, 0, null);
353
                        return;
354
                } else if (status.equals("Selection.WithHandlers")) {
355
                        // Movemos los handlers que hemos seleccionado
356
                        // previamente dentro del m?todo select()
357
                        double xPrev = 0;
358
                        double yPrev = 0;
359
                        for (int k = 0; k < selectedHandler.size(); k++) {
360
                                Handler h = (Handler) selectedHandler.get(k);
361
                                xPrev = h.getPoint().getX();
362
                                yPrev = h.getPoint().getY();
363
                                h.set(x, y);
364
                        }
365
                        // Y una vez movidos los v?rtices (handles)
366
                        // redibujamos la nueva geometr?a.
367
                        for (int i = 0; i < rowselectedHandlers.size(); i++) {
368
                                Feature rowEd = (Feature) rowselectedHandlers.get(i);
369
                                Geometry geom = ((Geometry) rowEd.getDefaultGeometry())
370
                                                .cloneGeometry();
371
                                g.setColor(Color.gray);
372
                                DrawOperationContext doc = new DrawOperationContext();
373
                                doc.setGraphics((Graphics2D) g);
374
                                doc.setViewPort(vp);
375
                                doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
376
                                try {
377
                                        geom.invokeOperation(Draw.CODE, doc);
378
                                } catch (GeometryOperationNotSupportedException e) {
379
                                        e.printStackTrace();
380
                                } catch (GeometryOperationException e) {
381
                                        e.printStackTrace();
382
                                }
383
                        }
384
                        for (int k = 0; k < selectedHandler.size(); k++) {
385
                                Handler h = (Handler) selectedHandler.get(k);
386
                                h.set(xPrev, yPrev);
387
                        }
388
                        return;
389
                } else {
390
                        if (!vle.getLayer().isVisible())
391
                                return;
392
                        try {
393
                                Image imgSel = vle.getSelectionImage();
394
                                if (imgSel != null)
395
                                        g.drawImage(imgSel, 0, 0, null);
396
                                Image imgHand = vle.getHandlersImage();
397
                                if (imgHand != null)
398
                                        g.drawImage(imgHand, 0, 0, null);
399
                        } catch (Exception e) {
400
                        }
401
                }
402
        }
403

    
404
        /**
405
         * Add a diferent option.
406
         *
407
         * @param sel
408
         *            DOCUMENT ME!
409
         * @param s
410
         *            Diferent option.
411
         */
412
        public void addOption(String s) {
413
                ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
414
                                .getPreviousState();
415
                String status = actualState.getName();
416
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
417
                // _fsm.getState());
418
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
419
                if (s.equals(PluginServices.getText(this, "cancel"))) {
420
                        init();
421
                        return;
422
                } else if (s.equals(PluginServices.getText(this, "select_all"))) {
423
                        selectAll();
424
                        init();
425
                        return;
426
                }
427
                if (status.equals("Selection.FirstPoint")) {
428
                        setType(s);
429
                        return;
430
                } else if (status.equals("Selection.NextPointPolygon")) {
431
                        if (s.equals(PluginServices.getText(this, "end_polygon"))
432
                                        || s.equalsIgnoreCase(PluginServices.getText(this,
433
                                                        "ComplexSelectionCADTool.end"))) {
434
                                Geometry polygon = getGeometryPolygon(null);
435
                                GeneralPathX gpx = new GeneralPathX();
436
                                gpx.append(polygon, true);
437
                                if (gpx.isCCW()) {
438
                                        gpx.flip();
439
                                        polygon = geomFactory.createPolygon2D(gpx);
440
                                }
441
                                selectWithPolygon(polygon);
442
                                pointsPolygon.clear();
443
                                setType(PluginServices.getText(this, "inside_circle"));
444
                                return;
445
                        }
446
                }
447
                init();
448
        }
449

    
450
        private long selectAll() {
451
                VectorialLayerEdited vle = getVLE();
452
                PluginServices.getMDIManager().setWaitCursor();
453
                vle.selectAll();
454
                long countSelection = 0;
455
                try {
456
                        countSelection = ((FeatureSelection) vle.getFeatureStore()
457
                                        .getSelection()).getSize();
458
                } catch (ReadException e) {
459
                        // TODO Auto-generated catch block
460
                        e.printStackTrace();
461
                } catch (DataException e) {
462
                        // TODO Auto-generated catch block
463
                        e.printStackTrace();
464
                }
465
                PluginServices.getMDIManager().restoreCursor();
466
                if (countSelection > 0) {
467
                        nextState = "Selection.WithSelectedFeatures";
468
                } else
469
                        nextState = "Selection.FirstPoint";
470
                end();
471
                return countSelection;
472
        }
473

    
474
        private Geometry getGeometryPolygon(Point2D p) {
475
                Point2D[] points = (Point2D[]) pointsPolygon.toArray(new Point2D[0]);
476
                GeneralPathX gpx = new GeneralPathX();
477
                for (int i = 0; i < points.length; i++) {
478
                        if (i == 0) {
479
                                gpx.moveTo(points[i].getX(), points[i].getY());
480
                        } else {
481
                                gpx.lineTo(points[i].getX(), points[i].getY());
482
                        }
483
                }
484
                if (p != null) {
485
                        gpx.lineTo(p.getX(), p.getY());
486
                        gpx.closePath();
487
                        Geometry polyline = geomFactory.createPolyline2D(gpx);
488
                        return polyline;
489
                }
490
                gpx.closePath();
491
                Geometry polygon = geomFactory.createPolygon2D(gpx);
492
                return polygon;
493
        }
494

    
495
        /*
496
         * (non-Javadoc)
497
         *
498
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
499
         */
500
        public void addValue(double d) {
501
        }
502

    
503
        public void end() {
504
                if (!getNextTool().equals("complex_selection"))
505
                        CADExtension.setCADTool(getNextTool(), false);
506
        }
507

    
508
        public String getName() {
509
                return PluginServices.getText(this, "complex_selection_");
510
        }
511

    
512
        public boolean selectFeatures(double x, double y, InputEvent event) {
513
                ComplexSelectionCADToolState actualState = _fsm.getState();
514

    
515
                String status = actualState.getName();
516
                VectorialLayerEdited vle = getVLE();
517

    
518
                if ((status.equals("Selection.FirstPoint"))
519
                                || (status.equals("Selection.WithSelectedFeatures"))) {
520
                        PluginServices.getMDIManager().setWaitCursor();
521
                        firstPoint = new Point2D.Double(x, y);
522
                        vle.selectWithPoint(x, y, multipleSelection);
523
                        PluginServices.getMDIManager().restoreCursor();
524
                }
525
                long countSelection = 0;
526
                try {
527
                        countSelection = ((FeatureSelection) vle.getFeatureStore()
528
                                        .getSelection()).getSize();
529
                } catch (ReadException e) {
530
                        // TODO Auto-generated catch block
531
                        e.printStackTrace();
532
                } catch (DataException e) {
533
                        // TODO Auto-generated catch block
534
                        e.printStackTrace();
535
                }
536
                if (countSelection > 0) {
537
                        nextState = "Selection.WithSelectedFeatures";
538
                        return true;
539
                } else {
540
                        {
541
                                nextState = "Selection.SecondPoint";
542
                                return true;
543
                        }
544
                }
545
        }
546

    
547
        public int selectHandlers(double x, double y, InputEvent event) {
548
                Point2D auxPoint = new Point2D.Double(x, y);
549

    
550
                VectorialLayerEdited vle = getVLE();
551
                ArrayList selectedHandler = vle.getSelectedHandler();
552
                selectedHandler.clear();
553

    
554
                // Se comprueba si se pincha en una gemometr?a
555
                PluginServices.getMDIManager().setWaitCursor();
556

    
557
                double tam = getCadToolAdapter().getMapControl().getViewPort()
558
                                .toMapDistance(MapControl.tolerance);
559

    
560
                Handler[] handlers = null;
561
                rowselectedHandlers.clear();
562
                FeatureStore featureStore = null;
563
                try {
564
                        featureStore = vle.getFeatureStore();
565
                } catch (ReadException e) {
566
                        // TODO Auto-generated catch block
567
                        e.printStackTrace();
568
                }
569
                Iterator iterator = null;
570
                try {
571
                        iterator = ((FeatureSelection) featureStore.getSelection())
572
                                        .iterator();
573
                } catch (DataException e1) {
574
                        // TODO Auto-generated catch block
575
                        e1.printStackTrace();
576
                }
577
                while (iterator.hasNext()) {
578
                        Feature feature = (Feature) iterator.next();
579

    
580
                        Geometry geom = ((Geometry) feature.getDefaultGeometry())
581
                                        .cloneGeometry();
582
                        handlers = geom.getHandlers(Geometry.SELECTHANDLER);
583
                        // y miramos los handlers de cada entidad seleccionada
584
                        double min = tam;
585

    
586
                        for (int j = 0; j < handlers.length; j++) {
587
                                Point2D handlerPoint = handlers[j].getPoint();
588
                                double distance = auxPoint.distance(handlerPoint);
589
                                if (distance <= min) {
590
                                        min = distance;
591
                                        selectedHandler.add(handlers[j]);
592
                                        EditableFeature eFeature;
593
                                        try {
594
                                                eFeature = featureStore.createNewFeature(false);
595
                                                FeatureType featureType = featureStore
596
                                                                .getDefaultFeatureType();
597
                                                for (int i = 0; i < featureType.size(); i++) {
598
                                                        eFeature.set(i, feature.get(i));
599
                                                }
600
                                                eFeature.setGeometry(featureType
601
                                                                .getDefaultGeometryAttributeName(), geom);
602
                                        } catch (InitializeException e) {
603
                                                // TODO Auto-generated catch block
604
                                                e.printStackTrace();
605
                                        } catch (DataException e) {
606
                                                // TODO Auto-generated catch block
607
                                                e.printStackTrace();
608
                                        }
609
                                        rowselectedHandlers.add(feature);
610
                                }
611
                        }
612
                }
613
                PluginServices.getMDIManager().restoreCursor();
614

    
615
                int numHandlesSelected = selectedHandler.size();
616

    
617
                /*
618
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
619
                 */
620

    
621
                return numHandlesSelected;
622
        }
623

    
624
        public String getType() {
625
                return type;
626
        }
627

    
628
        public void setType(String type) {
629
                if (type.equalsIgnoreCase(PluginServices.getText(this,
630
                                "ComplexSelectionCADTool.outrectangle"))) {
631
                        this.type = PluginServices.getText(this, "out_rectangle");
632
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
633
                                "ComplexSelectionCADTool.intropolygon"))) {
634
                        this.type = PluginServices.getText(this, "inside_polygon");
635
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
636
                                "ComplexSelectionCADTool.crosspolygon"))) {
637
                        this.type = PluginServices.getText(this, "cross_polygon");
638
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
639
                                "ComplexSelectionCADTool.outpolygon"))) {
640
                        this.type = PluginServices.getText(this, "out_polygon");
641
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
642
                                "ComplexSelectionCADTool.introcircle"))) {
643
                        this.type = PluginServices.getText(this, "inside_circle");
644
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
645
                                "ComplexSelectionCADTool.crosscircle"))) {
646
                        this.type = PluginServices.getText(this, "cross_circle");
647
                } else if (type.equalsIgnoreCase(PluginServices.getText(this,
648
                                "ComplexSelectionCADTool.outcircle"))) {
649
                        this.type = PluginServices.getText(this, "out_circle");
650
                } else if (type.equals(PluginServices.getText(this, "select_all"))) {
651
                        selectAll();
652
                        init();
653
                } else {
654
                        this.type = type;
655
                }
656
                pointsPolygon.clear();
657
        }
658

    
659
        /*
660
         * (non-Javadoc)
661
         *
662
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
663
         *      double, double)
664
         */
665
        public void transition(double x, double y, InputEvent event) {
666
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState()
667
                                + " x= " + x + " y=" + y);
668
                try {
669
                        _fsm.addPoint(x, y, event);
670
                } catch (Exception e) {
671
                        init();
672
                }
673
                System.out.println("ESTADO ACTUAL: " + getStatus());
674

    
675
                FLyrVect lv = (FLyrVect) ((VectorialLayerEdited) CADExtension
676
                                .getEditionManager().getActiveLayerEdited()).getLayer();
677
                com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
678
                                .getMDIManager().getAllWindows();
679

    
680
                for (int i = 0; i < views.length; i++) {
681
                        if (views[i] instanceof Table) {
682
                                Table table = (Table) views[i];
683
                                if (table.getModel().getAssociatedTable() != null
684
                                                && table.getModel().getAssociatedTable().equals(lv))
685
                                        table.updateSelection();
686
                        }
687
                }
688
        }
689

    
690
        public String getStatus() {
691
                try {
692
                        ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
693
                                        .getPreviousState();
694
                        String status = actualState.getName();
695

    
696
                        return status;
697
                } catch (NullPointerException e) {
698
                        return "Selection.FirstPoint";
699
                }
700
        }
701

    
702
        /*
703
         * (non-Javadoc)
704
         *
705
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
706
         *      java.lang.String)
707
         */
708
        public void transition(String s) throws CommandException {
709
                if (!super.changeCommand(s)) {
710

    
711
                        _fsm.addOption(s);
712

    
713
                }
714
        }
715

    
716
        /*
717
         * (non-Javadoc)
718
         *
719
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
720
         *      double)
721
         */
722
        public void transition(double d) {
723
                _fsm.addValue(d);
724
        }
725

    
726
        public String toString() {
727
                return "_complex_selection";
728
        }
729

    
730
        public String getNextState() {
731
                return nextState;
732
        }
733

    
734
}