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 @ 43938
History | View | Annotate | Download (5.86 KB)
1 | 40556 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 41264 | jjdelcerro | * 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 | 40556 | jjdelcerro | *
|
11 | 41264 | jjdelcerro | * 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 | 40556 | jjdelcerro | *
|
16 | 41264 | jjdelcerro | * 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 | 40556 | jjdelcerro | *
|
20 | 41264 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40556 | jjdelcerro | */
|
23 | 40435 | jjdelcerro | package org.gvsig.selectiontools.app.extension; |
24 | |||
25 | import java.util.ArrayList; |
||
26 | 41264 | jjdelcerro | import java.util.List; |
27 | 40435 | jjdelcerro | |
28 | import javax.swing.JOptionPane; |
||
29 | |||
30 | 40938 | jldominguez | import org.slf4j.Logger; |
31 | import org.slf4j.LoggerFactory; |
||
32 | |||
33 | 40435 | jjdelcerro | import org.gvsig.andami.IconThemeHelper; |
34 | import org.gvsig.andami.plugins.Extension; |
||
35 | 41264 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
36 | import org.gvsig.app.ApplicationManager; |
||
37 | 40435 | jjdelcerro | 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 | 41264 | jjdelcerro | private static Logger logger |
53 | = LoggerFactory.getLogger(SelectByBufferExtension.class); |
||
54 | 40435 | jjdelcerro | public static final String BUFFER_SELECTION_TOOL_NAME = "bufferSelection"; |
55 | |||
56 | public void initialize() { |
||
57 | 41264 | jjdelcerro | IconThemeHelper.registerIcon("action", "selection-select-by-buffer", this); |
58 | IconThemeHelper.registerIcon("cursor", "cursor-select-by-buffer", this); |
||
59 | 40435 | jjdelcerro | } |
60 | |||
61 | public void execute(String actionCommand) { |
||
62 | 41264 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
63 | 40435 | jjdelcerro | |
64 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
65 | if (view == null) { |
||
66 | return;
|
||
67 | } |
||
68 | ViewDocument document = view.getViewDocument(); |
||
69 | 40435 | jjdelcerro | |
70 | 41264 | jjdelcerro | if (actionCommand.equals("SELBUFFER")) { |
71 | MapContext mapContext = document.getMapContext(); |
||
72 | 40435 | jjdelcerro | |
73 | 41264 | jjdelcerro | FLayer layers[] = mapContext.getLayers().getActives();
|
74 | FLayer layer; |
||
75 | List<FLyrVect> usefulLayers = new ArrayList<FLyrVect>(); |
||
76 | int emptySelectionLayers = 0; |
||
77 | 40435 | jjdelcerro | |
78 | 41264 | jjdelcerro | 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 | 40435 | jjdelcerro | } |
87 | 41264 | jjdelcerro | } 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 | 40435 | jjdelcerro | } |
94 | } |
||
95 | 41264 | jjdelcerro | } |
96 | 40435 | jjdelcerro | |
97 | 41264 | jjdelcerro | if (usefulLayers.isEmpty() || emptySelectionLayers == usefulLayers.size()) {
|
98 | application.messageDialog( |
||
99 | application.translate("_There_are_no_geometries_selected"),
|
||
100 | application.translate("Warning"),
|
||
101 | 40435 | jjdelcerro | JOptionPane.WARNING_MESSAGE);
|
102 | 41264 | jjdelcerro | return;
|
103 | 40435 | jjdelcerro | } |
104 | 41264 | jjdelcerro | |
105 | // Creates and displays the configuration panel
|
||
106 | application.getUIManager().addWindow( |
||
107 | new BufferConfigurationPanel(usefulLayers, view));
|
||
108 | 40435 | jjdelcerro | } |
109 | 41264 | jjdelcerro | |
110 | 40435 | jjdelcerro | } |
111 | |||
112 | public boolean isVisible() { |
||
113 | 41264 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
114 | |||
115 | 41269 | jjdelcerro | 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 | 40435 | jjdelcerro | } |
123 | |||
124 | public boolean isEnabled() { |
||
125 | 41264 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
126 | 40435 | jjdelcerro | |
127 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
128 | if (view == null) { |
||
129 | 40435 | jjdelcerro | return false; |
130 | } |
||
131 | 41264 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
132 | MapContext mapa = document.getMapContext(); |
||
133 | 40435 | jjdelcerro | |
134 | 41264 | jjdelcerro | 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 | 40938 | jldominguez | } |
144 | 41264 | jjdelcerro | } catch (DataException e) {
|
145 | logger.warn("Error While getting selection for layer '" + layer.getName() + "'.", e); |
||
146 | 40938 | jldominguez | } |
147 | 40435 | jjdelcerro | } |
148 | } |
||
149 | return false; |
||
150 | } |
||
151 | } |