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 / ZoomToSelectViewExtension.java @ 47676

History | View | Annotate | Download (4.25 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 javax.swing.JOptionPane;
26
import org.gvsig.andami.IconThemeHelper;
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.feature.FeatureSelection;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
38
import org.gvsig.tools.exception.BaseException;
39

    
40
/**
41
 * Extensi?n de zoom a lo seleccionado teniendo como ventana activa una vista.
42
 *
43
 */
44
@SuppressWarnings("UseSpecificCatch")
45
public class ZoomToSelectViewExtension extends Extension {
46

    
47
//    private static final Logger logger = LoggerFactory.getLogger(ZoomToSelectExtension.class);
48

    
49
    @Override
50
    public void initialize() {
51
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-to-selection", this);
52
    }
53

    
54
    @Override
55
    public void execute(String s) {
56
        ApplicationManager application = ApplicationLocator.getManager();
57

    
58
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
59
        if (view == null) {
60
            return;
61
        }
62
        ViewDocument document = view.getViewDocument();
63
        MapContext mapa = document.getMapContext();
64
        Envelope selectedExtent;
65
        try {
66
            selectedExtent = mapa.getSelectionBounds();
67
            mapa.zoomToEnvelope(selectedExtent);
68
        } catch (BaseException e) {
69
            String msg = "Can't zoom to the seleccion.";
70
            logger.warn(msg, e);
71
            application.message(msg, JOptionPane.WARNING_MESSAGE);
72
        }
73
    }
74

    
75
    @Override
76
    public boolean isVisible() {
77
        ApplicationManager application = ApplicationLocator.getManager();
78

    
79
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
80
        if (document == null) {
81
            return false;
82
        }
83
        return document.getMapContext().getLayers().getLayersCount() > 0;
84
    }
85

    
86
    @Override
87
    public boolean isEnabled() {
88
        ApplicationManager application = ApplicationLocator.getManager();
89

    
90
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
91
        if (view == null) {
92
            return false;
93
        }
94
        ViewDocument document = view.getViewDocument();
95
        if( document == null ) {
96
            return false;
97
        }
98
        boolean hasActiveVectorLayers = false;
99
        boolean hasSelection = false;
100
        for (FLayer layer : document.getMapContext().getLayers().getActives()) {
101
            if( layer.isActive() && layer.isAvailable() && layer instanceof FLyrVect ) {
102
                try {
103
                    hasActiveVectorLayers = true;
104
                    FeatureStore store = ((FLyrVect)layer).getFeatureStore();
105
                    if( store.isFeatureSelectionAvailable()) {
106
                        if( !store.isFeatureSelectionEmpty() ) {
107
                            hasSelection = true;
108
                        }
109
                    }
110
                } catch (Exception ex) {
111
                }
112
            }
113
        }
114
        return hasActiveVectorLayers && hasSelection;
115
    }
116

    
117
}