Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_902 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SelectionCADTool.java @ 10681

History | View | Annotate | Download (15.8 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.AnnotationEditableAdapter;
66
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
67
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
68
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
69
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
70
import com.iver.cit.gvsig.fmap.layers.FLayer;
71
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
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.gui.panels.TextFieldEdit;
79
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
80
import com.iver.cit.gvsig.project.documents.table.gui.Table;
81
import com.iver.cit.gvsig.project.documents.view.gui.View;
82

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

    
91
        private SelectionCADToolContext _fsm;
92

    
93
        protected Point2D firstPoint;
94

    
95
        //private Point2D lastPoint;
96

    
97

    
98

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

    
105
        protected boolean multipleSelection=false;
106
        /**
107
         * Crea un nuevo SelectionCADTool.
108
         */
109
        public SelectionCADTool() {
110
        }
111
        /**
112
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
113
         * carga previa a la utilizaci?n de la herramienta.
114
         */
115
        public void init() {
116
                _fsm = new SelectionCADToolContext(this);
117
                setNextTool("selection");
118
                setType(PluginServices.getText(this,"simple"));
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, double)
126
         */
127
        public void transition(double x, double y, InputEvent event) {
128
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState()
129
                                + " x= " + x + " y=" + y);
130
                try{
131
                _fsm.addPoint(x, y, event);
132
                }catch (Exception e) {
133
                        init();
134
                }
135
                System.out.println("ESTADO ACTUAL: " + getStatus());
136

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

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

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

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

    
175
        public String getNextState() {
176
                return nextState;
177
        }
178

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

    
187
                            lyrAnnotation.setSelectedEditing();
188
                            lyrAnnotation.setInEdition(lyrAnnotation.getRecordset().getSelection().nextSetBit(0));
189
                            FLabel fl=lyrAnnotation.getLabel(lyrAnnotation.getInEdition());
190
                                        if (fl!=null){
191

    
192
                                                View vista=(View)PluginServices.getMDIManager().getActiveWindow();
193
                                                TextFieldEdit tfe=new TextFieldEdit(lyrAnnotation);
194

    
195
                                                tfe.show(vista.getMapControl().getViewPort().fromMapPoint(fl.getOrig()),vista.getMapControl());
196
                                        }
197
                }
198
            }
199

    
200
                } catch (DriverLoadException e) {
201
                        e.printStackTrace();
202
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
203
                        e.printStackTrace();
204
                }
205

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

    
246
                                IGeometry ig = feat.getGeometry();
247
                                        // Movemos la geometr?a
248
                    UtilFunctions.moveGeom(ig, x -
249
                            firstPoint.getX(), y - firstPoint.getY());
250
                                }else {
251
                                        // Movemos los handlers que hemos seleccionado
252
                                        // previamente dentro del m?todo select()
253
                                        for (int k = 0; k < selectedHandler.size(); k++) {
254
                                                Handler h = (Handler) selectedHandler.get(k);
255
                                                h.set(x, y);
256
                                        }
257
                                }
258
                                modifyFeature(row.getIndex(), feat);
259
                                selectedRowsAux.add(new DefaultRowEdited(feat,IRowEdited.STATUS_MODIFIED,row.getIndex()));
260
                        }
261
                        firstPoint=new Point2D.Double(x,y);
262
                        vle.setSelectionCache(selectedRowsAux);
263
                        //clearSelection();
264
                        //selectedRow.addAll(selectedRowsAux);
265
                        try {
266
                                String description=PluginServices.getText(this,"move_handlers");
267
                                vea.endComplexRow(description);
268
                        } catch (IOException e) {
269
                                e.printStackTrace();
270
                        } catch (DriverIOException e) {
271
                                e.printStackTrace();
272
                        }
273
                }
274
        }
275

    
276
        /**
277
         * Receives second point
278
         * @param x
279
         * @param y
280
         * @return numFeatures selected
281
         */
282
        public int selectWithSecondPoint(double x, double y, InputEvent event) {
283
                VectorialLayerEdited vle = getVLE();
284
                PluginServices.getMDIManager().setWaitCursor();
285
                vle.selectWithSecondPoint(x,y);
286
                ArrayList selectedRow = vle.getSelectedRow();
287
                PluginServices.getMDIManager().restoreCursor();
288
                if (selectedRow.size() > 0) {
289
                        nextState = "Selection.WithSelectedFeatures";
290
                } else
291
                        nextState = "Selection.FirstPoint";
292
                return selectedRow.size();
293
        }
294

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

    
362
        /**
363
         * Add a diferent option.
364
         *
365
         * @param sel
366
         *            DOCUMENT ME!
367
         * @param s
368
         *            Diferent option.
369
         */
370
        public void addOption(String s) {
371
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
372
                                .getPreviousState();
373
                String status = actualState.getName();
374
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
375
                // _fsm.getState());
376
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
377
                if (s.equals(PluginServices.getText(this,"cancel"))){
378
                        init();
379
                        return;
380
                }
381
                if (status.equals("Selection.FirstPoint")) {
382
                        setType(s);
383
                        return;
384
                }
385
                init();
386
        }
387

    
388

    
389

    
390

    
391
        /*
392
         * (non-Javadoc)
393
         *
394
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
395
         */
396
        public void addValue(double d) {
397
        }
398

    
399
        public String getStatus() {
400
                try {
401
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
402
                                        .getPreviousState();
403
                        String status = actualState.getName();
404

    
405
                        return status;
406
                } catch (NullPointerException e) {
407
                        return "Selection.FirstPoint";
408
                }
409
        }
410

    
411

    
412

    
413
        public void end() {
414
                if (!getNextTool().equals("selection"))
415
                        CADExtension.setCADTool(getNextTool(),false);
416
        }
417

    
418
        public String getName() {
419
                return PluginServices.getText(this,"selection_");
420
        }
421

    
422
        public boolean selectFeatures(double x, double y, InputEvent event) {
423
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
424
                                .getState();
425

    
426
                String status = actualState.getName();
427
                VectorialLayerEdited vle = getVLE();
428

    
429

    
430
                if ((status.equals("Selection.FirstPoint"))
431
                                || (status.equals("Selection.WithSelectedFeatures"))) {
432
                        PluginServices.getMDIManager().setWaitCursor();
433
                        firstPoint = new Point2D.Double(x, y);
434
                        vle.selectWithPoint(x,y,multipleSelection);
435
                        PluginServices.getMDIManager().restoreCursor();
436
                }
437
                ArrayList selectedRow = vle.getSelectedRow();
438
                if (selectedRow.size() > 0) {
439
                        nextState = "Selection.WithSelectedFeatures";
440
                        return true;
441
                } else {
442
                        {
443
                                nextState = "Selection.SecondPoint";
444
                                return true;
445
                        }
446
                }
447
        }
448

    
449
        public int selectHandlers(double x, double y, InputEvent event) {
450
                Point2D auxPoint = new Point2D.Double(x, y);
451

    
452
                VectorialLayerEdited vle = getVLE();
453
                ArrayList selectedHandler = vle.getSelectedHandler();
454
                ArrayList selectedRow = vle.getSelectedRow();
455
                System.out.println("DENTRO DE selectHandlers. selectedRow.size= "
456
                                + selectedRow.size());
457
                selectedHandler.clear();
458

    
459
                // Se comprueba si se pincha en una gemometr?a
460
                PluginServices.getMDIManager().setWaitCursor();
461

    
462
                double tam = getCadToolAdapter().getMapControl().getViewPort()
463
                                .toMapDistance(tolerance);
464

    
465
                Handler[] handlers = null;
466
                rowselectedHandlers.clear();
467
                for (int i = 0; i < selectedRow.size(); i++) {
468
                        IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
469

    
470
                        IFeature fea = (IFeature) rowEd.getLinkedRow();
471
                        handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
472
                        // y miramos los handlers de cada entidad seleccionada
473
                        double min = tam;
474
                        // int hSel = -1;
475

    
476
                        for (int j = 0; j < handlers.length; j++) {
477
                                Point2D handlerPoint = handlers[j].getPoint();
478
                                double distance = auxPoint.distance(handlerPoint);
479
                                if (distance <= min) {
480
                                        min = distance;
481
                                        // hSel = j;
482
                                        selectedHandler.add(handlers[j]);
483
                                        rowselectedHandlers.add(rowEd);
484
                                }
485
                        }
486
                }
487
                PluginServices.getMDIManager().restoreCursor();
488

    
489
                int numHandlesSelected = selectedHandler.size();
490

    
491
                /*
492
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
493
                 */
494

    
495
                return numHandlesSelected;
496
        }
497

    
498
        public String getType() {
499
                return type;
500
        }
501

    
502
        public void setType(String type) {
503
                if (type.equals("S") || type.equals("s")){
504
                        this.type=PluginServices.getText(this,"simple");
505
                }else{
506
                        this.type = type;
507
                }
508
                pointsPolygon.clear();
509
        }
510

    
511
        public String toString() {
512
                return "_selection";
513
        }
514
        public void multipleSelection(boolean b) {
515
                multipleSelection=b;
516

    
517
        }
518

    
519
}