Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / ViewSelectionControls.java @ 21743

History | View | Annotate | Download (7.91 KB)

1 14821 jmvivo
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2 6513 jaume
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23 14821 jmvivo
 *   Av. Blasco Ib��ez, 50
24 6513 jaume
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
42
/* CVS MESSAGES:
43 7163 fjp
 *
44
 * $Id$
45
 * $Log$
46 10626 caballero
 * Revision 1.16  2007-03-06 16:34:02  caballero
47
 * Exceptions
48
 *
49
 * Revision 1.15  2007/01/04 07:24:32  caballero
50 9532 caballero
 * isModified
51
 *
52
 * Revision 1.14  2006/12/27 11:08:56  fdiaz
53 9483 fdiaz
 * Arreglado el metodo hasVectorLayers para que no se pare en los grupos de capas cuando estos no tienen capas vectoriales.
54
 *
55
 * Revision 1.13  2006/09/25 16:54:12  caballero
56 7572 caballero
 * invert selection
57
 *
58
 * Revision 1.12  2006/09/20 13:17:40  jaume
59 7407 jaume
 * *** empty log message ***
60
 *
61
 * Revision 1.11  2006/09/20 10:56:56  sbayarri
62 14821 jmvivo
 * Refactorizaci�n clases View y ProjectView
63 7392 sbayarri
 *
64
 * Revision 1.10  2006/09/15 10:40:03  caballero
65 7304 caballero
 * extensibilidad de documentos
66
 *
67
 * Revision 1.9  2006/09/11 16:14:15  fjp
68 7190 fjp
 * Invertir seleccion. Falta subirlo al branch de la 1.0
69
 *
70
 * Revision 1.8  2006/09/11 06:53:13  fjp
71 7163 fjp
 * Antes del parto
72
 *
73
 * Revision 1.7  2006/09/04 16:26:54  fjp
74 14821 jmvivo
 * Seleccionar por pol�gono
75 7163 fjp
 *
76
 * Revision 1.6  2006/08/29 07:56:27  cesar
77
 * Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
78
 *
79
 * Revision 1.5  2006/08/29 07:21:08  cesar
80
 * Rename com.iver.cit.gvsig.fmap.Fmap class to com.iver.cit.gvsig.fmap.MapContext
81
 *
82
 * Revision 1.4  2006/08/29 07:13:53  cesar
83
 * Rename class com.iver.andami.ui.mdiManager.View to com.iver.andami.ui.mdiManager.IWindow
84
 *
85
 * Revision 1.3  2006/08/18 08:40:04  jmvivo
86
 * Actualizado para que el isEnabled tenga en cuenta que las capas esten 'avialable'
87
 *
88
 * Revision 1.2  2006/07/27 06:20:28  jaume
89
 * *** empty log message ***
90
 *
91
 * Revision 1.1  2006/07/26 09:53:05  jaume
92 14821 jmvivo
 * separadas las herramientas de selecci�n del viewControls al ViewSelectionControls
93 7163 fjp
 *
94
 *
95
 */
96 6513 jaume
package com.iver.cit.gvsig;
97
98
import org.apache.log4j.Logger;
99 21367 vcaballero
import org.gvsig.data.ReadException;
100
import org.gvsig.data.vectorial.FeatureCollection;
101
import org.gvsig.data.vectorial.FeatureStore;
102 20994 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
103
import org.gvsig.fmap.mapcontext.layers.FBitSet;
104
import org.gvsig.fmap.mapcontext.layers.FLayer;
105
import org.gvsig.fmap.mapcontext.layers.FLayers;
106
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
107
import org.gvsig.fmap.mapcontrol.MapControl;
108 6513 jaume
109
import com.iver.andami.PluginServices;
110 7163 fjp
import com.iver.andami.messages.NotificationManager;
111 6513 jaume
import com.iver.andami.plugins.Extension;
112 7407 jaume
import com.iver.andami.ui.mdiManager.IWindow;
113 6513 jaume
import com.iver.cit.gvsig.gui.selectionByTheme.DefaultSelectionByThemeModel;
114
import com.iver.cit.gvsig.gui.selectionByTheme.MySelectionByThemeListener;
115
import com.iver.cit.gvsig.gui.selectionByTheme.SelectionByTheme;
116 9532 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
117 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
118 7304 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
119 6513 jaume
120
/**
121 7163 fjp
 * Extension that handles the selection tools, selection tools have sense on
122
 * vectorial layers only.
123 7407 jaume
 *
124 6513 jaume
 * @author jaume dominguez faus - jaume.dominguez@iver.es
125 7407 jaume
 *
126 6513 jaume
 */
127
public class ViewSelectionControls extends Extension {
128 7163 fjp
        private static Logger logger = Logger.getLogger(ViewControls.class
129
                        .getName());
130 6513 jaume
131
        public void initialize() {
132
                // TODO Auto-generated method stub
133 14821 jmvivo
                registerIcons();
134
        }
135 6513 jaume
136 14821 jmvivo
        private void registerIcons(){
137
138 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
139 14821 jmvivo
                                "view-select-geometry",
140
                                this.getClass().getClassLoader().getResource("images/Select.png")
141
                        );
142
143 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
144 14821 jmvivo
                                "view-select-by-rectangle",
145
                                this.getClass().getClassLoader().getResource("images/SelEspacial2b.png")
146
                        );
147
148 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
149 14821 jmvivo
                                "view-select-by-polygon",
150
                                this.getClass().getClassLoader().getResource("images/SelectByPolygon.png")
151
                        );
152
153 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
154 14821 jmvivo
                                "view-select-invert",
155
                                this.getClass().getClassLoader().getResource("images/invertSelection.png")
156
                        );
157 6513 jaume
        }
158
159
        public void execute(String actionCommand) {
160 6880 cesar
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
161 7392 sbayarri
                IProjectView model = vista.getModel();
162 6878 cesar
                MapContext mapa = model.getMapContext();
163 6513 jaume
                MapControl mapCtrl = vista.getMapControl();
164
                logger.debug("Comand : " + actionCommand);
165 7163 fjp
                if (actionCommand.equals("SELRECT")) {
166 6513 jaume
                        mapCtrl.setTool("rectSelection");
167 9532 caballero
                        ((ProjectDocument)vista.getModel()).setModified(true);
168 7163 fjp
                } else if (actionCommand.equals("SELPOINT")) {
169 6513 jaume
                        mapCtrl.setTool("pointSelection");
170 9532 caballero
                        ((ProjectDocument)vista.getModel()).setModified(true);
171 7163 fjp
                } else if (actionCommand.equals("SELPOL")) {
172 7011 fjp
                        mapCtrl.setTool("polSelection");
173 9532 caballero
                        ((ProjectDocument)vista.getModel()).setModified(true);
174 7163 fjp
                } else if (actionCommand.equals("SELECTIONBYSHAPE")) {
175 6513 jaume
                        SelectionByTheme dlg = new SelectionByTheme();
176 7163 fjp
                        // FLayer[] layers = mapa.getLayers().getActives();
177
                        // int count = 0;
178 6513 jaume
                        dlg.setModel(new DefaultSelectionByThemeModel());
179
                        dlg.addSelectionListener(new MySelectionByThemeListener());
180 6880 cesar
                        PluginServices.getMDIManager().addWindow(dlg);
181 9532 caballero
                        ((ProjectDocument)vista.getModel()).setModified(true);
182 7190 fjp
                } else if (actionCommand.equals("INVERT_SELECTION")) {
183 7163 fjp
                        for (int i = 0; i < mapa.getLayers().getActives().length; i++) {
184
                                FLayer lyr = mapa.getLayers().getActives()[i];
185
                                if (lyr.isAvailable() && lyr instanceof FLyrVect) {
186
                                        FLyrVect lyrVect = (FLyrVect) lyr;
187 21367 vcaballero
                                        FeatureStore fs;
188 7163 fjp
                                        try {
189 21367 vcaballero
                                                fs = lyrVect.getFeatureStore();
190
                                                FeatureCollection fCollectionAll=(FeatureCollection)fs.getDataCollection();
191
                                                FeatureCollection fCollection = (FeatureCollection)fs.getSelection();
192
                                                fCollectionAll.invalidate(fCollection);
193
                                                fs.setSelection(fCollectionAll);
194
                                        } catch (ReadException e) {
195 7163 fjp
                                                e.printStackTrace();
196
                                                NotificationManager.addError(e);
197
                                        }
198
199
                                }
200
                        }
201 9532 caballero
                        ((ProjectDocument)vista.getModel()).setModified(true);
202 6513 jaume
                }
203
        }
204
205
        public boolean isEnabled() {
206 7407 jaume
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
207 6513 jaume
                if (f instanceof View) {
208
209
                        View vista = (View) f;
210 7392 sbayarri
                        IProjectView model = vista.getModel();
211 6878 cesar
                        MapContext mapa = model.getMapContext();
212 6513 jaume
213
                        for (int i = 0; i < mapa.getLayers().getActives().length; i++) {
214
                                FLayer lyr = mapa.getLayers().getActives()[i];
215 6779 jmvivo
                                if (lyr.isAvailable() && lyr instanceof FLyrVect) {
216 6513 jaume
                                        return true;
217
                                }
218
                        }
219
                        return false;
220
                }
221
                return true;
222
        }
223
224
        public boolean isVisible() {
225 7407 jaume
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
226 6513 jaume
227 6533 jaume
                if (f != null && f instanceof View) {
228 6513 jaume
                        View vista = (View) f;
229 7392 sbayarri
                        IProjectView model = vista.getModel();
230 6878 cesar
                        MapContext mapa = model.getMapContext();
231 6513 jaume
232 7407 jaume
                        return hasVectorLayers(mapa.getLayers());
233
234
                }
235
                return false;
236
        }
237
238
        private boolean hasVectorLayers(FLayers layers) {
239
                for (int i = 0; i < layers.getLayersCount(); i++) {
240
                        FLayer lyr =layers.getLayer(i);
241 9483 fdiaz
                        if (lyr instanceof FLayers){
242
                                if (hasVectorLayers((FLayers) lyr)){
243
                                        return true;
244
                                }
245
                        } else if (lyr instanceof FLyrVect) {
246 7407 jaume
                                return true;
247 6513 jaume
                        }
248
                }
249 6533 jaume
                return false;
250 6513 jaume
        }
251 6533 jaume
}