Statistics
| Revision:

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

History | View | Annotate | Download (15.7 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.util.ArrayList;
51

    
52
import com.hardcode.driverManager.DriverLoadException;
53
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.edition.AnnotationEditableAdapter;
65
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
67
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
68
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
71
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
72
import com.iver.cit.gvsig.gui.cad.CADTool;
73
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
74
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
75
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
77
import com.iver.cit.gvsig.gui.panels.TextFieldEdit;
78
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
79
import com.iver.cit.gvsig.project.documents.table.gui.Table;
80
import com.iver.cit.gvsig.project.documents.view.gui.View;
81

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

    
90
        private SelectionCADToolContext _fsm;
91

    
92
        protected Point2D firstPoint;
93

    
94
        //private Point2D lastPoint;
95

    
96

    
97

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

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

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

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

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

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

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

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

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

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

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

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

    
199
                } catch (DriverLoadException 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
                        try {
218
                                pointDoubleClick((MapControl)event.getComponent());
219
                        } catch (ReadDriverException e) {
220
                                e.printStackTrace();
221
                        }
222
                        return;
223
                }
224
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
225
                                .getPreviousState();
226
                String status = actualState.getName();
227
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
228
                                                                                                                // _fsm.getState());
229
                VectorialLayerEdited vle = getVLE();
230
                VectorialEditableAdapter vea=vle.getVEA();
231
                ArrayList selectedHandler = vle.getSelectedHandler();
232
                ArrayList selectedRow = vle.getSelectedRow();
233
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
234
                if (status.equals("Selection.FirstPoint")) {
235
                        firstPoint=new Point2D.Double(x,y);
236
                        pointsPolygon.add(firstPoint);
237
                } else if (status.equals("Selection.SecondPoint")) {
238
                } else if (status.equals("Selection.WithFeatures")) {
239
                } else if (status.equals("Selection.WithHandlers")) {
240
                        vea.startComplexRow();
241
                        ArrayList selectedRowsAux=new ArrayList();
242
                        for (int i = 0; i < selectedRow.size(); i++) {
243
                                IRowEdited row = (IRowEdited) selectedRow.get(i);
244
                                IFeature feat = (IFeature) row.getLinkedRow().cloneRow();
245
                                if (vea instanceof AnnotationEditableAdapter) {
246

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

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

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

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

    
383

    
384

    
385

    
386
        /*
387
         * (non-Javadoc)
388
         *
389
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
390
         */
391
        public void addValue(double d) {
392
        }
393

    
394
        public String getStatus() {
395
                try {
396
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
397
                                        .getPreviousState();
398
                        String status = actualState.getName();
399

    
400
                        return status;
401
                } catch (NullPointerException e) {
402
                        return "Selection.FirstPoint";
403
                }
404
        }
405

    
406

    
407

    
408
        public void end() {
409
                if (!getNextTool().equals("selection"))
410
                        CADExtension.setCADTool(getNextTool(),false);
411
        }
412

    
413
        public String getName() {
414
                return PluginServices.getText(this,"selection_");
415
        }
416

    
417
        public boolean selectFeatures(double x, double y, InputEvent event) {
418
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
419
                                .getState();
420

    
421
                String status = actualState.getName();
422
                VectorialLayerEdited vle = getVLE();
423

    
424

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

    
444
        public int selectHandlers(double x, double y, InputEvent event) {
445
                Point2D auxPoint = new Point2D.Double(x, y);
446

    
447
                VectorialLayerEdited vle = getVLE();
448
                ArrayList selectedHandler = vle.getSelectedHandler();
449
                ArrayList selectedRow = vle.getSelectedRow();
450
                System.out.println("DENTRO DE selectHandlers. selectedRow.size= "
451
                                + selectedRow.size());
452
                selectedHandler.clear();
453

    
454
                // Se comprueba si se pincha en una gemometr?a
455
                PluginServices.getMDIManager().setWaitCursor();
456

    
457
                double tam = getCadToolAdapter().getMapControl().getViewPort()
458
                                .toMapDistance(tolerance);
459

    
460
                Handler[] handlers = null;
461
                rowselectedHandlers.clear();
462
                for (int i = 0; i < selectedRow.size(); i++) {
463
                        IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
464

    
465
                        IFeature fea = (IFeature) rowEd.getLinkedRow();
466
                        handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
467
                        // y miramos los handlers de cada entidad seleccionada
468
                        double min = tam;
469
                        // int hSel = -1;
470

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

    
484
                int numHandlesSelected = selectedHandler.size();
485

    
486
                /*
487
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
488
                 */
489

    
490
                return numHandlesSelected;
491
        }
492

    
493
        public String getType() {
494
                return type;
495
        }
496

    
497
        public void setType(String type) {
498
                if (type.equals("S") || type.equals("s")){
499
                        this.type=PluginServices.getText(this,"simple");
500
                }else{
501
                        this.type = type;
502
                }
503
                pointsPolygon.clear();
504
        }
505

    
506
        public String toString() {
507
                return "_selection";
508
        }
509
        public void multipleSelection(boolean b) {
510
                multipleSelection=b;
511

    
512
        }
513

    
514
}