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 @ 40972
History | View | Annotate | Download (9.91 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_layers_clipboard.tmp"; |
72 |
private static String CLIPBOARD_FOLDER = |
73 |
System.getProperty("user.home") + File.separator + "gvSIG" |
74 |
+ File.separator + "clipboard-layers"; |
75 |
|
76 |
static {
|
77 |
File f = new File(CLIPBOARD_FOLDER); |
78 |
f.mkdirs(); |
79 |
} |
80 |
|
81 |
private static/* XML */PersistenceManager persManager = ToolsLocator |
82 |
.getPersistenceManager(); |
83 |
|
84 |
private CopyPasteLayersUtils() {
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* Gets the outputstream for writing contents to the
|
89 |
* clipboard
|
90 |
*
|
91 |
* @return
|
92 |
* @throws IOException
|
93 |
*/
|
94 |
private static OutputStream getClipboardOStream() throws IOException { |
95 |
String strf = CLIPBOARD_FOLDER+ File.separator + CLIPBOARD_FILE_NAME; |
96 |
File f = new File(strf); |
97 |
if (f.exists()) {
|
98 |
/*
|
99 |
* If file exists, it is removed
|
100 |
* (clipboard content overwritten)
|
101 |
*/
|
102 |
f.delete(); |
103 |
} |
104 |
f.createNewFile(); |
105 |
/*
|
106 |
* File will be removed on exit
|
107 |
*/
|
108 |
f.deleteOnExit(); |
109 |
return new FileOutputStream(f); |
110 |
} |
111 |
|
112 |
public static void clearClipboard() throws IOException { |
113 |
String strf = CLIPBOARD_FOLDER + File.separator + CLIPBOARD_FILE_NAME; |
114 |
File f = new File(strf); |
115 |
if (f.exists()) {
|
116 |
f.delete(); |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Gets the input stream to read the clipboard or null
|
122 |
* if clipboard is empty
|
123 |
*
|
124 |
* @return
|
125 |
* @throws IOException
|
126 |
*/
|
127 |
private static InputStream getClipboardIStream() throws IOException { |
128 |
String strf = CLIPBOARD_FOLDER + File.separator + CLIPBOARD_FILE_NAME; |
129 |
File f = new File(strf); |
130 |
if (f.exists()) {
|
131 |
return new FileInputStream(f); |
132 |
} else {
|
133 |
return null; |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Returns the content of the clipboard as an instance
|
140 |
* of FLayers or null if clipboard is empty or does not
|
141 |
* contain instance of FLayers
|
142 |
*
|
143 |
* @return
|
144 |
* @throws PersistenceException
|
145 |
*/
|
146 |
public static FLayers getClipboardAsFLayers() throws PersistenceException { |
147 |
|
148 |
InputStream is = null; |
149 |
|
150 |
try {
|
151 |
is = getClipboardIStream(); |
152 |
|
153 |
if (is == null) { |
154 |
return null; |
155 |
} |
156 |
|
157 |
Object obj = persManager.getObject(is);
|
158 |
is.close(); |
159 |
if (obj instanceof FLayers) { |
160 |
return (FLayers) obj;
|
161 |
} else {
|
162 |
return null; |
163 |
} |
164 |
} catch (Exception e) { |
165 |
logger.info("While getting object from clipboard: ", e);
|
166 |
throw new PersistenceException(e); |
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Stores the given {@link PersistentState}
|
173 |
* to clipboard
|
174 |
*
|
175 |
* @param st
|
176 |
* @throws PersistenceException
|
177 |
*/
|
178 |
public static void saveToClipboard(PersistentState st) |
179 |
throws PersistenceException {
|
180 |
|
181 |
OutputStream os = null; |
182 |
try {
|
183 |
os = getClipboardOStream(); |
184 |
persManager.saveState(st, os); |
185 |
os.close(); |
186 |
if (st.getContext().getErrors() != null) { |
187 |
throw st.getContext().getErrors();
|
188 |
} |
189 |
} catch (Exception ex) { |
190 |
throw new PersistenceException(ex); |
191 |
} |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Gets an array of layers as a {@link PersistentState}
|
196 |
* of an instance of FLayers
|
197 |
*
|
198 |
* @param actives
|
199 |
* @param ctxt
|
200 |
* @return
|
201 |
* @throws PersistenceException
|
202 |
*/
|
203 |
public static PersistentState getAsFLayersPersistentState(FLayer[] actives, |
204 |
MapContext ctxt) throws PersistenceException {
|
205 |
|
206 |
FLayers lyrs = new FLayers();
|
207 |
lyrs.setMapContext(ctxt); |
208 |
lyrs.setName("copy-paste-root");
|
209 |
|
210 |
FLayer item = null;
|
211 |
if (actives != null) { |
212 |
for (int i = 0; i < actives.length; i++) { |
213 |
item = actives[i]; |
214 |
try {
|
215 |
item = item.cloneLayer(); |
216 |
} catch (Exception ex) { |
217 |
throw new PersistenceException(ex); |
218 |
} |
219 |
lyrs.addLayer(item); |
220 |
} |
221 |
} |
222 |
|
223 |
PersistentState state = null;
|
224 |
state = persManager.getState(lyrs, true);
|
225 |
|
226 |
if (state.getContext().getErrors() != null) { |
227 |
throw state.getContext().getErrors();
|
228 |
} |
229 |
return state;
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* Remosves the layers from the their parent
|
234 |
* @param actives
|
235 |
* @param mc
|
236 |
* @return
|
237 |
*/
|
238 |
public static boolean removeLayers(FLayer[] actives, MapContext mc) { |
239 |
|
240 |
if (actives == null || actives.length == 0) { |
241 |
return false; |
242 |
} |
243 |
|
244 |
for (int i = 0; i < actives.length; i++) { |
245 |
if (actives[i].isEditing() && actives[i].isAvailable()) {
|
246 |
JOptionPane.showMessageDialog(
|
247 |
(Component) PluginServices.getMainFrame(),
|
248 |
Messages.getText("no_se_puede_borrar_una_capa_en_edicion"),
|
249 |
Messages.getText("eliminar_capa"),
|
250 |
JOptionPane.WARNING_MESSAGE);
|
251 |
return false; |
252 |
} |
253 |
} |
254 |
|
255 |
mc.beginAtomicEvent(); |
256 |
for (int i = actives.length - 1; i >= 0; i--) { |
257 |
try {
|
258 |
// actives[i].getParentLayer().removeLayer(actives[i]);
|
259 |
// FLayers lyrs=getMapContext().getLayers();
|
260 |
// lyrs.addLayer(actives[i]);
|
261 |
actives[i].getParentLayer().removeLayer(actives[i]); |
262 |
|
263 |
// Cierra todas las ventanas asociadas a la capa
|
264 |
IWindow[] wList =
|
265 |
PluginServices.getMDIManager().getAllWindows(); |
266 |
for (int j = 0; j < wList.length; j++) { |
267 |
String name = wList[j].getWindowInfo().getAdditionalInfo();
|
268 |
for (int k = 0; k < actives.length; k++) { |
269 |
if (name != null && actives != null |
270 |
&& actives[k] != null
|
271 |
&& actives[k].getName() != null
|
272 |
&& name.compareTo(actives[k].getName()) == 0)
|
273 |
PluginServices.getMDIManager() |
274 |
.closeWindow(wList[j]); |
275 |
} |
276 |
} |
277 |
|
278 |
} catch (CancelationException e1) {
|
279 |
JOptionPane.showMessageDialog((Component) PluginServices |
280 |
.getMainFrame(), Messages |
281 |
.getText("No_ha_sido_posible_realizar_la_operacion"),
|
282 |
Messages.getText("eliminar_capa"),
|
283 |
JOptionPane.WARNING_MESSAGE);
|
284 |
logger.info("Error while removing layers.", e1);
|
285 |
return false; |
286 |
} |
287 |
} |
288 |
mc.endAtomicEvent(); |
289 |
Project project = |
290 |
((ProjectExtension) PluginServices |
291 |
.getExtension(ProjectExtension.class)).getProject(); |
292 |
project.setModified(true);
|
293 |
PluginServices.getMainFrame().enableControls(); |
294 |
return true; |
295 |
|
296 |
} |
297 |
|
298 |
/**
|
299 |
* Adds layers to target {@link FLayers}
|
300 |
*
|
301 |
* @param clipboard_root
|
302 |
* @param target_root
|
303 |
* @return
|
304 |
*/
|
305 |
public static boolean addLayers(FLayers clipboard_root, FLayers target_root) { |
306 |
|
307 |
if (clipboard_root == null || clipboard_root.getLayersCount() == 0) { |
308 |
return false; |
309 |
} |
310 |
|
311 |
int n = clipboard_root.getLayersCount();
|
312 |
FLayer item = null;
|
313 |
for (int i = 0; i < n; i++) { |
314 |
item = clipboard_root.getLayer(i); |
315 |
target_root.addLayer(item); |
316 |
} |
317 |
return true; |
318 |
} |
319 |
|
320 |
} |