Statistics
| Revision:

root / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / SelectionCadTool.java @ 1728

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

    
45
import com.iver.cit.gvsig.fmap.core.FGeometry;
46
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
47
import com.iver.cit.gvsig.fmap.core.Handler;
48
import com.iver.cit.gvsig.fmap.core.IGeometry;
49
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
50
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
51
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
52
import com.iver.cit.gvsig.fmap.edition.cad.Status;
53
import com.iver.cit.gvsig.fmap.layers.FBitSet;
54
import com.iver.cit.gvsig.gui.cad.CadTool;
55
import com.iver.cit.gvsig.gui.cad.automaton.Seleccion;
56

    
57
import com.iver.fsac.Automaton;
58

    
59
import java.awt.Color;
60
import java.awt.Graphics;
61
import java.awt.Graphics2D;
62
import java.awt.geom.Point2D;
63
import java.awt.geom.Rectangle2D;
64

    
65
import java.io.IOException;
66

    
67
import java.util.ArrayList;
68

    
69

    
70
/**
71
 * Herramienta para seleccionar las geometr?as por rectangulo o por hacer un
72
 * click sobre ellas.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class SelectionCadTool extends AbstractCadTool {
77
        private static Status[] STATUS = {
78
                        new Status(""), new Status(""),
79
                        new Status("Precise punto del rect?ngulo de selecci?n"),
80
                        new Status(""), new Status("Precise punto de estiramiento"),
81
                };
82
        public final static int tolerance = 5;
83
        private Seleccion selectionStatus = new Seleccion();
84
        private Point2D firstPoint;
85
        private Point2D lastPoint;
86
        private ArrayList selectedHandler = new ArrayList();
87
        private ArrayList selectedGeometry = new ArrayList();
88
        private ArrayList selectedGeometryIndex = new ArrayList();
89

    
90
        /**
91
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
92
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
93
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
94
         */
95
        public int transition(String text,
96
                final EditableFeatureSource editingSource,
97
                final FBitSet selectedGeometries, final double[] values) {
98
                int ret = selectionStatus.transition(text);
99

    
100
                int status = selectionStatus.getStatus();
101

    
102
                if (status == 0) {
103
                } else if (status == 1) {
104
                        firstPoint = new Point2D.Double(values[0], values[1]);
105

    
106
                        /*double tolerance = getCadToolAdapter().getMapControl().getViewPort()
107
                                                                   .toMapDistance(SelectionCadTool.tolerance);
108

109
                        Rectangle2D r = new Rectangle2D.Double(firstPoint.getX() -
110
                                        tolerance, firstPoint.getY() - tolerance, tolerance * 2,
111
                                        tolerance * 2);
112
*/
113
                        if (selectedGeometries.cardinality() > 0) {
114
                                //Se comprueba si se pincha un handler. El m?s cercano (o los m?s cercanos si hay empate)
115
                                try {
116
                                        selectedGeometry.clear();
117
                                        selectedGeometryIndex.clear();
118
                                        selectedHandler.clear();
119

    
120
                                        double min = Double.MAX_VALUE;
121

    
122
                                        for (int i = selectedGeometries.nextSetBit(0); i >= 0;
123
                                                        i = selectedGeometries.nextSetBit(i + 1)) {
124
                                                Handler[] handlers = editingSource.getGeometry(i)
125
                                                                                                                  .getHandlers(FGeometry.SELECTHANDLER);
126

    
127
                                                IGeometry clonedGeometry = null;
128
                                                
129
                                                for (int j = 0; j < handlers.length; j++) {
130
                                                        Point2D handlerPoint = handlers[j].getPoint();
131

    
132
                                                        //                                                        if (r.contains(handlerPoint.getX(),
133
                                                        //                                                                        handlerPoint.getY())) {
134
                                                        double distance = firstPoint.distance(handlerPoint);
135

    
136
                                                        if ((distance <= min) && (distance < getCadToolAdapter().getMapControl().getViewPort()
137
                                                                         .toMapDistance(SelectionCadTool.tolerance))) {
138
                                                                if (distance < min) {
139
                                                                        selectedGeometry.clear();
140
                                                                        selectedGeometryIndex.clear();
141
                                                                        selectedHandler.clear();
142
                                                                }
143

    
144
                                                                min = distance;
145

    
146
                                                                if (clonedGeometry == null) {
147
                                                                        clonedGeometry = editingSource.getGeometry(i)
148
                                                                                                                                  .cloneGeometry();
149
                                                                }
150

    
151
                                                                selectedGeometry.add(clonedGeometry);
152
                                                                selectedHandler.add(clonedGeometry.getHandlers(
153
                                                                                FGeometry.SELECTHANDLER)[j]);
154
                                                                selectedGeometryIndex.add(new Integer(i));
155
                                                        }
156
                                                }
157
                                        }
158

    
159
                                        if (selectedGeometry.size() > 0) {
160
                                                ret = ret | selectionStatus.transition("handler");
161
                                        }
162
                                } catch (IOException e) {
163
                                        e.printStackTrace();
164
                                } catch (DriverIOException e) {
165
                                        e.printStackTrace();
166
                                }
167
                        }
168

    
169
                        if ((selectedGeometry.size() == 0) ||
170
                                        (selectedGeometries.cardinality() == 0)) {
171
                                // Se comprueba si se pincha en una gemometr?a
172
                                PluginServices.getMDIManager().setWaitCursor();
173

    
174
                                double tam = getCadToolAdapter().getMapControl().getViewPort()
175
                                                                 .toMapDistance(SelectionCadTool.tolerance);
176
                                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() -
177
                                                tam, firstPoint.getY() - tam, tam*2, tam*2);
178
                                int[] indexes = editingSource.getGeometriesIndexes(rect);
179

    
180
                                selectedGeometries.clear();
181

    
182
                                try {
183
                                        for (int i = 0; i < indexes.length; i++) {
184
                                                if (editingSource.getGeometry(indexes[i])
185
                                                                                         .fastIntersects(rect.getX(),
186
                                                                        rect.getY(), rect.getWidth(),
187
                                                                        rect.getHeight())) {
188
                                                        selectedGeometries.set(indexes[i], true);
189
                                                }
190
                                        }
191
                                } catch (IOException e) {
192
                                        e.printStackTrace();
193
                                } catch (DriverIOException e) {
194
                                        e.printStackTrace();
195
                                }
196

    
197
                                PluginServices.getMDIManager().restoreCursor();
198

    
199
                                if (selectedGeometries.cardinality() > 0) {
200
                                        ret = ret | selectionStatus.transition("sel");
201
                                } else {
202
                                        ret = ret | selectionStatus.transition("no_sel");
203
                                }
204
                        }
205

    
206
                        return ret;
207
                } else if (status == 3) {
208
                        PluginServices.getMDIManager().setWaitCursor();
209

    
210
                        if (values.length != 0) {
211
                                lastPoint = new Point2D.Double(values[0], values[1]);
212
                        }
213

    
214
                        selectedGeometries.clear();
215

    
216
                        try {
217
                                double x;
218
                                double y;
219
                                double w;
220
                                double h;
221

    
222
                                if (firstPoint.getX() < lastPoint.getX()) {
223
                                        x = firstPoint.getX();
224
                                        w = lastPoint.getX() - firstPoint.getX();
225
                                } else {
226
                                        x = lastPoint.getX();
227
                                        w = firstPoint.getX() - lastPoint.getX();
228
                                }
229

    
230
                                if (firstPoint.getY() < lastPoint.getY()) {
231
                                        y = firstPoint.getY();
232
                                        h = lastPoint.getY() - firstPoint.getY();
233
                                } else {
234
                                        y = lastPoint.getY();
235
                                        h = firstPoint.getY() - lastPoint.getY();
236
                                }
237

    
238
                                Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
239

    
240
                                int[] indexes = editingSource.getGeometriesIndexes(new Rectangle2D.Double(
241
                                                        x, y, w, h));
242

    
243
                                for (int i = 0; i < indexes.length; i++) {
244
                                if (firstPoint.getX()<lastPoint.getX()){
245
                                        if (rect.contains(editingSource.getGeometry(indexes[i])
246
                                                                                                           .getBounds2D())) {
247
                                                selectedGeometries.set(indexes[i], true);
248
                                        }
249
                                }else{
250
                                        if (editingSource.getGeometry(indexes[i]).intersects(rect)){
251
                                                selectedGeometries.set(indexes[i], true);
252
                                        }
253
                                }
254
                                }
255
                        } catch (DriverIOException e) {
256
                                e.printStackTrace();
257
                        } catch (IOException e) {
258
                                e.printStackTrace();
259
                        }
260

    
261
                        PluginServices.getMDIManager().restoreCursor();
262

    
263
                        if (selectedGeometries.cardinality() > 0) {
264
                                ret = ret | selectionStatus.transition("sel");
265
                        } else {
266
                                ret = ret | selectionStatus.transition("no_sel");
267
                        }
268

    
269
                        return ret;
270
                } else if (status == 5) {
271
                        for (int i = 0; i < selectedGeometry.size(); i++) {
272
                                Handler h = (Handler) selectedHandler.get(i);
273
                                IGeometry geom = (IGeometry) selectedGeometry.get(i);
274
                                int index = ((Integer) selectedGeometryIndex.get(i)).intValue();
275

    
276
                                h.set(values[0], values[1]);
277

    
278
                                try {
279
                                        editingSource.modifyGeometry(index, geom);
280
                                } catch (IOException e) {
281
                                        e.printStackTrace();
282
                                } catch (DriverIOException e) {
283
                                        e.printStackTrace();
284
                                }
285
                        }
286

    
287
                        ret = ret | selectionStatus.transition("done");
288

    
289
                        return ret;
290
                }
291

    
292
                return ret;
293
        }
294

    
295
        /**
296
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
297
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
298
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
299
         */
300
        public void drawOperation(Graphics g, EditableFeatureSource efs,
301
                FBitSet selectedGeometries, double x, double y) {
302
                int status = selectionStatus.getStatus();
303

    
304
                if (status == 2) {
305
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
306
                                        4);
307
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
308
                        elShape.lineTo(x, firstPoint.getY());
309
                        elShape.lineTo(x, y);
310
                        elShape.lineTo(firstPoint.getX(), y);
311
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
312
                                ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
313
                                                getCadToolAdapter().getMapControl().getViewPort(),
314
                                                CadTool.selectSymbol);
315
                } else if (status == 4) {
316
                        for (int i = 0; i < selectedGeometry.size(); i++) {
317
                                Handler h = (Handler) selectedHandler.get(i);
318
                                IGeometry geom = ((IGeometry) selectedGeometry.get(i)).cloneGeometry();
319
                                int index = ((Integer) selectedGeometryIndex.get(i)).intValue();
320
                                g.setColor(Color.gray);
321
                                h.set(x, y);
322
                                geom.draw((Graphics2D) g,
323
                                        getCadToolAdapter().getCadMapControl().getMapControl()
324
                                                .getViewPort(), CadTool.modifySymbol);
325
                        }
326
                }
327
        }
328

    
329
        /**
330
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
331
         */
332
        public String getQuestion() {
333
                return STATUS[selectionStatus.getStatus()].getQuestion();
334
        }
335

    
336
        /**
337
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
338
         */
339
        public void initializeStatus() {
340
                selectionStatus.initialize();
341
        }
342

    
343
        /**
344
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
345
         */
346
        public Automaton getAutomaton() {
347
                return selectionStatus;
348
        }
349

    
350
        /**
351
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
352
         */
353
        public String getName() {
354
                return "SELECCI?N";
355
        }
356
}