Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / ClearSelectionExtension.java @ 38564

History | View | Annotate | Download (5.56 KB)

1
/*
2
 * Created on 31-may-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.gvsig.app.extension;
48

    
49
import org.gvsig.andami.IconThemeHelper;
50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.messages.NotificationManager;
52
import org.gvsig.andami.plugins.Extension;
53
import org.gvsig.app.project.documents.AbstractDocument;
54
import org.gvsig.app.project.documents.view.ViewDocument;
55
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
56
import org.gvsig.fmap.dal.exception.DataException;
57
import org.gvsig.fmap.dal.exception.ReadException;
58
import org.gvsig.fmap.dal.feature.FeatureStore;
59
import org.gvsig.fmap.mapcontext.MapContext;
60
import org.gvsig.fmap.mapcontext.layers.FLayer;
61
import org.gvsig.fmap.mapcontext.layers.FLayers;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63
import org.gvsig.fmap.mapcontrol.MapControl;
64

    
65

    
66

    
67
/**
68
 * Extensi?n encargada de limpiar la selecci?n.
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class ClearSelectionExtension extends Extension {
73
        /**
74
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
75
         */
76
        public void execute(String s) {
77
                if (s.equalsIgnoreCase("selection-clear-view") ) {
78
                        boolean refresh = false;
79
                        org.gvsig.andami.ui.mdiManager.IWindow view =PluginServices.getMDIManager().getActiveWindow();
80

    
81
                        if (view instanceof AbstractViewPanel){
82
                                AbstractViewPanel vista = (AbstractViewPanel) view;
83
                                ViewDocument model = vista.getModel();
84
                                MapContext mapa = model.getMapContext();
85
                                MapControl mapCtrl = vista.getMapControl();
86
                                FLayers layers = mapa.getLayers();
87
                                refresh = clearSelectionOfView(layers);
88

    
89
                                if (refresh) {
90
                                        mapCtrl.drawMap(false);
91
                                }
92
                                ((AbstractDocument)vista.getModel()).setModified(true);
93
                        }
94
        }
95
    }
96

    
97

    
98
        private boolean clearSelectionOfView(FLayers layers){
99
                boolean refresh=false;
100

    
101
                for (int i = 0; i < layers.getLayersCount(); i++) {
102
                        FLayer lyr =layers.getLayer(i);
103
                        if (lyr instanceof FLayers){
104
                                refresh = refresh || clearSelectionOfView((FLayers) lyr);
105
                        } else if (lyr instanceof FLyrVect) {
106
                                FLyrVect lyrVect = (FLyrVect) lyr;
107
                                if (lyrVect.isActive()) {
108
                                        try {
109
                                                FeatureStore featureStore;
110

    
111
                                                featureStore = ((FLyrVect)lyr).getFeatureStore();
112
                                                if (featureStore.getFeatureSelection().getSize() != 0) {
113
                                                        refresh = true;
114
                                                }
115
                                        featureStore.getFeatureSelection().deselectAll();
116
                                        } catch (ReadException e) {
117
                                                e.printStackTrace();
118
                                        } catch (DataException e) {
119
                                                // TODO Auto-generated catch block
120
                                                e.printStackTrace();
121
                                        }
122
                                }
123
                        }
124
                }
125
                return refresh;
126
        }
127
        /**
128
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
129
         */
130
        public boolean isVisible() {
131
                org.gvsig.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
132

    
133
                if (view == null) {
134
                        return false;
135
                }
136
                if (view instanceof AbstractViewPanel) {
137
                        MapContext mapa = ((AbstractViewPanel) view).getModel().getMapContext();
138
                        return mapa.getLayers().getLayersCount() > 0;
139
                }
140

    
141
                return false;
142
        }
143

    
144
        /**
145
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
146
         */
147
        public boolean isEnabled() {
148
                org.gvsig.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
149
                if (view == null) {
150
                        return false;
151
                }
152
                if (view instanceof AbstractViewPanel){
153
                        MapContext mapa = ((AbstractViewPanel) view).getMapControl().getMapContext();
154
                        return hasVectorLayersWithSelection(mapa.getLayers());
155
                }
156
                return false;
157
        }
158

    
159
        private boolean hasVectorLayersWithSelection(FLayers layers) {
160
                for (int i = 0; i < layers.getLayersCount(); i++) {
161
                        FLayer lyr =layers.getLayer(i);
162
                        if (lyr instanceof FLayers){
163
                                if (hasVectorLayersWithSelection((FLayers) lyr)){
164
                                        return true;
165
                                }
166
                        } else if (lyr instanceof FLyrVect) {
167
                                FLyrVect lyrVect = (FLyrVect) lyr;
168
                                if (lyrVect.isActive()) {
169
                                        if (lyrVect.isAvailable()){
170
                                                try {
171
                                                        if (lyrVect.getFeatureStore().getFeatureSelection()
172
                                                                        .getSize() > 0) {
173
                                                                return true;
174
                                                        }
175
                                                } catch (DataException e) {
176
                                                        e.printStackTrace();
177
                                                        NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto",e);
178
                                                }
179
                                        }
180
                                }
181
                        }
182
                }
183
                return false;
184
        }
185

    
186
        /**
187
         * @see org.gvsig.andami.plugins.IExtension#initialize()
188
         */
189
        public void initialize() {
190
                IconThemeHelper.registerIcon("action", "edit-clear", this);
191
        }
192
}