Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ComplexSelectionCADTool.java @ 6071

History | View | Annotate | Download (17.4 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.io.IOException;
51
import java.util.ArrayList;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.cit.gvsig.CADExtension;
55
import com.iver.cit.gvsig.fmap.MapControl;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
58
import com.iver.cit.gvsig.fmap.core.Handler;
59
import com.iver.cit.gvsig.fmap.core.IFeature;
60
import com.iver.cit.gvsig.fmap.core.IGeometry;
61
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
62
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
63
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
65
import com.iver.cit.gvsig.gui.cad.CADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.ComplexSelectionCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.ComplexSelectionCADToolContext.ComplexSelectionCADToolState;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70

    
71
/**
72
 * DOCUMENT ME!
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class ComplexSelectionCADTool extends SelectionCADTool {
77
        //public final static int tolerance = 4;
78

    
79
        private ComplexSelectionCADToolContext _fsm;
80

    
81
        //private Point2D firstPoint;
82

    
83
        //private Point2D lastPoint;
84

    
85

    
86

    
87
        //private String nextState;
88
// Registros de los que se ha sleccionado alg?n handler.
89
        //private ArrayList rowselectedHandlers=new ArrayList();
90
        //private String type=PluginServices.getText(this,"inside_circle");
91
        //private ArrayList pointsPolygon=new ArrayList();
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
                        pointDoubleClick((MapControl)event.getComponent());
124
                        return;
125
                }
126
                ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
127
                                .getPreviousState();
128
                String status = actualState.getName();
129
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
130
                                                                                                                // _fsm.getState());
131
                VectorialLayerEdited vle = getVLE();
132
                VectorialEditableAdapter vea=vle.getVEA();
133
                ArrayList selectedHandler = vle.getSelectedHandler();
134
                ArrayList selectedRow = vle.getSelectedRow();
135
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
136
                if (status.equals("Selection.FirstPoint")) {
137
                        firstPoint=new Point2D.Double(x,y);
138
                        pointsPolygon.add(firstPoint);
139
                } else if (status.equals("Selection.SecondPoint")) {
140
                } else if (status.equals("Selection.WithFeatures")) {
141
                } else if (status.equals("Selection.WithHandlers")) {
142
                        vea.startComplexRow();
143
                        for (int i = 0; i < selectedRow.size(); i++) {
144
                                IRowEdited row = (IRowEdited) selectedRow.get(i);
145
                                // Movemos los handlers que hemos seleccionado
146
                                // previamente dentro del m?todo select()
147
                                for (int k = 0; k < selectedHandler.size(); k++) {
148
                                        Handler h = (Handler) selectedHandler.get(k);
149
                                        h.set(x, y);
150
                                }
151

    
152
                                modifyFeature(row.getIndex(), (IFeature) row.getLinkedRow().cloneRow());
153
                        }
154
                        try {
155
                                String description=PluginServices.getText(this,"move_handlers");
156
                                vea.endComplexRow(description);
157
                        } catch (IOException e) {
158
                                e.printStackTrace();
159
                        } catch (DriverIOException e) {
160
                                e.printStackTrace();
161
                        }
162
                }else if (status.equals("Selection.NextPointPolygon")) {
163
                        pointsPolygon.add(new Point2D.Double(x,y));
164
                }
165
        }
166

    
167
        /**
168
         * Receives second point
169
         * @param x
170
         * @param y
171
         * @return numFeatures selected
172
         */
173
        public int selectWithSecondPointOutRectangle(double x, double y, InputEvent event) {
174
                Point2D lastPoint=new Point2D.Double(x,y);
175
                GeneralPathX gpx=new GeneralPathX();
176
                gpx.moveTo(firstPoint.getX(),firstPoint.getY());
177
                gpx.lineTo(lastPoint.getX(),firstPoint.getY());
178
                gpx.lineTo(lastPoint.getX(),lastPoint.getY());
179
                gpx.lineTo(firstPoint.getX(),lastPoint.getY());
180
                gpx.closePath();
181
                IGeometry rectangle=ShapeFactory.createPolygon2D(gpx);
182
                return selectWithPolygon(rectangle);
183
        }
184
        /**
185
         * Receives second point
186
         * @param x
187
         * @param y
188
         * @return numFeatures selected
189
         */
190
        public int selectWithCircle(double x, double y, InputEvent event) {
191
                IGeometry circle=ShapeFactory.createCircle(firstPoint,new Point2D.Double(x,y));
192
                return selectWithPolygon(circle);
193
        }
194
        public int selectWithPolygon(IGeometry polygon) {
195
                VectorialLayerEdited vle = getVLE();
196
                PluginServices.getMDIManager().setWaitCursor();
197

    
198
                if (getType().equals(PluginServices.getText(this,"inside_circle")) || getType().equals(PluginServices.getText(this,"inside_polygon"))) {
199
                        vle.selectInsidePolygon(polygon);
200
                } else if (getType().equals(PluginServices.getText(this,"cross_circle")) || getType().equals(PluginServices.getText(this,"cross_polygon"))) {
201
                        vle.selectCrossPolygon(polygon);
202
                } else if (getType().equals(PluginServices.getText(this,"out_circle")) || getType().equals(PluginServices.getText(this,"out_polygon")) || getType().equals(PluginServices.getText(this,"out_rectangle"))) {
203
                        vle.selectOutPolygon(polygon);
204
                }
205
                ArrayList selectedRow = vle.getSelectedRow();
206
                PluginServices.getMDIManager().restoreCursor();
207
                if (selectedRow.size() > 0) {
208
                        nextState = "Selection.WithSelectedFeatures";
209
                        end();
210
                } else
211
                        nextState = "Selection.FirstPoint";
212
                return selectedRow.size();
213
        }
214

    
215
        /**
216
         * M?todo para dibujar la lo necesario para el estado en el que nos
217
         * encontremos.
218
         *
219
         * @param g
220
         *            Graphics sobre el que dibujar.
221
         * @param selectedGeometries
222
         *            BitSet con las geometr?as seleccionadas.
223
         * @param x
224
         *            par?metro x del punto que se pase para dibujar.
225
         * @param y
226
         *            par?metro x del punto que se pase para dibujar.
227
         */
228
        public void drawOperation(Graphics g, double x, double y) {
229
                ComplexSelectionCADToolState actualState = _fsm.getState();
230
                String status = actualState.getName();
231
                VectorialLayerEdited vle = getVLE();
232
                ArrayList selectedHandler = vle.getSelectedHandler();
233
                ViewPort vp=vle.getLayer().getFMap().getViewPort();
234
                if (status.equals("Selection.SecondPoint") || status.equals("Selection.SecondPointOutRectangle")) {
235
                        // Dibuja el rect?ngulo de selecci?n
236
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
237
                                        4);
238
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
239
                        elShape.lineTo(x, firstPoint.getY());
240
                        elShape.lineTo(x, y);
241
                        elShape.lineTo(firstPoint.getX(), y);
242
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
243
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
244
                                        vp,
245
                                        CADTool.selectSymbol);
246
                        Image img = vle.getSelectionImage();
247
                g.drawImage(img, 0, 0, null);
248
                return;
249
                }if (status.equals("Selection.SecondPointCircle")) {
250
                        // Dibuja el c?rculo de selecci?n
251
                        ShapeFactory.createCircle(firstPoint,new Point2D.Double(x,y)).draw((Graphics2D) g,
252
                                        vp,
253
                                        CADTool.selectSymbol);
254
                        Image img = vle.getSelectionImage();
255
                g.drawImage(img, 0, 0, null);
256
                return;
257
                }else if (status.equals("Selection.NextPointPolygon")) {
258
                        // Dibuja el pol?gono de selecci?n
259
                        IGeometry polygon=getGeometryPolygon(new Point2D.Double(x,y));
260
                        polygon.draw((Graphics2D) g,
261
                                        vp,
262
                                        CADTool.selectSymbol);
263
                        Image img = vle.getSelectionImage();
264
                g.drawImage(img, 0, 0, null);
265
                return;
266
                }else if (status.equals("Selection.WithHandlers")) {
267
                        // Movemos los handlers que hemos seleccionado
268
                        // previamente dentro del m?todo select()
269
                        for (int k = 0; k < selectedHandler.size(); k++) {
270
                                Handler h = (Handler) selectedHandler.get(k);
271
                                h.set(x, y);
272
                        }
273
                        // Y una vez movidos los v?rtices (handles)
274
                        // redibujamos la nueva geometr?a.
275
                        for (int i = 0; i < rowselectedHandlers.size(); i++) {
276
                                IRowEdited rowEd = (IRowEdited) rowselectedHandlers.get(i);
277
                                IGeometry geom = ((IFeature) rowEd.getLinkedRow())
278
                                                .getGeometry().cloneGeometry();
279
                                g.setColor(Color.gray);
280
                                geom.draw((Graphics2D) g, vp, CADTool.modifySymbol);
281
                        }
282
                        return;
283
                }else{
284
                        try{
285
                        Image imgSel = vle.getSelectionImage();
286
                if (imgSel!=null)
287
                        g.drawImage(imgSel, 0, 0, null);
288
                Image imgHand = vle.getHandlersImage();
289
                if (imgHand!=null)
290
                        g.drawImage(imgHand, 0, 0, null);
291
                        }catch (Exception e) {
292
                        }
293
                }
294
        }
295

    
296
        /**
297
         * Add a diferent option.
298
         *
299
         * @param sel
300
         *            DOCUMENT ME!
301
         * @param s
302
         *            Diferent option.
303
         */
304
        public void addOption(String s) {
305
                ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
306
                                .getPreviousState();
307
                String status = actualState.getName();
308
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
309
                // _fsm.getState());
310
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
311
                if (s.equals(PluginServices.getText(this,"cancel"))){
312
                        init();
313
                        return;
314
                }else if (s.equals(PluginServices.getText(this,"select_all"))){
315
                        selectAll();
316
                        init();
317
                        return;
318
                }
319
                if (status.equals("Selection.FirstPoint")) {
320
                        setType(s);
321
                        return;
322
                }else if (status.equals("Selection.NextPointPolygon")){
323
                        if (s.equals(PluginServices.getText(this,"end_polygon")) || s.equals("E") || s.equals("e")) {
324
                        IGeometry polygon=getGeometryPolygon(null);
325
                        selectWithPolygon(polygon);
326
                        pointsPolygon.clear();
327
                        setType(PluginServices.getText(this,"inside_circle"));
328
                        return;
329
                        }
330
                }
331
                init();
332
        }
333
        private int selectAll() {
334
                VectorialLayerEdited vle = getVLE();
335
                PluginServices.getMDIManager().setWaitCursor();
336
                vle.selectAll();
337
                ArrayList selectedRow = vle.getSelectedRow();
338
                PluginServices.getMDIManager().restoreCursor();
339
                if (selectedRow.size() > 0) {
340
                        nextState = "Selection.WithSelectedFeatures";
341
                } else
342
                        nextState = "Selection.FirstPoint";
343
                end();
344
                return selectedRow.size();
345
        }
346

    
347
        private IGeometry getGeometryPolygon(Point2D p) {
348
                Point2D[] points = (Point2D[]) pointsPolygon.toArray(new Point2D[0]);
349
                GeneralPathX gpx = new GeneralPathX();
350
                for (int i = 0; i < points.length; i++) {
351
                        if (i == 0) {
352
                                gpx.moveTo(points[i].getX(), points[i].getY());
353
                        } else {
354
                                gpx.lineTo(points[i].getX(), points[i].getY());
355
                        }
356
                }
357
                if (p!=null){
358
                        gpx.lineTo(p.getX(),p.getY());
359
                        gpx.closePath();
360
                        IGeometry polyline = ShapeFactory.createPolyline2D(gpx);
361
                        return polyline;
362
                }
363
                gpx.closePath();
364
                IGeometry polygon = ShapeFactory.createPolygon2D(gpx);
365
                return polygon;
366
        }
367

    
368
        /*
369
         * (non-Javadoc)
370
         *
371
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
372
         */
373
        public void addValue(double d) {
374
        }
375

    
376
        public void end() {
377
                if (!getNextTool().equals("complex_selection"))
378
                        CADExtension.setCADTool(getNextTool(),false);
379
        }
380

    
381
        public String getName() {
382
                return PluginServices.getText(this,"complex_selection_");
383
        }
384

    
385
        public boolean selectFeatures(double x, double y, InputEvent event) {
386
                ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
387
                                .getState();
388

    
389
                String status = actualState.getName();
390
                VectorialLayerEdited vle = getVLE();
391

    
392

    
393
                if ((status.equals("Selection.FirstPoint"))
394
                                || (status.equals("Selection.WithSelectedFeatures"))) {
395
                        PluginServices.getMDIManager().setWaitCursor();
396
                        firstPoint = new Point2D.Double(x, y);
397
                        vle.selectWithPoint(x,y,multipleSelection);
398
                        PluginServices.getMDIManager().restoreCursor();
399
                }
400
                ArrayList selectedRow = vle.getSelectedRow();
401
                if (selectedRow.size() > 0) {
402
                        nextState = "Selection.WithSelectedFeatures";
403
                        return true;
404
                } else {
405
                        {
406
                                nextState = "Selection.SecondPoint";
407
                                return true;
408
                        }
409
                }
410
        }
411

    
412
        public int selectHandlers(double x, double y, InputEvent event) {
413
                Point2D auxPoint = new Point2D.Double(x, y);
414

    
415
                VectorialLayerEdited vle = getVLE();
416
                ArrayList selectedHandler = vle.getSelectedHandler();
417
                ArrayList selectedRow = vle.getSelectedRow();
418
                System.out.println("DENTRO DE selectHandlers. selectedRow.size= "
419
                                + selectedRow.size());
420
                selectedHandler.clear();
421

    
422
                // Se comprueba si se pincha en una gemometr?a
423
                PluginServices.getMDIManager().setWaitCursor();
424

    
425
                double tam = getCadToolAdapter().getMapControl().getViewPort()
426
                                .toMapDistance(tolerance);
427

    
428
                Handler[] handlers = null;
429
                rowselectedHandlers.clear();
430
                for (int i = 0; i < selectedRow.size(); i++) {
431
                        IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
432

    
433
                        IFeature fea = (IFeature) rowEd.getLinkedRow();
434
                        handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
435
                        // y miramos los handlers de cada entidad seleccionada
436
                        double min = tam;
437
                        // int hSel = -1;
438

    
439
                        for (int j = 0; j < handlers.length; j++) {
440
                                Point2D handlerPoint = handlers[j].getPoint();
441
                                double distance = auxPoint.distance(handlerPoint);
442
                                if (distance <= min) {
443
                                        min = distance;
444
                                        // hSel = j;
445
                                        selectedHandler.add(handlers[j]);
446
                                        rowselectedHandlers.add(rowEd);
447
                                }
448
                        }
449
                }
450
                PluginServices.getMDIManager().restoreCursor();
451

    
452
                int numHandlesSelected = selectedHandler.size();
453

    
454
                /*
455
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
456
                 */
457

    
458
                return numHandlesSelected;
459
        }
460

    
461
        public String getType() {
462
                return type;
463
        }
464

    
465
        public void setType(String type) {
466
                if (type.equals("OR") || type.equals("or")){
467
                        this.type=PluginServices.getText(this,"out_rectangle");
468
                }else if (type.equals("IP") || type.equals("ip")){
469
                        this.type=PluginServices.getText(this,"inside_polygon");
470
                }else if (type.equals("CP") || type.equals("cp")){
471
                        this.type=PluginServices.getText(this,"cross_polygon");
472
                }else if (type.equals("OP") || type.equals("op")){
473
                        this.type=PluginServices.getText(this,"out_polygon");
474
                }else if (type.equals("IC") || type.equals("ic")){
475
                        this.type=PluginServices.getText(this,"inside_circle");
476
                }else if (type.equals("CC") || type.equals("cc")){
477
                        this.type=PluginServices.getText(this,"cross_circle");
478
                }else if (type.equals("OC") || type.equals("oc")){
479
                        this.type=PluginServices.getText(this,"cross_circle");
480
                }else if (type.equals(PluginServices.getText(this,"select_all"))){
481
                        selectAll();
482
                        init();
483
                }else{
484
                        this.type = type;
485
                }
486
                pointsPolygon.clear();
487
        }
488
        /*
489
         * (non-Javadoc)
490
         *
491
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
492
         *      double, double)
493
         */
494
        public void transition(double x, double y, InputEvent event) {
495
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState()
496
                                + " x= " + x + " y=" + y);
497
                try{
498
                _fsm.addPoint(x, y, event);
499
                }catch (Exception e) {
500
                        init();
501
                }
502
                System.out.println("ESTADO ACTUAL: " + getStatus());
503

    
504
                // ESTO LO QUITO POR AHORA, PERO PUEDE QUE LO NECESITEMOS VOLVER A PONER.
505
                // Lo he quitado porque cuando seleccionas algo con CAD, molesta que
506
                // te hagan un redibujado.
507
                /* FLyrVect lv=(FLyrVect)((VectorialLayerEdited)CADExtension.getEditionManager().getActiveLayerEdited()).getLayer();
508
                lv.getSource().getRecordset().getSelectionSupport().fireSelectionEvents(); */
509
        }
510
        public String getStatus() {
511
                try {
512
                        ComplexSelectionCADToolState actualState = (ComplexSelectionCADToolState) _fsm
513
                                        .getPreviousState();
514
                        String status = actualState.getName();
515

    
516
                        return status;
517
                } catch (NullPointerException e) {
518
                        return "Selection.FirstPoint";
519
                }
520
        }
521
        /*
522
         * (non-Javadoc)
523
         *
524
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
525
         *      java.lang.String)
526
         */
527
        public void transition(String s) throws CommandException {
528
                if (!super.changeCommand(s)){
529

    
530
                        _fsm.addOption(s);
531

    
532
            }
533
        }
534
        /*
535
         * (non-Javadoc)
536
         *
537
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
538
         *      double)
539
         */
540
        public void transition(double d) {
541
                _fsm.addValue(d);
542
        }
543

    
544
        public String toString() {
545
                return "_complex_selection";
546
        }
547
        public String getNextState() {
548
                return nextState;
549
        }
550

    
551
}