Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SelectionCADTool.java @ 33205

History | View | Annotate | Download (17.6 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 org.gvsig.editing.gui.cad.tools;
42

    
43
import java.awt.Color;
44
import java.awt.Image;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47
import java.util.ArrayList;
48

    
49
import org.gvsig.andami.PluginServices;
50
import org.gvsig.andami.messages.NotificationManager;
51
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
52
import org.gvsig.editing.CADExtension;
53
import org.gvsig.editing.gui.cad.DefaultCADTool;
54
import org.gvsig.editing.gui.cad.exception.CommandException;
55
import org.gvsig.editing.gui.cad.tools.smc.SelectionCADToolContext;
56
import org.gvsig.editing.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
57
import org.gvsig.editing.layers.VectorialLayerEdited;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.tools.dispose.DisposableIterator;
61
import org.gvsig.fmap.dal.feature.EditableFeature;
62
import org.gvsig.fmap.dal.feature.Feature;
63
import org.gvsig.fmap.dal.feature.FeatureSet;
64
import org.gvsig.fmap.dal.feature.FeatureStore;
65
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException;
66
import org.gvsig.fmap.geom.Geometry;
67
import org.gvsig.fmap.geom.handler.Handler;
68
import org.gvsig.fmap.geom.primitive.Curve;
69
import org.gvsig.fmap.geom.primitive.GeneralPathX;
70
import org.gvsig.fmap.mapcontext.MapContext;
71
import org.gvsig.fmap.mapcontext.ViewPort;
72
import org.gvsig.fmap.mapcontext.layers.FLayer;
73
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
74
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
75

    
76

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

    
85
        protected SelectionCADToolContext _fsm;
86

    
87
        protected Point2D firstPoint;
88

    
89
        protected String nextState;
90
        // Registros de los que se ha seleccionado alg?n handler.
91
        protected ArrayList rowselectedHandlers=new ArrayList();
92
        protected String type=PluginServices.getText(this,"simple");
93
        //        protected ArrayList pointsPolygon=new ArrayList();
94

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

    
111
        /*
112
         * (non-Javadoc)
113
         *
114
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
115
         *      double, double)
116
         */
117
        public void transition(double x, double y, InputEvent event) {
118
                System.out.println("TRANSICION DESDE ESTADO " + _fsm.getState()
119
                                + " x= " + x + " y=" + y);
120
                try{
121
                        _fsm.addPoint(x, y, event);
122
                }catch (Exception e) {
123
                        init();
124
                        PluginServices.getMDIManager().restoreCursor();
125
                }
126
                System.out.println("ESTADO ACTUAL: " + getStatus());
127

    
128
                // ESTO LO QUITO POR AHORA, PERO PUEDE QUE LO NECESITEMOS VOLVER A PONER.
129
                // Lo he quitado porque cuando seleccionas algo con CAD, molesta que
130
                // te hagan un redibujado.
131
                FLyrVect lv=(FLyrVect)((VectorialLayerEdited)CADExtension.getEditionManager().getActiveLayerEdited()).getLayer();
132
                //lv.getSource().getRecordset().getSelectionSupport().fireSelectionEvents();
133
                org.gvsig.andami.ui.mdiManager.IWindow[] views = PluginServices.getMDIManager().getAllWindows();
134

    
135
                for (int i=0 ; i<views.length ; i++){
136
                        if (views[i] instanceof FeatureTableDocumentPanel){
137
                                FeatureTableDocumentPanel table=(FeatureTableDocumentPanel)views[i];
138
                                if (table.getModel().getAssociatedLayer()!=null && table.getModel().getAssociatedLayer().equals(lv)) {
139
                                        table.updateSelection();
140
                                }
141
                        }
142
                }
143
        }
144

    
145
        /*
146
         * (non-Javadoc)
147
         *
148
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
149
         *      double)
150
         */
151
        public void transition(double d) {
152
                _fsm.addValue(d);
153
        }
154

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

    
167
        public String getNextState() {
168
                return nextState;
169
        }
170

    
171
        protected void pointDoubleClick(MapContext map) throws ReadException {
172
                FLayer[] actives = map
173
                .getLayers().getActives();
174
                //        for (int i=0; i < actives.length; i++){
175
                //            if (actives[i] instanceof FLyrAnnotation && actives[i].isEditing()) {
176
                //                FLyrAnnotation lyrAnnotation = (FLyrAnnotation) actives[i];
177
                //                       lyrAnnotation.setSelectedEditing();
178
                //                       lyrAnnotation.setInEdition(lyrAnnotation.getRecordset().getSelection().nextSetBit(0));
179
                //                       FLabel fl=lyrAnnotation.getLabel(lyrAnnotation.getInEdition());
180
                //                       if (fl!=null){
181
                //                               View vista=(View)PluginServices.getMDIManager().getActiveWindow();
182
                //                                       TextFieldEdit tfe=new TextFieldEdit(lyrAnnotation);
183
                //                                tfe.show(vista.getMapControl().getViewPort().fromMapPoint(fl.getOrig()),vista.getMapControl());
184
                //                               }
185
                //            }
186
                //        }
187
        }
188
        /**
189
         * Equivale al transition del prototipo pero sin pasarle como par? metro el
190
         * editableFeatureSource que ya estar? creado.
191
         *
192
         * @param selection
193
         *            Bitset con las geometr?as que est?n seleccionadas.
194
         * @param x
195
         *            par?metro x del punto que se pase en esta transici?n.
196
         * @param y
197
         *            par?metro y del punto que se pase en esta transici?n.
198
         */
199
        public void addPoint(double x, double y, InputEvent event) {
200
                //                if (event!=null && ((MouseEvent)event).getClickCount()==2){
201
                //                        try {
202
                //                                pointDoubleClick((MapControl)event.getComponent());
203
                //                        } catch (ReadException e) {
204
                //                                NotificationManager.addError(e.getMessage(),e);
205
                //                        }
206
                //                        return;
207
                //                }
208
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
209
                .getPreviousState();
210
                String status = actualState.getName();
211
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
212
                // _fsm.getState());
213
                VectorialLayerEdited vle = getVLE();
214
                FeatureStore featureStore=null;
215
                DisposableIterator iterator = null;
216
                try {
217
                        featureStore = vle.getFeatureStore();
218

    
219
                        ArrayList selectedHandler = vle.getSelectedHandler();
220
                        FeatureSet selection = (FeatureSet) featureStore.getSelection();// vle.getSelectedRow();
221
                        System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
222
                        if (status.equals("Selection.FirstPoint")) {
223
                                firstPoint = new Point2D.Double(x, y);
224
                                // pointsPolygon.add(firstPoint);
225
                        } else if (status.equals("Selection.SecondPoint")) {
226
                        } else if (status.equals("Selection.WithFeatures")) {
227
                        } else if (status.equals("Selection.WithHandlers")) {
228
                                String description = PluginServices.getText(this,
229
                                "move_handlers");
230
                                featureStore.beginEditingGroup(description);
231
                                try {
232
                                        iterator = selection.iterator();
233
                                        while (iterator.hasNext()) {
234
                                                Feature feature = (Feature) iterator.next();
235
                                                // }
236
                                                // for (int i = 0; i < selectedRow.size(); i++) {
237
                                                // IRowEdited row = (IRowEdited) selectedRow.get(i);
238
                                                // IFeature feat = (IFeature)
239
                                                // row.getLinkedRow().cloneRow();
240
                                                Geometry ig = feature.getDefaultGeometry()
241
                                                .cloneGeometry();
242
                                                // if (vea instanceof AnnotationEditableAdapter) {
243
                                                // // Movemos la geometr?a
244
                                                // UtilFunctions.moveGeom(ig, x -
245
                                                // firstPoint.getX(), y - firstPoint.getY());
246
                                                // }else {
247
                                                // Movemos los handlers que hemos seleccionado
248
                                                // previamente dentro del m?todo select()
249
                                                Handler[] handlers = ig
250
                                                .getHandlers(Geometry.SELECTHANDLER);
251
                                                for (int k = 0; k < selectedHandler.size(); k++) {
252
                                                        Handler h = (Handler) selectedHandler.get(k);
253
                                                        for (int j = 0; j < handlers.length; j++) {
254
                                                                if (h.getPoint().equals(handlers[j].getPoint())) {
255
                                                                        handlers[j].set(x, y);
256
                                                                }
257
                                                        }
258
                                                }
259
                                                // }
260
                                                EditableFeature eFeature = feature.getEditable();
261
                                                eFeature.setGeometry(featureStore
262
                                                                .getDefaultFeatureType()
263
                                                                .getDefaultGeometryAttributeName(), ig);
264
                                                featureStore.update(eFeature);
265
                                                // modifyFeature(feature, feature);
266
                                                // selectedRowsAux.add(new
267
                                                // DefaultRowEdited(feat,IRowEdited.STATUS_MODIFIED,row.getIndex()));
268
                                        }
269
                                        firstPoint = new Point2D.Double(x, y);
270
                                        // vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS,
271
                                        // selectedRowsAux);
272
                                        // clearSelection();
273
                                        // selectedRow.addAll(selectedRowsAux);
274

    
275
                                } finally {
276
                                        featureStore.endEditingGroup();
277
                                }
278
                        }
279
                } catch (DataException e) {
280
                        // TODO Auto-generated catch block
281
                        e.printStackTrace();
282
                } finally {
283
                        if (iterator != null) {
284
                                iterator.dispose();
285
                        }
286
                }
287
        }
288

    
289
        /**
290
         * Receives second point
291
         * @param x
292
         * @param y
293
         * @return numFeatures selected
294
         */
295
        public long selectWithSecondPoint(double x, double y, InputEvent event) {
296
                VectorialLayerEdited vle = getVLE();
297
                PluginServices.getMDIManager().setWaitCursor();
298
                vle.selectWithSecondPoint(x,y);
299
                FeatureSet selection=null;
300
                try {
301
                        selection = (FeatureSet)vle.getFeatureStore().getSelection();
302
                        PluginServices.getMDIManager().restoreCursor();
303
                        long countSel=selection.getSize();
304
                        if (countSel > 0) {
305
                                nextState = "Selection.WithSelectedFeatures";
306
                        } else {
307
                                nextState = "Selection.FirstPoint";
308
                        }
309
                        return countSel;
310
                } catch (ReadException e) {
311
                        e.printStackTrace();
312
                        return 0;
313
                } catch (DataException e) {
314
                        e.printStackTrace();
315
                        return 0;
316
                }
317
        }
318

    
319
        /**
320
         * M?todo para dibujar la lo necesario para el estado en el que nos
321
         * encontremos.
322
         *
323
         * @param g
324
         *            Graphics sobre el que dibujar.
325
         * @param selectedGeometries
326
         *            BitSet con las geometr?as seleccionadas.
327
         * @param x
328
         *            par?metro x del punto que se pase para dibujar.
329
         * @param y
330
         *            par?metro x del punto que se pase para dibujar.
331
         */
332
        public void drawOperation(MapControlDrawer renderer, double x, double y) {
333
                SelectionCADToolState actualState = _fsm.getState();
334
                String status = actualState.getName();
335
                VectorialLayerEdited vle = getVLE();
336
                if (vle == null) {
337
                        return;
338
                }
339
                ArrayList selectedHandler = vle.getSelectedHandler();
340
                ViewPort vp=vle.getLayer().getMapContext().getViewPort();
341
                if (status.equals("Selection.SecondPoint")) {
342
                        // Dibuja el rect?ngulo de selecci?n
343
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
344
                                        4);
345
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
346
                        elShape.lineTo(x, firstPoint.getY());
347
                        elShape.lineTo(x, y);
348
                        elShape.lineTo(firstPoint.getX(), y);
349
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
350

    
351
                        Curve curve = createCurve(elShape);
352
                        renderer.draw(curve, mapControlManager.getGeometrySelectionSymbol());
353

    
354
                        return;
355
                }else if (status.equals("Selection.WithHandlers")) {
356
                        // Movemos los handlers que hemos seleccionado
357
                        // previamente dentro del m?todo select()
358
                        double xPrev=0;
359
                        double yPrev=0;
360
                        for (int k = 0; k < selectedHandler.size(); k++) {
361
                                Handler h = (Handler)selectedHandler.get(k);
362
                                xPrev=h.getPoint().getX();
363
                                yPrev=h.getPoint().getY();
364
                                h.set(x, y);
365
                        }
366
                        // Y una vez movidos los v?rtices (handles)
367
                        // redibujamos la nueva geometr?a.
368
                        for (int i = 0; i < rowselectedHandlers.size(); i++) {
369
                                Feature feature=(Feature)rowselectedHandlers.get(i);
370

    
371
                                Geometry geom=(feature.getDefaultGeometry()).cloneGeometry();
372
                                renderer.setColor(Color.gray);
373
                                renderer.draw(geom, mapControlManager.getAxisReferenceSymbol());
374
                        }
375
                        for (int k = 0; k < selectedHandler.size(); k++) {
376
                                Handler h = (Handler)selectedHandler.get(k);
377
                                h.set(xPrev, yPrev);
378
                        }
379
                        return;
380
                }
381
        }
382

    
383
        /**
384
         * Add a diferent option.
385
         *
386
         * @param sel
387
         *            DOCUMENT ME!
388
         * @param s
389
         *            Diferent option.
390
         */
391
        public void addOption(String s) {
392
                SelectionCADToolState actualState = (SelectionCADToolState) _fsm
393
                .getPreviousState();
394
                String status = actualState.getName();
395
                System.out.println("PREVIOUSSTATE =" + status); // + "ESTADO ACTUAL: " +
396
                // _fsm.getState());
397
                System.out.println("STATUS ACTUAL = " + _fsm.getTransition());
398
                if (s.equals(PluginServices.getText(this,"cancel"))){
399
                        init();
400
                        return;
401
                }
402
                if (status.equals("Selection.FirstPoint")) {
403
                        setType(s);
404
                        return;
405
                }
406
                init();
407
        }
408

    
409

    
410

    
411

    
412
        /*
413
         * (non-Javadoc)
414
         *
415
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
416
         */
417
        public void addValue(double d) {
418
        }
419

    
420
        public String getStatus() {
421
                try {
422
                        SelectionCADToolState actualState = (SelectionCADToolState) _fsm
423
                        .getPreviousState();
424
                        String status = actualState.getName();
425

    
426
                        return status;
427
                } catch (NullPointerException e) {
428
                        return "Selection.FirstPoint";
429
                }
430
        }
431

    
432

    
433

    
434
        public void end() {
435
                if (!getNextTool().equals("selection")) {
436
                        CADExtension.setCADTool(getNextTool(),false);
437
                }
438
        }
439

    
440
        public String getName() {
441
                return PluginServices.getText(this,"selection_");
442
        }
443

    
444
        public boolean selectFeatures(double x, double y, InputEvent event) {
445
                SelectionCADToolState actualState = _fsm
446
                .getState();
447

    
448
                String status = actualState.getName();
449
                VectorialLayerEdited vle = getVLE();
450
                multipleSelection=event.isControlDown();
451

    
452
                if ((status.equals("Selection.FirstPoint"))
453
                                || (status.equals("Selection.WithSelectedFeatures"))) {
454
                        PluginServices.getMDIManager().setWaitCursor();
455
                        firstPoint = new Point2D.Double(x, y);
456
                        try {
457
                                vle.getFeatureStore().beginEditingGroup(getName());
458
                                vle.selectWithPoint(x,y,multipleSelection);
459
                                vle.getFeatureStore().endEditingGroup();
460
                        } catch (NeedEditingModeException e) {
461
                                NotificationManager.showMessageError(getName(), e);
462
                        } catch (ReadException e) {
463
                                NotificationManager.showMessageError(getName(), e);
464
                        }
465
                        PluginServices.getMDIManager().restoreCursor();
466
                }
467
                FeatureSet selection=null;
468
                try {
469
                        selection = (FeatureSet)vle.getFeatureStore().getSelection();
470

    
471
                        //                ArrayList selectedRow = vle.getSelectedRow();
472
                        long countSel=selection.getSize();
473
                        if (countSel > 0) {
474
                                nextState = "Selection.WithSelectedFeatures";
475
                                return true;
476
                        } else {
477
                                {
478
                                        nextState = "Selection.SecondPoint";
479
                                        return true;
480
                                }
481
                        }
482
                } catch (ReadException e) {
483
                        e.printStackTrace();
484
                        return false;
485
                } catch (DataException e) {
486
                        e.printStackTrace();
487
                        return false;
488
                }
489
        }
490

    
491
        public int selectHandlers(double x, double y, InputEvent event) {
492
                Point2D auxPoint = new Point2D.Double(x, y);
493

    
494
                VectorialLayerEdited vle = getVLE();
495
                ArrayList selectedHandler = vle.getSelectedHandler();
496
                FeatureSet selection=null;
497
                DisposableIterator iterator = null;
498
                try {
499
                        selection = (FeatureSet)vle.getFeatureStore().getSelection();
500

    
501
                        // ArrayList selectedRow = vle.getSelectedRow();
502
                        long countSel = selection.getSize();
503
                        System.out.println("DENTRO DE selectHandlers. selectedRow.size= "
504
                                        + countSel);
505
                        selectedHandler.clear();
506

    
507
                        // Se comprueba si se pincha en una gemometr?a
508
                        PluginServices.getMDIManager().setWaitCursor();
509

    
510
                        double tam = getCadToolAdapter().getMapControl().getViewPort()
511
                        .toMapDistance(mapControlManager.getTolerance());
512

    
513
                        Handler[] handlers = null;
514
                        rowselectedHandlers.clear();
515
                        iterator = selection.iterator();
516
                        while (iterator.hasNext()) {
517
                                Feature feature = (Feature) iterator.next();
518

    
519
                                // }
520
                                // for (int i = 0; i < selectedRow.size(); i++) {
521
                                // IRowEdited rowEd = (IRowEdited) selectedRow.get(i);
522
                                //
523
                                // IFeature fea = (IFeature) rowEd.getLinkedRow();
524
                                handlers = (feature.getDefaultGeometry())
525
                                .getHandlers(Geometry.SELECTHANDLER);
526
                                // y miramos los handlers de cada entidad seleccionada
527
                                double min = tam;
528
                                // int hSel = -1;
529

    
530
                                for (int j = 0; j < handlers.length; j++) {
531
                                        Point2D handlerPoint = handlers[j].getPoint();
532
                                        double distance = auxPoint.distance(handlerPoint);
533
                                        if (distance <= min) {
534
                                                min = distance;
535
                                                // hSel = j;
536
                                                selectedHandler.add(handlers[j]);
537
                                                rowselectedHandlers.add(feature);
538
                                        }
539
                                }
540
                        }
541
                        PluginServices.getMDIManager().restoreCursor();
542
                } catch (DataException e) {
543
                        // TODO Auto-generated catch block
544
                        e.printStackTrace();
545
                } finally {
546
                        if (iterator != null) {
547
                                iterator.dispose();
548
                        }
549
                }
550

    
551
                int numHandlesSelected = selectedHandler.size();
552

    
553
                /*
554
                 * if (numHandlesSelected == 0) selectFeatures(x,y);
555
                 */
556

    
557
                return numHandlesSelected;
558
        }
559

    
560
        public String getType() {
561
                return type;
562
        }
563

    
564
        public void setType(String type) {
565
                if (type.equals("S") || type.equals("s")){
566
                        this.type=PluginServices.getText(this,"simple");
567
                }else{
568
                        this.type = type;
569
                }
570
                //                pointsPolygon.clear();
571
        }
572

    
573
        public String toString() {
574
                return "_selection";
575
        }
576
        public void multipleSelection(boolean b) {
577
                multipleSelection=b;
578

    
579
        }
580

    
581
}