Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.extension / src / main / java / org / gvsig / selectiontools / app / extension / tools / PolyLineSelectionListener.java @ 38564

History | View | Annotate | Download (6.47 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.Point;
28
import java.awt.Toolkit;
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.feature.FeatureSelection;
36
import org.gvsig.fmap.dal.feature.FeatureSet;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
39
import org.gvsig.fmap.geom.Geometry.TYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43
import org.gvsig.fmap.geom.primitive.GeneralPathX;
44
import org.gvsig.fmap.geom.primitive.Surface;
45
import org.gvsig.fmap.mapcontext.layers.FLayer;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.fmap.mapcontrol.MapControl;
48
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
49
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
50
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
51

    
52
/**
53
 * <p>
54
 * Listener that selects all features of the active and vector layers which
55
 * intersect with the defined polyline in the associated {@link MapControl
56
 * MapControl} object.
57
 * </p>
58
 * 
59
 * <p>
60
 * The selection will be produced after user finishes the creation of the
61
 * polyline.
62
 * </p>
63
 * 
64
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
65
 */
66
public class PolyLineSelectionListener implements PolylineListener {
67

    
68
    /**
69
     * The image to display when the cursor is active.
70
     */
71
//    private final Image img = PluginServices.getIconTheme().get("cursor-select-by-polygon").getImage();
72

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

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

    
85
    /**
86
     * <p>
87
     * Creates a new <code>PolygonSelectionListener</code> object.
88
     * </p>
89
     * 
90
     * @param mc
91
     *            the <code>MapControl</code> where is drawn the polyline
92
     */
93
    public PolyLineSelectionListener(MapControl mc) {
94
        this.mapCtrl = mc;
95
    }
96

    
97
    /*
98
     * (non-Javadoc)
99
     * 
100
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
101
     */
102
    public Cursor getCursor() {
103
            if( cur == null ) {
104
                    cur = Toolkit.getDefaultToolkit().createCustomCursor(this.getImageCursor(),
105
                            new Point(16, 16),
106
                            "");
107
            }
108
        return cur;
109
    }
110

    
111
    /*
112
     * (non-Javadoc)
113
     * 
114
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
115
     */
116
    public boolean cancelDrawing() {
117
        return false;
118
    }
119

    
120
    /*
121
     * (non-Javadoc)
122
     * 
123
     * @see
124
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver
125
     * .cit.gvsig.fmap.tools.Events.MeasureEvent)
126
     */
127
    public void points(MeasureEvent event) throws BehaviorException {
128
    }
129

    
130
    /*
131
     * (non-Javadoc)
132
     * 
133
     * @see
134
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com
135
     * .iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
136
     */
137
    public void pointFixed(MeasureEvent event) throws BehaviorException {
138
    }
139

    
140
    /*
141
     * (non-Javadoc)
142
     * 
143
     * @see
144
     * com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished
145
     * (com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
146
     */
147
    public void polylineFinished(MeasureEvent event) throws BehaviorException {
148
        try {
149
            GeneralPathX gp = event.getGP();
150
            GeometryManager manager = GeometryLocator.getGeometryManager();
151
            Geometry geom = null;
152
            Surface surface = null;
153
            try {
154
                surface =
155
                    (Surface) manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
156
                surface.setGeneralPath(gp);
157
                geom = surface;
158
            } catch (CreateGeometryException e1) {
159
                NotificationManager.showMessageError(PluginServices.getText(null,
160
                    "Failed_creating_geometry"),
161
                    e1);
162
            }
163
            if (geom == null)
164
                return;
165

    
166
            FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
167

    
168
            for (int i = 0; i < actives.length; i++) {
169
                if (actives[i] instanceof FLyrVect) {
170
                    FLyrVect lyrVect = (FLyrVect) actives[i];
171
                    FeatureSet newSelection = null;
172

    
173
                    newSelection =
174
                        lyrVect.queryByGeometry(geom, lyrVect.getFeatureStore()
175
                            .getDefaultFeatureType());
176

    
177
                    if (event.getEvent().isControlDown()) {
178
                        ((FeatureSelection) lyrVect.getDataStore()
179
                            .getSelection()).select(newSelection);
180
                    } else {
181
                        lyrVect.getFeatureStore().setSelection(newSelection);
182
                    }
183
                }
184
            }
185
        } catch (com.vividsolutions.jts.geom.TopologyException topEx) {
186
            NotificationManager.showMessageError(PluginServices.getText(null,
187
                "Failed_selecting_geometries_by_polyline_topology_exception_explanation"),
188
                topEx);
189
        } catch (Exception ex) {
190
            NotificationManager.showMessageError(PluginServices.getText(null,
191
                "Failed_selecting_geometries"), ex);
192
        }
193
    }
194

    
195
    public Image getImageCursor() {
196
        return IconThemeHelper.getImage("cursor-select-by-polyline");
197
    }
198
}