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 / clipboard / CopyFeaturesToClipboardExtension.java @ 44655

History | View | Annotate | Download (4.48 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.extension.clipboard;
25

    
26
import javax.json.JsonArray;
27
import javax.swing.JOptionPane;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.andami.IconThemeHelper;
33
import org.gvsig.andami.plugins.Extension;
34
import org.gvsig.app.ApplicationLocator;
35
import org.gvsig.app.ApplicationManager;
36
import org.gvsig.app.project.documents.view.ViewDocument;
37
import org.gvsig.app.project.documents.view.ViewManager;
38
import org.gvsig.fmap.dal.feature.FeatureSelection;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.i18n.I18nManager;
43

    
44
public class CopyFeaturesToClipboardExtension extends Extension {
45

    
46
    private static final Logger LOGGER = LoggerFactory.getLogger(CopyFeaturesToClipboardExtension.class);
47

    
48
    public void initialize() {
49

    
50
        IconThemeHelper.registerIcon("action", "layer-modify-clipboard-copy", this);
51
    }
52

    
53
    public void execute(String actionCommand) {
54

    
55
        if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-copy") != 0) {
56
            return;
57
        }
58
        ApplicationManager application = ApplicationLocator.getManager();
59
        ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
60
        if( viewdoc == null ) {
61
            return;
62
        }
63
        MapContext mapContext = viewdoc.getMapContext();
64
        FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer();
65
        if( layer==null ) {
66
            return;
67
        }
68
        try {
69
            FeatureSelection selection = layer.getFeatureStore().getFeatureSelection();
70

    
71
            JsonArray json = selection.toJSON();
72
            application.putInClipboard(json.toString());
73

    
74
            I18nManager i18n = ToolsLocator.getI18nManager();
75
            application.messageDialog(
76
                    i18n.getTranslation("_Number_of_features_copied_to_clipboard")
77
                        + ":   " + selection.getSize(),
78
                    null, 
79
                    i18n.getTranslation("_Copy_selected_features_to_clipboard"),
80
                    JOptionPane.INFORMATION_MESSAGE, 
81
                    "CopyFeaturesToClipboard"
82
            );
83

    
84
        } catch (Exception e) {
85
            LOGGER.warn("Can't get the selection as JSON.", e);
86
        }
87
    }
88

    
89
    public boolean isEnabled() {
90
        // it's enabled if there is exactly one vector layer in the active view
91
        // and it has a selection
92

    
93
        ApplicationManager application = ApplicationLocator.getManager();
94
        ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
95
        if( viewdoc == null ) {
96
            return false;
97
        }
98
        MapContext mapContext = viewdoc.getMapContext();
99
        FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer();
100
        if( layer==null ) {
101
            return false;
102
        }
103
        if (!layer.isAvailable()) {
104
            // This can happen when opening a persisted project
105
            // and there is a "slow" layer (GeoDB)
106
            return false;
107
        }
108
        try {
109
            FeatureSelection selection = layer.getFeatureStore().getFeatureSelection();
110
            return !selection.isEmpty();
111
        } catch (Exception ex) {
112
            LOGGER.warn("Can't get selection from layer '"+layer.getName()+"'.", ex);
113
            return false;
114
        }
115
    }
116

    
117
    public boolean isVisible() {
118
        ApplicationManager application = ApplicationLocator.getManager();
119
        return application.getActiveDocument(ViewManager.TYPENAME)!=null;
120
    }
121

    
122
}