Statistics
| Revision:

root / trunk / extensions / extSelectionTools / src / org / gvsig / selectionTools / tools / CircleSelectionListener.java @ 27833

History | View | Annotate | Download (5.61 KB)

1
package org.gvsig.selectionTools.tools;
2

    
3

    
4
/* gvSIG. Geographic Information System of the Valencian Government
5
 *
6
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
7
 * of the Valencian Government (CIT)
8
 * 
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 * 
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *  
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
22
 * MA  02110-1301, USA.
23
 * 
24
 */
25

    
26
import java.awt.Cursor;
27
import java.awt.Image;
28
import java.awt.Point;
29
import java.awt.Toolkit;
30
import java.awt.event.MouseEvent;
31
import java.awt.geom.Point2D;
32

    
33
import javax.swing.ImageIcon;
34

    
35
import com.iver.andami.PluginServices;
36
import com.iver.andami.messages.NotificationManager;
37
import com.iver.cit.gvsig.fmap.MapControl;
38
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
39
import com.iver.cit.gvsig.fmap.core.IGeometry;
40
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
41
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
42
import com.iver.cit.gvsig.fmap.layers.FBitSet;
43
import com.iver.cit.gvsig.fmap.layers.FLayer;
44
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
45
import com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy;
46
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
47
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
48
import com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener;
49

    
50

    
51
/**
52
 * <p>Listener that selects all features of the active, available and vector layers which intersect with the defined
53
 *  circle area in the associated {@link MapControl MapControl} object.</p>
54
 * 
55
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
56
 */
57
public class CircleSelectionListener implements CircleListener {
58
        /**
59
         * The image to display when the cursor is active.
60
         */
61
        private final Image img = new ImageIcon(this.getClass().getResource("images/circle-cursor-icon.png")).getImage(); 
62

    
63
        /**
64
         * The cursor used to work with this tool listener.
65
         * 
66
         * @see #getCursor()
67
         */
68
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(16, 16), "");
69

    
70
        /**
71
         * Reference to the <code>MapControl</code> object that uses.
72
         */
73
        private MapControl mapCtrl;
74

    
75
        /**
76
         * <p>Creates a new listener for selecting circular areas.</p>
77
         *
78
         * @param mc the <code>MapControl</code> object where the measures are made 
79
         */
80
        public CircleSelectionListener(MapControl mc) {
81
                this.mapCtrl = mc;
82
        }
83

    
84
        /*
85
         * (non-Javadoc)
86
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.CircleListener#circle(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
87
         */
88
        public void circle(MeasureEvent event) throws BehaviorException {
89
                if (event.getEvent().getID() == MouseEvent.MOUSE_RELEASED) {
90
                        FLayer[] activeLayers = mapCtrl.getMapContext().getLayers().getActives();
91
        
92
                        FLayer layer;
93
                        FLyrVect lyrVect;
94
        
95
                        Point2D.Double center = new Point2D.Double(event.getXs()[0].doubleValue(), event.getYs()[0].doubleValue());
96
                        Point2D.Double point2 = new Point2D.Double(event.getXs()[1].doubleValue(), event.getYs()[1].doubleValue());
97

    
98
                        double radius = center.distance(point2);
99
                        IGeometry geom = ShapeFactory.createCircle(center, radius);
100

    
101
                        double flatness;
102

    
103
                        // Creates the geometry
104
                        // If the scale is < 500 -> approximates the circle with a polyline with more points, as many as
105
                        // smaller would be the scale
106
                        if (mapCtrl.getMapContext().getScaleView() < 500) {
107
                                GeneralPathX gP = new GeneralPathX();
108
                                flatness = mapCtrl.getMapContext().getScaleView() * FConverter.FLATNESS / (500 * 2); // The number 2 forces to create the double of points 
109
                                gP.append(geom.getPathIterator(null, flatness), true);
110
                                geom = ShapeFactory.createPolygon2D(gP);
111
                        }
112
                        else {
113
                                // Bigger scale -> Smaller flatness
114
                                GeneralPathX gP = new GeneralPathX();
115
                                flatness = FConverter.FLATNESS / (mapCtrl.getMapContext().getScaleView() * 2); // *2 to reduce the number of lines of the polygon
116
                                gP.append(geom.getPathIterator(null, flatness), true);
117
                                geom = ShapeFactory.createPolygon2D(gP);
118
                        }
119

    
120
                FBitSet newBitSet, oldBitSet;
121

    
122
                        for (int i = 0; i < activeLayers.length; i++) {
123
                                layer = activeLayers[i];
124

    
125
                                if ((layer.isAvailable()) && (layer instanceof FLyrVect)) {
126
                                        lyrVect = (FLyrVect) layer;
127

    
128
                                        try {
129
                                                oldBitSet = lyrVect.getSource().getRecordset().getSelection();
130

    
131
                                                newBitSet = lyrVect.queryByShape(geom, DefaultStrategy.INTERSECTS);
132

    
133
                                if (event.getEvent().isControlDown())
134
                                    newBitSet.xor(oldBitSet);
135

    
136
                                lyrVect.getRecordset().setSelection(newBitSet);
137
                                        } catch (com.vividsolutions.jts.geom.TopologyException topEx) {
138
                                                NotificationManager.showMessageError(PluginServices.getText(null, "Failed_selecting_geometries_by_circle_topology_exception_explanation"), topEx);
139
                                        } catch (Exception ex) {
140
                                                NotificationManager.showMessageError(PluginServices.getText(null, "Failed_selecting_geometries"), ex);
141
                                        }
142
                                }
143
                        }
144
                }
145
        }
146

    
147
        /*
148
         * (non-Javadoc)
149
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
150
         */
151
        public Cursor getCursor() {
152
                return cur;
153
        }
154

    
155
        /*
156
         * (non-Javadoc)
157
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
158
         */
159
        public boolean cancelDrawing() {
160
                return false;
161
        }
162
}