Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2058 / extensions / org.gvsig.selectiontools.app / org.gvsig.selectiontools.app.extension / src / main / java / org / gvsig / selectiontools / app / extension / SelectByBufferExtension.java @ 39267

History | View | Annotate | Download (6.98 KB)

1 33404 fdiaz
package org.gvsig.selectiontools.app.extension;
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 38564 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
30 33404 fdiaz
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.plugins.Extension;
32
import org.gvsig.andami.ui.mdiManager.IWindow;
33
import org.gvsig.app.project.documents.view.ViewDocument;
34
import org.gvsig.app.project.documents.view.gui.IView;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.mapcontext.MapContext;
37
import org.gvsig.fmap.mapcontext.layers.FLayer;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39
import org.gvsig.selectiontools.app.extension.tools.buffer.gui.BufferConfigurationPanel;
40
41
/**
42
 * <p>
43
 * Extension to add support for selecting the geometries of the active vector
44
 * layers that intersect with a buffer around their previously selected
45
 * geometries.
46
 * </p>
47
 *
48
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
49
 */
50
public class SelectByBufferExtension extends Extension {
51
52
    public static final String BUFFER_SELECTION_TOOL_NAME = "bufferSelection";
53
54
    /*
55
     * @see com.iver.andami.plugins.IExtension#initialize()
56
     */
57
    public void initialize() {
58 38564 jjdelcerro
            IconThemeHelper.registerIcon("action", "selection-select-by-buffer", this);
59
            IconThemeHelper.registerIcon("cursor", "cursor-select-by-buffer", this);
60 33404 fdiaz
    }
61
62
    /*
63
     * (non-Javadoc)
64
     *
65
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
66
     */
67
    public void execute(String actionCommand) {
68
        if (actionCommand.equals("SELBUFFER")) {
69
            IWindow win = PluginServices.getMDIManager().getActiveWindow();
70
71
            if (win instanceof IView) {
72
                IView view = (IView) win;
73
                ViewDocument model = view.getViewDocument();
74
75
                /*
76
                 * Unavaliable tool with views in geographic projections
77
                 */
78
                if (!view.getMapControl().getProjection().isProjected()) {
79
                    JOptionPane.showMessageDialog(null,
80
                        PluginServices.getText(null,
81
                            "Tool_unavaliable_with_view_in_geographic_projection"),
82
                        PluginServices.getText(this, "Warning"),
83
                        JOptionPane.ERROR_MESSAGE);
84
                    return;
85
                }
86
87
                MapContext mapContext = model.getMapContext();
88
89
                // If there is at least one active vector layer that has
90
                // geometries selected -> can use this tool, otherwise notifies
91
                // the
92
                // limitation in a JOptionPane
93
                FLayer layers[] = mapContext.getLayers().getActives();
94
                FLayer layer;
95
                ArrayList<FLyrVect> usefulLayers = new ArrayList<FLyrVect>();
96
                int emptySelectionLayers = 0;
97
98
                for (int i = 0; i < layers.length; i++) {
99
                    layer = layers[i];
100
101
                    if ((layer instanceof FLyrVect) && (layer.isAvailable())
102
                        && (layer.isActive())) {
103
                        usefulLayers.add((FLyrVect) layer);
104
                        try {
105
                            if (((FLyrVect) layer).getFeatureStore()
106
                                .getFeatureSelection()
107
                                .isEmpty()) {
108
                                emptySelectionLayers++;
109
                            }
110
                        } catch (DataException e) {
111
                            JOptionPane.showMessageDialog(null,
112
                                PluginServices.getText(null,
113
                                    "Failed_selecting_layer")
114
                                    + ": "
115
                                    + layer.getName(),
116
                                PluginServices.getText(null, "Warning"),
117
                                JOptionPane.WARNING_MESSAGE);
118
                        }
119
                    }
120
                }
121
122
                if (usefulLayers.size() == 0
123
                    || emptySelectionLayers == usefulLayers.size()) {
124
                    JOptionPane.showMessageDialog(null,
125
                        PluginServices.getText(null,
126
                            "There_are_no_geometries_selected"),
127
                        PluginServices.getText(null, "Warning"),
128
                        JOptionPane.WARNING_MESSAGE);
129
130
                    return;
131
                }
132
133
                // Creates and displays the configuration panel
134
                PluginServices.getMDIManager()
135
                    .addWindow(new BufferConfigurationPanel((FLyrVect[]) usefulLayers.toArray(new FLyrVect[0]),
136
                        view));
137
            }
138
        }
139
    }
140
141
    /*
142
     * @see com.iver.andami.plugins.IExtension#isVisible()
143
     */
144
    public boolean isVisible() {
145
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
146
147
        if (f == null) {
148
            return false;
149
        }
150
151
        if (f instanceof IView) {
152
            IView vista = (IView) f;
153
            ViewDocument model = vista.getViewDocument();
154
            MapContext mapa = model.getMapContext();
155
156
            return mapa.getLayers().getLayersCount() > 0;
157
        }
158
159
        return false;
160
    }
161
162
    /*
163
     * @see com.iver.andami.plugins.IExtension#isEnabled()
164
     */
165
    public boolean isEnabled() {
166
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
167
168
        if (f == null) {
169
            return false;
170
        }
171
172
        if (f instanceof IView) {
173
            IView vista = (IView) f;
174
            ViewDocument model = vista.getViewDocument();
175
176
            /*
177
             * Unavaliable tool with views in geographic projections
178
             */
179
            if (!(vista.getMapControl().getProjection().isProjected())) {
180
                return false;
181
            }
182
183
            MapContext mapa = model.getMapContext();
184
185
            FLayer layers[] = mapa.getLayers().getActives();
186
            FLayer layer;
187
188
            for (int i = 0; i < layers.length; i++) {
189
                layer = layers[i];
190
191
                if ((layer instanceof FLyrVect) && (layer.isAvailable())
192
                    && (layer.isActive()))
193
                    return true;
194
            }
195
        }
196
197
        return false;
198
    }
199
}