Statistics
| Revision:

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

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.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) {
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
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
202
                        e.printStackTrace();
203
                }
204

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

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

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

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

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

    
387

    
388

    
389

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

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

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

    
410

    
411

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

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

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

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

    
428

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

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

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

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

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

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

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

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

    
488
                int numHandlesSelected = selectedHandler.size();
489

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

    
494
                return numHandlesSelected;
495
        }
496

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

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

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

    
516
        }
517

    
518
}