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

History | View | Annotate | Download (5.78 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
 * @author Vicente Caballero Navarro
45
 */
46
public class ClearSelectionExtension extends Extension {
47
48 41248 jjdelcerro
    /**
49
     * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
50
     */
51
    public void execute(String s) {
52
        ApplicationManager application = ApplicationLocator.getManager();
53 40435 jjdelcerro
54 41248 jjdelcerro
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
55
        if (document == null) {
56
            return;
57 40435 jjdelcerro
        }
58 41248 jjdelcerro
        if (s.equalsIgnoreCase("selection-clear-view")) {
59
            IView vista = (IView) document.getMainComponent();
60
            MapContext mapa = document.getMapContext();
61
            MapControl mapCtrl = vista.getMapControl();
62
            FLayers layers = mapa.getLayers();
63
            boolean refresh = clearSelectionOfView(layers);
64
            if (refresh) {
65
                mapCtrl.drawMap(false);
66
            }
67
            document.setModified(true);
68
        }
69 40435 jjdelcerro
    }
70
71 41248 jjdelcerro
    private boolean clearSelectionOfView(FLayers layers) {
72
        boolean refresh = false;
73 40435 jjdelcerro
74 41248 jjdelcerro
        for (int i = 0; i < layers.getLayersCount(); i++) {
75
            FLayer lyr = layers.getLayer(i);
76
            if (lyr instanceof FLayers) {
77
                refresh = refresh || clearSelectionOfView((FLayers) lyr);
78
            } else if (lyr instanceof FLyrVect) {
79
                FLyrVect lyrVect = (FLyrVect) lyr;
80
                if (lyrVect.isActive()) {
81
                    try {
82
                        FeatureStore featureStore;
83 40435 jjdelcerro
84 41248 jjdelcerro
                        featureStore = ((FLyrVect) lyr).getFeatureStore();
85
                        if (!featureStore.getFeatureSelection().isEmpty()) {
86
                            refresh = true;
87
                        }
88
                        featureStore.getFeatureSelection().deselectAll();
89
                    } catch (ReadException e) {
90
                        e.printStackTrace();
91
                    } catch (DataException e) {
92
                        // TODO Auto-generated catch block
93
                        e.printStackTrace();
94
                    }
95
                }
96
            }
97
        }
98
        return refresh;
99
    }
100 40435 jjdelcerro
101 41248 jjdelcerro
    public boolean isVisible() {
102
        ApplicationManager application = ApplicationLocator.getManager();
103 40435 jjdelcerro
104 41248 jjdelcerro
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
105
        if (document == null) {
106
            return false;
107
        }
108 40435 jjdelcerro
109 41248 jjdelcerro
        MapContext mapa = document.getMapContext();
110
        return mapa.getLayers().getLayersCount() > 0;
111
    }
112 40435 jjdelcerro
113 41248 jjdelcerro
    public boolean isEnabled() {
114
        ApplicationManager application = ApplicationLocator.getManager();
115 40435 jjdelcerro
116 41248 jjdelcerro
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
117
        if (document == null) {
118
            return false;
119
        }
120 40435 jjdelcerro
121 41248 jjdelcerro
        MapContext mapa = document.getMapContext();
122
        return hasVectorLayersWithSelection(mapa.getLayers());
123
    }
124
125
    private boolean hasVectorLayersWithSelection(FLayers layers) {
126
        for (int i = 0; i < layers.getLayersCount(); i++) {
127
            FLayer lyr = layers.getLayer(i);
128
            if (lyr instanceof FLayers) {
129
                if (hasVectorLayersWithSelection((FLayers) lyr)) {
130
                    return true;
131
                }
132
            } else if (lyr instanceof FLyrVect) {
133
                FLyrVect lyrVect = (FLyrVect) lyr;
134
                if (lyrVect.isActive()) {
135
                    if (lyrVect.isAvailable()) {
136
                        try {
137
                            if (!lyrVect.getFeatureStore().getFeatureSelection().isEmpty()) {
138
                                return true;
139
                            }
140
                        } catch (DataException e) {
141
                            e.printStackTrace();
142
                            NotificationManager.addWarning("Capa " + lyrVect.getName() + " sin recordset correcto", e);
143
                        }
144
                    }
145
                }
146
            }
147
        }
148
        return false;
149
    }
150
151
    /**
152
     * @see org.gvsig.andami.plugins.IExtension#initialize()
153
     */
154
    public void initialize() {
155
        IconThemeHelper.registerIcon("action", "edit-clear", this);
156
    }
157 40435 jjdelcerro
}