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 / SelectByBufferExtension.java @ 40556

History | View | Annotate | Download (7.92 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.selectiontools.app.extension;
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.util.ArrayList;
49

    
50
import javax.swing.JOptionPane;
51

    
52
import org.gvsig.andami.IconThemeHelper;
53
import org.gvsig.andami.PluginServices;
54
import org.gvsig.andami.plugins.Extension;
55
import org.gvsig.andami.ui.mdiManager.IWindow;
56
import org.gvsig.app.project.documents.view.ViewDocument;
57
import org.gvsig.app.project.documents.view.gui.IView;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.mapcontext.MapContext;
60
import org.gvsig.fmap.mapcontext.layers.FLayer;
61
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
62
import org.gvsig.selectiontools.app.extension.tools.buffer.gui.BufferConfigurationPanel;
63

    
64
/**
65
 * <p>
66
 * Extension to add support for selecting the geometries of the active vector
67
 * layers that intersect with a buffer around their previously selected
68
 * geometries.
69
 * </p>
70
 * 
71
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
72
 */
73
public class SelectByBufferExtension extends Extension {
74

    
75
    public static final String BUFFER_SELECTION_TOOL_NAME = "bufferSelection";
76

    
77
    /*
78
     * @see com.iver.andami.plugins.IExtension#initialize()
79
     */
80
    public void initialize() {
81
            IconThemeHelper.registerIcon("action", "selection-select-by-buffer", this);
82
            IconThemeHelper.registerIcon("cursor", "cursor-select-by-buffer", this);
83
    }
84

    
85
    /*
86
     * (non-Javadoc)
87
     * 
88
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
89
     */
90
    public void execute(String actionCommand) {
91
        if (actionCommand.equals("SELBUFFER")) {
92
            IWindow win = PluginServices.getMDIManager().getActiveWindow();
93

    
94
            if (win instanceof IView) {
95
                IView view = (IView) win;
96
                ViewDocument model = view.getViewDocument();
97

    
98
                /*
99
                 * Unavaliable tool with views in geographic projections
100
                 */
101
                if (!view.getMapControl().getProjection().isProjected()) {
102
                    JOptionPane.showMessageDialog(null,
103
                        PluginServices.getText(null,
104
                            "Tool_unavaliable_with_view_in_geographic_projection"),
105
                        PluginServices.getText(this, "Warning"),
106
                        JOptionPane.ERROR_MESSAGE);
107
                    return;
108
                }
109

    
110
                MapContext mapContext = model.getMapContext();
111

    
112
                // If there is at least one active vector layer that has
113
                // geometries selected -> can use this tool, otherwise notifies
114
                // the
115
                // limitation in a JOptionPane
116
                FLayer layers[] = mapContext.getLayers().getActives();
117
                FLayer layer;
118
                ArrayList<FLyrVect> usefulLayers = new ArrayList<FLyrVect>();
119
                int emptySelectionLayers = 0;
120

    
121
                for (int i = 0; i < layers.length; i++) {
122
                    layer = layers[i];
123

    
124
                    if ((layer instanceof FLyrVect) && (layer.isAvailable())
125
                        && (layer.isActive())) {
126
                        usefulLayers.add((FLyrVect) layer);
127
                        try {
128
                            if (((FLyrVect) layer).getFeatureStore()
129
                                .getFeatureSelection()
130
                                .isEmpty()) {
131
                                emptySelectionLayers++;
132
                            }
133
                        } catch (DataException e) {
134
                            JOptionPane.showMessageDialog(null,
135
                                PluginServices.getText(null,
136
                                    "Failed_selecting_layer")
137
                                    + ": "
138
                                    + layer.getName(),
139
                                PluginServices.getText(null, "Warning"),
140
                                JOptionPane.WARNING_MESSAGE);
141
                        }
142
                    }
143
                }
144

    
145
                if (usefulLayers.size() == 0
146
                    || emptySelectionLayers == usefulLayers.size()) {
147
                    JOptionPane.showMessageDialog(null,
148
                        PluginServices.getText(null,
149
                            "There_are_no_geometries_selected"),
150
                        PluginServices.getText(null, "Warning"),
151
                        JOptionPane.WARNING_MESSAGE);
152

    
153
                    return;
154
                }
155

    
156
                // Creates and displays the configuration panel
157
                PluginServices.getMDIManager()
158
                    .addWindow(new BufferConfigurationPanel((FLyrVect[]) usefulLayers.toArray(new FLyrVect[0]),
159
                        view));
160
            }
161
        }
162
    }
163

    
164
    /*
165
     * @see com.iver.andami.plugins.IExtension#isVisible()
166
     */
167
    public boolean isVisible() {
168
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
169

    
170
        if (f == null) {
171
            return false;
172
        }
173

    
174
        if (f instanceof IView) {
175
            IView vista = (IView) f;
176
            ViewDocument model = vista.getViewDocument();
177
            MapContext mapa = model.getMapContext();
178

    
179
            return mapa.getLayers().getLayersCount() > 0;
180
        }
181

    
182
        return false;
183
    }
184

    
185
    /*
186
     * @see com.iver.andami.plugins.IExtension#isEnabled()
187
     */
188
    public boolean isEnabled() {
189
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
190

    
191
        if (f == null) {
192
            return false;
193
        }
194

    
195
        if (f instanceof IView) {
196
            IView vista = (IView) f;
197
            ViewDocument model = vista.getViewDocument();
198

    
199
            /*
200
             * Unavaliable tool with views in geographic projections
201
             */
202
            if (!(vista.getMapControl().getProjection().isProjected())) {
203
                return false;
204
            }
205

    
206
            MapContext mapa = model.getMapContext();
207

    
208
            FLayer layers[] = mapa.getLayers().getActives();
209
            FLayer layer;
210

    
211
            for (int i = 0; i < layers.length; i++) {
212
                layer = layers[i];
213

    
214
                if ((layer instanceof FLyrVect) && (layer.isAvailable())
215
                    && (layer.isActive()))
216
                    return true;
217
            }
218
        }
219

    
220
        return false;
221
    }
222
}