Statistics
| Revision:

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

History | View | Annotate | Download (13.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 com.iver.andami.PluginServices;
44

    
45
import com.iver.cit.gvsig.CADExtension;
46
import com.iver.cit.gvsig.fmap.DriverException;
47
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
48
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
49
import com.iver.cit.gvsig.fmap.core.Handler;
50
import com.iver.cit.gvsig.fmap.core.IFeature;
51
import com.iver.cit.gvsig.fmap.core.IGeometry;
52
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
53
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
54
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
55
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
56
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57
import com.iver.cit.gvsig.gui.cad.CADTool;
58
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
59
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext;
60
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
61
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
62

    
63
import java.awt.Color;
64
import java.awt.Graphics;
65
import java.awt.Graphics2D;
66
import java.awt.event.InputEvent;
67
import java.awt.geom.Point2D;
68
import java.awt.geom.Rectangle2D;
69
import java.io.IOException;
70

    
71
import java.util.ArrayList;
72

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

    
81
        private SelectionCADToolContext _fsm;
82

    
83
        private Point2D firstPoint;
84

    
85
        private Point2D lastPoint;
86

    
87
        private String tool = "selection";
88

    
89
        private String nextState;
90

    
91
        /**
92
         * Crea un nuevo LineCADTool.
93
         */
94
        public SelectionCADTool() {
95
        }
96

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

    
106
        /*
107
         * (non-Javadoc)
108
         *
109
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
110
         *      double, double)
111
         */
112
        public void transition(double x, double y, InputEvent event) {
113
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState() + " x= " + x
114
                                + " y=" + y);
115
                _fsm.addPoint(x, y, event);
116
                System.out.println("ESTADO ACTUAL: " + getStatus());
117
                
118
        }
119

    
120
        /*
121
         * (non-Javadoc)
122
         *
123
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
124
         *      double)
125
         */
126
        public void transition(double d) {
127
                // _fsm.addValue(sel,d);
128
        }
129

    
130
        /*
131
         * (non-Javadoc)
132
         *
133
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
134
         *      java.lang.String)
135
         */
136
        public void transition(String s) {
137
                _fsm.addOption(s);
138
        }
139

    
140
        public String getNextState() {
141
                return nextState;
142
        }
143

    
144

    
145
        /**
146
         * Equivale al transition del prototipo pero sin pasarle como par? metro el
147
         * editableFeatureSource que ya estar? creado.
148
         *
149
         * @param selection
150
         *            Bitset con las geometr?as que est?n seleccionadas.
151
         * @param x
152
         *            par?metro x del punto que se pase en esta transici?n.
153
         * @param y
154
         *            par?metro y del punto que se pase en esta transici?n.
155
         */
156
        public void addPoint(double x, double y, InputEvent event) {
157
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
158
                                .getPreviousState();
159
                String status = actualState.getName();
160
                System.out.println("PREVIOUSSTATE =" + status);
161
                /* FBitSet selection = getCadToolAdapter().getVectorialAdapter()
162
                                .getSelection(); */
163
                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
164
                                .getEditionManager().getActiveLayerEdited();
165
                ArrayList selectedHandler = vle.getSelectedHandler();
166
                ArrayList selectedRow = vle.getSelectedRow();
167
                // ArrayList selectedRowIndex = vle.getSelectedRowIndex();
168

    
169
                        if (status.equals("Selection.FirstPoint")) {
170
                        } else if (status.equals("Selection.SecondPoint")) {
171
                                lastPoint = new Point2D.Double(x, y);
172
                                // selection.clear();
173
                                selectedRow.clear();
174

    
175
                                double x1;
176
                                double y1;
177
                                double w1;
178
                                double h1;
179

    
180
                                if (firstPoint.getX() < lastPoint.getX()) {
181
                                        x1 = firstPoint.getX();
182
                                        w1 = lastPoint.getX() - firstPoint.getX();
183
                                } else {
184
                                        x1 = lastPoint.getX();
185
                                        w1 = firstPoint.getX() - lastPoint.getX();
186
                                }
187

    
188
                                if (firstPoint.getY() < lastPoint.getY()) {
189
                                        y1 = firstPoint.getY();
190
                                        h1 = lastPoint.getY() - firstPoint.getY();
191
                                } else {
192
                                        y1 = lastPoint.getY();
193
                                        h1 = firstPoint.getY() - lastPoint.getY();
194
                                }
195

    
196
                                Rectangle2D rect = new Rectangle2D.Double(x1, y1, w1, h1);
197

    
198
                                VectorialEditableAdapter vea = getCadToolAdapter()
199
                                                .getVectorialAdapter();
200
                                String strEPSG = getCadToolAdapter().getMapControl()
201
                                                .getViewPort().getProjection().getAbrev().substring(5);
202
                                IRowEdited[] feats;
203
                                try {
204
                                        feats = vea.getFeatures(rect, strEPSG);
205

    
206
                                        for (int i = 0; i < feats.length; i++) {
207
                                                IGeometry geom = ((IFeature) feats[i].getLinkedRow())
208
                                                                .getGeometry();
209
        
210
                                                if (firstPoint.getX() < lastPoint.getX()) {
211
                                                        if (rect.contains(geom.getBounds2D())) {
212
                                                                selectedRow.add(feats[i]);
213
                                                        }
214
                                                } else {
215
                                                        if (geom.intersects(rect)) { // , 0.1)){
216
                                                                selectedRow.add(feats[i]);
217
                                                        }
218
                                                }
219
                                        }
220
                                } catch (DriverException e) {
221
                                        // TODO Auto-generated catch block
222
                                        e.printStackTrace();
223
                                }
224

    
225
                                PluginServices.getMDIManager().restoreCursor();
226
                                if (selectedRow.size() > 0)
227
                                {
228
                                        nextState = "Selection.WithSelectedFeatures";
229
                                }
230
                                else 
231
                                        nextState = "Selection.FirstPoint";
232

    
233
                        } else if (status.equals("Selection.WithHandlers")) {
234
                                for (int i = 0; i < selectedRow.size(); i++) {
235
                                        IRowEdited row = (IRowEdited) selectedRow.get(i);
236
                                        // int index = ((Integer) selectedRowIndex.get(i)).intValue();
237

    
238
                                        // Movemos los handlers que hemos seleccionado
239
                                        // previamente dentro del m?todo select()
240
                                        for (int k = 0; k < selectedHandler.size(); k++) {
241
                                                Handler h = (Handler) selectedHandler.get(k);
242
                                                h.set(x, y);
243
                                        }
244

    
245
                                        modifyFeature(row.getIndex(), (IFeature) row.getLinkedRow());
246
                                }
247
                        }
248
        }
249

    
250
        /**
251
         * M?todo para dibujar la lo necesario para el estado en el que nos
252
         * encontremos.
253
         *
254
         * @param g
255
         *            Graphics sobre el que dibujar.
256
         * @param selectedGeometries
257
         *            BitSet con las geometr?as seleccionadas.
258
         * @param x
259
         *            par?metro x del punto que se pase para dibujar.
260
         * @param y
261
         *            par?metro x del punto que se pase para dibujar.
262
         */
263
        public void drawOperation(Graphics g, double x, double y) {
264
                SelectionCADToolState actualState = _fsm.getState();
265
                String status = actualState.getName();
266
                /* FBitSet selection = getCadToolAdapter().getVectorialAdapter()
267
                                .getSelection(); */
268
                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
269
                                .getEditionManager().getActiveLayerEdited();
270
                ArrayList selectedHandler = vle.getSelectedHandler();
271
                ArrayList selectedRow = vle.getSelectedRow();
272
                // ArrayList selectedRowIndex = vle.getSelectedRowIndex();
273

    
274
                /* if (selection.cardinality() == 0) {
275
                        selectedRow.clear();
276
                        selectedRowIndex.clear();
277
                        selectedHandler.clear();
278
                } */
279

    
280
                drawHandlers(g, selectedRow, getCadToolAdapter().getMapControl()
281
                                .getViewPort().getAffineTransform());
282

    
283
                if (status.equals("Selection.SecondPoint")) {
284
                        // Dibuja el rect?ngulo de selecci?n
285
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
286
                                        4);
287
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
288
                        elShape.lineTo(x, firstPoint.getY());
289
                        elShape.lineTo(x, y);
290
                        elShape.lineTo(firstPoint.getX(), y);
291
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
292
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
293
                                        getCadToolAdapter().getMapControl().getViewPort(),
294
                                        CADTool.selectSymbol);
295
                } else if (status.equals("Selection.WithHandlers")) {
296
                        // Movemos los handlers que hemos seleccionado
297
                        // previamente dentro del m?todo select()
298
                        for (int k = 0; k < selectedHandler.size(); k++) {
299
                                Handler h = (Handler) selectedHandler.get(k);
300
                                h.set(x, y);
301
                        }
302

    
303
                        // Y una vez movidos los v?rtices (handles)
304
                        // redibujamos la nueva geometr?a.
305
                        for (int i = 0; i < selectedRow.size(); i++) {
306
                                IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
307
                                IGeometry geom = ((IFeature) rowEd.getLinkedRow()).getGeometry()
308
                                                .cloneGeometry();
309
                                g.setColor(Color.gray);
310
                                geom.draw((Graphics2D) g, getCadToolAdapter().getMapControl()
311
                                                .getViewPort(), CADTool.modifySymbol);
312
                        }
313
                }
314
        }
315

    
316
        /**
317
         * Add a diferent option.
318
         *
319
         * @param sel
320
         *            DOCUMENT ME!
321
         * @param s
322
         *            Diferent option.
323
         */
324
        public void addOption(String s) {
325
                // TODO Auto-generated method stub
326
        }
327

    
328
        /*
329
         * (non-Javadoc)
330
         *
331
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
332
         */
333
        public void addValue(double d) {
334
        }
335

    
336
        public String getStatus() {
337
                try {
338
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
339
                                        .getPreviousState();
340
                        String status = actualState.getName();
341

    
342
                        return status;
343
                } catch (NullPointerException e) {
344
                        return "ExecuteMap.Initial";
345
                }
346
        }
347

    
348
        public String getTool() {
349
                return tool;
350
        }
351

    
352
        public void setNextTool(String tool) {
353
                this.tool = tool;
354
        }
355

    
356
        public void end() {
357
                CADExtension.setCADTool(getTool());
358
        }
359

    
360
        public String getName() {
361
                return "SELECCION";
362
        }
363

    
364
        public boolean selectFeatures(double x, double y) {
365
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
366
                .getState();
367

    
368
                String status = actualState.getName();
369
                if (!status.equals("Selection.FirstPoint")) return true;
370
                
371
                firstPoint = new Point2D.Double(x, y);
372

    
373
//                FBitSet selection = getCadToolAdapter().getVectorialAdapter()
374
//                                .getSelection();
375
                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
376
                                .getEditionManager().getActiveLayerEdited();
377
                ArrayList selectedRow = vle.getSelectedRow();
378
                ArrayList selectedHandler = vle.getSelectedHandler();
379
                selectedRow.clear();
380
                selectedHandler.clear();
381
                
382
                // ArrayList selectedRowIndex = vle.getSelectedRowIndex();
383

    
384
                // Se comprueba si se pincha en una gemometr?a
385
                PluginServices.getMDIManager().setWaitCursor();
386

    
387
                double tam = getCadToolAdapter().getMapControl().getViewPort()
388
                                .toMapDistance(tolerance);
389
                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
390
                                firstPoint.getY() - tam, tam * 2, tam * 2);
391
                VectorialEditableAdapter vea = getCadToolAdapter()
392
                                .getVectorialAdapter();
393
                String strEPSG = getCadToolAdapter().getMapControl().getViewPort()
394
                                .getProjection().getAbrev().substring(5);
395
                IRowEdited[] feats;
396
                 
397
                try {
398
                        feats = vea.getFeatures(rect, strEPSG);
399
//                        selection.clear();
400

    
401
                        for (int i = 0; i < feats.length; i++) {
402
                                IFeature feat = (IFeature) feats[i].getLinkedRow();
403
                                IGeometry geom = feat.getGeometry();
404

    
405
                                if (geom.intersects(rect)) { // , 0.1)){
406
//                                        selection.set(feats[i].getIndex(), true);
407
                                        selectedRow.add(feats[i]);
408
                                }
409
                        }
410
                } catch (DriverException e1) {
411
                        e1.printStackTrace();
412
                }                
413
                PluginServices.getMDIManager().restoreCursor();
414
                if (selectedRow.size() > 0)
415
                {
416
                        nextState = "Selection.WithSelectedFeatures";
417
                        return true;
418
                }
419
                if (nextState.equals("Selection.SecondPoint"))
420
                        nextState = "Selection.FirstPoint";
421
                else
422
                        nextState = "Selection.SecondPoint";
423
                return true; // Truco de Vicente
424
        }
425

    
426
        public int selectHandlers(double x, double y) {
427
                // firstPoint = new Point2D.Double(x, y);
428
                
429
                Point2D auxPoint = new Point2D.Double(x, y);
430

    
431
                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
432
                                .getEditionManager().getActiveLayerEdited();
433
                ArrayList selectedHandler = vle.getSelectedHandler();
434
                ArrayList selectedRow = vle.getSelectedRow();
435
                System.out.println("DENTRO DE selectHandlers. selectedRow.size= " + selectedRow.size());
436
                selectedHandler.clear();
437
                
438
                // Se comprueba si se pincha en una gemometr?a
439
                PluginServices.getMDIManager().setWaitCursor();
440

    
441
                double tam = getCadToolAdapter().getMapControl().getViewPort()
442
                                .toMapDistance(tolerance);
443
                
444
                Handler[] handlers = null;
445
                for (int i=0; i < selectedRow.size(); i++)
446
                {
447
                        IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
448
                        
449
                        IFeature fea = (IFeature) rowEd.getLinkedRow();
450
                        handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
451
                        // y miramos los handlers de cada entidad seleccionada
452
                        double min = tam;
453
                        // int hSel = -1;
454
                        for (int j = 0; j < handlers.length; j++) {
455
                                Point2D handlerPoint = handlers[j].getPoint();
456
                                double distance = auxPoint.distance(handlerPoint);
457
                                if (distance <= min) {
458
                                        min = distance;
459
                                        //hSel = j;
460
                                        selectedHandler.add(handlers[j]);
461
                                }
462
                        }
463
                }
464
                PluginServices.getMDIManager().restoreCursor();
465
                
466
                int numHandlesSelected = selectedHandler.size();
467
                
468
                /* if (numHandlesSelected == 0)
469
                        selectFeatures(x,y); */
470

    
471
                return numHandlesSelected;
472
        }
473
}