Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / CopyPasteExtension.java @ 6728

History | View | Annotate | Download (41.3 KB)

1 6635 jmvivo
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.UnsupportedFlavorException;
8
import java.awt.event.ActionEvent;
9
import java.io.IOException;
10
import java.io.StringReader;
11
import java.io.StringWriter;
12
import java.util.ArrayList;
13 6645 jmvivo
import java.util.Arrays;
14
import java.util.Comparator;
15 6635 jmvivo
import java.util.Enumeration;
16
import java.util.Hashtable;
17
import java.util.Iterator;
18
import java.util.Map;
19
20
import javax.swing.JMenuItem;
21
import javax.swing.JOptionPane;
22
23
import org.exolab.castor.xml.MarshalException;
24
import org.exolab.castor.xml.Marshaller;
25
import org.exolab.castor.xml.ValidationException;
26
27
import com.hardcode.gdbms.engine.data.DataSourceFactory;
28
import com.hardcode.gdbms.engine.data.SourceInfo;
29
import com.iver.andami.PluginServices;
30
import com.iver.andami.plugins.Extension;
31
import com.iver.cit.gvsig.fmap.DriverException;
32
import com.iver.cit.gvsig.fmap.layers.CancelationException;
33
import com.iver.cit.gvsig.fmap.layers.FLayer;
34
import com.iver.cit.gvsig.fmap.layers.FLayers;
35
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
36
import com.iver.cit.gvsig.fmap.layers.XMLException;
37
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
38
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
39
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
40
import com.iver.cit.gvsig.gui.project.OpenException;
41
import com.iver.cit.gvsig.gui.project.SaveException;
42 6676 jmvivo
import com.iver.cit.gvsig.gui.toc.AbstractTocContextMenuAction;
43 6635 jmvivo
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
44 6676 jmvivo
import com.iver.cit.gvsig.gui.toc.ITocItem;
45 6635 jmvivo
import com.iver.cit.gvsig.gui.toc.TocMenuEntry;
46 6676 jmvivo
import com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction;
47 6635 jmvivo
import com.iver.cit.gvsig.project.Project;
48
import com.iver.cit.gvsig.project.ProjectElement;
49
import com.iver.cit.gvsig.project.ProjectMap;
50
import com.iver.cit.gvsig.project.ProjectTable;
51
import com.iver.cit.gvsig.project.ProjectView;
52
import com.iver.utiles.XMLEntity;
53
import com.iver.utiles.extensionPoints.ExtensionPoints;
54
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
55
import com.iver.utiles.extensionPoints.IExtensionBuilder;
56
import com.iver.utiles.xmlEntity.generate.XmlTag;
57
58
public class CopyPasteExtension extends Extension {
59
60
        public void initialize() {
61 6676 jmvivo
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
62
63
64
65 6635 jmvivo
                // TOC
66 6676 jmvivo
                MyTocMenuEntry copy = new CopyTocMenuEntry();
67
                MyTocMenuEntry cut = new CutTocMenuEntry();
68
                MyTocMenuEntry paste = new PasteTocMenuEntry();
69 6635 jmvivo
                Utiles utiles = new Utiles();
70
                copy.setUtiles(utiles);
71
                cut.setUtiles(utiles);
72
                paste.setUtiles(utiles);
73 6676 jmvivo
74
                extensionPoints.add("View_TocActions","Copy","Copy selectes layers to system clipboard",copy);
75
                extensionPoints.add("View_TocActions","Cut","Cut selectes layers to system clipboard", cut);
76
                extensionPoints.add("View_TocActions","Paste","Paste layers from system clipboard",paste);
77
78
                /*
79 6635 jmvivo
            FPopupMenu.addEntry(copy);
80
            FPopupMenu.addEntry(cut);
81
            FPopupMenu.addEntry(paste);
82 6676 jmvivo
                */
83 6635 jmvivo
84 6676 jmvivo
            // ProjectWindow
85 6635 jmvivo
                CopyProjectElement copyProjectElement = new CopyProjectElement();
86
                CutProjectElement cutProjectElement = new CutProjectElement();
87
                PasteProjectElement pasteProjectElementView = new PasteProjectElement();
88
                PasteProjectElement pasteProjectElementTable = new PasteProjectElement();
89
                PasteProjectElement pasteProjectElementMap = new PasteProjectElement();
90
91
                copyProjectElement.setUtiles(utiles);
92
                cutProjectElement.setUtiles(utiles);
93
                pasteProjectElementView.setUtiles(utiles);
94
                pasteProjectElementTable.setUtiles(utiles);
95
                pasteProjectElementMap.setUtiles(utiles);
96
97
                pasteProjectElementView.setType("views");
98
                pasteProjectElementTable.setType("tables");
99
                pasteProjectElementMap.setType("maps");
100
101
102 6676 jmvivo
                extensionPoints.add("DocumentActions_View","Copy","Copy selectes documento to system clipboard",copyProjectElement);
103
                extensionPoints.add("DocumentActions_View","Cut","Cut selectes documento to system clipboard", cutProjectElement);
104
                extensionPoints.add("DocumentActions_View","Paste","Paste views from system clipboard",pasteProjectElementView);
105 6635 jmvivo
106
107 6676 jmvivo
                extensionPoints.add("DocumentActions_Table","Copy","Copy selectes documento to system clipboard",copyProjectElement);
108
                extensionPoints.add("DocumentActions_Table","Cut","Cut selectes documento to system clipboard", cutProjectElement);
109
                extensionPoints.add("DocumentActions_Table","Paste","Paste tables from system clipboard",pasteProjectElementTable);
110 6635 jmvivo
111 6676 jmvivo
                extensionPoints.add("DocumentActions_Map","Copy","Copy selectes documento to system clipboard",copyProjectElement);
112
                extensionPoints.add("DocumentActions_Map","Cut","Cut selectes documento to system clipboard", cutProjectElement);
113
                extensionPoints.add("DocumentActions_Map","Paste","Paste maps from system clipboard",pasteProjectElementMap);
114 6635 jmvivo
115
        }
116
117
        public void execute(String actionCommand) {
118
                // TODO Auto-generated method stub
119
120
        }
121
122
        public boolean isEnabled() {
123
                return false;
124
        }
125
126
        public boolean isVisible() {
127
                return false;
128
        }
129
130
131
}
132
133 6676 jmvivo
abstract class MyDocumentAction extends AbstractDocumentContextMenuAction implements IExtensionBuilder {
134 6635 jmvivo
        protected Utiles utiles;
135
136
        public void setUtiles(Utiles utiles) {
137
                this.utiles = utiles;
138
        }
139
140
        public String getGroup() {
141
                return "ClipboardActions";
142
        }
143
144
145
        public Object create() {
146
                return this;
147
        }
148
149
        public Object create(Object[] args) {
150
                // TODO Auto-generated method stub
151
                return this;
152
        }
153
154
        public Object create(Map args) {
155
                // TODO Auto-generated method stub
156
                return this;
157
        }
158
}
159
160
161 6676 jmvivo
class CopyProjectElement extends MyDocumentAction{
162
        public String getDescription() {
163
                //FIXME: Falta claves
164
                //return PluginServices.getText(this,"tooltip_copiar_al_portapapeles");
165
                return null;
166
        }
167
168 6635 jmvivo
        public int getOrder() {
169 6676 jmvivo
170 6635 jmvivo
                return 0;
171
        }
172
173 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
174 6635 jmvivo
                return true;
175
        }
176
177 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
178 6635 jmvivo
                return selectedItems.length > 0;
179
        }
180
181
182 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
183 6635 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
184
                if (xml == null) {
185
                        //TODO: arreglar esto
186
                        return;
187
                }
188
189
                String data = this.utiles.marshallXMLEntity(xml);
190
                if (data == null) {
191
                        //TODO: arreglar esto
192
                        return;
193
                }
194
                this.utiles.putInClipboard(data);
195
        }
196
197
        public String getText() {
198
                return PluginServices.getText(this, "copiar");
199
        }
200
201
}
202
203 6676 jmvivo
class CutProjectElement extends MyDocumentAction {
204
        public String getDescription() {
205
                //FIXME: Falta claves
206
                //return PluginServices.getText(this,"tooltip_cortar_al_portapapeles");
207
                return null;
208
        }
209
210 6635 jmvivo
        public int getOrder() {
211
                return 1;
212
        }
213
214 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
215 6635 jmvivo
                return true;
216
        }
217
218 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
219 6635 jmvivo
                return selectedItems.length > 0;
220
        }
221
222
223 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
224 6635 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
225
                if (xml == null) {
226
                        //TODO: arreglar esto
227
                        return;
228
                }
229
230
                String data = this.utiles.marshallXMLEntity(xml);
231
                if (data == null) {
232
                        //TODO: arreglar esto
233
                        return;
234
                }
235
                this.utiles.putInClipboard(data);
236
237
238
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_el_documento"));
239
            if (option!=JOptionPane.OK_OPTION) {
240
                    return;
241
            }
242
243
244 6701 jmvivo
                this.utiles.removeDocuments(selectedItems);
245
246 6635 jmvivo
        }
247
248
        public String getText() {
249
                return PluginServices.getText(this, "cortar");
250
        }
251
252
}
253
254
class PasteProjectElement extends MyDocumentAction {
255
        private String type;
256 6676 jmvivo
257
        public String getDescription() {
258
                //FIXME: Falta claves
259
                //return PluginServices.getText(this,"tooltip_pegar_desde_el_portapapeles");
260
                return null;
261
        }
262
263 6635 jmvivo
        public int getOrder() {
264
                return 2;
265
        }
266
267
        public void setType(String type) {
268
                this.type = type;
269
        }
270
271
        public String getType(String type) {
272
                return this.type;
273
        }
274
275 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
276 6635 jmvivo
                return true;
277
        }
278
279 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
280 6635 jmvivo
                String sourceString = this.utiles.getFromClipboard();
281
                if (sourceString == null) return false;
282
283
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
284
                if (xml == null) return false;
285
286
                if (!this.utiles.checkXMLRootNode(xml)) return false;
287
288
                if (this.utiles.getXMLEntityChildOfType(xml,this.type) == null) return false;
289
                return true;
290
        }
291
292
293 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
294 6635 jmvivo
                String sourceString = this.utiles.getFromClipboard();
295
                if (sourceString == null) return;
296
297
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
298
                if (xml == null) return;
299
300
                if (!this.utiles.checkXMLRootNode(xml)) return;
301
302
                if (this.type.equals("views")) {
303
                        this.utiles.loadViewsFromXML(xml);
304
                } else if (this.type.equals("tables")) {
305
                        this.utiles.loadTablesFromXML(xml);
306
                } else if (this.type.equals("maps")) {
307
                        this.utiles.loadMapsFromXML(xml);
308
                } else {
309
                        //TODO que hacer aqui??
310
                        return;
311
                }
312
313
        }
314
315
        public String getText() {
316
                return PluginServices.getText(this, "pegar");
317
        }
318
319
}
320
321
322
323 6676 jmvivo
abstract class  MyTocMenuEntry extends AbstractTocContextMenuAction {
324 6635 jmvivo
        protected Utiles utiles;
325 6676 jmvivo
326 6635 jmvivo
        public void setUtiles(Utiles utiles) {
327
                this.utiles = utiles;
328
        }
329 6676 jmvivo
330
        public String getGroup() {
331
                return "copyPasteLayer";
332
        }
333
334
        public int getGroupOrder() {
335
                return 60;
336
        }
337 6635 jmvivo
338
}
339
340 6676 jmvivo
class CopyTocMenuEntry extends MyTocMenuEntry{
341
        public int getOrder() {
342
                return 0;
343
        }
344
345
        public String getText() {
346
                return PluginServices.getText(this, "copiar");
347
        }
348
349
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
350
                return selectedItems.length >= 1 && isTocItemBranch(item);
351
        }
352 6635 jmvivo
353
354 6676 jmvivo
        public void execute(ITocItem item, FLayer[] selectedItems) {
355
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
356 6635 jmvivo
                if (xml == null) {
357
                        //TODO: arreglar esto
358
                        return;
359
                }
360
361
                String data = this.utiles.marshallXMLEntity(xml);
362
                if (data == null) {
363
                        //TODO: arreglar esto
364
                        return;
365
                }
366
367
                this.utiles.putInClipboard(data);
368
369
        }
370
371
372 6676 jmvivo
}
373 6635 jmvivo
374 6676 jmvivo
class CutTocMenuEntry extends MyTocMenuEntry{
375
        public int getOrder() {
376
                return 1;
377 6635 jmvivo
        }
378 6676 jmvivo
379
        public String getText() {
380
                return PluginServices.getText(this, "cortar");
381
        }
382
383
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
384
                return selectedItems.length >= 1 && isTocItemBranch(item);
385
        }
386 6635 jmvivo
387 6676 jmvivo
388
        public void execute(ITocItem item, FLayer[] selectedItems) {
389
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
390 6635 jmvivo
                if (xml == null) {
391
                        //TODO: arreglar esto
392
                        return;
393
                }
394
395
                String data = this.utiles.marshallXMLEntity(xml);
396
                if (data == null) {
397
                        //TODO: arreglar esto
398
                        return;
399
                }
400
401
402
                this.utiles.putInClipboard(data);
403
404
405
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
406
            if (option!=JOptionPane.OK_OPTION) {
407
                    return;
408
            }
409
                getMapContext().beginAtomicEvent();
410
411
412 6676 jmvivo
                boolean isOK =this.utiles.removeLayers(selectedItems);
413 6635 jmvivo
414
                getMapContext().endAtomicEvent();
415
416
                if (isOK) {
417
                        getMapContext().invalidate();
418
                        if (getMapContext().getLayers().getLayersCount()==0) {
419
                                PluginServices.getMainFrame().enableControls();
420
                        }
421
                }
422
423
        }
424
}
425
426
427 6676 jmvivo
class PasteTocMenuEntry extends MyTocMenuEntry{
428 6635 jmvivo
        private XMLEntity xml=null;
429
430 6676 jmvivo
        public int getOrder() {
431
                return 2;
432
        }
433
434
        public String getText() {
435
                return PluginServices.getText(this, "pegar");
436
        }
437
438
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
439
                if (isTocItemBranch(item)) {
440
                        FLayer lyr = getNodeLayer(item);
441 6635 jmvivo
                        if (lyr instanceof FLayers) {
442
                                this.xml = this.getCheckedXMLFromClipboard();
443 6676 jmvivo
                                return true;
444 6635 jmvivo
                        }
445
446 6676 jmvivo
                } else if (!isTocItemLeaf(item)) {
447
                        if (getNodeLayer(item) == null) {
448 6635 jmvivo
                                this.xml = this.getCheckedXMLFromClipboard();
449 6676 jmvivo
                                return this.xml != null;
450
                        }
451
                }
452
                return false;
453 6635 jmvivo
        }
454 6676 jmvivo
455 6635 jmvivo
        private XMLEntity getCheckedXMLFromClipboard() {
456
                String sourceString = this.utiles.getFromClipboard();
457
                if (sourceString == null) return null;
458
459
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
460
                if (xml == null) return null;
461
462
                if (!this.utiles.checkXMLRootNode(xml)) return null;
463
464
                if (this.utiles.getXMLEntityChildOfType(xml,"layers") == null) return null;
465
466
                return  xml;
467
        }
468
469 6676 jmvivo
        public void execute(ITocItem item, FLayer[] selectedItems) {
470 6635 jmvivo
                FLayers root;
471
472
                if (this.xml == null) return;
473
474 6676 jmvivo
                if (isTocItemBranch(item)) {
475
                        root = (FLayers)getNodeLayer(item);
476
                } else if (getNodeLayer(item) == null){
477 6635 jmvivo
                        root = getMapContext().getLayers();
478
                } else {
479
                        return;
480
                }
481
                getMapContext().beginAtomicEvent();
482
483
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
484
485
                getMapContext().endAtomicEvent();
486
487
                if (isOK) getMapContext().invalidate();
488
        }
489
490
}
491
492
493
class Utiles {
494
495
        /*
496
         *
497
======================================
498
 Comportamiento del Pegar documentos:
499
======================================
500

501
?Pegar vista.
502
        Si ya existe una vista en el proyecto con el mismo nombre.
503
                1. Abortar
504
                2. Pedir nuevo nombre.
505
                        Que hacemos con las tablas asociadas.
506
                                No se pegan
507
        Si alguna de las tablas a pegar de las que van en
508
        el portapapeles ya existen en el proyecto.
509
                1. abortamos
510
                2. Informamos al usuario y no se pegan las tablas.
511

512
Pegar tabla.
513
        Si alguna de las tablas existe.
514
                Se pega igualmente (apareceran tablas duplicadas)
515

516
Pegar mapa.
517
        Si el mapa ya existe en el proyecto.
518
                1. Abortar
519
                2. renombrar el mapa
520
        Si alguna vista ya existe en el proyecto.
521
                1. Abortar
522
                2. Usar la vista que ya existe en el proyecto y no
523
                   pegar la nueva vista.
524
        Si alguna de las tablas a pegar de las que van en
525
                el portapapeles ya existen en el proyecto.
526
                        1. abortamos
527
                        2. Informamos al usuario y no se pegan las tablas.
528

529

530
         */
531
532
533
        /*
534
         *
535
         *
536
         *
537
         * Funciones Publicas para generar XML (copiar)
538
         *
539
         *
540
        */
541
542
        /**
543
         * Genera un XMLEntity con la informacion necesaria
544
         * para copiar los elementos de selectedItems en
545
         * otro proyecto
546
         */
547
        public XMLEntity generateXMLCopyDocuments(ProjectElement[] selectedItems) {
548
                if (selectedItems.length == 0) return null;
549
550
                if (selectedItems[0] instanceof ProjectView) {
551
                        ProjectView[] views = new ProjectView[selectedItems.length];
552
                        System.arraycopy(selectedItems,0,views,0,selectedItems.length);
553
                        return this.generateXMLCopyViews(views);
554
                } else if (selectedItems[0] instanceof ProjectMap) {
555
                        ProjectMap[] maps = new ProjectMap[selectedItems.length];
556
                        System.arraycopy(selectedItems,0,maps,0,selectedItems.length);
557
                        return this.generateXMLCopyMaps(maps);
558
                } else if (selectedItems[0] instanceof ProjectTable) {
559
                        ProjectTable[] tables = new ProjectTable[selectedItems.length];
560
                        System.arraycopy(selectedItems,0,tables,0,selectedItems.length);
561
                        return this.generateXMLCopyTables(tables);
562
                } else {
563
                        //FIXME:????
564
                        return null;
565
                }
566
        }
567
568
        public XMLEntity generateXMLCopyViews(ProjectView[] selectedItems) {
569
                XMLEntity xml = this.newRootNode();
570
571
                XMLEntity xmlTables = this.newTablesNode();
572
                XMLEntity xmlDataSources = this.newDataSourcesNode();
573
                XMLEntity xmlViews = this.newViewsNode();
574
575
                for (int i=0;i < selectedItems.length; i++) {
576
                        if (!this.addToXMLView(selectedItems[i],xmlViews,xmlTables,xmlDataSources)) return null;
577
578
                }
579
580
581
                if (xmlDataSources.getNumChild() > 0) {
582
                        xml.addChild(xmlDataSources);
583
                }
584
                if (xmlViews.getNumChild() > 0) {
585
                        xml.addChild(xmlViews);
586
                }
587
                if (xmlTables.getNumChild() > 0) {
588
                        xml.addChild(xmlTables);
589
                }
590
591
                return xml;
592
593
        }
594
595
596
        public XMLEntity generateXMLCopyMaps(ProjectMap[] selectedItems) {
597
                XMLEntity xml = this.newRootNode();
598
599
                XMLEntity xmlTables = this.newTablesNode();
600
                XMLEntity xmlDataSources = this.newDataSourcesNode();
601
                XMLEntity xmlViews = this.newViewsNode();
602
                XMLEntity xmlMaps = this.newMapsNode();
603
604
                for (int i=0;i < selectedItems.length; i++) {
605
                        if (!this.addToXMLMap(selectedItems[i],xmlMaps,xmlViews,xmlTables,xmlDataSources)) return null;
606
607
                }
608
609
610
                if (xmlDataSources.getNumChild() > 0) {
611
                        xml.addChild(xmlDataSources);
612
                }
613
                if (xmlViews.getNumChild() > 0) {
614
                        xml.addChild(xmlViews);
615
                }
616
                if (xmlTables.getNumChild() > 0) {
617
                        xml.addChild(xmlTables);
618
                }
619
                if (xmlMaps.getNumChild() > 0) {
620
                        xml.addChild(xmlMaps);
621
                }
622
623
624
                return xml;
625
        }
626
627
        public XMLEntity generateXMLCopyTables(ProjectTable[] selectedItems) {
628
                XMLEntity xml = this.newRootNode();
629
630
                XMLEntity xmlTables = this.newTablesNode();
631
                XMLEntity xmlDataSources = this.newDataSourcesNode();
632
633
                for (int i=0;i < selectedItems.length; i++) {
634
                        if (!this.addToXMLTable(selectedItems[i],xmlTables,xmlDataSources,null)) return null;
635
                }
636
637
638
                if (xmlDataSources.getNumChild() > 0) {
639
                        xml.addChild(xmlDataSources);
640
                }
641
                if (xmlTables.getNumChild() > 0) {
642
                        xml.addChild(xmlTables);
643
                }
644
645
                return xml;
646
        }
647
648
649
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
650
651
                XMLEntity xml = this.newRootNode();
652
                XMLEntity xmlLayers = this.newLayersNode();
653
                XMLEntity xmlTables = this.newTablesNode();
654
                XMLEntity xmlDataSources = this.newDataSourcesNode();
655
656
                for (int i=0;i < actives.length; i++) {
657
                        if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) return null;
658
659
                }
660
661
                if (xmlDataSources.getNumChild() > 0) {
662
                        xml.addChild(xmlDataSources);
663
                }
664
                if (xmlLayers.getNumChild() > 0) {
665
                        xml.addChild(xmlLayers);
666
                }
667
                if (xmlTables.getNumChild() > 0) {
668
                        xml.addChild(xmlTables);
669
                }
670
671
                return xml;
672
673
        }
674
675
676
677
        /*
678
         *
679
         *
680
         *
681
         * Funciones Publicas de carga de un XML (pegar)
682
         *
683
         *
684
         *
685
        */
686
687
        public boolean loadLayersFromXML(XMLEntity xml, FLayers root) {
688
                XMLEntity xmlLayers = this.getXMLEntityChildOfType(xml,"layers");
689
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
690
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
691
692
                if (xmlLayers == null ) return false;
693
694
                // Se pegan las tablas igualmente
695
                /*
696
                Project project = this.getProject();
697

698
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
699
                */
700
701
702
                if (xmlDataSources != null)  {
703
                        if (!this.registerDataSources(xmlDataSources)) return false;
704
                }
705
706
                if (!this.addLayers(xmlLayers,root)) return false;
707
708
                if (xmlTables != null)  {
709
                        if (!this.addTables(xmlTables)) return false;
710
                }
711
712
                return true;
713
714
        }
715
716
717
        public boolean loadViewsFromXML(XMLEntity xml) {
718
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
719
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
720
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
721
722
                if (xmlViews == null ) return false;
723
724
                Project project = this.getProject();
725
726
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
727
728
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
729
730
                if (viewsConflits != null && viewsConflits.size() > 0) {
731
                        int option = JOptionPane.showConfirmDialog(
732
                                        (Component)PluginServices.getMainFrame(),
733
                                        "<html>"+
734 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
735
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
736 6635 jmvivo
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
737
                                                PluginServices.getText(this,"desea_continuar") +
738
                                        "</html>",
739
                                        PluginServices.getText(this,"pegar_vistas"),
740
                                        JOptionPane.YES_NO_OPTION
741
                                        );
742
                        if (option != JOptionPane.YES_OPTION) {
743
                                return false;
744
                        }
745
                        Enumeration en = viewsConflits.elements();
746
                        while (en.hasMoreElements()) {
747
                                XMLEntity view = (XMLEntity)en.nextElement();
748
                                String newName = JOptionPane.showInputDialog(
749
                                                (Component)PluginServices.getMainFrame(),
750
                                                "<html>"+
751 6645 jmvivo
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
752 6635 jmvivo
                                                "</html>", //Mensaje
753
                                                view.getStringProperty("name") //Valor por defecto
754
                                                );
755
                                if (newName == null) {
756
                                        JOptionPane.showMessageDialog(
757
                                                        (Component)PluginServices.getMainFrame(),
758
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
759
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
760
                                                        JOptionPane.ERROR_MESSAGE
761
                                                        );
762
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
763
                                        JOptionPane.showMessageDialog(
764
                                                        (Component)PluginServices.getMainFrame(),
765
                                                        "<html>"+
766
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
767
                                                                PluginServices.getText(this,"nombre_no_valido")+
768
                                                        "</html>",//Mensaje
769
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
770
                                                        JOptionPane.ERROR_MESSAGE
771
                                                        );
772
                                        return false;
773
                                }
774
                                view.setName(newName);
775
                        }
776
                        if (xmlTables != null) xmlTables.removeAllChilds();
777
                        tablesConflits = null;
778
                }
779
780
                if (tablesConflits != null && tablesConflits.size() > 0) {
781
                        int option = JOptionPane.showConfirmDialog(
782
                                        (Component)PluginServices.getMainFrame(),
783
                                        "<html>" +
784 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
785 6635 jmvivo
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
786
                                                PluginServices.getText(this,"desea_continuar") +
787
                                        "</html>", //Mensaje
788
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
789
                                        JOptionPane.YES_NO_OPTION
790
                                        );
791
                        if (option != JOptionPane.YES_OPTION) {
792
                                return false;
793
                        }
794
                        xmlTables.removeAllChilds();
795
                }
796
797
798
                if (xmlDataSources != null)  {
799
                        if (!this.registerDataSources(xmlDataSources)) return false;
800
                }
801
802
                if (!this.addViews(xmlViews)) return false;
803
804
                if (xmlTables != null)  {
805
                        if (!this.addTables(xmlTables)) return false;
806
                }
807
808
                return true;
809
        }
810
811
        public boolean loadTablesFromXML(XMLEntity xml) {
812
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
813
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
814
815
816
                if (xmlTables == null ) return false;
817
818
                /*
819
                Project project = this.getProject();
820

821
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
822
                */
823
824
                if (xmlDataSources != null)  {
825
                        if (!this.registerDataSources(xmlDataSources)) return false;
826
                }
827
828
829
830
                return this.addTables(xmlTables);
831
        }
832
833
        public boolean loadMapsFromXML(XMLEntity xml) {
834
                XMLEntity xmlMaps = this.getXMLEntityChildOfType(xml,"Maps");
835
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
836
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
837
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
838
839
                if (xmlMaps == null ) return false;
840
841
                Project project = this.getProject();
842
843
                Hashtable mapsConflits = this.getConflicts(xmlMaps,project.getMaps());
844
845
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
846
847
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
848
849
850
                if (mapsConflits != null && mapsConflits.size() > 0) {
851
                        int option = JOptionPane.showConfirmDialog(
852
                                        (Component)PluginServices.getMainFrame(),
853
                                        "<html>"+
854 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_mapas_al_pegar") + "<br>" +
855
                                                PluginServices.getText(this,"debera_introducir_nombres_para_los_mapas_a_pegar") + "<br>" +
856 6635 jmvivo
                                        "</html>", //Mensaje
857
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
858
                                        JOptionPane.YES_NO_OPTION
859
                                        );
860
                        if (option != JOptionPane.YES_OPTION) {
861
                                return false;
862
                        }
863
                        Enumeration en = mapsConflits.elements();
864
                        while (en.hasMoreElements()) {
865
                                XMLEntity map = (XMLEntity)en.nextElement();
866
                                String newName = JOptionPane.showInputDialog(
867
                                                (Component)PluginServices.getMainFrame(),
868
                                                "<html>"+
869
                                                        PluginServices.getText(this,"nuevo_nombre_para_el_mapa") +" "+  map.getStringProperty("name") + ":" +
870
                                            "</html>", //Mensaje
871
                                                map.getStringProperty("name") //Valor por defecto
872
                                                );
873
                                if (newName == null) {
874
                                        JOptionPane.showMessageDialog(
875
                                                        (Component)PluginServices.getMainFrame(),
876
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
877
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
878
                                                        JOptionPane.ERROR_MESSAGE
879
                                                        );
880
                                } else if (newName.equalsIgnoreCase(map.getStringProperty("name")) ) {
881
                                        JOptionPane.showMessageDialog(
882
                                                        (Component)PluginServices.getMainFrame(),
883
                                                        "<html>"+
884
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
885
                                                                PluginServices.getText(this,"nombre_no_valido")+
886
                                                        "</html>",//Mensaje
887
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
888
                                                        JOptionPane.ERROR_MESSAGE
889
                                                        );
890
                                        return false;
891
                                }
892
                                map.setName(newName);
893
                        }
894
                }
895
896
                if (viewsConflits != null && viewsConflits.size() > 0) {
897
                        int option = JOptionPane.showConfirmDialog(
898
                                        (Component)PluginServices.getMainFrame(),
899
                                        "<html>"+
900 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
901 6635 jmvivo
                                                PluginServices.getText(this,"no_se_pegaran_las_vistas_del_conflicto") + "<br>" +
902
                                                PluginServices.getText(this,"desea_continuar") +
903
                                        "</html>",
904
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
905
                                        JOptionPane.YES_NO_OPTION
906
                                        );
907
                        if (option != JOptionPane.YES_OPTION) {
908
                                return false;
909
                        }
910 6645 jmvivo
                        // Eliminamos las vistas del xml que no vamos a importar
911 6635 jmvivo
912 6645 jmvivo
                        // Esto me devuelve los indices en orden inverso
913
                        int[] indexes = this.getIndexOfConflict(viewsConflits);
914
                        for (int i=0;i < indexes.length;i++) {
915
                                xmlViews.removeChild(indexes[i]);
916
                        }
917
                        viewsConflits = null;
918
919 6635 jmvivo
                }
920
921
922
                if (tablesConflits != null && tablesConflits.size() > 0) {
923
                        int option = JOptionPane.showConfirmDialog(
924
                                        (Component)PluginServices.getMainFrame(),
925
                                        "<html>" +
926
                                                PluginServices.getText(this,"conflito_de_nombres_de_tablas_al_pegar") + "<br>" +
927
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
928
                                                PluginServices.getText(this,"desea_continuar") +
929
                                        "</html>", //Mensaje
930
                                        PluginServices.getText(this,"pegar_mapas"),
931
                                        JOptionPane.YES_NO_OPTION
932
                                        );
933
                        if (option != JOptionPane.YES_OPTION) {
934
                                return false;
935
                        }
936
                        xmlTables.removeAllChilds();
937
                }
938
939
940
                if (xmlDataSources != null)  {
941
                        if (!this.registerDataSources(xmlDataSources)) return false;
942
                }
943
944
                if (xmlViews != null)  {
945
                        if (!this.addViews(xmlViews)) return false;
946
                }
947
948
                if (xmlTables != null)  {
949
                        if (!this.addTables(xmlTables)) return false;
950
                }
951
952
                return this.addMaps(xmlMaps);
953
954
        }
955
956
957
958
959
960
961
962
963
964
965
966 6645 jmvivo
        /**
967
         * Devuelve las claves de conflits ordenados
968
         * en orden inverso. Las claves se esperan que
969
         * sean instancias de Integer
970
         */
971
        private int[] getIndexOfConflict(Hashtable conflits) {
972
                Object[] tmpArray = conflits.keySet().toArray();
973
                Arrays.sort(tmpArray,new Comparator() {
974
                        public int compare(Object o1, Object o2) {
975
                                return ((Integer)o2).intValue() - ((Integer)o1).intValue();
976
                        }
977
                }
978
                );
979
                int[] indexes = new int[] {tmpArray.length};
980
                for (int i = 0;i< tmpArray.length;i++) {
981
                        indexes[i] = ((Integer)tmpArray[i]).intValue();
982
                }
983
                return indexes;
984 6635 jmvivo
985 6645 jmvivo
986
        }
987 6635 jmvivo
988
989
        private boolean addToXMLMapDependencies(ProjectMap map, XMLEntity xmlViews,XMLEntity xmlTables, XMLEntity xmlDataSources) {
990
                IFFrame[] components = map.getModel().getFFrames();
991
                for (int i=0; i < components.length; i++) {
992
                        if (components[i] instanceof FFrameView) {
993
                                ProjectView view = ((FFrameView)components[i]).getView();
994
                                if (findChildInXML(xmlViews,"name",view.getName())==null) {
995
                                        if (!this.addToXMLView(view,xmlViews,xmlTables,xmlDataSources)) return false;
996
                                }
997
                        }
998
                }
999
1000
                return true;
1001
        }
1002
1003
        private boolean addToXMLMap(ProjectMap map,XMLEntity xmlMaps,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1004
                try {
1005
                        xmlMaps.addChild(map.getXMLEntity());
1006
1007
                        return this.addToXMLMapDependencies(map,xmlViews,xmlTables,xmlDataSources);
1008
1009
                } catch (SaveException e) {
1010
                        // TODO Auto-generated catch block
1011
                        e.printStackTrace();
1012
                        return false;
1013
                }
1014
        }
1015
1016
        private boolean addToXMLView(ProjectView view,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1017
                try {
1018
                        xmlViews.addChild(view.getXMLEntity());
1019
1020
                        if (!this.addToXMLLayerDependencies(view.getMapContext().getLayers(),xmlTables,xmlDataSources)) return false;
1021
1022 6701 jmvivo
                        if (view.getMapOverViewContext() != null) {
1023
                                return this.addToXMLLayerDependencies(view.getMapOverViewContext().getLayers(),xmlTables,xmlDataSources);
1024
                        } else {
1025
                                return true;
1026
                        }
1027 6635 jmvivo
1028
1029
1030
                } catch (SaveException e) {
1031
                        // TODO Auto-generated catch block
1032
                        e.printStackTrace();
1033
                        return false;
1034
                }
1035
        }
1036
1037
1038
1039
        public boolean checkXMLRootNode(XMLEntity xml) {
1040
                if (!xml.contains("applicationName")) return false;
1041
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) return false;
1042
1043
                if (!xml.contains("version")) return false;
1044
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false;
1045
1046
                return true;
1047
        }
1048
1049
        private void fillXMLRootNode(XMLEntity xml) {
1050
                xml.putProperty("applicationName","gvSIG");
1051
                xml.putProperty("version",Version.format());
1052
        }
1053
1054
        public XMLEntity getXMLEntityChildOfType(XMLEntity xml,String type) {
1055
                int childCount = xml.getNumChild();
1056
                XMLEntity child;
1057
                for (int i=0; i < childCount; i++  ) {
1058
                        child = xml.getChild(i);
1059
                        if (child.contains("type")) {
1060
                                if (child.getStringProperty("type").equalsIgnoreCase(type)) {
1061
                                        return child;
1062
                                }
1063
                        }
1064
                }
1065
                return null;
1066
1067
        }
1068
1069
        private Hashtable getConflicts(XMLEntity xml,ArrayList elements) {
1070
                if (xml == null || xml.getNumChild() < 1) return null;
1071
                Hashtable conflits = new Hashtable();
1072
                for (int iXML=0;iXML < xml.getNumChild();iXML++) {
1073
                        XMLEntity child = xml.getChild(iXML);
1074
                        Iterator iter = elements.iterator();
1075
                        while (iter.hasNext()) {
1076
                                ProjectElement element = (ProjectElement)iter.next();
1077
                                if (element.getName().equalsIgnoreCase(child.getStringProperty("name"))) {
1078
                                        conflits.put(new Integer(iXML),child);
1079
                                        break;
1080
                                }
1081
1082
                        }
1083
                }
1084
                return conflits;
1085
        }
1086
1087
1088
1089
        private boolean registerDataSources(XMLEntity xmlDataSources) {
1090
                try {
1091
                        int numDataSources = xmlDataSources.getNumChild();
1092
1093
                        if (numDataSources == 0) return true;
1094
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
1095
1096
                        for (int i = 0; i < numDataSources; i++) {
1097
                                XMLEntity child = xmlDataSources.getChild(i);
1098
                                String name = child.getStringProperty("gdbmsname");
1099
1100
                                if (dsFactory.getDriverInfo(name) == null) {
1101
                                        if (child.getStringProperty("type").equals("otherDriverFile")) {
1102
                                                LayerFactory.getDataSourceFactory().addFileDataSource(
1103
                                                                child.getStringProperty("driverName"),
1104
                                                                name,
1105
                                                                child.getStringProperty("file")
1106
                                                );
1107
1108
1109
                                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
1110
                                                /*                                String layerName = child.getStringProperty("layerName");
1111
                                                 ProjectView vista = project.getViewByName(child.getStringProperty(
1112
                                                 "viewName"));
1113
                                                 FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
1114

1115
                                                 modelo = ((AlphanumericData) layer).getRecordset();
1116
                                                 associatedTable = (AlphanumericData) layer;
1117
                                                 */
1118
                                        } else if (child.getStringProperty("type").equals("db")) {
1119
                                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
1120
                                                                name,
1121
                                                                child.getStringProperty("host"),
1122
                                                                child.getIntProperty("port"),
1123
                                                                child.getStringProperty("user"),
1124
                                                                child.getStringProperty("password"),
1125
                                                                child.getStringProperty("dbName"),
1126
                                                                child.getStringProperty("tableName"),
1127
                                                                child.getStringProperty("driverInfo")
1128
                                                );
1129
                                        }
1130
                                }
1131
                        }
1132
1133
                        return true;
1134
                } catch (Exception e) {
1135
                        e.printStackTrace();
1136
                        return false;
1137
                }
1138
        }
1139
1140
        private boolean addTables(XMLEntity xmlTables) {
1141
                try {
1142
                        int numTables = xmlTables.getNumChild();
1143
                        if (numTables == 0) return true;
1144
1145
                        Project project = this.getProject();
1146
1147
                        for (int i = 0; i < numTables; i++) {
1148
                                try{
1149
                                        ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project);
1150
                                        project.addTable(ptable);
1151
                                        /*
1152
                                        if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
1153
                                                PluginServices.getMDIManager().addView(ptable.getAndamiView());
1154
                                                PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
1155
                                        }
1156
                                        */
1157
                                }catch(OpenException e){
1158
                                        e.printStackTrace();
1159
                                        return false;
1160
                                }
1161
                        }
1162
1163
                        project.setLinkTable();
1164
1165
                        return true;
1166
                } catch (Exception e) {
1167
                        e.printStackTrace();
1168
                        return false;
1169
                }
1170
        }
1171
1172
        public XMLEntity findChildInXML(XMLEntity xml,String propName,String value) {
1173
                int num = xml.getNumChild();
1174
                XMLEntity child;
1175
                for (int i=0;i < num; i++) {
1176
                        child = xml.getChild(i);
1177
                        if (child.getStringProperty(propName).equals(value)) {
1178
                                return child;
1179
                        }
1180
                }
1181
                return null;
1182
        }
1183
1184
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
1185
                try {
1186
                        XMLEntity child;
1187
                        int numLayers = xmlLayers.getNumChild();
1188
                        for (int i = 0; i < numLayers; i++) {
1189
                                child = xmlLayers.getChild(i);
1190
                                if (!root.addLayerFromXMLEntity(child,null)) return false;
1191
                        }
1192
                        return true;
1193
                } catch (Exception e) {
1194
                        e.printStackTrace();
1195
                        return false;
1196
                }
1197
1198
        }
1199
1200
1201
        private boolean addViews(XMLEntity xmlViews) {
1202
                try {
1203
                        Project project = this.getProject();
1204
                        XMLEntity child;
1205
                        int numLayers = xmlViews.getNumChild();
1206
                        for (int i = 0; i < numLayers; i++) {
1207
                                child = xmlViews.getChild(i);
1208
1209
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(child, project);
1210
                                project.addView(pv);
1211
1212
                        }
1213
                        return true;
1214
                } catch (Exception e) {
1215
                        e.printStackTrace();
1216
                        return false;
1217
                }
1218
1219
        }
1220
1221
        private boolean addMaps(XMLEntity xmlMaps) {
1222
                try {
1223
                        Project project = this.getProject();
1224
                        XMLEntity child;
1225
                        int numLayers = xmlMaps.getNumChild();
1226
                        for (int i = 0; i < numLayers; i++) {
1227
                                child = xmlMaps.getChild(i);
1228
1229
                                ProjectMap pm = (ProjectMap) ProjectMap.createFromXML(child, project);
1230
                                project.addMap(pm);
1231
1232
                        }
1233
                        return true;
1234
                } catch (Exception e) {
1235
                        e.printStackTrace();
1236
                        return false;
1237
                }
1238
1239
        }
1240
1241
        private Project getProject() {
1242
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1243
        }
1244
1245
        private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) {
1246
                if (project== null) {
1247
                        project = this.getProject();
1248
                }
1249
                    xmlDataSources.addChild(project.getSourceInfoXMLEntity(source));
1250
1251
                    return true;
1252
        }
1253
1254
        private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) {
1255
                if (project== null) {
1256
                        project = this.getProject();
1257
                }
1258
                if (findChildInXML(xmlTables,"name",pt.getName()) != null) return true;
1259
                XMLEntity xmlTable = null;
1260
                try {
1261
                        xmlTable = pt.getXMLEntity();
1262
1263
                        xmlTables.addChild(xmlTable);
1264
1265 6701 jmvivo
                        if (pt.getAssociatedTable() != null) {
1266
                                this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
1267
                        }
1268 6635 jmvivo
1269
                        if (pt.getLinkTable() != null) {
1270
                                if (findChildInXML(xmlTables,"name",pt.getLinkTable()) == null)  {
1271
                                        ProjectTable ptLink = project.getTable(pt.getLinkTable());
1272
                                        if (!this.addToXMLTable(ptLink,xmlTables,xmlDataSources,project)) return false;
1273
                                }
1274
                        }
1275
                } catch (SaveException e) {
1276
                        // TODO Auto-generated catch block
1277
                        e.printStackTrace();
1278
                        return false;
1279
                } catch (DriverException e) {
1280
                        // TODO Auto-generated catch block
1281
                        e.printStackTrace();
1282
                        return false;
1283
                }
1284
1285
                    return true;
1286
        }
1287
1288
        private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1289
                try {
1290
                        Project project = this.getProject();
1291
1292
                        if (lyr instanceof FLayers) {
1293
                                FLayers lyrs = (FLayers)lyr;
1294
                                int count = lyrs.getLayersCount();
1295
                                for (int i=0;i < count;i++) {
1296
                                        FLayer subLyr = lyrs.getLayer(i);
1297
                                        this.addToXMLLayerDependencies(subLyr,xmlTables,xmlDataSources);
1298
                                }
1299
1300
                    } else if (lyr instanceof AlphanumericData){
1301
                    if (!this.addToXMLDataSource(
1302
                                ((AlphanumericData)lyr).getRecordset().getSourceInfo(),
1303
                                xmlDataSources,
1304
                                project
1305
1306
                    )) return false;
1307
1308
                ProjectTable pt = project.getTable((AlphanumericData) lyr);
1309
                if (pt != null) {
1310
                        if (!this.addToXMLTable(pt,xmlTables,xmlDataSources,project)) return false;
1311
                }
1312
1313
            }
1314
1315
                } catch (DriverException e) {
1316
                        // TODO Auto-generated catch block
1317
                        e.printStackTrace();
1318
                        return false;
1319
                }
1320
                return true;
1321
1322
        }
1323
1324
        private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1325
                try {
1326
                        xmlLayers.addChild(lyr.getXMLEntity());
1327
1328
                        return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources);
1329
1330
                } catch (XMLException e) {
1331
                        e.printStackTrace();
1332
                        return false;
1333
                }
1334
        }
1335
1336
        private XMLEntity newRootNode() {
1337
                XMLEntity xml = new XMLEntity();
1338
                fillXMLRootNode(xml);
1339
                return xml;
1340
        }
1341
1342
        private XMLEntity newLayersNode() {
1343
                XMLEntity xmlLayers = new XMLEntity();
1344
                xmlLayers.putProperty("type","layers");
1345
                return xmlLayers;
1346
        }
1347
1348
        private XMLEntity newDataSourcesNode() {
1349
                XMLEntity xmlDataSources = new XMLEntity();
1350
                xmlDataSources.putProperty("type","dataSources");
1351
                return xmlDataSources;
1352
        }
1353
1354
        private XMLEntity newTablesNode() {
1355
                XMLEntity xmlTables = new XMLEntity();
1356
                xmlTables.putProperty("type","tables");
1357
                return xmlTables;
1358
        }
1359
1360
        private XMLEntity newViewsNode() {
1361
                XMLEntity xmlTables = new XMLEntity();
1362
                xmlTables.putProperty("type","views");
1363
                return xmlTables;
1364
        }
1365
1366
        private XMLEntity newMapsNode() {
1367
                XMLEntity xmlTables = new XMLEntity();
1368
                xmlTables.putProperty("type","maps");
1369
                return xmlTables;
1370
        }
1371
1372
1373
        public void putInClipboard(String data) {
1374
                StringSelection ss = new StringSelection(data);
1375
1376
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
1377
        }
1378
1379
        public String marshallXMLEntity(XMLEntity xml) {
1380
                StringWriter buffer = new StringWriter();
1381
1382
                Marshaller m;
1383
                try {
1384
                        m = new Marshaller(buffer);
1385
                } catch (IOException e4) {
1386
                        // TODO Auto-generated catch block
1387
                        e4.printStackTrace();
1388
                        return null;
1389
                }
1390
                m.setEncoding("ISO-8859-1");
1391
1392
                try {
1393
                        m.marshal(xml.getXmlTag());
1394
                        //if (i < actives.length-1) buffer.write("\n##layer-separator##\n");
1395
                } catch (MarshalException e2) {
1396
                        // TODO Auto-generated catch block
1397
                        e2.printStackTrace();
1398
                        return null;
1399
                } catch (ValidationException e3) {
1400
                        // TODO Auto-generated catch block
1401
                        e3.printStackTrace();
1402
                        return null;
1403
                }
1404
1405
                return buffer.toString();
1406
1407
        }
1408
1409
        public XMLEntity unMarshallXMLEntity(String data) {
1410
                StringReader reader = new StringReader(data);
1411
1412
                XmlTag tag;
1413
                try {
1414
                        tag = (XmlTag) XmlTag.unmarshal(reader);
1415
                } catch (MarshalException e) {
1416
                        return null;
1417
                } catch (ValidationException e) {
1418
                        return null;
1419
                }
1420
                XMLEntity xml=new XMLEntity(tag);
1421
1422
                return xml;
1423
        }
1424
1425
        public String getFromClipboard() {
1426
1427
                 try {
1428
                        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
1429
                } catch (UnsupportedFlavorException e) {
1430
                        return null;
1431
                } catch (IOException e) {
1432
                        // TODO Auto-generated catch block
1433
                        return null;
1434
                }
1435
        }
1436
1437
        public boolean removeLayers(FLayer[] actives) {
1438
            for (int i = actives.length-1; i>=0; i--){
1439
                try {
1440
                                //actives[i].getParentLayer().removeLayer(actives[i]);
1441
                                //FLayers lyrs=getMapContext().getLayers();
1442
                                //lyrs.addLayer(actives[i]);
1443
                                actives[i].getParentLayer().removeLayer(actives[i]);
1444
1445
                if (actives[i] instanceof AlphanumericData){
1446
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1447
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
1448
1449
                    ArrayList tables = project.getTables();
1450
                    for (int j = 0; j < tables.size(); j++) {
1451
                        if (tables.get(j) == pt){
1452
                            project.delTable(j);
1453
                            break;
1454
                        }
1455
                    }
1456
1457
                    PluginServices.getMDIManager().closeSingletonView(pt);
1458
                }
1459
1460
1461
                    } catch (CancelationException e1) {
1462
                            e1.printStackTrace();
1463
                            return false;
1464
                    }
1465
            }
1466
                return true;
1467
        }
1468 6701 jmvivo
1469
        public boolean removeDocuments(ProjectElement[] selectedItems) {
1470
                Project p = this.getProject();
1471
                ProjectElement element;
1472
                int index;
1473
                for (int i=selectedItems.length-1;i>=0;i--) {
1474
1475
                        element = selectedItems[i];
1476
1477
                        if (element instanceof ProjectMap) {
1478
1479
                                if (element.isLocked()) {
1480
                                        JOptionPane.showMessageDialog(
1481
                                                (Component)PluginServices.getMainFrame(),
1482
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1483
                                        );
1484
                                        //return false;
1485
                                } else {
1486
                                        PluginServices.getMDIManager().closeSingletonView(element);
1487
                                        p.delMap(p.getMaps().indexOf(element));
1488
                                }
1489
                        } else if (element instanceof ProjectTable) {
1490
                                if (element.isLocked()) {
1491
                                        JOptionPane.showMessageDialog(
1492
                                                (Component)PluginServices.getMainFrame(),
1493
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1494
                                        );
1495 6635 jmvivo
1496 6701 jmvivo
                                        //return false;
1497
                                } else {
1498
                                        PluginServices.getMDIManager().closeSingletonView(element);
1499
                                        p.delTable(p.getTables().indexOf(element));
1500
                                }
1501
                        } else {
1502
                                if (element.isLocked()) {
1503
                                        JOptionPane.showMessageDialog(
1504
                                                (Component)PluginServices.getMainFrame(),
1505
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1506
                                        );
1507
                                        //return false;
1508
                                } else {
1509
                                        PluginServices.getMDIManager().closeSingletonView(element);
1510
                                        p.delView(p.getViews().indexOf(element));
1511
                                }
1512
                        }
1513
                }
1514
                return true;
1515
        }
1516
1517 6635 jmvivo
}