Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / actions / CopyPasteLayersUtils.java @ 39574

History | View | Annotate | Download (8.59 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 or null
84
     * if clipboard is empty
85
     * 
86
     * @return
87
     * @throws IOException
88
     */
89
    private static InputStream getClipboardIStream() throws IOException {
90
        String strf =
91
            System.getProperty("user.home") + File.separator + "gvSIG"
92
                + File.separator + CLIPBOARD_FILE_NAME;
93
        File f = new File(strf);
94
        if (f.exists()) {
95
            return new FileInputStream(f);
96
        } else {
97
            return null;
98
        }
99

    
100
    }
101

    
102
    /**
103
     * Returns the content of the clipboard as an instance
104
     * of FLayers or null if clipboard is empty or does not
105
     * contain instance of FLayers
106
     * 
107
     * @return
108
     * @throws PersistenceException
109
     */
110
    public static FLayers getClipboardAsFLayers() throws PersistenceException {
111

    
112
        InputStream is = null;
113

    
114
        try {
115
            is = getClipboardIStream();
116
            
117
            if (is == null) {
118
                return null;
119
            }
120
            
121
            Object obj = persManager.getObject(is);
122
            is.close();
123
            if (obj instanceof FLayers) {
124
                return (FLayers) obj;
125
            } else {
126
                return null;
127
            }
128
        } catch (Exception e) {
129
            logger.info("While getting object from clipboard: ", e);
130
            throw new PersistenceException(e);
131
        }
132

    
133
    }
134

    
135
    /**
136
     * Stores the given {@link PersistentState}
137
     * to clipboard
138
     * 
139
     * @param st
140
     * @throws PersistenceException
141
     */
142
    public static void saveToClipboard(PersistentState st)
143
        throws PersistenceException {
144

    
145
        OutputStream os = null;
146
        try {
147
            os = getClipboardOStream();
148
            persManager.saveState(st, os);
149
            os.close();
150
            if (st.getContext().getErrors() != null) {
151
                throw st.getContext().getErrors();
152
            }
153
        } catch (Exception ex) {
154
            throw new PersistenceException(ex);
155
        }
156
    }
157

    
158
    /**
159
     * Gets an array of layers as a {@link PersistentState}
160
     * of an instance of FLayers
161
     * 
162
     * @param actives
163
     * @param ctxt
164
     * @return
165
     * @throws PersistenceException
166
     */
167
    public static PersistentState getAsFLayersPersistentState(FLayer[] actives,
168
        MapContext ctxt) throws PersistenceException {
169

    
170
        FLayers lyrs = new FLayers();
171
        lyrs.setMapContext(ctxt);
172
        lyrs.setName("copy-paste-root");
173

    
174
        FLayer item = null;
175
        if (actives != null) {
176
            for (int i = 0; i < actives.length; i++) {
177
                item = actives[i];
178
                try {
179
                    item = item.cloneLayer();
180
                } catch (Exception ex) {
181
                    throw new PersistenceException(ex);
182
                }
183
                lyrs.addLayer(item);
184
            }
185
        }
186

    
187
        PersistentState state = null;
188
        state = persManager.getState(lyrs, true);
189

    
190
        if (state.getContext().getErrors() != null) {
191
            throw state.getContext().getErrors();
192
        }
193
        return state;
194
    }
195

    
196
    /**
197
     * Remosves the layers from the their parent
198
     * @param actives
199
     * @param mc
200
     * @return
201
     */
202
    public static boolean removeLayers(FLayer[] actives, MapContext mc) {
203

    
204
        if (actives == null || actives.length == 0) {
205
            return false;
206
        }
207

    
208
        for (int i = 0; i < actives.length; i++) {
209
            if (actives[i].isEditing() && actives[i].isAvailable()) {
210
                JOptionPane.showMessageDialog(
211
                    (Component) PluginServices.getMainFrame(),
212
                    Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
213
                    Messages.getText("eliminar_capa"),
214
                    JOptionPane.WARNING_MESSAGE);
215
                return false;
216
            }
217
        }
218

    
219
        mc.beginAtomicEvent();
220
        for (int i = actives.length - 1; i >= 0; i--) {
221
            try {
222
                // actives[i].getParentLayer().removeLayer(actives[i]);
223
                // FLayers lyrs=getMapContext().getLayers();
224
                // lyrs.addLayer(actives[i]);
225
                actives[i].getParentLayer().removeLayer(actives[i]);
226

    
227
                // Cierra todas las ventanas asociadas a la capa
228
                IWindow[] wList =
229
                    PluginServices.getMDIManager().getAllWindows();
230
                for (int j = 0; j < wList.length; j++) {
231
                    String name = wList[j].getWindowInfo().getAdditionalInfo();
232
                    for (int k = 0; k < actives.length; k++) {
233
                        if (name != null && actives != null
234
                            && actives[k] != null
235
                            && actives[k].getName() != null
236
                            && name.compareTo(actives[k].getName()) == 0)
237
                            PluginServices.getMDIManager()
238
                                .closeWindow(wList[j]);
239
                    }
240
                }
241

    
242
            } catch (CancelationException e1) {
243
                JOptionPane.showMessageDialog((Component) PluginServices
244
                    .getMainFrame(), Messages
245
                    .getText("No_ha_sido_posible_realizar_la_operacion"),
246
                    Messages.getText("eliminar_capa"),
247
                    JOptionPane.WARNING_MESSAGE);
248
                logger.info("Error while removing layers.", e1);
249
                return false;
250
            }
251
        }
252
        mc.endAtomicEvent();
253
        Project project =
254
            ((ProjectExtension) PluginServices
255
                .getExtension(ProjectExtension.class)).getProject();
256
        project.setModified(true);
257
        PluginServices.getMainFrame().enableControls();
258
        return true;
259

    
260
    }
261

    
262
    /**
263
     * Adds layers to target {@link FLayers}
264
     * 
265
     * @param clipboard_root
266
     * @param target_root
267
     * @return
268
     */
269
    public static boolean addLayers(FLayers clipboard_root, FLayers target_root) {
270

    
271
        if (clipboard_root == null || clipboard_root.getLayersCount() == 0) {
272
            return false;
273
        }
274

    
275
        int n = clipboard_root.getLayersCount();
276
        FLayer item = null;
277
        for (int i = 0; i < n; i++) {
278
            item = clipboard_root.getLayer(i);
279
            target_root.addLayer(item);
280
        }
281
        return true;
282
    }
283

    
284
}