Statistics
| Revision:

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

History | View | Annotate | Download (9.52 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 com.vividsolutions.jts.geom.Envelope;
60

    
61
import java.awt.Graphics;
62
import java.awt.Graphics2D;
63
import java.awt.geom.Point2D;
64
import java.awt.geom.Rectangle2D;
65

    
66
import java.io.IOException;
67
import java.util.ArrayList;
68

    
69

    
70
/**
71
 * DOCUMENT ME!
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class SelectionCadTool extends AbstractCadTool {
76
        private static Status[] STATUS = {
77
                        new Status(""),
78
                        new Status(""),
79
                        new Status("Siguiente punto del rect?ngulo de selecci?n"),
80
                        new Status(""),
81
                        new Status("Precise punto de estiramiento"),
82
                };
83
        private final static int tolerance = 5;
84
        private Seleccion selectionStatus = new Seleccion();
85
        private Point2D firstPoint;
86
        private Point2D lastPoint;
87
        private ArrayList selectedHandler = new ArrayList();
88
        private ArrayList selectedGeometry = new ArrayList();
89
        private ArrayList selectedGeometryIndex = new ArrayList();
90

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

    
101
                int status = selectionStatus.getStatus();
102

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

    
107
                        double tolerance = getCadToolAdapter().getMapControl().getViewPort().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
                                try {
115
                                        selectedGeometry.clear();
116
                                        selectedGeometryIndex.clear();
117
                                        selectedHandler.clear();
118
                                        for (int i = selectedGeometries.nextSetBit(0); i >= 0;
119
                                                        i = selectedGeometries.nextSetBit(i + 1)) {
120
                                                Handler[] handlers = editingSource.getGeometry(i)
121
                                                                                                                  .getHandlers(FGeometry.SELECTHANDLER);
122

    
123
                                                IGeometry clonedGeometry = null;
124
                                                for (int j = 0; j < handlers.length; j++) {
125
                                                        Point2D handlerPoint = handlers[j].getPoint();
126

    
127
                                                        if (r.contains(handlerPoint.getX(),
128
                                                                                handlerPoint.getY())) {
129
                                                                if (clonedGeometry == null){
130
                                                                        clonedGeometry = editingSource.getGeometry(i).cloneGeometry();
131
                                                                }
132
                                                                selectedGeometry.add(clonedGeometry);
133
                                                                selectedHandler.add(clonedGeometry.getHandlers(FGeometry.SELECTHANDLER)[j]);
134
                                                                selectedGeometryIndex.add(new Integer(i));
135
                                                        }
136
                                                }
137
                                        }
138
                                        if (selectedGeometry.size() > 0)
139
                                                ret = ret | selectionStatus.transition("handler");
140
                                } catch (IOException e) {
141
                                        e.printStackTrace();
142
                                } catch (DriverIOException e) {
143
                                        e.printStackTrace();
144
                                }
145
                        }
146
                        if ((selectedGeometry.size() == 0) || (selectedGeometries.cardinality() == 0)){
147
                                PluginServices.getMDIManager().setWaitCursor();
148
                                double tam = getCadToolAdapter().getMapControl().getViewPort()
149
                                 .toMapDistance(SelectionCadTool.tolerance);
150
                                Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
151
                                                firstPoint.getY() - tam, tam, tam);
152
                                int[] indexes = editingSource.getGeometriesIndexes(rect);
153
                        
154
                                selectedGeometries.clear();
155

    
156
                                for (int i = 0; i < indexes.length; i++) {
157
                                        try {
158
                                                if (editingSource.getGeometry(indexes[i]).fastIntersects(rect.getX(),
159
                                                                        rect.getY(), rect.getWidth(), rect.getHeight())) {
160
                                                        selectedGeometries.set(indexes[i], true);
161
                                                        break;
162
                                                }
163
                                        } catch (IOException e) {
164
                                                e.printStackTrace();
165
                                        } catch (DriverIOException e) {
166
                                                e.printStackTrace();
167
                                        }
168
                                }
169

    
170
                                PluginServices.getMDIManager().restoreCursor();
171

    
172
                                if (selectedGeometries.cardinality() > 0) {
173
                                        ret = ret | selectionStatus.transition("sel");
174
                                } else {
175
                                        ret = ret | selectionStatus.transition("no_sel");
176
                                }
177
                        }
178

    
179
                        return ret;
180
                } else if (status == 3) {
181
                        PluginServices.getMDIManager().setWaitCursor();
182
                        if (values.length != 0) {
183
                                lastPoint = new Point2D.Double(values[0], values[1]);
184
                        }
185

    
186
                        selectedGeometries.clear();
187

    
188
                        try {
189
                                double x;
190
                                double y;
191
                                double w;
192
                                double h;
193

    
194
                                if (firstPoint.getX() < lastPoint.getX()) {
195
                                        x = firstPoint.getX();
196
                                        w = lastPoint.getX() - firstPoint.getX();
197
                                } else {
198
                                        x = lastPoint.getX();
199
                                        w = firstPoint.getX() - lastPoint.getX();
200
                                }
201

    
202
                                if (firstPoint.getY() < lastPoint.getY()) {
203
                                        y = firstPoint.getY();
204
                                        h = lastPoint.getY() - firstPoint.getY();
205
                                } else {
206
                                        y = lastPoint.getY();
207
                                        h = firstPoint.getY() - lastPoint.getY();
208
                                }
209

    
210
                                Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
211

    
212
                                for (int i=0;i<editingSource.getGeometryCount();i++){
213
                                        if (rect.contains(editingSource.getGeometry(i).getBounds2D())){
214
                                                selectedGeometries.set(i,true);
215
                                    }
216
                                }
217
                                 /*
218
                                int[] indexes = editingSource.getGeometriesIndexes(new Rectangle2D.Double(
219
                                                        x, y, x + w, y + h));
220

221
                                for (int i = 0; i < indexes.length; i++) {
222
                                        if (rect.contains(editingSource.getGeometry(indexes[i])
223
                                                                                                           .getBounds2D())) {
224
                                                selectedGeometries.set(indexes[i], true);
225
                                        }
226
                                }*/
227
                        } catch (DriverIOException e) {
228
                                e.printStackTrace();
229
                        } catch (IOException e) {
230
                                e.printStackTrace();
231
                        }
232

    
233
                        PluginServices.getMDIManager().restoreCursor();
234
                        if (selectedGeometries.cardinality() > 0) {
235
                                ret = ret | selectionStatus.transition("sel");
236
                        } else {
237
                                ret = ret | selectionStatus.transition("no_sel");
238
                        }
239

    
240
                        return ret;
241
                } else if (status == 5) {
242
                        for (int i = 0; i < selectedGeometry.size(); i++) {
243
                                Handler h = (Handler) selectedHandler.get(i);
244
                                IGeometry geom = (IGeometry) selectedGeometry.get(i);
245
                                int index = ((Integer) selectedGeometryIndex.get(i)).intValue();
246

    
247
                                h.set(values[0], values[1]);
248
                                try {
249
                                        editingSource.modifyGeometry(index, geom);
250
                                } catch (IOException e) {
251
                                        e.printStackTrace();
252
                                } catch (DriverIOException e) {
253
                                        e.printStackTrace();
254
                                }
255
                                
256
                        }
257
                        ret = ret | selectionStatus.transition("done");
258

    
259
                        return ret;
260
                }
261

    
262
                return ret;
263
        }
264

    
265
        /**
266
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
267
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
268
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
269
         */
270
        public void drawOperation(Graphics g, EditableFeatureSource efs,
271
                FBitSet selectedGeometries, double x, double y) {
272
                int status = selectionStatus.getStatus();
273

    
274
                if (status == 2) {
275
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
276
                                        4);
277
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
278
                        elShape.lineTo(x, firstPoint.getY());
279
                        elShape.lineTo(x, y);
280
                        elShape.lineTo(firstPoint.getX(), y);
281
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
282
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
283
                                getCadToolAdapter().getMapControl().getViewPort(),
284
                                CadTool.drawingSymbol);
285
                }else if (status == 4) {
286
                        for (int i = 0; i < selectedGeometry.size(); i++) {
287
                                Handler h = (Handler) selectedHandler.get(i);
288
                                IGeometry geom = ((IGeometry) selectedGeometry.get(i)).cloneGeometry();
289
                                int index = ((Integer) selectedGeometryIndex.get(i)).intValue();
290

    
291
                                h.set(x, y);
292
                                geom.draw((Graphics2D)g, getCadToolAdapter().getCadMapControl().getMapControl().getViewPort(), CadTool.drawingSymbol);                        
293
                        }
294
                }
295
        }
296

    
297
        /**
298
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
299
         */
300
        public String getQuestion() {
301
                return STATUS[selectionStatus.getStatus()].getQuestion();
302
        }
303

    
304
        /**
305
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
306
         */
307
        public void initializeStatus() {
308
                selectionStatus.initialize();
309
        }
310

    
311
        /**
312
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
313
         */
314
        public Automaton getAutomaton() {
315
                return selectionStatus;
316
        }
317
}