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 / ZoomToSelectExtension.java @ 44259

History | View | Annotate | Download (4.01 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 41248 jjdelcerro
import javax.swing.JOptionPane;
26 40435 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
27
import org.gvsig.andami.plugins.Extension;
28 41248 jjdelcerro
import org.gvsig.app.ApplicationLocator;
29
import org.gvsig.app.ApplicationManager;
30
import org.gvsig.app.project.documents.view.ViewDocument;
31 41264 jjdelcerro
import org.gvsig.app.project.documents.view.gui.IView;
32 40435 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
33
import org.gvsig.fmap.mapcontext.MapContext;
34
import org.gvsig.fmap.mapcontext.layers.FLayer;
35
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
36
import org.gvsig.tools.exception.BaseException;
37 41248 jjdelcerro
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39 40435 jjdelcerro
40
/**
41
 * Extensi?n de zoom a lo seleccionado teniendo como ventana activa una vista.
42 41248 jjdelcerro
 *
43 40435 jjdelcerro
 */
44
public class ZoomToSelectExtension extends Extension {
45
46 41248 jjdelcerro
    private static final Logger logger = LoggerFactory
47
            .getLogger(ZoomToSelectExtension.class);
48 40435 jjdelcerro
49 41248 jjdelcerro
    public void initialize() {
50
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-to-selection", this);
51
    }
52 40435 jjdelcerro
53 41248 jjdelcerro
    public void execute(String s) {
54
        ApplicationManager application = ApplicationLocator.getManager();
55 40435 jjdelcerro
56 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
57
        if (view == null) {
58 41248 jjdelcerro
            return;
59
        }
60 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
61 41248 jjdelcerro
        MapContext mapa = document.getMapContext();
62
        Envelope selectedExtent = null;
63
        try {
64
            selectedExtent = mapa.getSelectionBounds();
65
            mapa.getViewPort().setEnvelope(selectedExtent);
66
        } catch (BaseException e) {
67
            String msg = "Can't zoom to the seleccion.";
68
            logger.warn(msg, e);
69
            application.message(msg, JOptionPane.WARNING_MESSAGE);
70 40435 jjdelcerro
        }
71 41248 jjdelcerro
    }
72 40435 jjdelcerro
73 41248 jjdelcerro
    public boolean isVisible() {
74
        ApplicationManager application = ApplicationLocator.getManager();
75 40435 jjdelcerro
76 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
77
        if (view == null) {
78 41248 jjdelcerro
            return false;
79
        }
80 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
81 41248 jjdelcerro
        return document.getMapContext().getLayers().getLayersCount() > 0;
82
    }
83 40435 jjdelcerro
84 41248 jjdelcerro
    public boolean isEnabled() {
85
        ApplicationManager application = ApplicationLocator.getManager();
86
87 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
88
        if (view == null) {
89 41248 jjdelcerro
            return false;
90
        }
91 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
92 41248 jjdelcerro
        FLayer[] selected = document.getMapContext().getLayers().getActives();
93
        if (selected.length == 1 && selected[0] instanceof FLyrVect && selected[0].isAvailable()) {
94
            FLyrVect layer = (FLyrVect) selected[0];
95
            try {
96
                if (!layer.getFeatureStore().getFeatureSelection().isEmpty()) {
97
                    return true;
98
                }
99
            } catch (Exception e) {
100
                String msg = "Can't check if selection if empty in layer '" + layer.getName() + "'.";
101
                logger.warn(msg, e);
102
                application.message(msg, JOptionPane.WARNING_MESSAGE);
103
            }
104
        }
105
        return false;
106
    }
107
108 40435 jjdelcerro
}