Statistics
| Revision:

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

History | View | Annotate | Download (12.6 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.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48
import java.io.IOException;
49
import java.util.ArrayList;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55
import com.iver.cit.gvsig.fmap.core.Handler;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
60
import com.iver.cit.gvsig.fmap.layers.FBitSet;
61
import com.iver.cit.gvsig.gui.cad.CADTool;
62
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext;
64
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
65

    
66
/**
67
 * DOCUMENT ME!
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class SelectionCADTool extends DefaultCADTool {
72
        public final static int tolerance = 4;
73

    
74
        private SelectionCADToolContext _fsm;
75

    
76
        private Point2D firstPoint;
77

    
78
        private Point2D lastPoint;
79

    
80
        private ArrayList selectedHandler = new ArrayList();
81

    
82
        private ArrayList selectedRow = new ArrayList();
83

    
84
        private ArrayList selectedRowIndex = new ArrayList();
85

    
86
        private String tool = "selection";
87

    
88
        private IGeometry clonedGeometry = null;
89

    
90
        private String state;
91

    
92
        // double
93
        // FLATNESS=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(2);
94

    
95
        /**
96
         * Crea un nuevo LineCADTool.
97
         */
98
        public SelectionCADTool() {
99

    
100
        }
101

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

    
111
        /*
112
         * (non-Javadoc)
113
         *
114
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
115
         *      double, double)
116
         */
117
        public void transition(double x, double y) {
118
                ((SelectionCADToolContext) _fsm).addPoint(x, y);
119
        }
120

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

    
131
        /*
132
         * (non-Javadoc)
133
         *
134
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
135
         *      java.lang.String)
136
         */
137
        public void transition(String s) {
138
                _fsm.addOption(s);
139
        }
140
        public String getState(){
141
                return state;
142
        }
143
        public boolean select(double x, double y) {
144
                firstPoint = new Point2D.Double(x, y);
145
                FBitSet selection = getCadToolAdapter().getVectorialAdapter()
146
                                .getSelection();
147
                // if ((selectedRow.size() == 0) ||
148
                // (selection.cardinality() == 0)) {
149
                // Se comprueba si se pincha en una gemometr?a
150
                PluginServices.getMDIManager().setWaitCursor();
151

    
152
                double tam = getCadToolAdapter().getMapControl().getViewPort()
153
                                .toMapDistance(tolerance);
154
                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
155
                                firstPoint.getY() - tam, tam * 2, tam * 2);
156
                int[] indexes = getCadToolAdapter().getVectorialAdapter()
157
                                .getRowsIndexes(rect);
158

    
159
                FBitSet prevSelection=(FBitSet)selection.clone();
160
                selection.clear();
161

    
162
                for (int i = 0; i < indexes.length; i++) {
163
                        try {
164
                                if (getCadToolAdapter().getVectorialAdapter().getShape(
165
                                                indexes[i]).intersects(rect)) {
166
                                        // .intersects(rect,FLATNESS)) {
167
                                        selection.set(indexes[i], true);
168
                                }
169
                        } catch (DriverIOException e) {
170
                                // TODO Auto-generated catch block
171
                                e.printStackTrace();
172
                        }
173
                }
174
                boolean isTheSame=false;
175
                if (prevSelection.cardinality()!=0 && selection.intersects(prevSelection) && !selection.equals(prevSelection)){
176
                        selection=prevSelection;
177
                        getCadToolAdapter().getVectorialAdapter().setSelection(selection);
178
                        isTheSame=true;
179
                }
180
                if (selection.cardinality() > 0) {
181
                        // Se comprueba si se pincha un handler. El m?s cercano (o los m?s
182
                        // cercanos si hay empate)
183
                        selectedRow.clear();
184
                        selectedRowIndex.clear();
185
                        selectedHandler.clear();
186

    
187
                        double min = Double.MAX_VALUE;
188

    
189
                        // Cogemos las entidades seleccionadas
190
                        for (int i = selection.nextSetBit(0); i >= 0; i = selection
191
                                        .nextSetBit(i + 1)) {
192
                                Handler[] handlers = null;
193

    
194
                                DefaultFeature fea = null;
195
                                try {
196
                                        fea = (DefaultFeature) getCadToolAdapter()
197
                                                        .getVectorialAdapter().getRow(i).getLinkedRow();
198
                                        clonedGeometry = fea.getGeometry().cloneGeometry();
199
                                        handlers = clonedGeometry
200
                                                        .getHandlers(IGeometry.SELECTHANDLER);
201
                                        selectedRow.add(new DefaultFeature(clonedGeometry, fea
202
                                                        .getAttributes()));
203
                                        selectedRowIndex.add(new Integer(i));
204
                                        // y miramos los handlers de cada entidad seleccionada
205
                                        for (int j = 0; j < handlers.length; j++) {
206
                                                Point2D handlerPoint = handlers[j].getPoint();
207
                                                double distance = firstPoint.distance(handlerPoint);
208
                                                if ((distance <= min)
209
                                                                && (distance < getCadToolAdapter()
210
                                                                                .getMapControl().getViewPort()
211
                                                                                .toMapDistance(tolerance))) {
212
                                                        min = distance;
213

    
214
                                                        selectedHandler.add(clonedGeometry
215
                                                                        .getHandlers(IGeometry.SELECTHANDLER)[j]);
216

    
217
                                                }
218
                                        }
219

    
220
                                } catch (DriverIOException e) {
221
                                        // TODO Auto-generated catch block
222
                                        e.printStackTrace();
223
                                } catch (IOException e) {
224
                                        // TODO Auto-generated catch block
225
                                        e.printStackTrace();
226
                                }
227
                        }
228

    
229

    
230
                }
231
                PluginServices.getMDIManager().restoreCursor();
232
                if ((selection.cardinality() > 0 && selectedHandler.size()>0) || isTheSame){
233
                        state = "Selection.EndPoint";
234
                }else if (selection.cardinality() > 0){
235
                        state ="Selection.FirstPoint";
236
                }else {
237
                        state = "Selection.SecondPoint";
238
                }
239
                return true;
240
        }
241

    
242
        /**
243
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
244
         * editableFeatureSource que ya estar? creado.
245
         *
246
         * @param selection
247
         *            Bitset con las geometr?as que est?n seleccionadas.
248
         * @param x
249
         *            par?metro x del punto que se pase en esta transici?n.
250
         * @param y
251
         *            par?metro y del punto que se pase en esta transici?n.
252
         */
253
        public void addPoint(double x, double y) {
254
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
255
                                .getPreviousState();
256
                String status = actualState.getName();
257
                FBitSet selection = getCadToolAdapter().getVectorialAdapter()
258
                                .getSelection();
259
                try {
260
                        if (status.equals("Selection.FirstPoint")) {
261
                                // firstPoint = new Point2D.Double(x, y);
262
                        } else if (status.equals("Selection.SecondPoint")) {
263
                                // PluginServices.getMDIManager().setWaitCursor();
264
                                lastPoint = new Point2D.Double(x, y);
265
                                selection.clear();
266

    
267
                                double x1;
268
                                double y1;
269
                                double w1;
270
                                double h1;
271

    
272
                                if (firstPoint.getX() < lastPoint.getX()) {
273
                                        x1 = firstPoint.getX();
274
                                        w1 = lastPoint.getX() - firstPoint.getX();
275
                                } else {
276
                                        x1 = lastPoint.getX();
277
                                        w1 = firstPoint.getX() - lastPoint.getX();
278
                                }
279

    
280
                                if (firstPoint.getY() < lastPoint.getY()) {
281
                                        y1 = firstPoint.getY();
282
                                        h1 = lastPoint.getY() - firstPoint.getY();
283
                                } else {
284
                                        y1 = lastPoint.getY();
285
                                        h1 = firstPoint.getY() - lastPoint.getY();
286
                                }
287

    
288
                                Rectangle2D rect = new Rectangle2D.Double(x1, y1, w1, h1);
289

    
290
                                int[] indexes = getCadToolAdapter().getVectorialAdapter()
291
                                                .getRowsIndexes(new Rectangle2D.Double(x1, y1, w1, h1));
292

    
293
                                for (int i = 0; i < indexes.length; i++) {
294
                                        if (firstPoint.getX() < lastPoint.getX()) {
295
                                                if (rect.contains(getCadToolAdapter()
296
                                                                .getVectorialAdapter().getShape(indexes[i])
297
                                                                .getBounds2D())) {
298
                                                        selection.set(indexes[i], true);
299
                                                }
300
                                        } else {
301
                                                if (getCadToolAdapter().getVectorialAdapter().getShape(
302
                                                                indexes[i]).intersects(rect)) { // , 0.1)){
303
                                                        selection.set(indexes[i], true);
304
                                                }
305
                                        }
306
                                }
307

    
308
                                PluginServices.getMDIManager().restoreCursor();
309
                                // cardinality = selection.cardinality();
310
                        } else if (status.equals("Selection.EndPoint")) {
311

    
312
                                for (int i = 0; i < selectedRow.size(); i++) {
313

    
314
                                        for (int k = 0; k < selectedHandler.size(); k++) {
315
                                                Handler h = (Handler) selectedHandler.get(k);
316
                                                h.set(x, y);
317
                                        }
318
                                        // System.out.println(h.getPoint());
319
                                        DefaultFeature row = (DefaultFeature) selectedRow.get(i);
320
                                        int index = ((Integer) selectedRowIndex.get(i)).intValue();
321
                                        // System.out.println(row.getGeometry().getBounds2D());
322
                                        // h.set(x, y);
323
                                        // getVectorialAdapter().modifyRow(index, row);
324

    
325
                                        modifyFeature(index, row);
326
                                }
327

    
328
                        }
329
                        // } catch (IOException e) {
330
                        // TODO Auto-generated catch block
331
                        // e.printStackTrace();
332
                } catch (DriverIOException e) {
333
                        // TODO Auto-generated catch block
334
                        e.printStackTrace();
335
                }
336
        }
337

    
338
        /**
339
         * M?todo para dibujar la lo necesario para el estado en el que nos
340
         * encontremos.
341
         *
342
         * @param g
343
         *            Graphics sobre el que dibujar.
344
         * @param selectedGeometries
345
         *            BitSet con las geometr?as seleccionadas.
346
         * @param x
347
         *            par?metro x del punto que se pase para dibujar.
348
         * @param y
349
         *            par?metro x del punto que se pase para dibujar.
350
         */
351
        public void drawOperation(Graphics g, double x, double y) {
352
                SelectionCADToolState actualState = _fsm.getState();
353
                String status = actualState.getName();
354
                FBitSet selection = getCadToolAdapter().getVectorialAdapter()
355
                                .getSelection();
356
                try {
357
                        drawHandlers(g, selection, getCadToolAdapter().getMapControl()
358
                                        .getViewPort().getAffineTransform());
359
                } catch (DriverIOException e) {
360
                        // TODO Auto-generated catch block
361
                        e.printStackTrace();
362
                }
363

    
364
                if (status.equals("Selection.SecondPoint")) {
365
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
366
                                        4);
367
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
368
                        elShape.lineTo(x, firstPoint.getY());
369
                        elShape.lineTo(x, y);
370
                        elShape.lineTo(firstPoint.getX(), y);
371
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
372
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
373
                                        getCadToolAdapter().getMapControl().getViewPort(),
374
                                        CADTool.selectSymbol);
375
                } else if (status.equals("Selection.EndPoint")) {
376
                        for (int i = 0; i < selectedRow.size(); i++) {
377
                                for (int k = 0; k < selectedHandler.size(); k++) {
378
                                        Handler h = (Handler) selectedHandler.get(k);
379
                                        h.set(x, y);
380
                                        System.out.println(h.getPoint());
381
                                }
382

    
383
                                IGeometry geom = ((IFeature) selectedRow.get(i)).getGeometry().cloneGeometry();
384
                                System.out.println(geom.getBounds2D());
385
                                // int index = ((Integer) selectedRowIndex.get(i)).intValue();
386
                                g.setColor(Color.gray);
387

    
388
                                geom.draw((Graphics2D) g, getCadToolAdapter().getMapControl()
389
                                                .getViewPort(), CADTool.modifySymbol);
390
                        }
391
                }
392

    
393
        }
394

    
395
        /**
396
         * Add a diferent option.
397
         *
398
         * @param sel
399
         *            DOCUMENT ME!
400
         * @param s
401
         *            Diferent option.
402
         */
403
        public void addOption(String s) {
404
                // TODO Auto-generated method stub
405
        }
406

    
407
        /*
408
         * (non-Javadoc)
409
         *
410
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
411
         */
412
        public void addValue(double d) {
413
        }
414

    
415
        /**
416
         * DOCUMENT ME!
417
         *
418
         * @return DOCUMENT ME!
419
         */
420
        public int getSelectedRowSize() {
421
                return this.selectedRow.size();
422
        }
423

    
424
        public String getStatus() {
425
                try {
426
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
427
                                        .getPreviousState();
428
                        String status = actualState.getName();
429
                        return status;
430
                } catch (NullPointerException e) {
431
                        return "ExecuteMap.Initial";
432
                }
433
        }
434

    
435
        public String getTool() {
436
                return tool;
437
        }
438

    
439
        public void setNextTool(String tool) {
440
                this.tool = tool;
441
        }
442

    
443
        public void end() {
444
                CADExtension.setCADTool(getTool());
445
        }
446

    
447
}