Statistics
| Revision:

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

History | View | Annotate | Download (6.36 KB)

1 6599 caballero
/*
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 com.iver.cit.gvsig;
48
49 22246 vcaballero
import org.gvsig.fmap.data.DataCollection;
50 22367 vcaballero
import org.gvsig.fmap.data.DataException;
51 21911 jmvivo
import org.gvsig.fmap.data.ReadException;
52 22360 jmvivo
import org.gvsig.fmap.data.feature.FeatureStore;
53 20994 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
54
import org.gvsig.fmap.mapcontext.layers.FLayer;
55
import org.gvsig.fmap.mapcontext.layers.FLayers;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
57
import org.gvsig.fmap.mapcontrol.MapControl;
58
59 6599 caballero
import com.iver.andami.PluginServices;
60 7011 fjp
import com.iver.andami.messages.NotificationManager;
61 6599 caballero
import com.iver.andami.plugins.Extension;
62 9532 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
63 7304 caballero
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
64
import com.iver.cit.gvsig.project.documents.table.gui.Table;
65 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
66 18345 vcaballero
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
67 6599 caballero
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 6880 cesar
                        com.iver.andami.ui.mdiManager.IWindow view =PluginServices.getMDIManager().getActiveWindow();
82 6599 caballero
83 18345 vcaballero
                        if (view instanceof BaseView){
84
                                BaseView vista = (BaseView) view;
85 7392 sbayarri
                                IProjectView model = vista.getModel();
86 6878 cesar
                                MapContext mapa = model.getMapContext();
87 6599 caballero
                                MapControl mapCtrl = vista.getMapControl();
88 9482 fdiaz
                                FLayers layers = mapa.getLayers();
89
                                refresh = clearSelectionOfView(layers);
90 6599 caballero
91 22367 vcaballero
//                                if (refresh) {
92
//                                        mapCtrl.drawMap(false);
93
//                                }
94 9532 caballero
                                ((ProjectDocument)vista.getModel()).setModified(true);
95 6599 caballero
                        }else if (view instanceof Table){
96
                                Table table = (Table) view;
97
                                ProjectTable model = table.getModel();
98 21665 vcaballero
                                FeatureStore featureStore=null;
99
//                                try {
100 22932 jmvivo
                                        featureStore = model.getModel();
101 21665 vcaballero
//                                } catch (ReadException e) {
102
//                                        e.printStackTrace();
103
//                                }
104
                                if (featureStore.getSelection().size() != 0) {
105 6599 caballero
                                        refresh = true;
106
                                }
107 21665 vcaballero
                            featureStore.getSelection().clear();
108 22367 vcaballero
//                            if (refresh) {
109
//                                    table.refresh();
110
//                                }
111 9532 caballero
                            table.getModel().setModified(true);
112 6599 caballero
                        }
113
                }
114
    }
115
116 9482 fdiaz
117
        private boolean clearSelectionOfView(FLayers layers){
118
                boolean refresh=false;
119 9532 caballero
120 9482 fdiaz
                for (int i = 0; i < layers.getLayersCount(); i++) {
121
                        FLayer lyr =layers.getLayer(i);
122
                        if (lyr instanceof FLayers){
123
                                refresh = refresh || clearSelectionOfView((FLayers) lyr);
124
                        } else if (lyr instanceof FLyrVect) {
125 9532 caballero
                                FLyrVect lyrVect = (FLyrVect) lyr;
126 9482 fdiaz
                                if (lyrVect.isActive()) {
127
                                        try {
128 21665 vcaballero
                                                FeatureStore featureStore;
129 9482 fdiaz
130 21665 vcaballero
                                                featureStore = ((FLyrVect)lyr).getFeatureStore();
131
                                                if (featureStore.getSelection().size() != 0) {
132 9482 fdiaz
                                                        refresh = true;
133
                                                }
134 22246 vcaballero
                                        DataCollection dc=featureStore.createSelection();
135
                                        featureStore.setSelection(dc);
136 21354 vcaballero
                                        } catch (ReadException e) {
137 9482 fdiaz
                                                e.printStackTrace();
138 22367 vcaballero
                                        } catch (DataException e) {
139
                                                // TODO Auto-generated catch block
140
                                                e.printStackTrace();
141 9482 fdiaz
                                        }
142
                                }
143
                        }
144
                }
145
                return refresh;
146
        }
147 6599 caballero
        /**
148
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
149
         */
150
        public boolean isVisible() {
151 6880 cesar
                com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
152 6599 caballero
153
                if (view == null) {
154
                        return false;
155
                }
156 18345 vcaballero
                if (view instanceof BaseView) {
157
                        MapContext mapa = ((BaseView) view).getModel().getMapContext();
158 6599 caballero
                        return mapa.getLayers().getLayersCount() > 0;
159
                }
160
                if (view instanceof Table) {
161
                        return true;
162
                }
163
164
                return false;
165
        }
166
167
        /**
168
         * @see com.iver.andami.plugins.IExtension#isEnabled()
169
         */
170
        public boolean isEnabled() {
171 6880 cesar
                com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
172 6599 caballero
                if (view == null) {
173
                        return false;
174
                }
175 18345 vcaballero
                if (view instanceof BaseView){
176
                        MapContext mapa = ((BaseView) view).getMapControl().getMapContext();
177 9482 fdiaz
                        return hasVectorLayersWithSelection(mapa.getLayers());
178 6599 caballero
                }
179
                if (view instanceof Table){
180
                        return ((Table)view).getSelectedRowIndices().length>0;
181
                }
182
                return false;
183
        }
184
185 9482 fdiaz
        private boolean hasVectorLayersWithSelection(FLayers layers) {
186
                for (int i = 0; i < layers.getLayersCount(); i++) {
187
                        FLayer lyr =layers.getLayer(i);
188
                        if (lyr instanceof FLayers){
189
                                if (hasVectorLayersWithSelection((FLayers) lyr)){
190
                                        return true;
191
                                }
192
                        } else if (lyr instanceof FLyrVect) {
193 9532 caballero
                                FLyrVect lyrVect = (FLyrVect) lyr;
194 9482 fdiaz
                                if (lyrVect.isActive()) {
195
                                        if (lyrVect.isAvailable()){
196
                                                try {
197 21665 vcaballero
                                                        if (lyrVect.getFeatureStore().getSelection().size() > 0)
198 9482 fdiaz
                                                                return true;
199 21354 vcaballero
                                                } catch (ReadException e) {
200 9482 fdiaz
                                                        e.printStackTrace();
201
                                                        NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto",e);
202
                                                }
203
                                        }
204
                                }
205
                        }
206
                }
207
                return false;
208
        }
209
210 6599 caballero
        /**
211
         * @see com.iver.andami.plugins.IExtension#initialize()
212
         */
213
        public void initialize() {
214 14821 jmvivo
                registerIcons();
215 6599 caballero
        }
216 14821 jmvivo
217
        private void registerIcons(){
218 15640 jmvivo
                PluginServices.getIconTheme().registerDefault(
219 14821 jmvivo
                                "view-clear-selection",
220
                                this.getClass().getClassLoader().getResource("images/delselection.png")
221
                        );
222
        }
223 6599 caballero
}