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 @ 41269

History | View | Annotate | Download (5.86 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.selectiontools.app.extension;
24

    
25
import java.util.ArrayList;
26
import java.util.List;
27

    
28
import javax.swing.JOptionPane;
29

    
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
import org.gvsig.andami.IconThemeHelper;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.app.ApplicationLocator;
36
import org.gvsig.app.ApplicationManager;
37
import org.gvsig.app.project.documents.view.ViewDocument;
38
import org.gvsig.app.project.documents.view.gui.IView;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.selectiontools.app.extension.tools.buffer.gui.BufferConfigurationPanel;
44

    
45
/**
46
 * Extension to add support for selecting the geometries of the active vector
47
 * layers that intersect with a buffer around their previously selected
48
 * geometries.
49
 */
50
public class SelectByBufferExtension extends Extension {
51

    
52
    private static Logger logger
53
            = LoggerFactory.getLogger(SelectByBufferExtension.class);
54
    public static final String BUFFER_SELECTION_TOOL_NAME = "bufferSelection";
55

    
56
    public void initialize() {
57
        IconThemeHelper.registerIcon("action", "selection-select-by-buffer", this);
58
        IconThemeHelper.registerIcon("cursor", "cursor-select-by-buffer", this);
59
    }
60

    
61
    public void execute(String actionCommand) {
62
        ApplicationManager application = ApplicationLocator.getManager();
63

    
64
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
65
        if (view == null) {
66
            return;
67
        }
68
        ViewDocument document = view.getViewDocument();
69

    
70
        if (actionCommand.equals("SELBUFFER")) {
71
            MapContext mapContext = document.getMapContext();
72

    
73
            FLayer layers[] = mapContext.getLayers().getActives();
74
            FLayer layer;
75
            List<FLyrVect> usefulLayers = new ArrayList<FLyrVect>();
76
            int emptySelectionLayers = 0;
77

    
78
            for (int i = 0; i < layers.length; i++) {
79
                layer = layers[i];
80
                if ((layer instanceof FLyrVect) && layer.isAvailable() && layer.isActive()) {
81
                    FLyrVect layervect = (FLyrVect) layer;
82
                    usefulLayers.add(layervect);
83
                    try {
84
                        if (layervect.getFeatureStore().getFeatureSelection().isEmpty()) {
85
                            emptySelectionLayers++;
86
                        }
87
                    } catch (DataException e) {
88
                        logger.warn("Error While getting selection for layer '" + layer.getName() + "'.", e);
89
                        application.messageDialog(
90
                                application.translate("Failed_selecting_layer") + ": " + layer.getName(),
91
                                application.translate("Warning"),
92
                                JOptionPane.WARNING_MESSAGE);
93
                    }
94
                }
95
            }
96

    
97
            if (usefulLayers.isEmpty() || emptySelectionLayers == usefulLayers.size()) {
98
                application.messageDialog(
99
                        application.translate("_There_are_no_geometries_selected"),
100
                        application.translate("Warning"),
101
                        JOptionPane.WARNING_MESSAGE);
102
                return;
103
            }
104

    
105
            // Creates and displays the configuration panel
106
            application.getUIManager().addWindow(
107
                    new BufferConfigurationPanel(usefulLayers, view));
108
        }
109

    
110
    }
111

    
112
    public boolean isVisible() {
113
        ApplicationManager application = ApplicationLocator.getManager();
114

    
115
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
116
        if (view == null) {
117
            return false;
118
        }
119
        ViewDocument document = view.getViewDocument();
120
        MapContext mapa = document.getMapContext();
121
        return mapa.getLayers().getLayersCount() > 0;
122
    }
123

    
124
    public boolean isEnabled() {
125
        ApplicationManager application = ApplicationLocator.getManager();
126

    
127
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
128
        if (view == null) {
129
            return false;
130
        }
131
        ViewDocument document = view.getViewDocument();
132
        MapContext mapa = document.getMapContext();
133

    
134
        FLayer layers[] = mapa.getLayers().getActives();
135
        FLayer layer;
136
        for (int i = 0; i < layers.length; i++) {
137
            layer = layers[i];
138
            if ((layer instanceof FLyrVect) && layer.isAvailable() && layer.isActive()) {
139
                FLyrVect layervect = (FLyrVect) layer;
140
                try {
141
                    if (!layervect.getFeatureStore().getFeatureSelection().isEmpty()) {
142
                        return true;
143
                    }
144
                } catch (DataException e) {
145
                    logger.warn("Error While getting selection for layer '" + layer.getName() + "'.", e);
146
                }
147
            }
148
        }
149
        return false;
150
    }
151
}