Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / ClearSelectionExtension.java @ 24759

History | View | Annotate | Download (6.46 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 com.iver.cit.gvsig;
48

    
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.exception.ReadException;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.fmap.mapcontext.MapContext;
53
import org.gvsig.fmap.mapcontext.layers.FLayer;
54
import org.gvsig.fmap.mapcontext.layers.FLayers;
55
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
56
import org.gvsig.fmap.mapcontrol.MapControl;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.cit.gvsig.project.documents.ProjectDocument;
62
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
63
import com.iver.cit.gvsig.project.documents.table.gui.Table;
64
import com.iver.cit.gvsig.project.documents.view.IProjectView;
65
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
66

    
67

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

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

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

    
121

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

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

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

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

    
168
                return false;
169
        }
170

    
171
        /**
172
         * @see com.iver.andami.plugins.IExtension#isEnabled()
173
         */
174
        public boolean isEnabled() {
175
                com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
176
                if (view == null) {
177
                        return false;
178
                }
179
                if (view instanceof BaseView){
180
                        MapContext mapa = ((BaseView) view).getMapControl().getMapContext();
181
                        return hasVectorLayersWithSelection(mapa.getLayers());
182
                }
183
                if (view instanceof Table){
184
                        return ((Table)view).getSelectedRowIndices().length>0;
185
                }
186
                return false;
187
        }
188

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

    
216
        /**
217
         * @see com.iver.andami.plugins.IExtension#initialize()
218
         */
219
        public void initialize() {
220
                registerIcons();
221
        }
222

    
223
        private void registerIcons(){
224
                PluginServices.getIconTheme().registerDefault(
225
                                "view-clear-selection",
226
                                this.getClass().getClassLoader().getResource("images/delselection.png")
227
                        );
228
        }
229
}