Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.extension / src / main / java / org / gvsig / selectiontools / app / extension / tools / CircleSelectionListener.java @ 38564

History | View | Annotate | Download (8.87 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
     * The image to display when the cursor is active.
69
     */
70
//    private final Image img = new ImageIcon(this.getClass()
71
//        .getClassLoader()
72
//        .getResource("images/circle-cursor-icon.png")).getImage();
73

    
74
    /**
75
     * The cursor used to work with this tool listener.
76
     * 
77
     * @see #getCursor()
78
     */
79
    private Cursor cur = null ; 
80

    
81
    /**
82
     * Reference to the <code>MapControl</code> object that uses.
83
     */
84
    private MapControl mapCtrl;
85

    
86
    /**
87
     * <p>
88
     * Creates a new listener for selecting circular areas.
89
     * </p>
90
     * 
91
     * @param mc
92
     *            the <code>MapControl</code> object where the measures are made
93
     */
94
    public CircleSelectionListener(MapControl mc) {
95
        this.mapCtrl = mc;
96
    }
97

    
98
    /*
99
     * (non-Javadoc)
100
     * 
101
     * @see
102
     * com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener#circle(com.iver
103
     * .cit.gvsig.fmap.tools.Events.MeasureEvent)
104
     */
105
    public void circle(MeasureEvent event) throws BehaviorException {
106
        if (event.getEvent().getID() == MouseEvent.MOUSE_RELEASED) {
107
            FLayer[] activeLayers =
108
                mapCtrl.getMapContext().getLayers().getActives();
109

    
110
            FLayer layer;
111
            FLyrVect lyrVect;
112

    
113
            Geometry geom = null;
114
            GeometryManager manager = GeometryLocator.getGeometryManager();
115
            Point center;
116
            try {
117
                center = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
118
                center.setX(event.getXs()[0].doubleValue());
119
                center.setY(event.getYs()[0].intValue());
120

    
121
                Point point2;
122
                point2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
123
                point2.setX(event.getXs()[1].doubleValue());
124
                point2.setY(event.getYs()[1].intValue());
125

    
126
                double radious = center.distance(point2);
127

    
128
                Circle circle = null;
129
                circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
130
                circle.setPoints(center, radious);
131
                geom = circle;
132
            } catch (CreateGeometryException e) {
133
                NotificationManager.showMessageError(PluginServices.getText(null,
134
                "Failed_creating_geometry"),
135
                e);
136
            } catch (GeometryOperationNotSupportedException e) {
137
                NotificationManager.showMessageError(PluginServices.getText(null,
138
                "Operation_not_supported"),
139
                e);
140
            } catch (GeometryOperationException e) {
141
                NotificationManager.showMessageError(PluginServices.getText(null,
142
                "Failed_performing_the_operation"),
143
                e);
144
            }
145
            if (geom == null)
146
                return;
147

    
148
            double flatness;
149

    
150
            // Creates the geometry
151
            // If the scale is < 500 -> approximates the circle with a polyline
152
            // with more points, as many as
153
            // smaller would be the scale
154
            if (mapCtrl.getMapContext().getScaleView() < 500) {
155
                GeneralPathX gP = new GeneralPathX();
156
                flatness = manager.getFlatness(); // ?????
157
                flatness =
158
                    mapCtrl.getMapContext().getScaleView() * flatness
159
                    / (500 * 2); // The number 2 forces to create the double
160
                // of points
161
                gP.append(geom.getPathIterator(null, flatness), true);
162
                try {
163
                    geom = manager.createSurface(gP, SUBTYPES.GEOM2D);
164
                } catch (CreateGeometryException e) {
165
                    NotificationManager.showMessageError(PluginServices.getText(null,
166
                    "Failed_creating_geometry"),
167
                    e);
168
                }
169
            } else {
170
                // Bigger scale -> Smaller flatness
171
                GeneralPathX gP = new GeneralPathX();
172
                flatness = manager.getFlatness(); // ?????
173
                flatness =
174
                    flatness / (mapCtrl.getMapContext().getScaleView() * 2);
175
                // *2 to reduce the number of lines of the polygon
176

    
177
                gP.append(geom.getPathIterator(null, flatness), true);
178
                try {
179
                    geom = manager.createSurface(gP, SUBTYPES.GEOM2D);
180
                } catch (CreateGeometryException e) {
181
                    NotificationManager.showMessageError(PluginServices.getText(null,
182
                    "Failed_creating_geometry"),
183
                    e);
184
                }
185
            }
186

    
187
            for (int i = 0; i < activeLayers.length; i++) {
188
                layer = activeLayers[i];
189

    
190
                if ((layer.isAvailable()) && (layer instanceof FLyrVect)) {
191
                    lyrVect = (FLyrVect) layer;
192
                    FeatureSet newSelection = null;
193

    
194
                    try {
195
                        newSelection =
196
                            lyrVect.queryByGeometry(geom,
197
                                lyrVect.getFeatureStore()
198
                                .getDefaultFeatureType());
199

    
200
                        if (event.getEvent().isControlDown()) {
201
                            ((FeatureSelection) lyrVect.getDataStore()
202
                                .getSelection()).select(newSelection);
203
                        } else {
204
                            lyrVect.getFeatureStore()
205
                            .setSelection(newSelection);
206
                        }
207

    
208
                    } catch (DataException e) {
209
                        NotificationManager.showMessageError(PluginServices.getText(null,
210
                            "Failed_selecting_geometries"),
211
                            e);
212
                    } finally {
213
                        newSelection.dispose();
214
                    }
215
                }
216
            }
217
        }
218
    }
219

    
220
    /*
221
     * (non-Javadoc)
222
     * 
223
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
224
     */
225
        public Cursor getCursor() {
226
                if (cur == null) {
227
                        cur = Toolkit.getDefaultToolkit().createCustomCursor(
228
                                        this.getImageCursor(), new java.awt.Point(16, 16), "");
229
                }
230
                return cur;
231
        }
232

    
233
    /*
234
     * (non-Javadoc)
235
     * 
236
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
237
     */
238
    public boolean cancelDrawing() {
239
        return false;
240
    }
241

    
242
    public Image getImageCursor() {
243
        return IconThemeHelper.getImage("cursor-select-by-circle");
244
    }
245
}