Statistics
| Revision:

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

History | View | Annotate | Download (6.8 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.PluginServices;
50
import org.gvsig.andami.messages.NotificationManager;
51
import org.gvsig.andami.plugins.Extension;
52
import org.gvsig.app.project.documents.AbstractDocument;
53
import org.gvsig.app.project.documents.table.TableDocument;
54
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
55
import org.gvsig.app.project.documents.view.ViewDocument;
56
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
57
import org.gvsig.fmap.dal.exception.DataException;
58
import org.gvsig.fmap.dal.exception.ReadException;
59
import org.gvsig.fmap.dal.feature.FeatureSelection;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.fmap.mapcontext.MapContext;
62
import org.gvsig.fmap.mapcontext.layers.FLayer;
63
import org.gvsig.fmap.mapcontext.layers.FLayers;
64
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
65
import org.gvsig.fmap.mapcontrol.MapControl;
66

    
67

    
68

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

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

    
91
                                if (refresh) {
92
                                        mapCtrl.drawMap(false);
93
                                }
94
                                ((AbstractDocument)vista.getModel()).setModified(true);
95
                        }else if (view instanceof FeatureTableDocumentPanel){
96
                                FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) view;
97
                                TableDocument model = table.getModel();
98
                                FeatureStore featureStore=null;
99
//                                try {
100
                                        featureStore = model.getStore();
101
//                                } catch (ReadException e) {
102
//                                        e.printStackTrace();
103
//                                }
104
                                try {
105
                                if (featureStore.getFeatureSelection().getSize() != 0) {
106
                                        refresh = true;
107
                                }
108
                            featureStore.getFeatureSelection().deselectAll();
109
                                } catch (DataException e) {
110
                                        NotificationManager.addError(e);
111
                                }
112
                            if (refresh) {
113
                                    table.repaint();
114
                                }
115
                            table.getModel().setModified(true);
116
                        }
117
                 else {
118
                                }
119
                        }
120
    }
121

    
122

    
123
        private boolean clearSelectionOfView(FLayers layers){
124
                boolean refresh=false;
125

    
126
                for (int i = 0; i < layers.getLayersCount(); i++) {
127
                        FLayer lyr =layers.getLayer(i);
128
                        if (lyr instanceof FLayers){
129
                                refresh = refresh || clearSelectionOfView((FLayers) lyr);
130
                        } else if (lyr instanceof FLyrVect) {
131
                                FLyrVect lyrVect = (FLyrVect) lyr;
132
                                if (lyrVect.isActive()) {
133
                                        try {
134
                                                FeatureStore featureStore;
135

    
136
                                                featureStore = ((FLyrVect)lyr).getFeatureStore();
137
                                                if (featureStore.getFeatureSelection().getSize() != 0) {
138
                                                        refresh = true;
139
                                                }
140
                                        featureStore.getFeatureSelection().deselectAll();
141
                                        } catch (ReadException e) {
142
                                                e.printStackTrace();
143
                                        } catch (DataException e) {
144
                                                // TODO Auto-generated catch block
145
                                                e.printStackTrace();
146
                                        }
147
                                }
148
                        }
149
                }
150
                return refresh;
151
        }
152
        /**
153
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
154
         */
155
        public boolean isVisible() {
156
                org.gvsig.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
157

    
158
                if (view == null) {
159
                        return false;
160
                }
161
                if (view instanceof AbstractViewPanel) {
162
                        MapContext mapa = ((AbstractViewPanel) view).getModel().getMapContext();
163
                        return mapa.getLayers().getLayersCount() > 0;
164
                }
165
                if (view instanceof FeatureTableDocumentPanel) {
166
                        return true;
167
                }
168

    
169
                return false;
170
        }
171

    
172
        /**
173
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
174
         */
175
        public boolean isEnabled() {
176
                org.gvsig.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
177
                if (view == null) {
178
                        return false;
179
                }
180
                if (view instanceof AbstractViewPanel){
181
                        MapContext mapa = ((AbstractViewPanel) view).getMapControl().getMapContext();
182
                        return hasVectorLayersWithSelection(mapa.getLayers());
183
                }
184
                if (view instanceof FeatureTableDocumentPanel){
185
                        try {
186
                                return !((FeatureSelection)((FeatureTableDocumentPanel)view).getModel().getStore().getSelection()).isEmpty();
187
                        } catch (DataException e) {
188
                                e.printStackTrace();
189
                        }
190
                }
191
                return false;
192
        }
193

    
194
        private boolean hasVectorLayersWithSelection(FLayers layers) {
195
                for (int i = 0; i < layers.getLayersCount(); i++) {
196
                        FLayer lyr =layers.getLayer(i);
197
                        if (lyr instanceof FLayers){
198
                                if (hasVectorLayersWithSelection((FLayers) lyr)){
199
                                        return true;
200
                                }
201
                        } else if (lyr instanceof FLyrVect) {
202
                                FLyrVect lyrVect = (FLyrVect) lyr;
203
                                if (lyrVect.isActive()) {
204
                                        if (lyrVect.isAvailable()){
205
                                                try {
206
                                                        if (lyrVect.getFeatureStore().getFeatureSelection()
207
                                                                        .getSize() > 0) {
208
                                                                return true;
209
                                                        }
210
                                                } catch (DataException e) {
211
                                                        e.printStackTrace();
212
                                                        NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto",e);
213
                                                }
214
                                        }
215
                                }
216
                        }
217
                }
218
                return false;
219
        }
220

    
221
        /**
222
         * @see org.gvsig.andami.plugins.IExtension#initialize()
223
         */
224
        public void initialize() {
225
                registerIcons();
226
        }
227

    
228
        private void registerIcons(){
229
                PluginServices.getIconTheme().registerDefault(
230
                                "view-clear-selection",
231
                                this.getClass().getClassLoader().getResource("images/delselection.png")
232
                        );
233
        }
234
}