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 / project / documents / view / toc / actions / CopyPasteLayersUtils.java @ 40558

History | View | Annotate | Download (9.53 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.project.documents.view.toc.actions;
25

    
26
import java.awt.Component;
27
import java.io.File;
28
import java.io.FileInputStream;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33

    
34
import javax.swing.JOptionPane;
35

    
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
import org.gvsig.app.extension.ProjectExtension;
42
import org.gvsig.app.project.Project;
43
import org.gvsig.fmap.mapcontext.MapContext;
44
import org.gvsig.fmap.mapcontext.layers.CancelationException;
45
import org.gvsig.fmap.mapcontext.layers.FLayer;
46
import org.gvsig.fmap.mapcontext.layers.FLayers;
47
import org.gvsig.i18n.Messages;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
import org.gvsig.tools.persistence.impl.exception.ObjectNotFoundException;
53

    
54
/**
55
 * This class provides utility methods to manage the TOC clipboard
56
 * which will contain one or more layers for copy/cut/paste
57
 * operations. It is currently implemented using a persistence
58
 * file where an instance of FLayers is stored.
59
 * 
60
 * @author jldominguez
61
 * 
62
 */
63
public class CopyPasteLayersUtils {
64

    
65
    private static Logger logger = LoggerFactory
66
        .getLogger(CopyPasteLayersUtils.class);
67

    
68
    /**
69
     * The file which is used to save the layers
70
     */
71
    private static String CLIPBOARD_FILE_NAME = "gvSIG_clipboard.tmp";
72

    
73
    private static/* XML */PersistenceManager persManager = ToolsLocator
74
        .getPersistenceManager();
75

    
76
    private CopyPasteLayersUtils() {
77
    }
78

    
79
    /**
80
     * Gets the outputstream for writing contents to the
81
     * clipboard
82
     * 
83
     * @return
84
     * @throws IOException
85
     */
86
    private static OutputStream getClipboardOStream() throws IOException {
87
        String strf =
88
            System.getProperty("user.home") + File.separator + "gvSIG"
89
                + File.separator + CLIPBOARD_FILE_NAME;
90
        File f = new File(strf);
91
        if (f.exists()) {
92
            /*
93
             * If file exists, it is removed
94
             * (clipboard content overwritten)
95
             */
96
            f.delete();
97
        }
98
        /*
99
         * File will be removed on exit
100
         */
101
        f.deleteOnExit();
102
        return new FileOutputStream(f);
103
    }
104

    
105
    /**
106
     * Gets the input stream to read the clipboard or null
107
     * if clipboard is empty
108
     * 
109
     * @return
110
     * @throws IOException
111
     */
112
    private static InputStream getClipboardIStream() throws IOException {
113
        String strf =
114
            System.getProperty("user.home") + File.separator + "gvSIG"
115
                + File.separator + CLIPBOARD_FILE_NAME;
116
        File f = new File(strf);
117
        if (f.exists()) {
118
            return new FileInputStream(f);
119
        } else {
120
            return null;
121
        }
122

    
123
    }
124

    
125
    /**
126
     * Returns the content of the clipboard as an instance
127
     * of FLayers or null if clipboard is empty or does not
128
     * contain instance of FLayers
129
     * 
130
     * @return
131
     * @throws PersistenceException
132
     */
133
    public static FLayers getClipboardAsFLayers() throws PersistenceException {
134

    
135
        InputStream is = null;
136

    
137
        try {
138
            is = getClipboardIStream();
139
            
140
            if (is == null) {
141
                return null;
142
            }
143
            
144
            Object obj = persManager.getObject(is);
145
            is.close();
146
            if (obj instanceof FLayers) {
147
                return (FLayers) obj;
148
            } else {
149
                return null;
150
            }
151
        } catch (Exception e) {
152
            logger.info("While getting object from clipboard: ", e);
153
            throw new PersistenceException(e);
154
        }
155

    
156
    }
157

    
158
    /**
159
     * Stores the given {@link PersistentState}
160
     * to clipboard
161
     * 
162
     * @param st
163
     * @throws PersistenceException
164
     */
165
    public static void saveToClipboard(PersistentState st)
166
        throws PersistenceException {
167

    
168
        OutputStream os = null;
169
        try {
170
            os = getClipboardOStream();
171
            persManager.saveState(st, os);
172
            os.close();
173
            if (st.getContext().getErrors() != null) {
174
                throw st.getContext().getErrors();
175
            }
176
        } catch (Exception ex) {
177
            throw new PersistenceException(ex);
178
        }
179
    }
180

    
181
    /**
182
     * Gets an array of layers as a {@link PersistentState}
183
     * of an instance of FLayers
184
     * 
185
     * @param actives
186
     * @param ctxt
187
     * @return
188
     * @throws PersistenceException
189
     */
190
    public static PersistentState getAsFLayersPersistentState(FLayer[] actives,
191
        MapContext ctxt) throws PersistenceException {
192

    
193
        FLayers lyrs = new FLayers();
194
        lyrs.setMapContext(ctxt);
195
        lyrs.setName("copy-paste-root");
196

    
197
        FLayer item = null;
198
        if (actives != null) {
199
            for (int i = 0; i < actives.length; i++) {
200
                item = actives[i];
201
                try {
202
                    item = item.cloneLayer();
203
                } catch (Exception ex) {
204
                    throw new PersistenceException(ex);
205
                }
206
                lyrs.addLayer(item);
207
            }
208
        }
209

    
210
        PersistentState state = null;
211
        state = persManager.getState(lyrs, true);
212

    
213
        if (state.getContext().getErrors() != null) {
214
            throw state.getContext().getErrors();
215
        }
216
        return state;
217
    }
218

    
219
    /**
220
     * Remosves the layers from the their parent
221
     * @param actives
222
     * @param mc
223
     * @return
224
     */
225
    public static boolean removeLayers(FLayer[] actives, MapContext mc) {
226

    
227
        if (actives == null || actives.length == 0) {
228
            return false;
229
        }
230

    
231
        for (int i = 0; i < actives.length; i++) {
232
            if (actives[i].isEditing() && actives[i].isAvailable()) {
233
                JOptionPane.showMessageDialog(
234
                    (Component) PluginServices.getMainFrame(),
235
                    Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
236
                    Messages.getText("eliminar_capa"),
237
                    JOptionPane.WARNING_MESSAGE);
238
                return false;
239
            }
240
        }
241

    
242
        mc.beginAtomicEvent();
243
        for (int i = actives.length - 1; i >= 0; i--) {
244
            try {
245
                // actives[i].getParentLayer().removeLayer(actives[i]);
246
                // FLayers lyrs=getMapContext().getLayers();
247
                // lyrs.addLayer(actives[i]);
248
                actives[i].getParentLayer().removeLayer(actives[i]);
249

    
250
                // Cierra todas las ventanas asociadas a la capa
251
                IWindow[] wList =
252
                    PluginServices.getMDIManager().getAllWindows();
253
                for (int j = 0; j < wList.length; j++) {
254
                    String name = wList[j].getWindowInfo().getAdditionalInfo();
255
                    for (int k = 0; k < actives.length; k++) {
256
                        if (name != null && actives != null
257
                            && actives[k] != null
258
                            && actives[k].getName() != null
259
                            && name.compareTo(actives[k].getName()) == 0)
260
                            PluginServices.getMDIManager()
261
                                .closeWindow(wList[j]);
262
                    }
263
                }
264

    
265
            } catch (CancelationException e1) {
266
                JOptionPane.showMessageDialog((Component) PluginServices
267
                    .getMainFrame(), Messages
268
                    .getText("No_ha_sido_posible_realizar_la_operacion"),
269
                    Messages.getText("eliminar_capa"),
270
                    JOptionPane.WARNING_MESSAGE);
271
                logger.info("Error while removing layers.", e1);
272
                return false;
273
            }
274
        }
275
        mc.endAtomicEvent();
276
        Project project =
277
            ((ProjectExtension) PluginServices
278
                .getExtension(ProjectExtension.class)).getProject();
279
        project.setModified(true);
280
        PluginServices.getMainFrame().enableControls();
281
        return true;
282

    
283
    }
284

    
285
    /**
286
     * Adds layers to target {@link FLayers}
287
     * 
288
     * @param clipboard_root
289
     * @param target_root
290
     * @return
291
     */
292
    public static boolean addLayers(FLayers clipboard_root, FLayers target_root) {
293

    
294
        if (clipboard_root == null || clipboard_root.getLayersCount() == 0) {
295
            return false;
296
        }
297

    
298
        int n = clipboard_root.getLayersCount();
299
        FLayer item = null;
300
        for (int i = 0; i < n; i++) {
301
            item = clipboard_root.getLayer(i);
302
            target_root.addLayer(item);
303
        }
304
        return true;
305
    }
306

    
307
}