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 @ 46277

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
    @Override
49
    public void initialize() {
50

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

    
54
    @Override
55
    public void execute(String actionCommand) {
56

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

    
73
            JsonArray json = selection.toJSON();
74
            application.putInClipboard(json.toString());
75

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

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

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

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

    
119
    @Override
120
    public boolean isVisible() {
121
        ApplicationManager application = ApplicationLocator.getManager();
122
        return application.getActiveDocument(ViewManager.TYPENAME)!=null;
123
    }
124

    
125
}