Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ClearSelectionExtension.java @ 28407

History | View | Annotate | Download (6.15 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 com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.cit.gvsig.fmap.MapContext;
54
import com.iver.cit.gvsig.fmap.MapControl;
55
import com.iver.cit.gvsig.fmap.layers.FLayer;
56
import com.iver.cit.gvsig.fmap.layers.FLayers;
57
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
58
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
59
import com.iver.cit.gvsig.project.documents.ProjectDocument;
60
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
61
import com.iver.cit.gvsig.project.documents.table.gui.Table;
62
import com.iver.cit.gvsig.project.documents.view.IProjectView;
63
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
64

    
65

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

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

    
88
                                if (refresh) {
89
                                        mapCtrl.drawMap(false);
90
                                }
91
                                ((ProjectDocument)vista.getModel()).setModified(true);
92
                        }else if (view instanceof Table){
93
                                Table table = (Table) view;
94
                                ProjectTable model = table.getModel();
95
                                SelectableDataSource dataSource=null;
96
                                try {
97
                                        dataSource = model.getModelo().getRecordset();
98
                                } catch (ReadDriverException e) {
99
                                        e.printStackTrace();
100
                                }
101
                                if (dataSource.getSelection().cardinality() != 0) {
102
                                        refresh = true;
103
                                }
104
                            dataSource.clearSelection();
105
                            if (refresh) {
106
                                    table.refresh();
107
                                }
108
                            table.getModel().setModified(true);
109
                        }
110
                }
111
    }
112

    
113

    
114
        private boolean clearSelectionOfView(FLayers layers){
115
                boolean refresh=false;
116

    
117
                for (int i = 0; i < layers.getLayersCount(); i++) {
118
                        FLayer lyr =layers.getLayer(i);
119
                        if (lyr instanceof FLayers){
120
                                refresh = refresh || clearSelectionOfView((FLayers) lyr);
121
                        } else if (lyr instanceof FLyrVect) {
122
                                FLyrVect lyrVect = (FLyrVect) lyr;
123
                                if (lyrVect.isActive()) {
124
                                        try {
125
                                                SelectableDataSource dataSource;
126

    
127
                                                dataSource = ((FLyrVect)lyr).getRecordset();
128
                                                if (dataSource.getSelection().cardinality() != 0) {
129
                                                        refresh = true;
130
                                                }
131
                                        dataSource.clearSelection();
132
                                        } catch (ReadDriverException e) {
133
                                                e.printStackTrace();
134
                                        }
135
                                }
136
                        }
137
                }
138
                return refresh;
139
        }
140
        /**
141
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
142
         */
143
        public boolean isVisible() {
144
                com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
145

    
146
                if (view == null) {
147
                        return false;
148
                }
149
                if (view instanceof BaseView) {
150
                        MapContext mapa = ((BaseView) view).getModel().getMapContext();
151
                        return mapa.getLayers().getLayersCount() > 0;
152
                }
153
                if (view instanceof Table) {
154
                        return true;
155
                }
156

    
157
                return false;
158
        }
159

    
160
        /**
161
         * @see com.iver.andami.plugins.IExtension#isEnabled()
162
         */
163
        public boolean isEnabled() {
164
                com.iver.andami.ui.mdiManager.IWindow view = PluginServices.getMDIManager().getActiveWindow();
165
                if (view == null) {
166
                        return false;
167
                }
168
                if (view instanceof BaseView){
169
                        MapContext mapa = ((BaseView) view).getMapControl().getMapContext();
170
                        return hasVectorLayersWithSelection(mapa.getLayers());
171
                }
172
                if (view instanceof Table){
173
                        return ((Table)view).getSelectedRowIndices().length>0;
174
                }
175
                return false;
176
        }
177

    
178
        private boolean hasVectorLayersWithSelection(FLayers layers) {
179
                for (int i = 0; i < layers.getLayersCount(); i++) {
180
                        FLayer lyr =layers.getLayer(i);
181
                        if (lyr instanceof FLayers){
182
                                if (hasVectorLayersWithSelection((FLayers) lyr)){
183
                                        return true;
184
                                }
185
                        } else if (lyr instanceof FLyrVect) {
186
                                FLyrVect lyrVect = (FLyrVect) lyr;
187
                                if (lyrVect.isActive()) {
188
                                        if (lyrVect.isAvailable()){
189
                                                try {
190
                                                        if (lyrVect.getRecordset().getSelection().cardinality() > 0)
191
                                                                return true;
192
                                                } catch (ReadDriverException e) {
193
                                                        e.printStackTrace();
194
                                                        NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto",e);
195
                                                }
196
                                        }
197
                                }
198
                        }
199
                }
200
                return false;
201
        }
202

    
203
        /**
204
         * @see com.iver.andami.plugins.IExtension#initialize()
205
         */
206
        public void initialize() {
207
                registerIcons();
208
        }
209

    
210
        private void registerIcons(){
211
                PluginServices.getIconTheme().registerDefault(
212
                                "view-clear-selection",
213
                                this.getClass().getClassLoader().getResource("images/delselection.png")
214
                        );
215
        }
216
}