Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / ClearSelectionExtension.java @ 44535

History | View | Annotate | Download (6.54 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.extension;
24

    
25
import org.gvsig.andami.IconThemeHelper;
26
import org.gvsig.andami.messages.NotificationManager;
27
import org.gvsig.andami.plugins.Extension;
28
import org.gvsig.app.ApplicationLocator;
29
import org.gvsig.app.ApplicationManager;
30
import org.gvsig.app.project.documents.view.ViewDocument;
31
import org.gvsig.app.project.documents.view.gui.IView;
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.exception.ReadException;
34
import org.gvsig.fmap.dal.feature.FeatureSelection;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.mapcontext.MapContext;
38
import org.gvsig.fmap.mapcontext.layers.FLayer;
39
import org.gvsig.fmap.mapcontext.layers.FLayers;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
41
import org.gvsig.fmap.mapcontrol.MapControl;
42

    
43
/**
44
 * Extensi?n encargada de limpiar la selecci?n.
45
 *
46
 */
47
public class ClearSelectionExtension extends Extension {
48

    
49

    
50
    public void execute(String s) {
51
        ApplicationManager application = ApplicationLocator.getManager();
52

    
53
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
54
        if (view == null) {
55
            return;
56
        }
57
        ViewDocument document = view.getViewDocument();
58
        if (s.equalsIgnoreCase("selection-clear-view")) {
59
            MapContext mapa = document.getMapContext();
60
            MapControl mapCtrl = view.getMapControl();
61
            FLayers layers = mapa.getLayers();
62
            boolean refresh = clearSelectionOfView(layers);
63
            if (refresh) {
64
                mapCtrl.drawMap(false);
65
            }
66
            document.setModified(true);
67
        }
68
    }
69

    
70
    private boolean clearSelectionOfView(FLayers layers) {
71
        boolean refresh = false;
72

    
73
        for (int i = 0; i < layers.getLayersCount(); i++) {
74
            FLayer lyr = layers.getLayer(i);
75
            if (lyr instanceof FLayers) {
76
                refresh = refresh || clearSelectionOfView((FLayers) lyr);
77
            } else if (lyr instanceof FLyrVect) {
78
                FLyrVect lyrVect = (FLyrVect) lyr;
79
                if (lyrVect.isActive()) {
80
                    try {
81
                        FeatureStore featureStore;
82

    
83
                        featureStore = ((FLyrVect) lyr).getFeatureStore();
84
                        if (!featureStore.getFeatureSelection().isEmpty()) {
85
                            refresh = true;
86
                        }
87
                        featureStore.getFeatureSelection().deselectAll();
88
                    } catch (ReadException e) {
89
                        e.printStackTrace();
90
                    } catch (DataException e) {
91
                        // TODO Auto-generated catch block
92
                        e.printStackTrace();
93
                    }
94
                }
95
            }
96
        }
97
        return refresh;
98
    }
99

    
100
    public boolean isVisible() {
101
        ApplicationManager application = ApplicationLocator.getManager();
102
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
103
        if (view == null) {
104
            return false;
105
        }
106
        ViewDocument document = view.getViewDocument();
107
        MapContext mapa = document.getMapContext();
108
        return mapa.getLayers().getLayersCount() > 0;
109
    }
110

    
111
    public boolean isEnabled() {
112
        ApplicationManager application = ApplicationLocator.getManager();
113

    
114
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
115
        if (view == null) {
116
            return false;
117
        }
118
        ViewDocument document = view.getViewDocument();
119
        if( document == null ) {
120
            return false;
121
        }
122
        boolean hasActiveVectorLayers = false;
123
        boolean hasSelection = false;
124
        for (FLayer layer : document.getMapContext().getLayers()) {
125
            if( layer.isActive() && layer.isAvailable() && layer instanceof FLyrVect ) {
126
                try {
127
                    hasActiveVectorLayers = true;
128
                    FeatureStore store = ((FLyrVect)layer).getFeatureStore();
129
                    FeatureSelection selection = store.getFeatureSelection();
130
                    if( !selection.isAvailable() || selection.isEmpty() ) {
131
                        return false;
132
                    } else {
133
                        hasSelection = true;
134
                    }
135
                } catch (Exception ex) {
136
                }
137
            }
138
        }
139
        return hasActiveVectorLayers && hasSelection;
140
    }
141

    
142
    private boolean hasVectorLayersWithSelection(FLayers layers) {
143
        for (int i = 0; i < layers.getLayersCount(); i++) {
144
            FLayer lyr = layers.getLayer(i);
145
            if (lyr instanceof FLayers) {
146
                if (hasVectorLayersWithSelection((FLayers) lyr)) {
147
                    return true;
148
                }
149
            } else if (lyr instanceof FLyrVect) {
150
                FLyrVect lyrVect = (FLyrVect) lyr;
151
                if (lyrVect.isActive()) {
152
                    if (lyrVect.isAvailable()) {
153
                        try {
154
                            if (!lyrVect.getFeatureStore().getFeatureSelection().isEmpty()) {
155
                                return true;
156
                            }
157
                        } catch (DataException e) {
158
                            e.printStackTrace();
159
                            NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto", e);
160
                        }
161
                    }
162
                }
163
            }
164
        }
165
        return false;
166
    }
167

    
168
    /**
169
     * @see org.gvsig.andami.plugins.IExtension#initialize()
170
     */
171
    public void initialize() {
172
        IconThemeHelper.registerIcon("action", "edit-clear", this);
173
    }
174
}