Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / clipboard / CopyFeaturesToClipboardExtension.java @ 40666

History | View | Annotate | Download (4.82 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.editing.clipboard;
25

    
26
import java.awt.Component;
27
import java.util.List;
28
import java.util.prefs.Preferences;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.andami.IconThemeHelper;
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.messages.NotificationManager;
38
import org.gvsig.andami.plugins.Extension;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.gui.preferencespage.GridPage;
42
import org.gvsig.app.project.documents.view.ViewDocument;
43
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
44
import org.gvsig.app.project.documents.view.gui.IView;
45
import org.gvsig.editing.clipboard.util.FeatureTextUtils;
46
import org.gvsig.editing.gui.cad.CADTool;
47
import org.gvsig.editing.gui.tokenmarker.ConsoleToken;
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.exception.ReadException;
50
import org.gvsig.fmap.dal.feature.FeatureSelection;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
54
import org.gvsig.fmap.mapcontrol.MapControl;
55
import org.gvsig.i18n.Messages;
56
import org.gvsig.utils.console.jedit.KeywordMap;
57
import org.gvsig.utils.console.jedit.Token;
58

    
59

    
60
public class CopyFeaturesToClipboardExtension extends Extension {
61
    
62
    private static Logger logger = LoggerFactory.getLogger(
63
                    CopyFeaturesToClipboardExtension.class);
64

    
65
        public void initialize() {
66
                
67
                IconThemeHelper.registerIcon("action", "layer-modify-clipboard-copy", this);
68
        }
69

    
70
        public void execute(String actionCommand) {
71

    
72
                if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-copy") != 0) {
73
                        return;
74
                }
75
                
76
                IWindow actw = actWin();
77
                
78
                if (actw instanceof IView) {
79
                        
80
                        IView vw = (IView) actw;
81
                        FLayer[] act_lyr = vw.getMapControl().getMapContext().getLayers().getActives();
82
                        if (act_lyr == null || act_lyr.length != 1
83
                                        || !(act_lyr[0] instanceof FLyrVect)) {
84
                                
85
                        } else {
86
                                FLyrVect vect = (FLyrVect) act_lyr[0];
87
                                FeatureSelection sele = null;
88
                                FeatureStore fsto = null;
89
                                try {
90
                                        fsto = vect.getFeatureStore();
91
                                        sele = (FeatureSelection) fsto.getSelection();
92
                                        StringBuilder strb = FeatureTextUtils.toString(
93
                                                        sele, fsto.getDefaultFeatureType());
94
                                        /*
95
                                         * Put selected features in clipboard.
96
                                         */
97
                                        PluginServices.putInClipboard(strb.toString());
98

    
99
                                        JOptionPane.showMessageDialog(
100
                                                        ApplicationLocator.getManager().getRootComponent(),
101
                                                        Messages.getText("_Number_of_features_copied_to_clipboard")
102
                                                        + ":   " + sele.getSize() + "    ",
103
                                                        Messages.getText("_Copy_selected_features_to_clipboard"),
104
                                                        JOptionPane.INFORMATION_MESSAGE);
105
                                        
106
                                } catch (DataException e) {
107
                                        logger.error("While getting store and selection. ", e);
108
                                }
109
                        }
110
                }                
111
                
112
        }
113

    
114
        public boolean isEnabled() {
115
                
116
                /*
117
                 * It's enabled if there is exactly one vector layer in the active view
118
                 * and it has a selection 
119
                 */
120
                
121
                IWindow actw = actWin();
122
                
123
                if (actw instanceof IView) {
124
                        
125
                        IView vw = (IView) actw;
126
                        FLayer[] act_lyr = vw.getMapControl().getMapContext().getLayers().getActives();
127
                        if (act_lyr == null || act_lyr.length != 1
128
                                        || !(act_lyr[0] instanceof FLyrVect)) {
129
                                return false;
130
                                
131
                        } else {
132
                                FLyrVect vect = (FLyrVect) act_lyr[0];
133
                                FeatureSelection sele = null;
134
                                long sele_count = 0;
135
                                try {
136
                                        sele = (FeatureSelection) vect.getFeatureStore().getSelection();
137
                                        sele_count = sele.getSize(); 
138
                                } catch (DataException e) {
139
                                        logger.error("While getting store. ", e);
140
                                        return false;
141
                                }
142
                                return sele_count > 0;
143
                        }
144
                        
145
                } else {
146
                        return false;
147
                }
148
        }
149

    
150
        public boolean isVisible() {
151

    
152
                return actWin() instanceof IView;
153
        }
154
        
155
        /**
156
         * Gets active window
157
         * @return
158
         */
159
        private IWindow actWin() {
160
                return ApplicationLocator.getManager().getActiveWindow();
161
        }
162

    
163

    
164
}