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

History | View | Annotate | Download (9.98 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.PluginsLocator;
41
import org.gvsig.andami.ui.mdiManager.IWindow;
42
import org.gvsig.app.extension.ProjectExtension;
43
import org.gvsig.app.project.Project;
44
import org.gvsig.fmap.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.layers.CancelationException;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontext.layers.FLayers;
48
import org.gvsig.i18n.Messages;
49
import org.gvsig.tools.ToolsLocator;
50
import org.gvsig.tools.persistence.PersistenceManager;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.gvsig.tools.persistence.exception.PersistenceException;
53
import org.gvsig.tools.persistence.impl.exception.ObjectNotFoundException;
54

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

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

    
69
    /**
70
     * The file which is used to save the layers
71
     */
72
    private static String CLIPBOARD_FILE_NAME = "gvSIG_layers_clipboard.tmp";
73
    private static File clipboardFolder = null;
74

    
75
    private CopyPasteLayersUtils() {
76
    }
77
    
78
    private static PersistenceManager getPersMan() {
79
        return ToolsLocator.getPersistenceManager();
80
    }
81
    
82
    private static File getClipboardFolder() {
83
        if (clipboardFolder == null) {
84
            File appf = PluginsLocator.getManager().getApplicationHomeFolder();
85
            clipboardFolder = new File(appf, "clipboard-layers");
86
            clipboardFolder.mkdirs();
87
        }
88
        return clipboardFolder;
89
        // 
90
        
91
    }
92
    /**
93
     * Gets the outputstream for writing contents to the
94
     * clipboard
95
     * 
96
     * @return
97
     * @throws IOException
98
     */
99
    private static OutputStream getClipboardOStream() throws IOException {
100
        
101
        File f = new File(getClipboardFolder(), CLIPBOARD_FILE_NAME);
102
        if (f.exists()) {
103
            /*
104
             * If file exists, it is removed
105
             * (clipboard content overwritten)
106
             */
107
            f.delete();
108
        }
109
        f.createNewFile();
110
        /*
111
         * File will be removed on exit
112
         */
113
        f.deleteOnExit();
114
        return new FileOutputStream(f);
115
    }
116
    
117
    public static void clearClipboard() throws IOException {
118

    
119
        File f = new File(getClipboardFolder(), CLIPBOARD_FILE_NAME);
120
        if (f.exists()) {
121
            f.delete();
122
        }
123
    }
124

    
125
    /**
126
     * Gets the input stream to read the clipboard or null
127
     * if clipboard is empty
128
     * 
129
     * @return
130
     * @throws IOException
131
     */
132
    private static InputStream getClipboardIStream() throws IOException {
133

    
134
        File f = new File(getClipboardFolder(), CLIPBOARD_FILE_NAME);
135
        if (f.exists()) {
136
            return new FileInputStream(f);
137
        } else {
138
            return null;
139
        }
140

    
141
    }
142

    
143
    /**
144
     * Returns the content of the clipboard as an instance
145
     * of FLayers or null if clipboard is empty or does not
146
     * contain instance of FLayers
147
     * 
148
     * @return
149
     * @throws PersistenceException
150
     */
151
    public static FLayers getClipboardAsFLayers() throws PersistenceException {
152

    
153
        InputStream is = null;
154

    
155
        try {
156
            is = getClipboardIStream();
157
            
158
            if (is == null) {
159
                return null;
160
            }
161
            
162
            Object obj = getPersMan().getObject(is);
163
            is.close();
164
            if (obj instanceof FLayers) {
165
                return (FLayers) obj;
166
            } else {
167
                return null;
168
            }
169
        } catch (Exception e) {
170
            logger.info("While getting object from clipboard: ", e);
171
            throw new PersistenceException(e);
172
        }
173

    
174
    }
175

    
176
    /**
177
     * Stores the given {@link PersistentState}
178
     * to clipboard
179
     * 
180
     * @param st
181
     * @throws PersistenceException
182
     */
183
    public static void saveToClipboard(PersistentState st)
184
        throws PersistenceException {
185

    
186
        OutputStream os = null;
187
        try {
188
            os = getClipboardOStream();
189
            getPersMan().saveState(st, os);
190
            os.close();
191
            if (st.getContext().getErrors() != null) {
192
                throw st.getContext().getErrors();
193
            }
194
        } catch (Exception ex) {
195
            throw new PersistenceException(ex);
196
        }
197
    }
198

    
199
    /**
200
     * Gets an array of layers as a {@link PersistentState}
201
     * of an instance of FLayers
202
     * 
203
     * @param actives
204
     * @param ctxt
205
     * @return
206
     * @throws PersistenceException
207
     */
208
    public static PersistentState getAsFLayersPersistentState(FLayer[] actives,
209
        MapContext ctxt) throws PersistenceException {
210

    
211
        FLayers lyrs = new FLayers();
212
        lyrs.setMapContext(ctxt);
213
        lyrs.setName("copy-paste-root");
214

    
215
        FLayer item = null;
216
        if (actives != null) {
217
            for (int i = 0; i < actives.length; i++) {
218
                item = actives[i];
219
                try {
220
                    item = item.cloneLayer();
221
                } catch (Exception ex) {
222
                    throw new PersistenceException(ex);
223
                }
224
                lyrs.addLayer(item);
225
            }
226
        }
227

    
228
        PersistentState state = null;
229
        state = getPersMan().getState(lyrs, true);
230

    
231
        if (state.getContext().getErrors() != null) {
232
            throw state.getContext().getErrors();
233
        }
234
        return state;
235
    }
236

    
237
    /**
238
     * Remosves the layers from the their parent
239
     * @param actives
240
     * @param mc
241
     * @return
242
     */
243
    public static boolean removeLayers(FLayer[] actives, MapContext mc) {
244

    
245
        if (actives == null || actives.length == 0) {
246
            return false;
247
        }
248

    
249
        for (int i = 0; i < actives.length; i++) {
250
            if (actives[i].isEditing() && actives[i].isAvailable()) {
251
                JOptionPane.showMessageDialog(
252
                    (Component) PluginServices.getMainFrame(),
253
                    Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
254
                    Messages.getText("eliminar_capa"),
255
                    JOptionPane.WARNING_MESSAGE);
256
                return false;
257
            }
258
        }
259

    
260
        mc.beginAtomicEvent();
261
        for (int i = actives.length - 1; i >= 0; i--) {
262
            try {
263
                // actives[i].getParentLayer().removeLayer(actives[i]);
264
                // FLayers lyrs=getMapContext().getLayers();
265
                // lyrs.addLayer(actives[i]);
266
                actives[i].getParentLayer().removeLayer(actives[i]);
267

    
268
                // Cierra todas las ventanas asociadas a la capa
269
                IWindow[] wList =
270
                    PluginServices.getMDIManager().getAllWindows();
271
                for (int j = 0; j < wList.length; j++) {
272
                    String name = wList[j].getWindowInfo().getAdditionalInfo();
273
                    for (int k = 0; k < actives.length; k++) {
274
                        if (name != null && actives != null
275
                            && actives[k] != null
276
                            && actives[k].getName() != null
277
                            && name.compareTo(actives[k].getName()) == 0)
278
                            PluginServices.getMDIManager()
279
                                .closeWindow(wList[j]);
280
                    }
281
                }
282

    
283
            } catch (CancelationException e1) {
284
                JOptionPane.showMessageDialog((Component) PluginServices
285
                    .getMainFrame(), Messages
286
                    .getText("No_ha_sido_posible_realizar_la_operacion"),
287
                    Messages.getText("eliminar_capa"),
288
                    JOptionPane.WARNING_MESSAGE);
289
                logger.info("Error while removing layers.", e1);
290
                return false;
291
            }
292
        }
293
        mc.endAtomicEvent();
294
        Project project =
295
            ((ProjectExtension) PluginServices
296
                .getExtension(ProjectExtension.class)).getProject();
297
        project.setModified(true);
298
        PluginServices.getMainFrame().enableControls();
299
        return true;
300

    
301
    }
302

    
303
    /**
304
     * Adds layers to target {@link FLayers}
305
     * 
306
     * @param clipboard_root
307
     * @param target_root
308
     * @return
309
     */
310
    public static boolean addLayers(FLayers clipboard_root, FLayers target_root) {
311

    
312
        if (clipboard_root == null || clipboard_root.getLayersCount() == 0) {
313
            return false;
314
        }
315

    
316
        int n = clipboard_root.getLayersCount();
317
        FLayer item = null;
318
        for (int i = 0; i < n; i++) {
319
            item = clipboard_root.getLayer(i);
320
            target_root.addLayer(item);
321
        }
322
        return true;
323
    }
324

    
325
}