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 @ 41177
History | View | Annotate | Download (5.1 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 |
DisposableIterator it = newSelection.fastIterator(); |
114 |
while(it.hasNext()) {
|
115 |
Object obj = it.next();
|
116 |
if(obj instanceof Feature) { |
117 |
Feature feat = ((Feature)obj); |
118 |
if(currentSelection.isSelected(feat))
|
119 |
currentSelection.deselect(feat); |
120 |
else
|
121 |
currentSelection.select(feat); |
122 |
} |
123 |
} |
124 |
} else {
|
125 |
lyrVect.getDataStore().setSelection(newSelection); |
126 |
} |
127 |
} finally {
|
128 |
DisposeUtils.dispose(newSelection); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
} catch (DataException e) {
|
134 |
throw new BehaviorException("No se pudo hacer la selecci?n", e); |
135 |
} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
136 |
throw new BehaviorException("No se pudo hacer la selecci?n", |
137 |
new CreateGeometryException(TYPES.SURFACE, SUBTYPES.GEOM2D, e));
|
138 |
} |
139 |
} |
140 |
} |