Statistics
| Revision:

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

History | View | Annotate | Download (5.81 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.iver.andami.PluginServices;
50
import com.iver.andami.messages.NotificationManager;
51
import com.iver.andami.plugins.Extension;
52
import com.iver.cit.gvsig.fmap.DriverException;
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.View;
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 View){
81
                                View vista = (View) 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;
96
                                dataSource = model.getModelo().getRecordset();
97
                                if (dataSource.getSelection().cardinality() != 0) {
98
                                        refresh = true;
99
                                }
100
                            dataSource.clearSelection();
101
                            if (refresh) {
102
                                    table.refresh();
103
                                }
104
                            table.getModel().setModified(true);
105
                        }
106
                }
107
    }
108

    
109

    
110
        private boolean clearSelectionOfView(FLayers layers){
111
                boolean refresh=false;
112

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

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

    
142
                if (view == null) {
143
                        return false;
144
                }
145
                if (view instanceof View) {
146
                        MapContext mapa = ((View) view).getModel().getMapContext();
147
                        return mapa.getLayers().getLayersCount() > 0;
148
                }
149
                if (view instanceof Table) {
150
                        return true;
151
                }
152

    
153
                return false;
154
        }
155

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

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

    
199
        /**
200
         * @see com.iver.andami.plugins.IExtension#initialize()
201
         */
202
        public void initialize() {
203
        }
204
}