Revision 39136 branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/project/documents/view/toc/actions/PasteLayersTocMenuEntry.java
PasteLayersTocMenuEntry.java | ||
---|---|---|
1 | 1 |
package org.gvsig.app.project.documents.view.toc.actions; |
2 | 2 |
|
3 |
import org.exolab.castor.xml.MarshalException; |
|
4 |
import org.exolab.castor.xml.ValidationException; |
|
3 |
import java.awt.Component; |
|
4 |
|
|
5 |
import javax.swing.JOptionPane; |
|
6 |
|
|
7 |
import org.slf4j.Logger; |
|
8 |
import org.slf4j.LoggerFactory; |
|
9 |
|
|
5 | 10 |
import org.gvsig.andami.PluginServices; |
6 | 11 |
import org.gvsig.app.extension.ProjectExtension; |
7 | 12 |
import org.gvsig.app.project.Project; |
... | ... | |
9 | 14 |
import org.gvsig.app.project.documents.view.toc.ITocItem; |
10 | 15 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
11 | 16 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
12 |
import org.gvsig.utils.XMLEntity; |
|
17 |
import org.gvsig.i18n.Messages; |
|
18 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
13 | 19 |
|
14 | 20 |
|
15 | 21 |
public class PasteLayersTocMenuEntry extends AbstractTocContextMenuAction { |
16 |
private XMLEntity xml=null; |
|
17 |
private CopyPasteLayersUtiles utiles = CopyPasteLayersUtiles.getInstance(); |
|
22 |
|
|
23 |
private static Logger logger = |
|
24 |
LoggerFactory.getLogger(PasteLayersTocMenuEntry.class); |
|
18 | 25 |
|
19 |
|
|
20 | 26 |
public String getGroup() { |
21 | 27 |
return "copyPasteLayer"; |
22 | 28 |
} |
... | ... | |
37 | 43 |
if (isTocItemBranch(item)) { |
38 | 44 |
FLayer lyr = getNodeLayer(item); |
39 | 45 |
if (lyr instanceof FLayers) { |
40 |
this.xml = this.getCheckedXMLFromClipboard();
|
|
41 |
return true;
|
|
46 |
FLayers aux = this.getFLayersFromClipboard();
|
|
47 |
return aux != null;
|
|
42 | 48 |
} |
43 | 49 |
|
44 | 50 |
} else if (!isTocItemLeaf(item)) { |
45 | 51 |
if (getNodeLayer(item) == null) { |
46 |
this.xml = this.getCheckedXMLFromClipboard();
|
|
47 |
return this.xml != null;
|
|
52 |
FLayers aux = this.getFLayersFromClipboard();
|
|
53 |
return aux != null;
|
|
48 | 54 |
} |
49 | 55 |
} |
50 | 56 |
return false; |
51 | 57 |
} |
52 | 58 |
|
53 |
private XMLEntity getCheckedXMLFromClipboard() { |
|
54 |
String sourceString = PluginServices.getFromClipboard(); |
|
55 |
if (sourceString == null) return null; |
|
56 |
|
|
57 |
XMLEntity xml; |
|
58 |
try { |
|
59 |
xml = XMLEntity.parse(sourceString); |
|
60 |
} catch (MarshalException e) { |
|
61 |
return null; |
|
62 |
} catch (ValidationException e) { |
|
63 |
return null; |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
if (!this.utiles.checkXMLRootNode(xml)) return null; |
|
68 |
|
|
69 |
if (xml.findChildren("type","layers") == null) return null; |
|
70 |
|
|
71 |
return xml; |
|
59 |
private FLayers getFLayersFromClipboard() { |
|
60 |
|
|
61 |
FLayers resp = null; |
|
62 |
try { |
|
63 |
resp = CopyPasteLayersUtils.getClipboardAsFLayers(); |
|
64 |
} catch (PersistenceException e) { |
|
65 |
logger.info("While parsing clipboard data as FLayers.", e); |
|
66 |
resp = null; |
|
67 |
} |
|
68 |
return resp; |
|
72 | 69 |
} |
73 | 70 |
|
74 | 71 |
public void execute(ITocItem item, FLayer[] selectedItems) { |
75 |
FLayers root; |
|
72 |
FLayers target_root;
|
|
76 | 73 |
|
77 |
if (this.xml == null) return; |
|
74 |
FLayers clipboard_root = this.getFLayersFromClipboard(); |
|
75 |
if (clipboard_root == null) { |
|
76 |
JOptionPane.showMessageDialog( |
|
77 |
(Component)PluginServices.getMainFrame(), |
|
78 |
Messages.getText("No_ha_sido_posible_realizar_la_operacion"), |
|
79 |
Messages.getText("pegar"), |
|
80 |
JOptionPane.ERROR_MESSAGE |
|
81 |
); |
|
82 |
logger.info("Unable to parse clipboard as flayers"); |
|
83 |
return; |
|
84 |
} |
|
78 | 85 |
|
79 | 86 |
if (isTocItemBranch(item)) { |
80 |
root = (FLayers)getNodeLayer(item);
|
|
87 |
target_root = (FLayers)getNodeLayer(item);
|
|
81 | 88 |
} else if (getNodeLayer(item) == null){ |
82 |
root = getMapContext().getLayers();
|
|
89 |
target_root = getMapContext().getLayers();
|
|
83 | 90 |
} else { |
84 | 91 |
return; |
85 | 92 |
} |
93 |
|
|
86 | 94 |
getMapContext().beginAtomicEvent(); |
87 | 95 |
|
88 |
boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
|
|
96 |
boolean isOK = CopyPasteLayersUtils.addLayers(clipboard_root, target_root);
|
|
89 | 97 |
|
90 | 98 |
getMapContext().endAtomicEvent(); |
91 | 99 |
|
Also available in: Unified diff