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

History | View | Annotate | Download (7.4 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
 *
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.mapcontext.layers.FLayer;
69
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
70
import org.gvsig.fmap.mapcontrol.MapControl;
71
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
72
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
73
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
74
75
/**
76
 * <p>
77
 * Listener that selects all features of the active and vector layers which
78
 * intersect with the defined polyline in the associated {@link MapControl
79
 * MapControl} object.
80
 * </p>
81
 *
82
 * <p>
83
 * The selection will be produced after user finishes the creation of the
84
 * polyline.
85
 * </p>
86
 *
87
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
88
 */
89
public class PolyLineSelectionListener implements PolylineListener {
90
91
    /**
92
     * The image to display when the cursor is active.
93
     */
94
//    private final Image img = PluginServices.getIconTheme().get("cursor-select-by-polygon").getImage();
95
96
    /**
97
     * The cursor used to work with this tool listener.
98
     *
99
     * @see #getCursor()
100
     */
101
    private Cursor cur = null;
102
103
    /**
104
     * Reference to the <code>MapControl</code> object that uses.
105
     */
106
    private MapControl mapCtrl;
107
108
    /**
109
     * <p>
110
     * Creates a new <code>PolygonSelectionListener</code> object.
111
     * </p>
112
     *
113
     * @param mc
114
     *            the <code>MapControl</code> where is drawn the polyline
115
     */
116
    public PolyLineSelectionListener(MapControl mc) {
117
        this.mapCtrl = mc;
118
    }
119
120
    /*
121
     * (non-Javadoc)
122
     *
123
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
124
     */
125
    public Cursor getCursor() {
126
            if( cur == null ) {
127
                    cur = Toolkit.getDefaultToolkit().createCustomCursor(this.getImageCursor(),
128
                            new Point(16, 16),
129
                            "");
130
            }
131
        return cur;
132
    }
133
134
    /*
135
     * (non-Javadoc)
136
     *
137
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
138
     */
139
    public boolean cancelDrawing() {
140
        return false;
141
    }
142
143
    /*
144
     * (non-Javadoc)
145
     *
146
     * @see
147
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver
148
     * .cit.gvsig.fmap.tools.Events.MeasureEvent)
149
     */
150
    public void points(MeasureEvent event) throws BehaviorException {
151
    }
152
153
    /*
154
     * (non-Javadoc)
155
     *
156
     * @see
157
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com
158
     * .iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
159
     */
160
    public void pointFixed(MeasureEvent event) throws BehaviorException {
161
    }
162
163
    /*
164
     * (non-Javadoc)
165
     *
166
     * @see
167
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished
168
     * (com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
169
     */
170
    public void polylineFinished(MeasureEvent event) throws BehaviorException {
171
        try {
172
            GeneralPathX gp = event.getGP();
173
            GeometryManager manager = GeometryLocator.getGeometryManager();
174
            Geometry geom = null;
175
            Surface surface = null;
176
            try {
177
                surface =
178
                    (Surface) manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
179
                surface.setGeneralPath(gp);
180
                geom = surface;
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
}