Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / actions / PasteLayersTocMenuEntry.java @ 9532

History | View | Annotate | Download (2.42 KB)

1
package com.iver.cit.gvsig.project.documents.view.toc.actions;
2

    
3
import org.exolab.castor.xml.MarshalException;
4
import org.exolab.castor.xml.ValidationException;
5

    
6
import com.iver.andami.PluginServices;
7
import com.iver.cit.gvsig.ProjectExtension;
8
import com.iver.cit.gvsig.fmap.layers.FLayer;
9
import com.iver.cit.gvsig.fmap.layers.FLayers;
10
import com.iver.cit.gvsig.project.Project;
11
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
12
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
13
import com.iver.utiles.XMLEntity;
14

    
15
public class PasteLayersTocMenuEntry extends AbstractTocContextMenuAction {
16
        private XMLEntity xml=null;
17
        private CopyPasteLayersUtiles utiles = CopyPasteLayersUtiles.getInstance();
18

    
19

    
20
        public String getGroup() {
21
                return "copyPasteLayer";
22
        }
23

    
24
        public int getGroupOrder() {
25
                return 60;
26
        }
27

    
28
        public int getOrder() {
29
                return 2;
30
        }
31

    
32
        public String getText() {
33
                return PluginServices.getText(this, "pegar");
34
        }
35

    
36
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
37
                if (isTocItemBranch(item)) {
38
                        FLayer lyr = getNodeLayer(item);
39
                        if (lyr instanceof FLayers) {
40
                                this.xml = this.getCheckedXMLFromClipboard();
41
                                return true;
42
                        }
43

    
44
                } else if (!isTocItemLeaf(item)) {
45
                        if (getNodeLayer(item) == null) {
46
                                this.xml = this.getCheckedXMLFromClipboard();
47
                                return this.xml != null;
48
                        }
49
                }
50
                return false;
51
        }
52

    
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;
72
        }
73

    
74
        public void execute(ITocItem item, FLayer[] selectedItems) {
75
                FLayers root;
76

    
77
                if (this.xml == null) return;
78

    
79
                if (isTocItemBranch(item)) {
80
                        root = (FLayers)getNodeLayer(item);
81
                } else if (getNodeLayer(item) == null){
82
                        root = getMapContext().getLayers();
83
                } else {
84
                        return;
85
                }
86
                getMapContext().beginAtomicEvent();
87

    
88
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
89

    
90
                getMapContext().endAtomicEvent();
91

    
92
                if (isOK) {
93
                        getMapContext().invalidate();
94
                        Project project=((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
95
                        project.setModified(true);
96
                }
97
        }
98

    
99

    
100
}