Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.mainplugin / src / main / java / org / gvsig / selectiontools / app / extension / tools / CircleSelectionListener.java @ 43510

History | View | Annotate | Download (9.57 KB)

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