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 / PolyLineSelectionListener.java @ 43938

History | View | Annotate | Download (7.41 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.Point;
51
import java.awt.Toolkit;
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.feature.FeatureSelection;
59
import org.gvsig.fmap.dal.feature.FeatureSet;
60
import org.gvsig.fmap.geom.Geometry;
61
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
62
import org.gvsig.fmap.geom.Geometry.TYPES;
63
import org.gvsig.fmap.geom.GeometryLocator;
64
import org.gvsig.fmap.geom.GeometryManager;
65
import org.gvsig.fmap.geom.exception.CreateGeometryException;
66
import org.gvsig.fmap.geom.primitive.GeneralPathX;
67
import org.gvsig.fmap.geom.primitive.Surface;
68
import org.gvsig.fmap.geom.primitive.Line;
69
import org.gvsig.fmap.mapcontext.layers.FLayer;
70
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
71
import org.gvsig.fmap.mapcontrol.MapControl;
72
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
73
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
74
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
75

    
76
/**
77
 * <p>
78
 * Listener that selects all features of the active and vector layers which
79
 * intersect with the defined polyline in the associated {@link MapControl
80
 * MapControl} object.
81
 * </p>
82
 * 
83
 * <p>
84
 * The selection will be produced after user finishes the creation of the
85
 * polyline.
86
 * </p>
87
 * 
88
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
89
 */
90
public class PolyLineSelectionListener implements PolylineListener {
91

    
92
    /**
93
     * The image to display when the cursor is active.
94
     */
95
//    private final Image img = PluginServices.getIconTheme().get("cursor-select-by-polygon").getImage();
96

    
97
    /**
98
     * The cursor used to work with this tool listener.
99
     * 
100
     * @see #getCursor()
101
     */
102
    private Cursor cur = null;
103

    
104
    /**
105
     * Reference to the <code>MapControl</code> object that uses.
106
     */
107
    private MapControl mapCtrl;
108

    
109
    /**
110
     * <p>
111
     * Creates a new <code>PolygonSelectionListener</code> object.
112
     * </p>
113
     * 
114
     * @param mc
115
     *            the <code>MapControl</code> where is drawn the polyline
116
     */
117
    public PolyLineSelectionListener(MapControl mc) {
118
        this.mapCtrl = mc;
119
    }
120

    
121
    /*
122
     * (non-Javadoc)
123
     * 
124
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
125
     */
126
    public Cursor getCursor() {
127
            if( cur == null ) {
128
                    cur = Toolkit.getDefaultToolkit().createCustomCursor(this.getImageCursor(),
129
                            new Point(16, 16),
130
                            "");
131
            }
132
        return cur;
133
    }
134

    
135
    /*
136
     * (non-Javadoc)
137
     * 
138
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
139
     */
140
    public boolean cancelDrawing() {
141
        return false;
142
    }
143

    
144
    /*
145
     * (non-Javadoc)
146
     * 
147
     * @see
148
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver
149
     * .cit.gvsig.fmap.tools.Events.MeasureEvent)
150
     */
151
    public void points(MeasureEvent event) throws BehaviorException {
152
    }
153

    
154
    /*
155
     * (non-Javadoc)
156
     * 
157
     * @see
158
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com
159
     * .iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
160
     */
161
    public void pointFixed(MeasureEvent event) throws BehaviorException {
162
    }
163

    
164
    /*
165
     * (non-Javadoc)
166
     * 
167
     * @see
168
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished
169
     * (com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
170
     */
171
    public void polylineFinished(MeasureEvent event) throws BehaviorException {
172
        try {
173
            GeneralPathX gp = event.getGP();
174
            GeometryManager manager = GeometryLocator.getGeometryManager();
175
            Geometry geom = null;
176
            Line line = null;
177
            try {
178
                line = (Line) manager.create(TYPES.LINE, SUBTYPES.GEOM2D);
179
                line.setGeneralPath(gp);
180
                geom = line;
181
            } catch (CreateGeometryException e1) {
182
                NotificationManager.showMessageError(PluginServices.getText(null,
183
                    "Failed_creating_geometry"),
184
                    e1);
185
            }
186
            if (geom == null)
187
                return;
188

    
189
            FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
190

    
191
            for (int i = 0; i < actives.length; i++) {
192
                if (actives[i] instanceof FLyrVect) {
193
                    FLyrVect lyrVect = (FLyrVect) actives[i];
194
                    FeatureSet newSelection = null;
195

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

    
200
                    if (event.getEvent().isControlDown()) {
201
                        ((FeatureSelection) lyrVect.getDataStore()
202
                            .getSelection()).select(newSelection);
203
                    } else {
204
                        lyrVect.getFeatureStore().setSelection(newSelection);
205
                    }
206
                }
207
            }
208
        } catch (com.vividsolutions.jts.geom.TopologyException topEx) {
209
            NotificationManager.showMessageError(PluginServices.getText(null,
210
                "Failed_selecting_geometries_by_polyline_topology_exception_explanation"),
211
                topEx);
212
        } catch (Exception ex) {
213
            NotificationManager.showMessageError(PluginServices.getText(null,
214
                "Failed_selecting_geometries"), ex);
215
        }
216
    }
217

    
218
    public Image getImageCursor() {
219
        return IconThemeHelper.getImage("cursor-select-by-polyline");
220
    }
221
}