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 @ 41094
History | View | Annotate | Download (10.1 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 |
public static boolean isClipboardEmpty() { |
144 |
File f = new File(getClipboardFolder(), CLIPBOARD_FILE_NAME); |
145 |
return !f.exists();
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Returns the content of the clipboard as an instance
|
150 |
* of FLayers or null if clipboard is empty or does not
|
151 |
* contain instance of FLayers
|
152 |
*
|
153 |
* @return
|
154 |
* @throws PersistenceException
|
155 |
*/
|
156 |
public static FLayers getClipboardAsFLayers() throws PersistenceException { |
157 |
|
158 |
InputStream is = null; |
159 |
|
160 |
try {
|
161 |
is = getClipboardIStream(); |
162 |
|
163 |
if (is == null) { |
164 |
return null; |
165 |
} |
166 |
|
167 |
Object obj = getPersMan().getObject(is);
|
168 |
is.close(); |
169 |
if (obj instanceof FLayers) { |
170 |
return (FLayers) obj;
|
171 |
} else {
|
172 |
return null; |
173 |
} |
174 |
} catch (Exception e) { |
175 |
logger.info("While getting object from clipboard: ", e);
|
176 |
throw new PersistenceException(e); |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Stores the given {@link PersistentState}
|
183 |
* to clipboard
|
184 |
*
|
185 |
* @param st
|
186 |
* @throws PersistenceException
|
187 |
*/
|
188 |
public static void saveToClipboard(PersistentState st) |
189 |
throws PersistenceException {
|
190 |
|
191 |
OutputStream os = null; |
192 |
try {
|
193 |
os = getClipboardOStream(); |
194 |
getPersMan().saveState(st, os); |
195 |
os.close(); |
196 |
if (st.getContext().getErrors() != null) { |
197 |
throw st.getContext().getErrors();
|
198 |
} |
199 |
} catch (Exception ex) { |
200 |
throw new PersistenceException(ex); |
201 |
} |
202 |
} |
203 |
|
204 |
/**
|
205 |
* Gets an array of layers as a {@link PersistentState}
|
206 |
* of an instance of FLayers
|
207 |
*
|
208 |
* @param actives
|
209 |
* @param ctxt
|
210 |
* @return
|
211 |
* @throws PersistenceException
|
212 |
*/
|
213 |
public static PersistentState getAsFLayersPersistentState(FLayer[] actives, |
214 |
MapContext ctxt) throws PersistenceException {
|
215 |
|
216 |
FLayers lyrs = new FLayers();
|
217 |
lyrs.setMapContext(ctxt); |
218 |
lyrs.setName("copy-paste-root");
|
219 |
|
220 |
FLayer item = null;
|
221 |
if (actives != null) { |
222 |
for (int i = 0; i < actives.length; i++) { |
223 |
item = actives[i]; |
224 |
try {
|
225 |
item = item.cloneLayer(); |
226 |
} catch (Exception ex) { |
227 |
throw new PersistenceException(ex); |
228 |
} |
229 |
lyrs.addLayer(item); |
230 |
} |
231 |
} |
232 |
|
233 |
PersistentState state = null;
|
234 |
state = getPersMan().getState(lyrs, true);
|
235 |
|
236 |
if (state.getContext().getErrors() != null) { |
237 |
throw state.getContext().getErrors();
|
238 |
} |
239 |
return state;
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* Remosves the layers from the their parent
|
244 |
* @param actives
|
245 |
* @param mc
|
246 |
* @return
|
247 |
*/
|
248 |
public static boolean removeLayers(FLayer[] actives, MapContext mc) { |
249 |
|
250 |
if (actives == null || actives.length == 0) { |
251 |
return false; |
252 |
} |
253 |
|
254 |
for (int i = 0; i < actives.length; i++) { |
255 |
if (actives[i].isEditing() && actives[i].isAvailable()) {
|
256 |
JOptionPane.showMessageDialog(
|
257 |
(Component) PluginServices.getMainFrame(),
|
258 |
Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
|
259 |
Messages.getText("eliminar_capa"),
|
260 |
JOptionPane.WARNING_MESSAGE);
|
261 |
return false; |
262 |
} |
263 |
} |
264 |
|
265 |
mc.beginAtomicEvent(); |
266 |
for (int i = actives.length - 1; i >= 0; i--) { |
267 |
try {
|
268 |
// actives[i].getParentLayer().removeLayer(actives[i]);
|
269 |
// FLayers lyrs=getMapContext().getLayers();
|
270 |
// lyrs.addLayer(actives[i]);
|
271 |
actives[i].getParentLayer().removeLayer(actives[i]); |
272 |
|
273 |
// Cierra todas las ventanas asociadas a la capa
|
274 |
IWindow[] wList =
|
275 |
PluginServices.getMDIManager().getAllWindows(); |
276 |
for (int j = 0; j < wList.length; j++) { |
277 |
String name = wList[j].getWindowInfo().getAdditionalInfo();
|
278 |
for (int k = 0; k < actives.length; k++) { |
279 |
if (name != null && actives != null |
280 |
&& actives[k] != null
|
281 |
&& actives[k].getName() != null
|
282 |
&& name.compareTo(actives[k].getName()) == 0)
|
283 |
PluginServices.getMDIManager() |
284 |
.closeWindow(wList[j]); |
285 |
} |
286 |
} |
287 |
|
288 |
} catch (CancelationException e1) {
|
289 |
JOptionPane.showMessageDialog((Component) PluginServices |
290 |
.getMainFrame(), Messages |
291 |
.getText("No_ha_sido_posible_realizar_la_operacion"),
|
292 |
Messages.getText("eliminar_capa"),
|
293 |
JOptionPane.WARNING_MESSAGE);
|
294 |
logger.info("Error while removing layers.", e1);
|
295 |
return false; |
296 |
} |
297 |
} |
298 |
mc.endAtomicEvent(); |
299 |
Project project = |
300 |
((ProjectExtension) PluginServices |
301 |
.getExtension(ProjectExtension.class)).getProject(); |
302 |
project.setModified(true);
|
303 |
PluginServices.getMainFrame().enableControls(); |
304 |
return true; |
305 |
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* Adds layers to target {@link FLayers}
|
310 |
*
|
311 |
* @param clipboard_root
|
312 |
* @param target_root
|
313 |
* @return
|
314 |
*/
|
315 |
public static boolean addLayers(FLayers clipboard_root, FLayers target_root) { |
316 |
|
317 |
if (clipboard_root == null || clipboard_root.getLayersCount() == 0) { |
318 |
return false; |
319 |
} |
320 |
|
321 |
int n = clipboard_root.getLayersCount();
|
322 |
FLayer item = null;
|
323 |
for (int i = 0; i < n; i++) { |
324 |
item = clipboard_root.getLayer(i); |
325 |
target_root.addLayer(item); |
326 |
} |
327 |
return true; |
328 |
} |
329 |
|
330 |
} |