Statistics
| Revision:

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

History | View | Annotate | Download (17.3 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.CommadException;
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 (((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
                                vea.endComplexRow();
156
                        } catch (IOException e) {
157
                                e.printStackTrace();
158
                        } catch (DriverIOException e) {
159
                                e.printStackTrace();
160
                        }
161
                }else if (status.equals("Selection.NextPointPolygon")) {
162
                        pointsPolygon.add(new Point2D.Double(x,y));
163
                }
164
        }
165

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

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

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

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

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

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

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

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

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

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

    
391

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

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

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

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

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

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

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

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

    
451
                int numHandlesSelected = selectedHandler.size();
452

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

    
457
                return numHandlesSelected;
458
        }
459

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

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

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

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

    
529
                        _fsm.addOption(s);
530

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

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

    
550
}