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 @ 44246

History | View | Annotate | Download (9.65 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.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
 *
31
 * 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
 *
36
 * 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
 *
41
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
44
 * MA  02110-1301, USA.
45
 *
46
 */
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.AbstractCircleListener;
78
import org.gvsig.fmap.mapcontrol.tools.Listeners.CircleListener;
79

    
80
/**
81
 * <p>
82
 * Listener that selects all features of the active, available and vector layers
83
 * which intersect with the defined circle area in the associated
84
 * {@link MapControl MapControl} object.
85
 * </p>
86
 *
87
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
88
 */
89
public class CircleSelectionListener extends AbstractCircleListener {
90

    
91

    
92

    
93
    /**
94
     * The cursor used to work with this tool listener.
95
     *
96
     * @see #getCursor()
97
     */
98
    private Cursor cur = null ;
99

    
100
    /**
101
     * Reference to the <code>MapControl</code> object that uses.
102
     */
103
    private MapControl mapCtrl;
104

    
105
    /**
106
     * <p>
107
     * Creates a new listener for selecting circular areas.
108
     * </p>
109
     *
110
     * @param mc
111
     *            the <code>MapControl</code> object where the measures are made
112
     */
113
    public CircleSelectionListener(MapControl mc) {
114
        this.mapCtrl = mc;
115
    }
116

    
117
    /*
118
     * (non-Javadoc)
119
     *
120
     * @see
121
     * com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener#circle(com.iver
122
     * .cit.gvsig.fmap.tools.Events.MeasureEvent)
123
     */
124
    public void circle(MeasureEvent event) throws BehaviorException {
125
        if (event.getEvent().getID() == MouseEvent.MOUSE_RELEASED) {
126
            FLayer[] activeLayers =
127
                mapCtrl.getMapContext().getLayers().getActives();
128

    
129
            FLayer layer;
130
            FLyrVect lyrVect;
131

    
132
            Geometry geom = null;
133
            GeometryManager manager = GeometryLocator.getGeometryManager();
134
            Point center;
135
            try {
136
                center = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
137
                center.setX(event.getXs()[0].doubleValue());
138
                center.setY(event.getYs()[0].doubleValue());
139

    
140
                Point point2;
141
                point2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
142
                point2.setX(event.getXs()[1].doubleValue());
143
                point2.setY(event.getYs()[1].doubleValue());
144

    
145
                double radious = center.distance(point2);
146

    
147
                Circle circle = null;
148
                circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
149
                circle.setPoints(center, radious);
150
                geom = circle;
151
            } catch (CreateGeometryException e) {
152
                NotificationManager.showMessageError(PluginServices.getText(null,
153
                "Failed_creating_geometry"),
154
                e);
155
            } catch (GeometryOperationNotSupportedException e) {
156
                NotificationManager.showMessageError(PluginServices.getText(null,
157
                "Operation_not_supported"),
158
                e);
159
            } catch (GeometryOperationException e) {
160
                NotificationManager.showMessageError(PluginServices.getText(null,
161
                "Failed_performing_the_operation"),
162
                e);
163
            }
164
            if (geom == null)
165
                return;
166

    
167
            double flatness;
168

    
169
            // Creates the geometry
170
            // If the scale is < 500 -> approximates the circle with a polyline
171
            // with more points, as many as
172
            // smaller would be the scale
173
            if (mapCtrl.getMapContext().getScaleView() < 500) {
174
                GeneralPathX gP = new GeneralPathX();
175
                flatness = manager.getFlatness(); // ?????
176
                flatness =
177
                    mapCtrl.getMapContext().getScaleView() * flatness
178
                    / (500 * 2); // The number 2 forces to create the double
179
                // of points
180
                gP.append(geom.getPathIterator(null, flatness), true);
181
                try {
182
                    geom = manager.createSurface(gP, SUBTYPES.GEOM2D);
183
                } catch (CreateGeometryException e) {
184
                    NotificationManager.showMessageError(PluginServices.getText(null,
185
                    "Failed_creating_geometry"),
186
                    e);
187
                }
188
            } else {
189
                // Bigger scale -> Smaller flatness
190
                GeneralPathX gP = new GeneralPathX();
191
                flatness = manager.getFlatness(); // ?????
192
                flatness =
193
                    flatness / (mapCtrl.getMapContext().getScaleView() * 2);
194
                // *2 to reduce the number of lines of the polygon
195

    
196
                gP.append(geom.getPathIterator(null, flatness), true);
197
                try {
198
                    geom = manager.createSurface(gP, SUBTYPES.GEOM2D);
199
                } catch (CreateGeometryException e) {
200
                    NotificationManager.showMessageError(PluginServices.getText(null,
201
                    "Failed_creating_geometry"),
202
                    e);
203
                }
204
            }
205

    
206
            for (int i = 0; i < activeLayers.length; i++) {
207
                layer = activeLayers[i];
208

    
209
                if ((layer.isAvailable()) && (layer instanceof FLyrVect)) {
210
                    lyrVect = (FLyrVect) layer;
211
                    FeatureSet newSelection = null;
212

    
213
                    try {
214
                        newSelection =
215
                            lyrVect.queryByGeometry(geom,
216
                                lyrVect.getFeatureStore()
217
                                .getDefaultFeatureType());
218

    
219
                        if (event.getEvent().isControlDown()) {
220
                            ((FeatureSelection) lyrVect.getDataStore()
221
                                .getSelection()).select(newSelection);
222
                        } else {
223
                            lyrVect.getFeatureStore()
224
                            .setSelection(newSelection);
225
                        }
226

    
227
                    } catch (DataException e) {
228
                        NotificationManager.showMessageError(PluginServices.getText(null,
229
                            "Failed_selecting_geometries"),
230
                            e);
231
                    } finally {
232
                        newSelection.dispose();
233
                    }
234
                }
235
            }
236
        }
237
    }
238

    
239
    /*
240
     * (non-Javadoc)
241
     *
242
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
243
     */
244
        public Cursor getCursor() {
245
                if (cur == null) {
246
                        cur = Toolkit.getDefaultToolkit().createCustomCursor(
247
                                        this.getImageCursor(), new java.awt.Point(16, 16), "");
248
                }
249
                return cur;
250
        }
251

    
252
    /*
253
     * (non-Javadoc)
254
     *
255
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
256
     */
257
    public boolean cancelDrawing() {
258
        return false;
259
    }
260

    
261
    public Image getImageCursor() {
262
        return IconThemeHelper.getImage("cursor-select-by-circle");
263
    }
264
}