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 @ 44531
History | View | Annotate | Download (4.4 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 | 44437 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
33 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
35 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
36 | import org.gvsig.fmap.mapcontext.MapContext; |
||
37 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
38 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
39 | import org.gvsig.tools.exception.BaseException; |
||
40 | 41248 | jjdelcerro | import org.slf4j.Logger; |
41 | import org.slf4j.LoggerFactory; |
||
42 | 40435 | jjdelcerro | |
43 | /**
|
||
44 | * Extensi?n de zoom a lo seleccionado teniendo como ventana activa una vista.
|
||
45 | 41248 | jjdelcerro | *
|
46 | 40435 | jjdelcerro | */
|
47 | public class ZoomToSelectExtension extends Extension { |
||
48 | |||
49 | 41248 | jjdelcerro | private static final Logger logger = LoggerFactory |
50 | .getLogger(ZoomToSelectExtension.class); |
||
51 | 40435 | jjdelcerro | |
52 | 41248 | jjdelcerro | public void initialize() { |
53 | IconThemeHelper.registerIcon("action", "view-navigation-zoom-to-selection", this); |
||
54 | } |
||
55 | 40435 | jjdelcerro | |
56 | 41248 | jjdelcerro | public void execute(String s) { |
57 | ApplicationManager application = ApplicationLocator.getManager(); |
||
58 | 40435 | jjdelcerro | |
59 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
60 | if (view == null) { |
||
61 | 41248 | jjdelcerro | return;
|
62 | } |
||
63 | 41264 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
64 | 41248 | jjdelcerro | MapContext mapa = document.getMapContext(); |
65 | Envelope selectedExtent = null;
|
||
66 | try {
|
||
67 | selectedExtent = mapa.getSelectionBounds(); |
||
68 | mapa.getViewPort().setEnvelope(selectedExtent); |
||
69 | } catch (BaseException e) {
|
||
70 | String msg = "Can't zoom to the seleccion."; |
||
71 | logger.warn(msg, e); |
||
72 | application.message(msg, JOptionPane.WARNING_MESSAGE);
|
||
73 | 40435 | jjdelcerro | } |
74 | 41248 | jjdelcerro | } |
75 | 40435 | jjdelcerro | |
76 | 41248 | jjdelcerro | public boolean isVisible() { |
77 | ApplicationManager application = ApplicationLocator.getManager(); |
||
78 | 40435 | jjdelcerro | |
79 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
80 | if (view == null) { |
||
81 | 41248 | jjdelcerro | return false; |
82 | } |
||
83 | 41264 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
84 | 41248 | jjdelcerro | return document.getMapContext().getLayers().getLayersCount() > 0; |
85 | } |
||
86 | 40435 | jjdelcerro | |
87 | 44437 | jjdelcerro | @Override
|
88 | 41248 | jjdelcerro | public boolean isEnabled() { |
89 | ApplicationManager application = ApplicationLocator.getManager(); |
||
90 | |||
91 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
92 | if (view == null) { |
||
93 | 41248 | jjdelcerro | return false; |
94 | } |
||
95 | 41264 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
96 | 44437 | jjdelcerro | if( document == null ) { |
97 | return false; |
||
98 | } |
||
99 | boolean hasActiveVectorLayers = false; |
||
100 | boolean hasSelection = false; |
||
101 | for (FLayer layer : document.getMapContext().getLayers()) {
|
||
102 | if( layer.isActive() && layer.isAvailable() && layer instanceof FLyrVect ) { |
||
103 | try {
|
||
104 | hasActiveVectorLayers = true;
|
||
105 | FeatureStore store = ((FLyrVect)layer).getFeatureStore(); |
||
106 | FeatureSelection selection = store.getFeatureSelection(); |
||
107 | if( !selection.isAvailable() ) {
|
||
108 | return false; |
||
109 | } |
||
110 | if( !selection.isEmpty() ) {
|
||
111 | hasSelection = true;
|
||
112 | } |
||
113 | } catch (Exception ex) { |
||
114 | 41248 | jjdelcerro | } |
115 | } |
||
116 | } |
||
117 | 44437 | jjdelcerro | return hasActiveVectorLayers && hasSelection;
|
118 | 41248 | jjdelcerro | } |
119 | |||
120 | 40435 | jjdelcerro | } |