Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / actions / PasteLayersTocMenuEntry.java @ 38056

History | View | Annotate | Download (3.24 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.MapContext;
9
import com.iver.cit.gvsig.fmap.layers.FLayer;
10
import com.iver.cit.gvsig.fmap.layers.FLayers;
11
import com.iver.cit.gvsig.project.Project;
12
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
13
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
14
import com.iver.utiles.XMLEntity;
15

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

    
20

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

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

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

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

    
37
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
38
                if (isTocItemBranch(item)) {
39
                        FLayer lyr = getNodeLayer(item);
40
                        //                        if (lyr instanceof FLayers) {
41
                        this.xml = this.getCheckedXMLFromClipboard();
42
                        //                                return true;
43
                        //                        }
44
                        return this.xml != null; // Podemos hacer paste en cualquier sitio y nos preguntar? si queremos meter la capa en ese sitio
45

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

    
55
        private XMLEntity getCheckedXMLFromClipboard() {
56
                String sourceString = PluginServices.getFromClipboard();
57
                if (sourceString == null) return null;
58

    
59
                //                System.out.println(sourceString);
60
                XMLEntity xml;
61
                try {
62
                        xml = XMLEntity.parse(sourceString);
63
                } catch (MarshalException e) {
64
                        return null;
65
                } catch (ValidationException e) {
66
                        return null;
67
                }
68

    
69

    
70
                if (!this.utiles.checkXMLRootNode(xml)) return null;
71

    
72
                if (xml.findChildren("type","layers") == null) return null;
73

    
74
                return  xml;
75
        }
76

    
77
        public void execute(ITocItem item, FLayer[] selectedItems) {
78
                FLayers root;
79

    
80
                if (this.xml == null) return;
81

    
82
                FLayer lyrOn = getNodeLayer(item);
83
                if (isTocItemBranch(item)) {
84
                        MapContext mapContext = getMapContext();
85
                        mapContext.beginAtomicEvent();
86
                        FLayers all = lyrOn.getParentLayer();
87

    
88
                        CopyPasteLayersUtiles.getInstance().loadLayersFromXML(xml, all);
89
                        int position = getPosition(all, lyrOn);
90
                        all.moveTo(0, position);
91

    
92
                        mapContext.endAtomicEvent();
93
                        mapContext.invalidate();
94
                } else if (lyrOn == null) {
95
                        root = getMapContext().getLayers();
96
                        getMapContext().beginAtomicEvent();
97

    
98
                        boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
99

    
100
                        getMapContext().endAtomicEvent();
101

    
102
                        if (isOK) {
103
                                getMapContext().invalidate();
104
                                Project project=((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
105
                                project.setModified(true);
106
                        }
107
                }
108
        }
109

    
110
        private int getPosition(FLayers layerGroup, FLayer lyrSelected){
111
                int pos = -1;
112
                for (int j=0; j < layerGroup.getLayersCount(); j++) {
113
                        if (layerGroup.getLayer(j).getName().equalsIgnoreCase(lyrSelected.getName())) {
114
                                pos = j;
115
                                break;
116
                        }
117
                }
118
                int offset = 2;
119
                return layerGroup.getLayersCount() - offset - pos;
120
        }
121

    
122
}