Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SelectionCADTool.java @ 5908

History | View | Annotate | Download (14.9 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.hardcode.driverManager.DriverLoadException;
54
import com.iver.andami.PluginServices;
55
import com.iver.cit.gvsig.CADExtension;
56
import com.iver.cit.gvsig.fmap.MapControl;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.Handler;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
64
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
65
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
69
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
70
import com.iver.cit.gvsig.gui.Table;
71
import com.iver.cit.gvsig.gui.View;
72
import com.iver.cit.gvsig.gui.Panels.TextFieldEdit;
73
import com.iver.cit.gvsig.gui.cad.CADTool;
74
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
75
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext;
77
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
78
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
79

    
80
/**
81
 * DOCUMENT ME!
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class SelectionCADTool extends DefaultCADTool {
86
        public static int tolerance = 4;
87

    
88
        private SelectionCADToolContext _fsm;
89

    
90
        protected Point2D firstPoint;
91

    
92
        //private Point2D lastPoint;
93

    
94

    
95

    
96
        protected String nextState;
97
// Registros de los que se ha sleccionado alg?n handler.
98
        protected ArrayList rowselectedHandlers=new ArrayList();
99
        protected String type=PluginServices.getText(this,"simple");
100
        protected ArrayList pointsPolygon=new ArrayList();
101

    
102
        protected boolean multipleSelection=false;
103
        /**
104
         * Crea un nuevo SelectionCADTool.
105
         */
106
        public SelectionCADTool() {
107
        }
108
        /**
109
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
110
         * carga previa a la utilizaci?n de la herramienta.
111
         */
112
        public void init() {
113
                _fsm = new SelectionCADToolContext(this);
114
                setNextTool("selection");
115
                setType(PluginServices.getText(this,"simple"));
116
        }
117

    
118
        /*
119
         * (non-Javadoc)
120
         *
121
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
122
         *      double, double)
123
         */
124
        public void transition(double x, double y, InputEvent event) {
125
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState()
126
                                + " x= " + x + " y=" + y);
127
                try{
128
                _fsm.addPoint(x, y, event);
129
                }catch (Exception e) {
130
                        init();
131
                }
132
                System.out.println("ESTADO ACTUAL: " + getStatus());
133

    
134
                // ESTO LO QUITO POR AHORA, PERO PUEDE QUE LO NECESITEMOS VOLVER A PONER.
135
                // Lo he quitado porque cuando seleccionas algo con CAD, molesta que
136
                // te hagan un redibujado.
137
                FLyrVect lv=(FLyrVect)((VectorialLayerEdited)CADExtension.getEditionManager().getActiveLayerEdited()).getLayer();
138
                //lv.getSource().getRecordset().getSelectionSupport().fireSelectionEvents();
139
                com.iver.andami.ui.mdiManager.View[] views = (com.iver.andami.ui.mdiManager.View[]) PluginServices.getMDIManager().getAllViews();
140

    
141
                for (int i=0 ; i<views.length ; i++){
142
                        if (views[i] instanceof Table){
143
                                Table table=(Table)views[i];
144
                                if (table.getModel().getAssociatedTable()!=null && table.getModel().getAssociatedTable().equals(lv))
145
                                        table.updateSelection();
146
                        }
147
                }
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         *
153
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
154
         *      double)
155
         */
156
        public void transition(double d) {
157
                _fsm.addValue(d);
158
        }
159

    
160
        /*
161
         * (non-Javadoc)
162
         *
163
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
164
         *      java.lang.String)
165
         */
166
        public void transition(String s) throws CommandException {
167
                if (!super.changeCommand(s)){
168
                        _fsm.addOption(s);
169
            }
170
        }
171

    
172
        public String getNextState() {
173
                return nextState;
174
        }
175

    
176
        protected void pointDoubleClick(MapControl map) {
177
                try {
178
                        FLayer[] actives = map.getMapContext()
179
            .getLayers().getActives();
180
            for (int i=0; i < actives.length; i++){
181
                if (actives[i] instanceof FLyrAnnotation && actives[i].isEditing()) {
182
                    FLyrAnnotation lyrAnnotation = (FLyrAnnotation) actives[i];
183

    
184
                            lyrAnnotation.setSelectedEditing();
185
                            lyrAnnotation.setInEdition(lyrAnnotation.getRecordset().getSelection().nextSetBit(0));
186
                            FLabel fl=lyrAnnotation.getLabel(lyrAnnotation.getInEdition());
187
                                        if (fl!=null){
188

    
189
                                                View vista=(View)PluginServices.getMDIManager().getActiveView();
190
                                                TextFieldEdit tfe=new TextFieldEdit(lyrAnnotation);
191

    
192
                                                tfe.show(vista.getMapControl().getViewPort().fromMapPoint(fl.getOrig()),vista.getMapControl());
193
                                        }
194
                }
195
            }
196

    
197
                } catch (DriverLoadException e) {
198
                        e.printStackTrace();
199
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
200
                        e.printStackTrace();
201
                }
202

    
203
}
204
        /**
205
         * Equivale al transition del prototipo pero sin pasarle como par? metro el
206
         * editableFeatureSource que ya estar? creado.
207
         *
208
         * @param selection
209
         *            Bitset con las geometr?as que est?n seleccionadas.
210
         * @param x
211
         *            par?metro x del punto que se pase en esta transici?n.
212
         * @param y
213
         *            par?metro y del punto que se pase en esta transici?n.
214
         */
215
        public void addPoint(double x, double y, InputEvent event) {
216
                if (event!=null && ((MouseEvent)event).getClickCount()==2){
217
                        pointDoubleClick((MapControl)event.getComponent());
218
                        return;
219
                }
220
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
221
                                .getPreviousState();
222
                String status = actualState.getName();
223
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
224
                                                                                                                // _fsm.getState());
225
                VectorialLayerEdited vle = getVLE();
226
                VectorialEditableAdapter vea=vle.getVEA();
227
                ArrayList selectedHandler = vle.getSelectedHandler();
228
                ArrayList selectedRow = vle.getSelectedRow();
229
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
230
                if (status.equals("Selection.FirstPoint")) {
231
                        firstPoint=new Point2D.Double(x,y);
232
                        pointsPolygon.add(firstPoint);
233
                } else if (status.equals("Selection.SecondPoint")) {
234
                } else if (status.equals("Selection.WithFeatures")) {
235
                } else if (status.equals("Selection.WithHandlers")) {
236
                        vea.startComplexRow();
237
                        for (int i = 0; i < selectedRow.size(); i++) {
238
                                IRowEdited row = (IRowEdited) selectedRow.get(i);
239
                                // Movemos los handlers que hemos seleccionado
240
                                // previamente dentro del m?todo select()
241
                                for (int k = 0; k < selectedHandler.size(); k++) {
242
                                        Handler h = (Handler) selectedHandler.get(k);
243
                                        h.set(x, y);
244
                                }
245

    
246
                                modifyFeature(row.getIndex(), (IFeature) row.getLinkedRow().cloneRow());
247
                        }
248
                        try {
249
                                vea.endComplexRow();
250
                        } catch (IOException e) {
251
                                e.printStackTrace();
252
                        } catch (DriverIOException e) {
253
                                e.printStackTrace();
254
                        }
255
                }
256
        }
257

    
258
        /**
259
         * Receives second point
260
         * @param x
261
         * @param y
262
         * @return numFeatures selected
263
         */
264
        public int selectWithSecondPoint(double x, double y, InputEvent event) {
265
                VectorialLayerEdited vle = getVLE();
266
                PluginServices.getMDIManager().setWaitCursor();
267
                vle.selectWithSecondPoint(x,y);
268
                ArrayList selectedRow = vle.getSelectedRow();
269
                PluginServices.getMDIManager().restoreCursor();
270
                if (selectedRow.size() > 0) {
271
                        nextState = "Selection.WithSelectedFeatures";
272
                } else
273
                        nextState = "Selection.FirstPoint";
274
                return selectedRow.size();
275
        }
276

    
277
        /**
278
         * M?todo para dibujar la lo necesario para el estado en el que nos
279
         * encontremos.
280
         *
281
         * @param g
282
         *            Graphics sobre el que dibujar.
283
         * @param selectedGeometries
284
         *            BitSet con las geometr?as seleccionadas.
285
         * @param x
286
         *            par?metro x del punto que se pase para dibujar.
287
         * @param y
288
         *            par?metro x del punto que se pase para dibujar.
289
         */
290
        public void drawOperation(Graphics g, double x, double y) {
291
                SelectionCADToolState actualState = _fsm.getState();
292
                String status = actualState.getName();
293
                VectorialLayerEdited vle = getVLE();
294
                if (vle == null) return;
295
                ArrayList selectedHandler = vle.getSelectedHandler();
296
                ViewPort vp=vle.getLayer().getFMap().getViewPort();
297
                if (status.equals("Selection.SecondPoint")) {
298
                        // Dibuja el rect?ngulo de selecci?n
299
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
300
                                        4);
301
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
302
                        elShape.lineTo(x, firstPoint.getY());
303
                        elShape.lineTo(x, y);
304
                        elShape.lineTo(firstPoint.getX(), y);
305
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
306
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
307
                                        vp,
308
                                        CADTool.selectSymbol);
309
                        Image img = vle.getSelectionImage();
310
                g.drawImage(img, 0, 0, null);
311
                return;
312
                }else if (status.equals("Selection.WithHandlers")) {
313
                        // Movemos los handlers que hemos seleccionado
314
                        // previamente dentro del m?todo select()
315
                        for (int k = 0; k < selectedHandler.size(); k++) {
316
                                Handler h = (Handler) selectedHandler.get(k);
317
                                h.set(x, y);
318
                        }
319
                        // Y una vez movidos los v?rtices (handles)
320
                        // redibujamos la nueva geometr?a.
321
                        for (int i = 0; i < rowselectedHandlers.size(); i++) {
322
                                IRowEdited rowEd = (IRowEdited) rowselectedHandlers.get(i);
323
                                IGeometry geom = ((IFeature) rowEd.getLinkedRow())
324
                                                .getGeometry().cloneGeometry();
325
                                g.setColor(Color.gray);
326
                                geom.draw((Graphics2D) g, vp, CADTool.modifySymbol);
327
                        }
328
                        return;
329
                }else{
330
                        try{
331
                        Image imgSel = vle.getSelectionImage();
332
                if (imgSel!=null)
333
                        g.drawImage(imgSel, 0, 0, null);
334
                Image imgHand = vle.getHandlersImage();
335
                if (imgHand!=null)
336
                        g.drawImage(imgHand, 0, 0, null);
337
                        }catch (Exception e) {
338
                        }
339
                }
340
        }
341

    
342
        /**
343
         * Add a diferent option.
344
         *
345
         * @param sel
346
         *            DOCUMENT ME!
347
         * @param s
348
         *            Diferent option.
349
         */
350
        public void addOption(String s) {
351
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
352
                                .getPreviousState();
353
                String status = actualState.getName();
354
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
355
                // _fsm.getState());
356
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
357
                if (s.equals(PluginServices.getText(this,"cancel"))){
358
                        init();
359
                        return;
360
                }
361
                if (status.equals("Selection.FirstPoint")) {
362
                        setType(s);
363
                        return;
364
                }
365
                init();
366
        }
367

    
368

    
369

    
370

    
371
        /*
372
         * (non-Javadoc)
373
         *
374
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
375
         */
376
        public void addValue(double d) {
377
        }
378

    
379
        public String getStatus() {
380
                try {
381
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
382
                                        .getPreviousState();
383
                        String status = actualState.getName();
384

    
385
                        return status;
386
                } catch (NullPointerException e) {
387
                        return "Selection.FirstPoint";
388
                }
389
        }
390

    
391

    
392

    
393
        public void end() {
394
                if (!getNextTool().equals("selection"))
395
                        CADExtension.setCADTool(getNextTool(),false);
396
        }
397

    
398
        public String getName() {
399
                return PluginServices.getText(this,"selection_");
400
        }
401

    
402
        public boolean selectFeatures(double x, double y, InputEvent event) {
403
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
404
                                .getState();
405

    
406
                String status = actualState.getName();
407
                VectorialLayerEdited vle = getVLE();
408

    
409

    
410
                if ((status.equals("Selection.FirstPoint"))
411
                                || (status.equals("Selection.WithSelectedFeatures"))) {
412
                        PluginServices.getMDIManager().setWaitCursor();
413
                        firstPoint = new Point2D.Double(x, y);
414
                        vle.selectWithPoint(x,y,multipleSelection);
415
                        PluginServices.getMDIManager().restoreCursor();
416
                }
417
                ArrayList selectedRow = vle.getSelectedRow();
418
                if (selectedRow.size() > 0) {
419
                        nextState = "Selection.WithSelectedFeatures";
420
                        return true;
421
                } else {
422
                        {
423
                                nextState = "Selection.SecondPoint";
424
                                return true;
425
                        }
426
                }
427
        }
428

    
429
        public int selectHandlers(double x, double y, InputEvent event) {
430
                Point2D auxPoint = new Point2D.Double(x, y);
431

    
432
                VectorialLayerEdited vle = getVLE();
433
                ArrayList selectedHandler = vle.getSelectedHandler();
434
                ArrayList selectedRow = vle.getSelectedRow();
435
                System.out.println("DENTRO DE selectHandlers. selectedRow.size= "
436
                                + selectedRow.size());
437
                selectedHandler.clear();
438

    
439
                // Se comprueba si se pincha en una gemometr?a
440
                PluginServices.getMDIManager().setWaitCursor();
441

    
442
                double tam = getCadToolAdapter().getMapControl().getViewPort()
443
                                .toMapDistance(tolerance);
444

    
445
                Handler[] handlers = null;
446
                rowselectedHandlers.clear();
447
                for (int i = 0; i < selectedRow.size(); i++) {
448
                        IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
449

    
450
                        IFeature fea = (IFeature) rowEd.getLinkedRow();
451
                        handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
452
                        // y miramos los handlers de cada entidad seleccionada
453
                        double min = tam;
454
                        // int hSel = -1;
455

    
456
                        for (int j = 0; j < handlers.length; j++) {
457
                                Point2D handlerPoint = handlers[j].getPoint();
458
                                double distance = auxPoint.distance(handlerPoint);
459
                                if (distance <= min) {
460
                                        min = distance;
461
                                        // hSel = j;
462
                                        selectedHandler.add(handlers[j]);
463
                                        rowselectedHandlers.add(rowEd);
464
                                }
465
                        }
466
                }
467
                PluginServices.getMDIManager().restoreCursor();
468

    
469
                int numHandlesSelected = selectedHandler.size();
470

    
471
                /*
472
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
473
                 */
474

    
475
                return numHandlesSelected;
476
        }
477

    
478
        public String getType() {
479
                return type;
480
        }
481

    
482
        public void setType(String type) {
483
                if (type.equals("S") || type.equals("s")){
484
                        this.type=PluginServices.getText(this,"simple");
485
                }else{
486
                        this.type = type;
487
                }
488
                pointsPolygon.clear();
489
        }
490

    
491
        public String toString() {
492
                return "_selection";
493
        }
494
        public void multipleSelection(boolean b) {
495
                multipleSelection=b;
496

    
497
        }
498

    
499
}