Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / PolygonSelectionListener.java @ 41176

History | View | Annotate | Download (5.32 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools;
25

    
26
import java.awt.Image;
27

    
28
import org.gvsig.fmap.IconThemeHelper;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureSelection;
32
import org.gvsig.fmap.dal.feature.FeatureSet;
33
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
34
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
35
import org.gvsig.fmap.geom.Geometry.TYPES;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.GeometryManager;
38
import org.gvsig.fmap.geom.primitive.GeneralPathX;
39
import org.gvsig.fmap.geom.primitive.Surface;
40
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
42
import org.gvsig.fmap.mapcontrol.MapControl;
43
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
44
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
45
import org.gvsig.tools.dispose.DisposableIterator;
46
import org.gvsig.tools.dispose.DisposeUtils;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
/**
51
 * <p>Listener that selects all features of the active and vector layers which intersect with the defined
52
 *  polygon area in the associated {@link MapControl MapControl} object.</p>
53
 *
54
 * <p>The selection will be produced after user finishes the creation of the polyline.</p>
55
 *
56
 * @author Vicente Caballero Navarro
57
 */
58
public class PolygonSelectionListener implements PolylineListener {
59
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
60
        private static final Logger logger = LoggerFactory.getLogger(PolygonSelectionListener.class);
61
        
62
        /**
63
         * The cursor used to work with this tool listener.
64
         *
65
         * @see #getCursor()
66
         */
67
        private MapControl mapCtrl;
68

    
69
        /**
70
          * <p>Creates a new <code>PolygonSelectionListener</code> object.</p>
71
         *
72
         * @param mc the <code>MapControl</code> where is drawn the polyline
73
         */
74
        public PolygonSelectionListener(MapControl mc) {
75
                this.mapCtrl = mc;
76
        }
77

    
78
        public Image getImageCursor() {
79
                return IconThemeHelper.getImage("cursor-select-by-polygon");
80
        }
81

    
82
        public boolean cancelDrawing() {
83
                return false;
84
        }
85

    
86
        public void points(MeasureEvent event) throws BehaviorException {
87
                // TODO Auto-generated method stub
88

    
89
        }
90

    
91
        public void pointFixed(MeasureEvent event) throws BehaviorException {
92

    
93
        }
94

    
95
        public void polylineFinished(MeasureEvent event) throws BehaviorException {
96
                try {
97
                        GeometryManager geomManager = GeometryLocator.getGeometryManager();
98

    
99
            GeneralPathX gp = event.getGP();
100
            Surface geom = (Surface)geomManager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
101
            geom.setGeneralPath(gp);           
102
            FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
103
            for (int i = 0; i < actives.length; i++) {
104
                if (actives[i] instanceof FLyrVect) {
105
                    FLyrVect lyrVect = (FLyrVect) actives[i];
106
                                        FeatureSet newSelection = null;
107
                                        try {
108
                                                newSelection = lyrVect.queryByGeometry(geom,
109
                                                                                lyrVect.getFeatureStore()
110
                                                                                                .getDefaultFeatureType());
111
                                                if (event.getEvent().isControlDown()) {
112
                                                        FeatureSelection currentSelection = (FeatureSelection)lyrVect.getFeatureStore().getSelection();
113
                                                        if(isAnyGeomAlreadySelected(currentSelection, newSelection)) {
114
                                                                currentSelection.deselect(newSelection);
115
                                                        } else {
116
                                                                currentSelection.select(newSelection);
117
                                                        }
118
                                                } else {
119
                                                        lyrVect.getDataStore().setSelection(newSelection);
120
                                                }
121
                                        } finally {
122
                                                DisposeUtils.dispose(newSelection);
123
                    }
124
                }
125
            }
126

    
127
                } catch (DataException e) {
128
                        throw new BehaviorException("No se pudo hacer la selecci?n", e);
129
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
130
                        throw new BehaviorException("No se pudo hacer la selecci?n", 
131
                                        new CreateGeometryException(TYPES.SURFACE, SUBTYPES.GEOM2D, e));
132
                }
133
        }
134
        
135
        private boolean isAnyGeomAlreadySelected(FeatureSelection currentSelection, FeatureSet newSelection) throws DataException {
136
                DisposableIterator it = newSelection.fastIterator();
137
                while(it.hasNext()) {
138
                        Object obj = it.next();
139
                        if(obj instanceof Feature) {
140
                                Feature feat = ((Feature)obj);
141
                                if(currentSelection.isSelected(feat))
142
                                        return true;
143
                        }
144
                }
145
                return false;
146
        }
147
}