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 @ 43510

History | View | Annotate | Download (5.69 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 41248 jjdelcerro
 * 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 40558 jjdelcerro
 *
11 41248 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 41248 jjdelcerro
 * 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 40558 jjdelcerro
 *
20 41248 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40558 jjdelcerro
 */
23 40435 jjdelcerro
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 41248 jjdelcerro
import org.gvsig.app.ApplicationLocator;
29
import org.gvsig.app.ApplicationManager;
30 40435 jjdelcerro
import org.gvsig.app.project.documents.view.ViewDocument;
31 41248 jjdelcerro
import org.gvsig.app.project.documents.view.gui.IView;
32 40435 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.exception.ReadException;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.FLayers;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39
import org.gvsig.fmap.mapcontrol.MapControl;
40
41
/**
42
 * Extensi?n encargada de limpiar la selecci?n.
43
 *
44
 */
45
public class ClearSelectionExtension extends Extension {
46
47 41264 jjdelcerro
48 41248 jjdelcerro
    public void execute(String s) {
49
        ApplicationManager application = ApplicationLocator.getManager();
50 40435 jjdelcerro
51 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
52
        if (view == null) {
53 41248 jjdelcerro
            return;
54 40435 jjdelcerro
        }
55 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
56 41248 jjdelcerro
        if (s.equalsIgnoreCase("selection-clear-view")) {
57
            MapContext mapa = document.getMapContext();
58 41264 jjdelcerro
            MapControl mapCtrl = view.getMapControl();
59 41248 jjdelcerro
            FLayers layers = mapa.getLayers();
60
            boolean refresh = clearSelectionOfView(layers);
61
            if (refresh) {
62
                mapCtrl.drawMap(false);
63
            }
64
            document.setModified(true);
65
        }
66 40435 jjdelcerro
    }
67
68 41248 jjdelcerro
    private boolean clearSelectionOfView(FLayers layers) {
69
        boolean refresh = false;
70 40435 jjdelcerro
71 41248 jjdelcerro
        for (int i = 0; i < layers.getLayersCount(); i++) {
72
            FLayer lyr = layers.getLayer(i);
73
            if (lyr instanceof FLayers) {
74
                refresh = refresh || clearSelectionOfView((FLayers) lyr);
75
            } else if (lyr instanceof FLyrVect) {
76
                FLyrVect lyrVect = (FLyrVect) lyr;
77
                if (lyrVect.isActive()) {
78
                    try {
79
                        FeatureStore featureStore;
80 40435 jjdelcerro
81 41248 jjdelcerro
                        featureStore = ((FLyrVect) lyr).getFeatureStore();
82
                        if (!featureStore.getFeatureSelection().isEmpty()) {
83
                            refresh = true;
84
                        }
85
                        featureStore.getFeatureSelection().deselectAll();
86
                    } catch (ReadException e) {
87
                        e.printStackTrace();
88
                    } catch (DataException e) {
89
                        // TODO Auto-generated catch block
90
                        e.printStackTrace();
91
                    }
92
                }
93
            }
94
        }
95
        return refresh;
96
    }
97 40435 jjdelcerro
98 41248 jjdelcerro
    public boolean isVisible() {
99
        ApplicationManager application = ApplicationLocator.getManager();
100 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
101
        if (view == null) {
102 41248 jjdelcerro
            return false;
103
        }
104 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
105 41248 jjdelcerro
        MapContext mapa = document.getMapContext();
106
        return mapa.getLayers().getLayersCount() > 0;
107
    }
108 40435 jjdelcerro
109 41248 jjdelcerro
    public boolean isEnabled() {
110
        ApplicationManager application = ApplicationLocator.getManager();
111 40435 jjdelcerro
112 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
113
        if (view == null) {
114 41248 jjdelcerro
            return false;
115
        }
116 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
117 41248 jjdelcerro
        MapContext mapa = document.getMapContext();
118
        return hasVectorLayersWithSelection(mapa.getLayers());
119
    }
120
121
    private boolean hasVectorLayersWithSelection(FLayers layers) {
122
        for (int i = 0; i < layers.getLayersCount(); i++) {
123
            FLayer lyr = layers.getLayer(i);
124
            if (lyr instanceof FLayers) {
125
                if (hasVectorLayersWithSelection((FLayers) lyr)) {
126
                    return true;
127
                }
128
            } else if (lyr instanceof FLyrVect) {
129
                FLyrVect lyrVect = (FLyrVect) lyr;
130
                if (lyrVect.isActive()) {
131
                    if (lyrVect.isAvailable()) {
132
                        try {
133
                            if (!lyrVect.getFeatureStore().getFeatureSelection().isEmpty()) {
134
                                return true;
135
                            }
136
                        } catch (DataException e) {
137
                            e.printStackTrace();
138
                            NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto", e);
139
                        }
140
                    }
141
                }
142
            }
143
        }
144
        return false;
145
    }
146
147
    /**
148
     * @see org.gvsig.andami.plugins.IExtension#initialize()
149
     */
150
    public void initialize() {
151
        IconThemeHelper.registerIcon("action", "edit-clear", this);
152
    }
153 40435 jjdelcerro
}