Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / ViewSelectionControls.java @ 25232

History | View | Annotate | Download (7.92 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
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
 *   Av. Blasco Ib��ez, 50
24
 *   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
 *
44
 * $Id: ViewSelectionControls.java 25232 2008-11-17 16:50:36Z jmvivo $
45
 * $Log$
46
 * 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
 * isModified
51
 *
52
 * Revision 1.14  2006/12/27 11:08:56  fdiaz
53
 * 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
 * invert selection
57
 *
58
 * Revision 1.12  2006/09/20 13:17:40  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.11  2006/09/20 10:56:56  sbayarri
62
 * Refactorizaci�n clases View y ProjectView
63
 *
64
 * Revision 1.10  2006/09/15 10:40:03  caballero
65
 * extensibilidad de documentos
66
 *
67
 * Revision 1.9  2006/09/11 16:14:15  fjp
68
 * Invertir seleccion. Falta subirlo al branch de la 1.0
69
 *
70
 * Revision 1.8  2006/09/11 06:53:13  fjp
71
 * Antes del parto
72
 *
73
 * Revision 1.7  2006/09/04 16:26:54  fjp
74
 * Seleccionar por pol�gono
75
 *
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
 * separadas las herramientas de selecci�n del viewControls al ViewSelectionControls
93
 *
94
 *
95
 */
96
package com.iver.cit.gvsig;
97

    
98
import org.apache.log4j.Logger;
99
import org.gvsig.fmap.dal.DataStore;
100
import org.gvsig.fmap.dal.exception.DataException;
101
import org.gvsig.fmap.dal.feature.FeatureStore;
102
import org.gvsig.fmap.mapcontext.MapContext;
103
import org.gvsig.fmap.mapcontext.layers.FLayer;
104
import org.gvsig.fmap.mapcontext.layers.FLayers;
105
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
106
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
107
import org.gvsig.fmap.mapcontrol.MapControl;
108

    
109
import com.iver.andami.PluginServices;
110
import com.iver.andami.messages.NotificationManager;
111
import com.iver.andami.plugins.Extension;
112
import com.iver.andami.ui.mdiManager.IWindow;
113
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
import com.iver.cit.gvsig.project.documents.ProjectDocument;
117
import com.iver.cit.gvsig.project.documents.view.IProjectView;
118
import com.iver.cit.gvsig.project.documents.view.gui.View;
119

    
120
/**
121
 * Extension that handles the selection tools, selection tools have sense on
122
 * vectorial layers only.
123
 *
124
 * @author jaume dominguez faus - jaume.dominguez@iver.es
125
 *
126
 */
127
public class ViewSelectionControls extends Extension {
128
        private static Logger logger = Logger.getLogger(ViewControls.class
129
                        .getName());
130

    
131
        public void initialize() {
132
                // TODO Auto-generated method stub
133
                registerIcons();
134
        }
135

    
136
        private void registerIcons(){
137

    
138
                PluginServices.getIconTheme().registerDefault(
139
                                "view-select-geometry",
140
                                this.getClass().getClassLoader().getResource("images/Select.png")
141
                        );
142

    
143
                PluginServices.getIconTheme().registerDefault(
144
                                "view-select-by-rectangle",
145
                                this.getClass().getClassLoader().getResource("images/SelEspacial2b.png")
146
                        );
147

    
148
                PluginServices.getIconTheme().registerDefault(
149
                                "view-select-by-polygon",
150
                                this.getClass().getClassLoader().getResource("images/SelectByPolygon.png")
151
                        );
152

    
153
                PluginServices.getIconTheme().registerDefault(
154
                                "view-select-invert",
155
                                this.getClass().getClassLoader().getResource("images/invertSelection.png")
156
                        );
157
        }
158

    
159
        public void execute(String actionCommand) {
160
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
161
                IProjectView model = vista.getModel();
162
                MapContext mapa = model.getMapContext();
163
                MapControl mapCtrl = vista.getMapControl();
164
                logger.debug("Comand : " + actionCommand);
165
                if (actionCommand.equals("SELRECT")) {
166
                        mapCtrl.setTool("rectSelection");
167
                        ((ProjectDocument)vista.getModel()).setModified(true);
168
                } else if (actionCommand.equals("SELPOINT")) {
169
                        mapCtrl.setTool("pointSelection");
170
                        ((ProjectDocument)vista.getModel()).setModified(true);
171
                } else if (actionCommand.equals("SELPOL")) {
172
                        mapCtrl.setTool("polSelection");
173
                        ((ProjectDocument)vista.getModel()).setModified(true);
174
                } else if (actionCommand.equals("SELECTIONBYSHAPE")) {
175
                        SelectionByTheme dlg = new SelectionByTheme();
176
                        // FLayer[] layers = mapa.getLayers().getActives();
177
                        // int count = 0;
178
                        dlg.setModel(new DefaultSelectionByThemeModel());
179
                        dlg.addSelectionListener(new MySelectionByThemeListener());
180
                        PluginServices.getMDIManager().addWindow(dlg);
181
                        ((ProjectDocument)vista.getModel()).setModified(true);
182
                } else if (actionCommand.equals("INVERT_SELECTION")) {
183
                        FLayer[] actives = mapa.getLayers().getActives();
184
                        for (int i = 0; i < actives.length; i++) {
185
                                FLayer lyr = actives[i];
186
                                if (lyr.isAvailable() && lyr instanceof SingleLayer) {
187
                                        SingleLayer lyrSingle = (SingleLayer) lyr;
188
                                        DataStore ds = lyrSingle.getDataStore();
189
                                        if (ds instanceof FeatureStore) {
190
                                                try {
191
                                                        ((FeatureStore) ds).getFeatureSelection().reverse();
192
                                                        ((ProjectDocument) vista.getModel())
193
                                                                        .setModified(true);
194
                                                } catch (DataException e) {
195
                                                        e.printStackTrace();
196
                                                        NotificationManager.addError(e);
197
                                                }
198
                                        } else {
199
                                                // TODO Not supported yet
200
                                        }
201

    
202

    
203
                                }
204
                        }
205
                        ((ProjectDocument)vista.getModel()).setModified(true);
206
                }
207
        }
208

    
209
        public boolean isEnabled() {
210
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
211
                if (f instanceof View) {
212

    
213
                        View vista = (View) f;
214
                        IProjectView model = vista.getModel();
215
                        MapContext mapa = model.getMapContext();
216

    
217
                        for (int i = 0; i < mapa.getLayers().getActives().length; i++) {
218
                                FLayer lyr = mapa.getLayers().getActives()[i];
219
                                if (lyr.isAvailable() && lyr instanceof FLyrVect) {
220
                                        return true;
221
                                }
222
                        }
223
                        return false;
224
                }
225
                return true;
226
        }
227

    
228
        public boolean isVisible() {
229
                IWindow f = PluginServices.getMDIManager().getActiveWindow();
230

    
231
                if (f != null && f instanceof View) {
232
                        View vista = (View) f;
233
                        IProjectView model = vista.getModel();
234
                        MapContext mapa = model.getMapContext();
235

    
236
                        return hasVectorLayers(mapa.getLayers());
237

    
238
                }
239
                return false;
240
        }
241

    
242
        private boolean hasVectorLayers(FLayers layers) {
243
                for (int i = 0; i < layers.getLayersCount(); i++) {
244
                        FLayer lyr =layers.getLayer(i);
245
                        if (lyr instanceof FLayers){
246
                                if (hasVectorLayers((FLayers) lyr)){
247
                                        return true;
248
                                }
249
                        } else if (lyr instanceof FLyrVect) {
250
                                return true;
251
                        }
252
                }
253
                return false;
254
        }
255
}