Statistics
| Revision:

root / trunk / extensions / extSelectionTools / src / org / gvsig / selectionTools / SelectByBufferExtension.java @ 37921

History | View | Annotate | Download (5.56 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 java.util.ArrayList;
26

    
27
import javax.swing.JOptionPane;
28

    
29
import org.gvsig.selectionTools.tools.buffer.gui.BufferConfigurationPanel;
30

    
31
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
32
import com.iver.andami.PluginServices;
33
import com.iver.andami.plugins.Extension;
34
import com.iver.andami.ui.mdiManager.IWindow;
35
import com.iver.cit.gvsig.fmap.MapContext;
36
import com.iver.cit.gvsig.fmap.layers.FLayer;
37
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
38
import com.iver.cit.gvsig.project.documents.view.IProjectView;
39
import com.iver.cit.gvsig.project.documents.view.gui.View;
40

    
41
/**
42
 * <p>Extension to add support for selecting the geometries of the active vector layers that
43
 *  intersect with a buffer around their previously selected geometries.</p>
44
 *
45
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
46
 */
47
public class SelectByBufferExtension extends Extension {
48
        public static final String BUFFER_SELECTION_TOOL_NAME = "bufferSelection";
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
                        "select-by-buffer-icon",
60
                        this.getClass().getClassLoader().getResource("images/select-by-buffer-icon.png")
61
                );
62
        }
63

    
64
        /*
65
         * (non-Javadoc)
66
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
67
         */
68
        public void execute(String actionCommand) {
69
                if (actionCommand.equals("SELBUFFER") ) {
70
                        IWindow view = PluginServices.getMDIManager().getActiveWindow();
71

    
72
                        if (view instanceof View) {
73
                                IProjectView model = ((View)view).getModel();
74

    
75
                                /*
76
                                 * Unavaliable tool with views in geographic projections
77
                                 */
78
                                if (! ((View)view).getMapControl().getProjection().isProjected()) {
79
                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "Tool_unavaliable_with_view_in_geographic_projection"), PluginServices.getText(this, "Warning"), JOptionPane.ERROR_MESSAGE);
80
                                        return;
81
                                }
82

    
83
                                MapContext mapContext = model.getMapContext();
84

    
85
                                // If there is at least one active vector layer that has geometries selected -> can use this tool, otherwise notifies the
86
                                //  limitation in a JOptionPane
87
                                FLayer layers[] = mapContext.getLayers().getActives();
88
                                FLayer layer;
89
                                ArrayList usefulLayers = new ArrayList();
90
                                int emptySelectionLayers = 0;
91

    
92
                                for (int i = 0; i < layers.length; i++) {
93
                                        layer = layers[i];
94

    
95
                                        if ((layer instanceof FLyrVect) && (layer.isAvailable()) && (layer.isActive())) {
96
                                                try {
97
                                                        usefulLayers.add((FLyrVect)layer);
98
                                                        if (((FLyrVect)layer).getSource().getRecordset().getSelection().cardinality() == 0) {
99
                                                                emptySelectionLayers++;
100
                                                        }
101
                                                }
102
                                                catch (ReadDriverException rde) {
103
                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "Failed_selecting_layer") + ": " + layer.getName(), PluginServices.getText(null, "Warning"), JOptionPane.WARNING_MESSAGE);
104
                                                }
105
                                        }
106
                                }
107

    
108
                                if (usefulLayers.size() == 0 || emptySelectionLayers == usefulLayers.size()) {
109
                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "There_are_no_geometries_selected"), PluginServices.getText(null, "Warning"), JOptionPane.WARNING_MESSAGE);
110

    
111
                                        return;
112
                                }
113

    
114
                                // Creates and displays the configuration panel
115
                                PluginServices.getMDIManager().addWindow(new BufferConfigurationPanel((FLyrVect[])usefulLayers.toArray(new FLyrVect[0]), (View)view));
116
                        }
117
                }
118
        }
119

    
120
        /*
121
         * @see com.iver.andami.plugins.IExtension#isVisible()
122
         */
123
        public boolean isVisible() {
124
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
125

    
126
                if (f == null) {
127
                        return false;
128
                }
129

    
130
                if (f instanceof View) {
131
                        View vista = (View) f;
132
                        IProjectView model = vista.getModel();
133
                        if (model == null)
134
                                return false;
135
                        MapContext mapa = model.getMapContext();
136

    
137
                        return mapa.getLayers().getLayersCount() > 0;
138
                }
139

    
140
                return false;
141
        }
142

    
143
        /*
144
         * @see com.iver.andami.plugins.IExtension#isEnabled()
145
         */
146
        public boolean isEnabled() {
147
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
148

    
149
                if (f == null) {
150
                        return false;
151
                }
152

    
153
                if (f instanceof View) {
154
                        View vista = (View) f;
155
                        IProjectView model = vista.getModel();
156

    
157
                        /*
158
                         * Unavaliable tool with views in geographic projections
159
                         */
160
                        if (! (vista.getMapControl().getProjection().isProjected())) {
161
                                return false;
162
                        }
163

    
164
                        MapContext mapa = model.getMapContext();
165

    
166
                        FLayer layers[] = mapa.getLayers().getActives();
167
                        FLayer layer;
168

    
169
                        for (int i = 0; i < layers.length; i++) {
170
                                layer = layers[i];
171

    
172
                                if ((layer instanceof FLyrVect) && (layer.isAvailable()) && (layer.isActive()))
173
                                        return true;
174
                        }
175
                }
176

    
177
                return false;
178
        }
179
}