Statistics
| Revision:

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

History | View | Annotate | Download (4.28 KB)

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

    
3
import java.awt.MouseInfo;
4
import java.awt.PointerInfo;
5

    
6
import javax.swing.JMenuItem;
7
import javax.swing.JPopupMenu;
8
import javax.swing.SwingUtilities;
9

    
10
import org.exolab.castor.xml.MarshalException;
11
import org.exolab.castor.xml.ValidationException;
12

    
13
import com.iver.andami.PluginServices;
14
import com.iver.cit.gvsig.ProjectExtension;
15
import com.iver.cit.gvsig.fmap.MapContext;
16
import com.iver.cit.gvsig.fmap.layers.FLayer;
17
import com.iver.cit.gvsig.fmap.layers.FLayers;
18
import com.iver.cit.gvsig.project.Project;
19
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
20
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
21
import com.iver.utiles.XMLEntity;
22

    
23
public class PasteLayersTocMenuEntry extends AbstractTocContextMenuAction {
24
        private XMLEntity xml=null;
25
        private CopyPasteLayersUtiles utiles = CopyPasteLayersUtiles.getInstance();
26

    
27

    
28
        public String getGroup() {
29
                return "copyPasteLayer";
30
        }
31

    
32
        public int getGroupOrder() {
33
                return 60;
34
        }
35

    
36
        public int getOrder() {
37
                return 2;
38
        }
39

    
40
        public String getText() {
41
                return PluginServices.getText(this, "pegar");
42
        }
43

    
44
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
45
                if (isTocItemBranch(item)) {
46
                        FLayer lyr = getNodeLayer(item);
47
//                        if (lyr instanceof FLayers) {
48
                                this.xml = this.getCheckedXMLFromClipboard();
49
//                                return true;
50
//                        }
51
                        return this.xml != null; // Podemos hacer paste en cualquier sitio y nos preguntar? si queremos meter la capa en ese sitio
52

    
53
                } else if (!isTocItemLeaf(item)) {
54
                        if (getNodeLayer(item) == null) {
55
                                this.xml = this.getCheckedXMLFromClipboard();
56
                                return this.xml != null;
57
                        }
58
                }
59
                return false;
60
        }
61

    
62
        private XMLEntity getCheckedXMLFromClipboard() {
63
                String sourceString = PluginServices.getFromClipboard();
64
                if (sourceString == null) return null;
65

    
66
//                System.out.println(sourceString);
67
                XMLEntity xml;
68
                try {
69
                        xml = XMLEntity.parse(sourceString);
70
                } catch (MarshalException e) {
71
                        return null;
72
                } catch (ValidationException e) {
73
                        return null;
74
                }
75

    
76

    
77
                if (!this.utiles.checkXMLRootNode(xml)) return null;
78

    
79
                if (xml.findChildren("type","layers") == null) return null;
80

    
81
                return  xml;
82
        }
83

    
84
        public void execute(ITocItem item, FLayer[] selectedItems) {
85
                FLayers root;
86

    
87
                if (this.xml == null) return;
88

    
89
                FLayer lyrOn = getNodeLayer(item);
90
                if (isTocItemBranch(item)) {
91
                        if (lyrOn instanceof FLayers) {
92
                                root = (FLayers)getNodeLayer(item);
93
                        }
94
                        else
95
                        {
96
                                // Cambio: Siempre arriba
97
//                                JPopupMenu pop = new JPopupMenu(PluginServices.getText(this, "Paste"));
98
//                                JMenuItem itUp = new JMenuItem(PluginServices.getText(this, "Paste_above_layer") + " " + lyrOn.getName());
99
//                                JMenuItem itDown = new JMenuItem(PluginServices.getText(this, "Paste_below_layer") + " " + lyrOn.getName());
100
//                                
101
//                                itUp.setActionCommand("UP");
102
//                                itDown.setActionCommand("DOWN");
103
//                                
104
//                                MyPasteListener lis = new MyPasteListener(pop, item, this.xml, lyrOn, getMapContext());                
105
//                                
106
//                                itUp.addActionListener(lis);
107
//                                itDown.addActionListener(lis);
108
//                                
109
//                                pop.add(itUp);
110
//                                pop.add(itDown);
111
//                                
112
//                                PointerInfo pi = MouseInfo.getPointerInfo();
113
//                                
114
//                                pop.show(null, pi.getLocation().x, pi.getLocation().y);
115

    
116
                                MapContext mapContext = getMapContext();
117
                                mapContext.beginAtomicEvent();
118
                                try {
119
                                        
120
                                        FLayers all = mapContext.getLayers();
121
                                        CopyPasteLayersUtiles.getInstance().loadLayersFromXML(xml, all);
122
                                                // ponemos las capas en posici?n
123
                                                int pos = -1;
124
                                                for (int j=0; j < all.getLayersCount(); j++) {
125
                                                        if (all.getLayer(j).getName().equalsIgnoreCase(lyrOn.getName())) {
126
                                                                pos = j;
127
                                                                break;
128
                                                        }
129
                                                }
130
                                                int corrected = 2;
131
                                                all.moveTo(0, all.getLayersCount()-corrected-pos);
132
                                } catch (Exception ex) {
133
                                        ex.printStackTrace();
134
                                }
135
                                                
136
                                mapContext.endAtomicEvent();
137
                                mapContext.invalidate();
138
                                
139

    
140
                                return;
141
                        }
142
                } else if (lyrOn == null){
143
                        root = getMapContext().getLayers();
144
                } else {
145
                        return;
146
                }
147

    
148
                getMapContext().beginAtomicEvent();
149
                
150
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
151

    
152
                getMapContext().endAtomicEvent();
153

    
154
                if (isOK) {
155
                        getMapContext().invalidate();
156
                        Project project=((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
157
                        project.setModified(true);
158
                }
159
        }
160

    
161

    
162
}