Statistics
| Revision:

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

History | View | Annotate | Download (4.39 KB)

1
package org.gvsig.selectionTools;
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 org.gvsig.selectionTools.tools.PolyLineSelectListener;
26

    
27
import com.iver.andami.PluginServices;
28
import com.iver.andami.plugins.Extension;
29
import com.iver.andami.ui.mdiManager.IWindow;
30
import com.iver.cit.gvsig.fmap.MapContext;
31
import com.iver.cit.gvsig.fmap.MapControl;
32
import com.iver.cit.gvsig.fmap.layers.FLayer;
33
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
34
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
35
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior;
36
import com.iver.cit.gvsig.fmap.tools.Behavior.PolylineBehavior;
37
import com.iver.cit.gvsig.project.documents.view.IProjectView;
38
import com.iver.cit.gvsig.project.documents.view.gui.View;
39
import com.iver.cit.gvsig.project.documents.view.toolListeners.StatusBarListener;
40

    
41
/**
42
 * <p>Extension to add support for selecting the geometries of the active vector layers that
43
 *  intersect with a polyline defined by the user.</p>
44
 *
45
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
46
 */
47
public class SelectByPolylineExtension extends Extension {
48
        public static final String POLYLINE_SELECTION_TOOL_NAME = "polylineSelection";
49
        
50
        /*
51
         * @see com.iver.andami.plugins.IExtension#initialize()
52
         */
53
        public void initialize() {
54
                registerIcons();
55
        }
56

    
57
        private void registerIcons() {
58
                PluginServices.getIconTheme().registerDefault(
59
                        "polyline-cursor-icon",
60
                        this.getClass().getClassLoader().getResource("images/polyline-cursor-icon.png")
61
                );
62
                
63
                PluginServices.getIconTheme().registerDefault(
64
                        "select-by-polyline-icon",
65
                        this.getClass().getClassLoader().getResource("images/select-by-polyline-icon.png")
66
                );
67
        }
68

    
69
        /*
70
         * (non-Javadoc)
71
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
72
         */
73
        public void execute(String actionCommand) {
74
                if (actionCommand.equals("SELPOLYLINE") ) {
75
                        IWindow view = PluginServices.getMDIManager().getActiveWindow();
76
                        if (view instanceof View) {
77
                                // Selection by polyline
78
                                MapControl mc = ((View)view).getMapControl();
79

    
80
                                // If current's view MapControl doesn't have the "CircleSelection" tool, adds it
81
                                if (!mc.getNamesMapTools().containsKey(POLYLINE_SELECTION_TOOL_NAME))        {
82
                                        PolyLineSelectListener polylineSelListener = new PolyLineSelectListener(mc);
83
                                        mc.addMapTool(POLYLINE_SELECTION_TOOL_NAME, new Behavior[] {new PolylineBehavior(polylineSelListener), new MouseMovementBehavior(new StatusBarListener(mc))});
84
                                }
85

    
86
                                mc.setTool(POLYLINE_SELECTION_TOOL_NAME);
87
                        }
88
                }
89
        }
90

    
91
        /*
92
         * @see com.iver.andami.plugins.IExtension#isVisible()
93
         */
94
        public boolean isVisible() {
95
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
96

    
97
                if (f == null) {
98
                        return false;
99
                }
100

    
101
                if (f instanceof View) {
102
                        View vista = (View) f;
103
                        IProjectView model = vista.getModel();
104
                        MapContext mapa = model.getMapContext();
105

    
106
                        return mapa.getLayers().getLayersCount() > 0;
107
                }
108

    
109
                return false;
110
        }
111

    
112
        /*
113
         * @see com.iver.andami.plugins.IExtension#isEnabled()
114
         */
115
        public boolean isEnabled() {
116
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
117

    
118
                if (f == null) {
119
                        return false;
120
                }
121

    
122
                if (f instanceof View) {
123
                        View vista = (View) f;
124
                        IProjectView model = vista.getModel();
125
                        MapContext mapa = model.getMapContext();
126

    
127
                        FLayer layers[] = mapa.getLayers().getActives();
128
                        FLayer layer;
129

    
130
                        for (int i = 0; i < layers.length; i++) {
131
                                layer = layers[i];
132
                                
133
                                if ((layer instanceof FLyrVect) && (layer.isAvailable()) && (layer.isActive()))
134
                                        return true;
135
                        }
136
                }
137

    
138
                return false;
139
        }
140
}