Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / CopyPasteLayersExtension.java @ 6439

History | View | Annotate | Download (15 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4
import java.awt.Toolkit;
5
import java.awt.datatransfer.DataFlavor;
6
import java.awt.datatransfer.StringSelection;
7
import java.awt.datatransfer.Transferable;
8
import java.awt.datatransfer.UnsupportedFlavorException;
9
import java.awt.event.ActionEvent;
10
import java.io.IOException;
11
import java.io.StringReader;
12
import java.io.StringWriter;
13
import java.util.ArrayList;
14

    
15
import javax.swing.JMenuItem;
16
import javax.swing.JOptionPane;
17

    
18
import org.exolab.castor.xml.MarshalException;
19
import org.exolab.castor.xml.Marshaller;
20
import org.exolab.castor.xml.ValidationException;
21

    
22
import com.hardcode.gdbms.engine.data.DataSourceFactory;
23
import com.iver.andami.PluginServices;
24
import com.iver.andami.plugins.Extension;
25
import com.iver.cit.gvsig.fmap.DriverException;
26
import com.iver.cit.gvsig.fmap.layers.CancelationException;
27
import com.iver.cit.gvsig.fmap.layers.FLayer;
28
import com.iver.cit.gvsig.fmap.layers.FLayers;
29
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
30
import com.iver.cit.gvsig.fmap.layers.XMLException;
31
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
32
import com.iver.cit.gvsig.gui.project.OpenException;
33
import com.iver.cit.gvsig.gui.project.SaveException;
34
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
35
import com.iver.cit.gvsig.gui.toc.TocMenuEntry;
36
import com.iver.cit.gvsig.project.Project;
37
import com.iver.cit.gvsig.project.ProjectTable;
38
import com.iver.utiles.XMLEntity;
39
import com.iver.utiles.xmlEntity.generate.XmlTag;
40

    
41
public class CopyPasteLayersExtension extends Extension {
42

    
43
        public void initialize() {
44
                myTocMenuEntry copy = new CopyTocMenuEntry();
45
                myTocMenuEntry cut = new CutTocMenuEntry();
46
                myTocMenuEntry paste = new PasteTocMenuEntry();
47
                Utiles utiles = new Utiles();
48
                copy.setUtiles(utiles);
49
                cut.setUtiles(utiles);
50
                paste.setUtiles(utiles);
51
            FPopupMenu.addEntry(copy);
52
            FPopupMenu.addEntry(cut);
53
            FPopupMenu.addEntry(paste);
54
                
55
        }
56

    
57
        public void execute(String actionCommand) {
58
                // TODO Auto-generated method stub
59
                
60
        }
61

    
62
        public boolean isEnabled() {
63
                return false;
64
        }
65

    
66
        public boolean isVisible() {
67
                return false;
68
        }
69

    
70
}
71

    
72
abstract class  myTocMenuEntry extends TocMenuEntry {        
73
        protected Utiles utiles;
74
        
75
        protected JMenuItem addMenuEntry(String title) {
76
                JMenuItem menuEntry = new JMenuItem(title);
77
                menuEntry.setFont(FPopupMenu.theFont);
78
                menuEntry.addActionListener(this);
79
                getMenu().add( menuEntry );                
80
                return menuEntry;
81
        }
82
        
83
        public void setUtiles(Utiles utiles) {
84
                this.utiles = utiles;
85
        }
86
        
87
}
88

    
89
class CopyTocMenuEntry extends myTocMenuEntry{
90
        private JMenuItem properties;
91

    
92
        public void initialize(FPopupMenu m) {
93
                super.initialize(m);
94
                getMenu().addSeparator();
95
                properties = addMenuEntry(PluginServices.getText(this, "copiar"));
96
                                
97
                FLayer[] actives = getMapContext().getLayers().getActives();
98
                if (actives.length >= 1) {
99
                        properties.setEnabled(true);                                
100
                } else {
101
                        properties.setEnabled(false);
102
                }
103

    
104

    
105
        }
106

    
107
        public void actionPerformed(ActionEvent e) {
108
                FLayer[] actives = getMapContext().getLayers().getActives();
109
                
110
                XMLEntity xml = this.utiles.generateXML(actives);
111
                if (xml == null) {
112
                        //TODO: arreglar esto
113
                        return;
114
                }
115
                
116
                String data = this.utiles.marshallXMLEntity(xml);
117
                if (data == null) {
118
                        //TODO: arreglar esto
119
                        return;
120
                }
121

    
122

    
123
                /*
124
                System.out.println("==============================================================");
125
                System.out.println(data);
126
                System.out.println("==============================================================");
127
                System.out.flush();
128
                */
129
                
130
                this.utiles.putInClipboard(data);                
131
                
132
        }
133
}
134

    
135
class CutTocMenuEntry extends myTocMenuEntry{
136
        private JMenuItem properties;
137
                
138

    
139
        public void initialize(FPopupMenu m) {                
140
                super.initialize(m);                        
141
                properties = addMenuEntry(PluginServices.getText(this, "cortar"));
142
                
143
                FLayer[] actives = getMapContext().getLayers().getActives();
144
                if (actives.length >=1) {
145
                        properties.setEnabled(true);                                
146
                } else {
147
                        properties.setEnabled(false);
148
                }
149
                        
150
                
151

    
152
        }
153

    
154
        public void actionPerformed(ActionEvent e) {
155
                FLayer[] actives = getMapContext().getLayers().getActives();
156
                if (actives.length< 1) {
157
                    return;
158
            }
159
                
160
                XMLEntity xml = this.utiles.generateXML(actives);
161
                if (xml == null) {
162
                        //TODO: arreglar esto
163
                        return;
164
                }
165
                
166
                String data = this.utiles.marshallXMLEntity(xml);
167
                if (data == null) {
168
                        //TODO: arreglar esto
169
                        return;
170
                }
171

    
172

    
173
                /*
174
                System.out.println("==============================================================");
175
                System.out.println(data);
176
                System.out.println("==============================================================");
177
                System.out.flush();
178
                */
179
                
180
                this.utiles.putInClipboard(data);
181
                
182
            
183
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
184
            if (option!=JOptionPane.OK_OPTION) {
185
                    return;
186
            }
187

    
188
                if (this.utiles.removeLayers(actives)) {                        
189
                        if (getMapContext().getLayers().getLayersCount()==0) {
190
                                PluginServices.getMainFrame().enableControls();
191
                        }                                
192
                }
193
        }
194
}
195

    
196

    
197
class PasteTocMenuEntry extends myTocMenuEntry{
198
        private JMenuItem properties;
199
        private XMLEntity xml=null;
200
        
201

    
202
        public void initialize(FPopupMenu m) {
203
                super.initialize(m);                        
204
                properties = new JMenuItem(PluginServices.getText(this, "pegar"));
205
                
206
                properties.setFont(FPopupMenu.theFont);
207
                
208
                properties.addActionListener(this);
209
                getMenu().add( properties );
210
                //getMenu().addSeparator();
211

    
212
                if (isTocItemBranch()) {
213
                        FLayer lyr = getNodeLayer();
214
                        if (lyr instanceof FLayers) {
215
                                this.xml = this.getCheckedXMLFromClipboard();
216
                                properties.setEnabled((this.xml != null));                                
217
                        } else {
218
                                properties.setEnabled(false);
219
                        }
220
                        
221
                } else if (isTocItemLeaf()) {
222
                        properties.setEnabled(false);
223
                }
224

    
225
        }
226
        
227
        private XMLEntity getCheckedXMLFromClipboard() {
228
                String sourceString = this.utiles.getFromClipboard();
229
                if (sourceString == null) return null;
230
                
231
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
232
                if (xml == null) return null;
233
                
234
                if (!this.utiles.checkXMLRootNode(xml)) return null;
235
                
236
                return  xml;
237
        }
238

    
239
        public void actionPerformed(ActionEvent e) {
240
                FLayers root;
241
                
242
                if (this.xml == null) return;
243
                
244
                if (isTocItemBranch()) {
245
                        root = (FLayers)getNodeLayer();
246
                } else {
247
                        root = getMapContext().getLayers();
248
                }
249
                getMapContext().beginAtomicEvent();
250
                
251
                if (!this.utiles.loadFromXML(this.xml,root)) return;
252
                
253
                getMapContext().endAtomicEvent();
254
                
255
        getMapContext().invalidate();
256
        }
257
        
258
}
259

    
260

    
261
class Utiles {
262
        public boolean checkXMLRootNode(XMLEntity xml) {
263
                if (!xml.contains("applicationName")) return false;
264
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) return false;
265
                
266
                if (!xml.contains("version")) return false;
267
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false;
268
                
269
                return true;
270
        }
271
        
272
        private void fillXMLRootNode(XMLEntity xml) {
273
                xml.putProperty("applicationName","gvSIG");
274
                xml.putProperty("version",Version.format());                
275
        }
276
        
277
        private XMLEntity getXMLEntityChildOfType(XMLEntity xml,String type) {
278
                int childCount = xml.getNumChild();
279
                XMLEntity child;
280
                for (int i=0; i < childCount; i++  ) {
281
                        child = xml.getChild(i);
282
                        if (child.contains("type")) {
283
                                if (child.getStringProperty("type").equalsIgnoreCase(type)) {
284
                                        return child;
285
                                }
286
                        }
287
                }
288
                return null;
289
                
290
        }
291
        
292
        public boolean loadFromXML(XMLEntity xml, FLayers root) {
293
                XMLEntity xmlLayers = this.getXMLEntityChildOfType(xml,"layers");
294
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
295
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
296
                
297
                if (xmlLayers == null ) return false;
298
                
299
                if (xmlDataSources != null)  {
300
                        if (!this.registerDataSources(xmlDataSources)) return false;
301
                }
302
                if (xmlTables != null)  {
303
                        if (!this.addTables(xmlTables)) return false;
304
                }
305

    
306
                if (!this.addLayers(xmlLayers,root)) return false;
307
        
308
                return true;
309
        }
310
        
311
        private boolean registerDataSources(XMLEntity xmlDataSources) {
312
                try {
313
                        int numDataSources = xmlDataSources.getNumChild();
314
                        
315
                        if (numDataSources == 0) return true;
316
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
317
                        
318
                        for (int i = 0; i < numDataSources; i++) {
319
                                XMLEntity child = xmlDataSources.getChild(i);
320
                                String name = child.getStringProperty("gdbmsname");
321
                                
322
                                if (dsFactory.getDriverInfo(name) == null) {
323
                                        if (child.getStringProperty("type").equals("otherDriverFile")) {                                        
324
                                                LayerFactory.getDataSourceFactory().addFileDataSource(
325
                                                                child.getStringProperty("driverName"), 
326
                                                                name,
327
                                                                child.getStringProperty("file")
328
                                                );
329
                                                
330
                                                
331
                                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
332
                                                /*                                String layerName = child.getStringProperty("layerName");
333
                                                 ProjectView vista = project.getViewByName(child.getStringProperty(
334
                                                 "viewName"));
335
                                                 FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
336
                                                 
337
                                                 modelo = ((AlphanumericData) layer).getRecordset();
338
                                                 associatedTable = (AlphanumericData) layer;
339
                                                 */
340
                                        } else if (child.getStringProperty("type").equals("db")) {
341
                                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
342
                                                                name,
343
                                                                child.getStringProperty("host"),
344
                                                                child.getIntProperty("port"),
345
                                                                child.getStringProperty("user"),
346
                                                                child.getStringProperty("password"),
347
                                                                child.getStringProperty("dbName"),
348
                                                                child.getStringProperty("tableName"),
349
                                                                child.getStringProperty("driverInfo")
350
                                                );
351
                                        }
352
                                }
353
                        }
354
                        
355
                        return true;
356
                } catch (Exception e) {
357
                        e.printStackTrace();
358
                        return false;
359
                }
360
        }
361
        
362
        private boolean addTables(XMLEntity xmlTables) {
363
                try {
364
                        int numTables = xmlTables.getNumChild();
365
                        if (numTables == 0) return true;
366
                        
367
                        Project project = this.getProject();
368
                        
369
                        for (int i = 0; i < numTables; i++) {
370
                                try{
371
                                        ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project);
372
                                        project.addTable(ptable);
373
                                        if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
374
                                                PluginServices.getMDIManager().addView(ptable.getAndamiView());
375
                                                PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
376
                                        }
377
                                }catch(OpenException e){
378
                                        e.printStackTrace();
379
                                        return false;
380
                                }
381
                        }
382
                        
383
                        project.setLinkTable();
384

    
385
                        return true;
386
                } catch (Exception e) {
387
                        e.printStackTrace();
388
                        return false;                        
389
                }
390
        }
391
        
392
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
393
                try {
394
                        XMLEntity child;
395
                        int numLayers = xmlLayers.getNumChild();
396
                        for (int i = 0; i < numLayers; i++) {
397
                                child = xmlLayers.getChild(i);
398
                                if (!root.addLayerFromXMLEntity(child,null)) return false;
399
                        }
400
                        return true;
401
                } catch (Exception e) {
402
                        e.printStackTrace();
403
                        return false;                        
404
                }
405
                
406
        }
407
        
408
        private Project getProject() {
409
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
410
        }
411
        
412

    
413
        
414
        public XMLEntity generateXML(FLayer[] actives) {
415
                FLayer lyr;
416
                Project project = this.getProject();
417
                
418
                XMLEntity xml = new XMLEntity();
419
                XMLEntity xmlLayers = new XMLEntity();
420
                XMLEntity xmlTables = new XMLEntity();
421
                XMLEntity xmlDataSources = new XMLEntity();
422
                
423
                fillXMLRootNode(xml);
424
                
425
                
426
                xmlLayers.putProperty("type","layers");
427
                xmlTables.putProperty("type","tables");
428
                xmlDataSources.putProperty("type","dataSources");
429
                
430
                for (int i=0;i < actives.length; i++) {
431
                        lyr = actives[i];
432
                        try {
433
                                xmlLayers.addChild(lyr.getXMLEntity());
434
                                
435
                if (lyr instanceof AlphanumericData){
436
                        xmlDataSources.addChild(
437
                          project.getSourceInfoXMLEntity(
438
                                ((AlphanumericData)lyr).getRecordset().getSourceInfo()
439
                          )
440
                        );
441
                        
442
                    ProjectTable pt = project.getTable((AlphanumericData) lyr);
443
                    if (pt != null) { 
444
                            xmlTables.addChild(pt.getXMLEntity());
445
                    }
446

    
447
                }
448
                                
449
                        } catch (XMLException e1) {
450
                                // TODO Auto-generated catch block
451
                                e1.printStackTrace();
452
                                return null;
453
                        } catch (SaveException e) {
454
                                // TODO Auto-generated catch block
455
                                e.printStackTrace();
456
                                return null;
457
                        } catch (DriverException e) {
458
                                // TODO Auto-generated catch block
459
                                e.printStackTrace();
460
                                return null;
461
                        }
462
                        
463
                }
464
                
465
                if (xmlDataSources.getNumChild() > 0) {
466
                        xml.addChild(xmlDataSources);
467
                }                
468
                if (xmlLayers.getNumChild() > 0) {
469
                        xml.addChild(xmlLayers);
470
                }
471
                if (xmlTables.getNumChild() > 0) {
472
                        xml.addChild(xmlTables);
473
                }
474
                
475
                return xml;
476
                
477
        }
478
        
479
        public void putInClipboard(String data) {
480
                StringSelection ss = new StringSelection(data);
481
                
482
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);                
483
        }
484
        
485
        public String marshallXMLEntity(XMLEntity xml) {
486
                StringWriter buffer = new StringWriter();
487

    
488
                Marshaller m;
489
                try {
490
                        m = new Marshaller(buffer);
491
                } catch (IOException e4) {
492
                        // TODO Auto-generated catch block
493
                        e4.printStackTrace();
494
                        return null;
495
                }
496
                m.setEncoding("ISO-8859-1");
497

    
498
                try {
499
                        m.marshal(xml.getXmlTag());
500
                        //if (i < actives.length-1) buffer.write("\n##layer-separator##\n");
501
                } catch (MarshalException e2) {
502
                        // TODO Auto-generated catch block
503
                        e2.printStackTrace();
504
                        return null;
505
                } catch (ValidationException e3) {
506
                        // TODO Auto-generated catch block
507
                        e3.printStackTrace();
508
                        return null;
509
                }
510
                
511
                return buffer.toString();
512
                
513
        }
514
        
515
        public XMLEntity unMarshallXMLEntity(String data) {
516
                StringReader reader = new StringReader(data);
517
                
518
                XmlTag tag;
519
                try {
520
                        tag = (XmlTag) XmlTag.unmarshal(reader);
521
                } catch (MarshalException e) {
522
                        return null;
523
                } catch (ValidationException e) {
524
                        return null;
525
                }
526
                XMLEntity xml=new XMLEntity(tag);
527
                
528
                return xml;
529
        }
530

    
531
        public String getFromClipboard() {        
532
                         
533
                 try {
534
                        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
535
                } catch (UnsupportedFlavorException e) {
536
                        return null;
537
                } catch (IOException e) {
538
                        // TODO Auto-generated catch block
539
                        return null;
540
                }                 
541
        }
542
        
543
        public boolean removeLayers(FLayer[] actives) {
544
            for (int i = actives.length-1; i>=0; i--){
545
                try {
546
                                //actives[i].getParentLayer().removeLayer(actives[i]);
547
                                //FLayers lyrs=getMapContext().getLayers();
548
                                //lyrs.addLayer(actives[i]);
549
                                actives[i].getParentLayer().removeLayer(actives[i]);
550

    
551
                if (actives[i] instanceof AlphanumericData){
552
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
553
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
554

    
555
                    ArrayList tables = project.getTables();
556
                    for (int j = 0; j < tables.size(); j++) {
557
                        if (tables.get(j) == pt){
558
                            project.delTable(j);
559
                            break;
560
                        }
561
                    }
562

    
563
                    PluginServices.getMDIManager().closeSingletonView(pt);
564
                }
565

    
566

    
567
                    } catch (CancelationException e1) {
568
                            e1.printStackTrace();
569
                            return false;
570
                    }
571
            }
572
                return true;
573
        }
574

    
575
}