Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / RectangleSelectionListener.java @ 41121

History | View | Annotate | Download (4.1 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.fmap.mapcontrol.tools;
25

    
26
import java.awt.Image;
27

    
28
import org.gvsig.fmap.IconThemeHelper;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.FeatureSelection;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.geom.primitive.Envelope;
33
import org.gvsig.fmap.mapcontext.layers.FLayer;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.fmap.mapcontrol.MapControl;
36
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent;
37
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener;
38
import org.gvsig.tools.dispose.DisposeUtils;
39

    
40
/**
41
 * <p>Listener that selects all features of the active and vector layers which intersect with the defined
42
 *  rectangle area in the associated {@link MapControl MapControl} object.</p>
43
 *
44
 * @author Vicente Caballero Navarro
45
 */
46
public class RectangleSelectionListener implements RectangleListener {
47
        /**
48
         * The image to display when the cursor is active.
49
         */
50
//        private final Image img = PluginServices.getIconTheme().get("cursor-select-by-polygon").getImage();
51

    
52
        /**
53
         * Reference to the <code>MapControl</code> object that uses.
54
         */
55
        private MapControl mapCtrl;
56

    
57
        /**
58
           * <p>Creates a new <code>RectangleSelectionListener</code> object.</p>
59
         *
60
         * @param mc the <code>MapControl</code> where is defined the rectangle
61
         */
62
        public RectangleSelectionListener(MapControl mc) {
63
                this.mapCtrl = mc;
64
        }
65

    
66
        /*
67
         * (non-Javadoc)
68
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
69
         */
70
        public void rectangle(EnvelopeEvent event) throws BehaviorException {
71
                try {
72
                        // mapCtrl.getMapContext().selectByRect(event.getWorldCoordRect());
73
            Envelope rect = event.getWorldCoordRect();
74
            FLayer[] actives = mapCtrl.getMapContext()
75
                .getLayers().getActives();
76
            for (int i=0; i < actives.length; i++){
77
                if (actives[i] instanceof FLyrVect) {
78
                    FLyrVect lyrVect = (FLyrVect) actives[i];
79
                    FeatureSelection oldSelection = (FeatureSelection) lyrVect.getFeatureStore().getSelection();
80
                                        FeatureSet newSelection = null;
81
                                        try {
82
                                                newSelection =
83
                                                                lyrVect.queryByEnvelope(rect,
84
                                                                                lyrVect.getFeatureStore()
85
                                                                                                .getDefaultFeatureType());
86
                                                if (event.getEvent().isControlDown()) {
87
                                                        oldSelection.select(newSelection);
88
                                                } else {
89
                                                        lyrVect.getDataStore().setSelection(newSelection);
90
                                                }
91
                                        } finally {
92
                                                DisposeUtils.dispose(newSelection);
93
                    }
94
                }
95
            }
96

    
97
                } catch (DataException e) {
98
                        throw new BehaviorException("No se pudo hacer la selecci?n", e);
99
                }
100
        }
101

    
102
        /*
103
         * (non-Javadoc)
104
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
105
         */
106
        public Image getImageCursor() {
107
                return IconThemeHelper.getImage("cursor-select-by-rectangle");
108
        }
109

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