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 / AddToLocatorExtension.java @ 44129

History | View | Annotate | Download (4.36 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.ProjectManager;
31
import org.gvsig.app.project.documents.view.ViewDocument;
32
import org.gvsig.app.project.documents.view.ViewManager;
33
import org.gvsig.app.project.documents.view.toc.gui.FPopupMenu;
34
import org.gvsig.fmap.mapcontext.MapContext;
35
import org.gvsig.fmap.mapcontext.layers.FLayer;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.i18n.I18nManager;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
/**
42
 * Extensi?n que a?ade una capa al locator
43
 *
44
 */
45
public class AddToLocatorExtension extends Extension {
46

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

    
49
    @Override
50
    public boolean isVisible() {
51
        ApplicationManager application = ApplicationLocator.getManager();
52
        ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
53
        if (view != null) {
54
            FLayer[] layers = view.getMapContext().getLayers().getActives();
55
            return layers != null && layers.length > 0;
56
        }
57
        return false;
58
    }
59

    
60
    @Override
61
    public void postInitialize() {
62
        super.postInitialize();
63
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
64
        ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
65
        viewManager.addTOCContextAction("layer-addtolocator");
66
    }
67

    
68
    @Override
69
    public void execute(String actionCommand) {
70
        this.execute(actionCommand, null);
71
    }
72

    
73
    @Override
74
    public void execute(String command, Object[] args) {
75
        ApplicationManager application = ApplicationLocator.getManager();
76
        I18nManager i18nManager = ToolsLocator.getI18nManager();
77
        if ("layer-addtolocator".equalsIgnoreCase(command)) {
78
            ViewDocument view = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
79
            if (view == null) {
80
                return;
81
            }
82
            MapContext mapOverViewContext = view.getMapOverViewContext();
83
            FLayer[] layers = view.getMapContext().getLayers().getActives();
84
            for (int i = 0; i < layers.length; i++) {
85
                FLayer layer = layers[i];
86
                try {
87
                    mapOverViewContext.getLayers().addLayer(layer.cloneLayer());
88
                    mapOverViewContext.invalidate();
89
                    view.setModified(true);
90
                } catch (Exception ex) {
91
                    logger.warn("Can't add layer '" + layer.getName() + "' to the overview.", ex);
92
                    application.messageDialog(
93
                            i18nManager.getTranslation("_Add_to_locator"),
94
                            i18nManager.getTranslation("_Cant_add_layer_{0}_to_locator", new String[] {layer.getName()})
95
                                + "\n\n"
96
                                + i18nManager.getTranslation("_See_the_error_log_for_more_information"),
97
                            JOptionPane.WARNING_MESSAGE
98
                    );
99
                }
100
            }
101
        }
102
    }
103

    
104
    @Override
105
    public boolean isEnabled() {
106
        return true;
107
    }
108

    
109
    @Override
110
    public void initialize() {
111
        IconThemeHelper.registerIcon("action", "layer-addtolocator", this);
112
    }
113
}