Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2058 / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / actions / CopyPasteLayersUtils.java @ 39222

History | View | Annotate | Download (8.38 KB)

1
package org.gvsig.app.project.documents.view.toc.actions;
2

    
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.io.OutputStream;
10

    
11
import javax.swing.JOptionPane;
12

    
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

    
16
import org.gvsig.andami.PluginServices;
17
import org.gvsig.andami.ui.mdiManager.IWindow;
18
import org.gvsig.app.extension.ProjectExtension;
19
import org.gvsig.app.project.Project;
20
import org.gvsig.fmap.mapcontext.MapContext;
21
import org.gvsig.fmap.mapcontext.layers.CancelationException;
22
import org.gvsig.fmap.mapcontext.layers.FLayer;
23
import org.gvsig.fmap.mapcontext.layers.FLayers;
24
import org.gvsig.i18n.Messages;
25
import org.gvsig.tools.ToolsLocator;
26
import org.gvsig.tools.persistence.PersistenceManager;
27
import org.gvsig.tools.persistence.PersistentState;
28
import org.gvsig.tools.persistence.exception.PersistenceException;
29
import org.gvsig.tools.persistence.impl.exception.ObjectNotFoundException;
30

    
31
/**
32
 * This class provides utility methods to manage the TOC clipboard
33
 * which will contain one or more layers for copy/cut/paste
34
 * operations. It is currently implemented using a persistence
35
 * file where an instance of FLayers is stored.
36
 * 
37
 * @author jldominguez
38
 * 
39
 */
40
public class CopyPasteLayersUtils {
41

    
42
    private static Logger logger = LoggerFactory
43
        .getLogger(CopyPasteLayersUtils.class);
44

    
45
    /**
46
     * The file which is used to save the layers
47
     */
48
    private static String CLIPBOARD_FILE_NAME = "gvSIG_clipboard.tmp";
49

    
50
    private static/* XML */PersistenceManager persManager = ToolsLocator
51
        .getPersistenceManager();
52

    
53
    private CopyPasteLayersUtils() {
54
    }
55

    
56
    /**
57
     * Gets the outputstream for writing contents to the
58
     * clipboard
59
     * 
60
     * @return
61
     * @throws IOException
62
     */
63
    private static OutputStream getClipboardOStream() throws IOException {
64
        String strf =
65
            System.getProperty("user.home") + File.separator + "gvSIG"
66
                + File.separator + CLIPBOARD_FILE_NAME;
67
        File f = new File(strf);
68
        if (f.exists()) {
69
            /*
70
             * If file exists, it is removed
71
             * (clipboard content overwritten)
72
             */
73
            f.delete();
74
        }
75
        /*
76
         * File will be removed on exit
77
         */
78
        f.deleteOnExit();
79
        return new FileOutputStream(f);
80
    }
81

    
82
    /**
83
     * Gets the input stream to read the clipboard
84
     * 
85
     * @return
86
     * @throws IOException
87
     */
88
    private static InputStream getClipboardIStream() throws IOException {
89
        String strf =
90
            System.getProperty("user.home") + File.separator + "gvSIG"
91
                + File.separator + CLIPBOARD_FILE_NAME;
92
        File f = new File(strf);
93
        if (f.exists()) {
94
            return new FileInputStream(f);
95
        } else {
96
            return null;
97
        }
98

    
99
    }
100

    
101
    /**
102
     * returns the content of the clipboard as an instance
103
     * of FLayers
104
     * @return
105
     * @throws PersistenceException
106
     */
107
    public static FLayers getClipboardAsFLayers() throws PersistenceException {
108

    
109
        InputStream is = null;
110

    
111
        try {
112
            is = getClipboardIStream();
113
            Object obj = persManager.getObject(is);
114
            is.close();
115
            if (obj instanceof FLayers) {
116
                return (FLayers) obj;
117
            } else {
118
                throw new ObjectNotFoundException();
119
            }
120
        } catch (Exception e) {
121
            logger.info("Clipboard file not found.", e);
122
            throw new PersistenceException(e);
123
        }
124

    
125
    }
126

    
127
    /**
128
     * Stores the given {@link PersistentState}
129
     * to clipboard
130
     * 
131
     * @param st
132
     * @throws PersistenceException
133
     */
134
    public static void saveToClipboard(PersistentState st)
135
        throws PersistenceException {
136

    
137
        OutputStream os = null;
138
        try {
139
            os = getClipboardOStream();
140
            persManager.saveState(st, os);
141
            os.close();
142
            if (st.getContext().getErrors() != null) {
143
                throw st.getContext().getErrors();
144
            }
145
        } catch (Exception ex) {
146
            throw new PersistenceException(ex);
147
        }
148
    }
149

    
150
    /**
151
     * Gets an array of layers as a {@link PersistentState}
152
     * of an instance of FLayers
153
     * 
154
     * @param actives
155
     * @param ctxt
156
     * @return
157
     * @throws PersistenceException
158
     */
159
    public static PersistentState getAsFLayersPersistentState(FLayer[] actives,
160
        MapContext ctxt) throws PersistenceException {
161

    
162
        FLayers lyrs = new FLayers();
163
        lyrs.setMapContext(ctxt);
164
        lyrs.setName("copy-paste-root");
165

    
166
        FLayer item = null;
167
        if (actives != null) {
168
            for (int i = 0; i < actives.length; i++) {
169
                item = actives[i];
170
                try {
171
                    item = item.cloneLayer();
172
                } catch (Exception ex) {
173
                    throw new PersistenceException(ex);
174
                }
175
                lyrs.addLayer(item);
176
            }
177
        }
178

    
179
        PersistentState state = null;
180
        state = persManager.getState(lyrs, true);
181

    
182
        if (state.getContext().getErrors() != null) {
183
            throw state.getContext().getErrors();
184
        }
185
        return state;
186
    }
187

    
188
    /**
189
     * Remosves the layers from the their parent
190
     * @param actives
191
     * @param mc
192
     * @return
193
     */
194
    public static boolean removeLayers(FLayer[] actives, MapContext mc) {
195

    
196
        if (actives == null || actives.length == 0) {
197
            return false;
198
        }
199

    
200
        for (int i = 0; i < actives.length; i++) {
201
            if (actives[i].isEditing() && actives[i].isAvailable()) {
202
                JOptionPane.showMessageDialog(
203
                    (Component) PluginServices.getMainFrame(),
204
                    Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
205
                    Messages.getText("eliminar_capa"),
206
                    JOptionPane.WARNING_MESSAGE);
207
                return false;
208
            }
209
        }
210

    
211
        mc.beginAtomicEvent();
212
        for (int i = actives.length - 1; i >= 0; i--) {
213
            try {
214
                // actives[i].getParentLayer().removeLayer(actives[i]);
215
                // FLayers lyrs=getMapContext().getLayers();
216
                // lyrs.addLayer(actives[i]);
217
                actives[i].getParentLayer().removeLayer(actives[i]);
218

    
219
                // Cierra todas las ventanas asociadas a la capa
220
                IWindow[] wList =
221
                    PluginServices.getMDIManager().getAllWindows();
222
                for (int j = 0; j < wList.length; j++) {
223
                    String name = wList[j].getWindowInfo().getAdditionalInfo();
224
                    for (int k = 0; k < actives.length; k++) {
225
                        if (name != null && actives != null
226
                            && actives[k] != null
227
                            && actives[k].getName() != null
228
                            && name.compareTo(actives[k].getName()) == 0)
229
                            PluginServices.getMDIManager()
230
                                .closeWindow(wList[j]);
231
                    }
232
                }
233

    
234
            } catch (CancelationException e1) {
235
                JOptionPane.showMessageDialog((Component) PluginServices
236
                    .getMainFrame(), Messages
237
                    .getText("No_ha_sido_posible_realizar_la_operacion"),
238
                    Messages.getText("eliminar_capa"),
239
                    JOptionPane.WARNING_MESSAGE);
240
                logger.info("Error while removing layers.", e1);
241
                return false;
242
            }
243
        }
244
        mc.endAtomicEvent();
245
        Project project =
246
            ((ProjectExtension) PluginServices
247
                .getExtension(ProjectExtension.class)).getProject();
248
        project.setModified(true);
249
        PluginServices.getMainFrame().enableControls();
250
        return true;
251

    
252
    }
253

    
254
    /**
255
     * Adds layers to target {@link FLayers}
256
     * 
257
     * @param clipboard_root
258
     * @param target_root
259
     * @return
260
     */
261
    public static boolean addLayers(FLayers clipboard_root, FLayers target_root) {
262

    
263
        if (clipboard_root == null || clipboard_root.getLayersCount() == 0) {
264
            return false;
265
        }
266

    
267
        int n = clipboard_root.getLayersCount();
268
        FLayer item = null;
269
        for (int i = 0; i < n; i++) {
270
            item = clipboard_root.getLayer(i);
271
            target_root.addLayer(item);
272
        }
273
        return true;
274
    }
275

    
276
}