svn-gvsig-desktop / tags / v2_0_0_Build_2056 / extensions / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.extension / src / main / java / org / gvsig / selectiontools / app / extension / tools / CircleSelectionListener.java @ 39042
History | View | Annotate | Download (8.64 KB)
1 |
package org.gvsig.selectiontools.app.extension.tools; |
---|---|
2 |
|
3 |
/* gvSIG. Geographic Information System of the Valencian Government
|
4 |
*
|
5 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
6 |
* of the Valencian Government (CIT)
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
21 |
* MA 02110-1301, USA.
|
22 |
*
|
23 |
*/
|
24 |
|
25 |
import java.awt.Cursor; |
26 |
import java.awt.Image; |
27 |
import java.awt.Toolkit; |
28 |
import java.awt.event.MouseEvent; |
29 |
|
30 |
import javax.swing.ImageIcon; |
31 |
|
32 |
import org.gvsig.andami.IconThemeHelper; |
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.messages.NotificationManager; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
37 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
38 |
import org.gvsig.fmap.geom.Geometry; |
39 |
import org.gvsig.fmap.geom.GeometryLocator; |
40 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
41 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
42 |
import org.gvsig.fmap.geom.GeometryManager; |
43 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
44 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
45 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
46 |
import org.gvsig.fmap.geom.primitive.Point; |
47 |
import org.gvsig.fmap.geom.primitive.Circle; |
48 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.fmap.mapcontrol.MapControl; |
52 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
53 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
54 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.CircleListener; |
55 |
|
56 |
/**
|
57 |
* <p>
|
58 |
* Listener that selects all features of the active, available and vector layers
|
59 |
* which intersect with the defined circle area in the associated
|
60 |
* {@link MapControl MapControl} object.
|
61 |
* </p>
|
62 |
*
|
63 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
64 |
*/
|
65 |
public class CircleSelectionListener implements CircleListener { |
66 |
|
67 |
|
68 |
|
69 |
/**
|
70 |
* The cursor used to work with this tool listener.
|
71 |
*
|
72 |
* @see #getCursor()
|
73 |
*/
|
74 |
private Cursor cur = null ; |
75 |
|
76 |
/**
|
77 |
* Reference to the <code>MapControl</code> object that uses.
|
78 |
*/
|
79 |
private MapControl mapCtrl;
|
80 |
|
81 |
/**
|
82 |
* <p>
|
83 |
* Creates a new listener for selecting circular areas.
|
84 |
* </p>
|
85 |
*
|
86 |
* @param mc
|
87 |
* the <code>MapControl</code> object where the measures are made
|
88 |
*/
|
89 |
public CircleSelectionListener(MapControl mc) {
|
90 |
this.mapCtrl = mc;
|
91 |
} |
92 |
|
93 |
/*
|
94 |
* (non-Javadoc)
|
95 |
*
|
96 |
* @see
|
97 |
* com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener#circle(com.iver
|
98 |
* .cit.gvsig.fmap.tools.Events.MeasureEvent)
|
99 |
*/
|
100 |
public void circle(MeasureEvent event) throws BehaviorException { |
101 |
if (event.getEvent().getID() == MouseEvent.MOUSE_RELEASED) { |
102 |
FLayer[] activeLayers =
|
103 |
mapCtrl.getMapContext().getLayers().getActives(); |
104 |
|
105 |
FLayer layer; |
106 |
FLyrVect lyrVect; |
107 |
|
108 |
Geometry geom = null;
|
109 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
110 |
Point center;
|
111 |
try {
|
112 |
center = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
113 |
center.setX(event.getXs()[0].doubleValue());
|
114 |
center.setY(event.getYs()[0].intValue());
|
115 |
|
116 |
Point point2;
|
117 |
point2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
118 |
point2.setX(event.getXs()[1].doubleValue());
|
119 |
point2.setY(event.getYs()[1].intValue());
|
120 |
|
121 |
double radious = center.distance(point2);
|
122 |
|
123 |
Circle circle = null;
|
124 |
circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
125 |
circle.setPoints(center, radious); |
126 |
geom = circle; |
127 |
} catch (CreateGeometryException e) {
|
128 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
129 |
"Failed_creating_geometry"),
|
130 |
e); |
131 |
} catch (GeometryOperationNotSupportedException e) {
|
132 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
133 |
"Operation_not_supported"),
|
134 |
e); |
135 |
} catch (GeometryOperationException e) {
|
136 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
137 |
"Failed_performing_the_operation"),
|
138 |
e); |
139 |
} |
140 |
if (geom == null) |
141 |
return;
|
142 |
|
143 |
double flatness;
|
144 |
|
145 |
// Creates the geometry
|
146 |
// If the scale is < 500 -> approximates the circle with a polyline
|
147 |
// with more points, as many as
|
148 |
// smaller would be the scale
|
149 |
if (mapCtrl.getMapContext().getScaleView() < 500) { |
150 |
GeneralPathX gP = new GeneralPathX();
|
151 |
flatness = manager.getFlatness(); // ?????
|
152 |
flatness = |
153 |
mapCtrl.getMapContext().getScaleView() * flatness |
154 |
/ (500 * 2); // The number 2 forces to create the double |
155 |
// of points
|
156 |
gP.append(geom.getPathIterator(null, flatness), true); |
157 |
try {
|
158 |
geom = manager.createSurface(gP, SUBTYPES.GEOM2D); |
159 |
} catch (CreateGeometryException e) {
|
160 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
161 |
"Failed_creating_geometry"),
|
162 |
e); |
163 |
} |
164 |
} else {
|
165 |
// Bigger scale -> Smaller flatness
|
166 |
GeneralPathX gP = new GeneralPathX();
|
167 |
flatness = manager.getFlatness(); // ?????
|
168 |
flatness = |
169 |
flatness / (mapCtrl.getMapContext().getScaleView() * 2);
|
170 |
// *2 to reduce the number of lines of the polygon
|
171 |
|
172 |
gP.append(geom.getPathIterator(null, flatness), true); |
173 |
try {
|
174 |
geom = manager.createSurface(gP, SUBTYPES.GEOM2D); |
175 |
} catch (CreateGeometryException e) {
|
176 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
177 |
"Failed_creating_geometry"),
|
178 |
e); |
179 |
} |
180 |
} |
181 |
|
182 |
for (int i = 0; i < activeLayers.length; i++) { |
183 |
layer = activeLayers[i]; |
184 |
|
185 |
if ((layer.isAvailable()) && (layer instanceof FLyrVect)) { |
186 |
lyrVect = (FLyrVect) layer; |
187 |
FeatureSet newSelection = null;
|
188 |
|
189 |
try {
|
190 |
newSelection = |
191 |
lyrVect.queryByGeometry(geom, |
192 |
lyrVect.getFeatureStore() |
193 |
.getDefaultFeatureType()); |
194 |
|
195 |
if (event.getEvent().isControlDown()) {
|
196 |
((FeatureSelection) lyrVect.getDataStore() |
197 |
.getSelection()).select(newSelection); |
198 |
} else {
|
199 |
lyrVect.getFeatureStore() |
200 |
.setSelection(newSelection); |
201 |
} |
202 |
|
203 |
} catch (DataException e) {
|
204 |
NotificationManager.showMessageError(PluginServices.getText(null,
|
205 |
"Failed_selecting_geometries"),
|
206 |
e); |
207 |
} finally {
|
208 |
newSelection.dispose(); |
209 |
} |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
/*
|
216 |
* (non-Javadoc)
|
217 |
*
|
218 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
219 |
*/
|
220 |
public Cursor getCursor() { |
221 |
if (cur == null) { |
222 |
cur = Toolkit.getDefaultToolkit().createCustomCursor(
|
223 |
this.getImageCursor(), new java.awt.Point(16, 16), ""); |
224 |
} |
225 |
return cur;
|
226 |
} |
227 |
|
228 |
/*
|
229 |
* (non-Javadoc)
|
230 |
*
|
231 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
232 |
*/
|
233 |
public boolean cancelDrawing() { |
234 |
return false; |
235 |
} |
236 |
|
237 |
public Image getImageCursor() { |
238 |
return IconThemeHelper.getImage("cursor-select-by-circle"); |
239 |
} |
240 |
} |