Statistics
| Revision:

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

History | View | Annotate | Download (43.3 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.UnsupportedFlavorException;
8
import java.io.IOException;
9
import java.io.StringReader;
10
import java.io.StringWriter;
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.Comparator;
14
import java.util.Enumeration;
15
import java.util.Hashtable;
16
import java.util.Iterator;
17
import java.util.Map;
18

    
19
import javax.swing.JOptionPane;
20

    
21
import org.exolab.castor.xml.MarshalException;
22
import org.exolab.castor.xml.Marshaller;
23
import org.exolab.castor.xml.ValidationException;
24

    
25
import com.hardcode.gdbms.engine.data.DataSourceFactory;
26
import com.hardcode.gdbms.engine.data.SourceInfo;
27
import com.iver.andami.PluginServices;
28
import com.iver.andami.plugins.Extension;
29
import com.iver.cit.gvsig.fmap.DriverException;
30
import com.iver.cit.gvsig.fmap.layers.CancelationException;
31
import com.iver.cit.gvsig.fmap.layers.FLayer;
32
import com.iver.cit.gvsig.fmap.layers.FLayers;
33
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
34
import com.iver.cit.gvsig.fmap.layers.XMLException;
35
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
36
import com.iver.cit.gvsig.project.Project;
37
import com.iver.cit.gvsig.project.documents.ProjectDocument;
38
import com.iver.cit.gvsig.project.documents.contextMenu.AbstractDocumentContextMenuAction;
39
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
40
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
41
import com.iver.cit.gvsig.project.documents.layout.ProjectMap;
42
import com.iver.cit.gvsig.project.documents.layout.ProjectMapFactory;
43
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
44
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
45
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
46
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
47
import com.iver.cit.gvsig.project.documents.view.ProjectView;
48
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
49
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
50
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
51
import com.iver.utiles.XMLEntity;
52
import com.iver.utiles.extensionPoints.IExtensionBuilder;
53
import com.iver.utiles.xmlEntity.generate.XmlTag;
54

    
55
public class CopyPasteExtension extends Extension {
56

    
57
        public void initialize() {
58
                /*
59
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
60

61

62

63
                // TOC
64
                MyTocMenuEntry copy = new CopyTocMenuEntry();
65
                MyTocMenuEntry cut = new CutTocMenuEntry();
66
                MyTocMenuEntry paste = new PasteTocMenuEntry();
67
                Utiles utiles = new Utiles();
68
                copy.setUtiles(utiles);
69
                cut.setUtiles(utiles);
70
                paste.setUtiles(utiles);
71

72
                extensionPoints.add("View_TocActions","Copy","Copy selectes layers to system clipboard",copy);
73
                extensionPoints.add("View_TocActions","Cut","Cut selectes layers to system clipboard", cut);
74
                extensionPoints.add("View_TocActions","Paste","Paste layers from system clipboard",paste);
75

76

77
            //FPopupMenu.addEntry(copy);
78
            //FPopupMenu.addEntry(cut);
79
            //FPopupMenu.addEntry(paste);
80

81

82
            // ProjectWindow
83
                CopyProjectElement copyProjectElement = new CopyProjectElement();
84
                CutProjectElement cutProjectElement = new CutProjectElement();
85
                PasteProjectElement pasteProjectElementView = new PasteProjectElement();
86
                PasteProjectElement pasteProjectElementTable = new PasteProjectElement();
87
                PasteProjectElement pasteProjectElementMap = new PasteProjectElement();
88

89
                copyProjectElement.setUtiles(utiles);
90
                cutProjectElement.setUtiles(utiles);
91
                pasteProjectElementView.setUtiles(utiles);
92
                pasteProjectElementTable.setUtiles(utiles);
93
                pasteProjectElementMap.setUtiles(utiles);
94

95
                pasteProjectElementView.setType("views");
96
                pasteProjectElementTable.setType("tables");
97
                pasteProjectElementMap.setType("maps");
98

99

100
                extensionPoints.add("DocumentActions_View","Copy","Copy selectes documento to system clipboard",copyProjectElement);
101
                extensionPoints.add("DocumentActions_View","Cut","Cut selectes documento to system clipboard", cutProjectElement);
102
                extensionPoints.add("DocumentActions_View","Paste","Paste views from system clipboard",pasteProjectElementView);
103

104

105
                extensionPoints.add("DocumentActions_Table","Copy","Copy selectes documento to system clipboard",copyProjectElement);
106
                extensionPoints.add("DocumentActions_Table","Cut","Cut selectes documento to system clipboard", cutProjectElement);
107
                extensionPoints.add("DocumentActions_Table","Paste","Paste tables from system clipboard",pasteProjectElementTable);
108

109
                extensionPoints.add("DocumentActions_Map","Copy","Copy selectes documento to system clipboard",copyProjectElement);
110
                extensionPoints.add("DocumentActions_Map","Cut","Cut selectes documento to system clipboard", cutProjectElement);
111
                extensionPoints.add("DocumentActions_Map","Paste","Paste maps from system clipboard",pasteProjectElementMap);
112

113
                */
114
        }
115

    
116
        public void execute(String actionCommand) {
117
                // TODO Auto-generated method stub
118

    
119
        }
120

    
121
        public boolean isEnabled() {
122
                return false;
123
        }
124

    
125
        public boolean isVisible() {
126
                return false;
127
        }
128

    
129

    
130
}
131

    
132
abstract class MyDocumentAction extends AbstractDocumentContextMenuAction implements IExtensionBuilder {
133
        protected Utiles utiles;
134

    
135
        public void setUtiles(Utiles utiles) {
136
                this.utiles = utiles;
137
        }
138

    
139
        public String getGroup() {
140
                return "ClipboardActions";
141
        }
142

    
143

    
144
        public Object create() {
145
                return this;
146
        }
147

    
148
        public Object create(Object[] args) {
149
                // TODO Auto-generated method stub
150
                return this;
151
        }
152

    
153
        public Object create(Map args) {
154
                // TODO Auto-generated method stub
155
                return this;
156
        }
157
}
158

    
159

    
160
class CopyProjectElement extends MyDocumentAction{
161
        public String getDescription() {
162
                //FIXME: Falta claves
163
                //return PluginServices.getText(this,"tooltip_copiar_al_portapapeles");
164
                return null;
165
        }
166

    
167
        public int getOrder() {
168

    
169
                return 0;
170
        }
171

    
172
        public boolean isVisible(ProjectDocument item, ProjectDocument[] selectedItems) {
173
                return true;
174
        }
175

    
176
        public boolean isEnabled(ProjectDocument item, ProjectDocument[] selectedItems) {
177
                return selectedItems.length > 0;
178
        }
179

    
180

    
181
        public void execute(ProjectDocument item, ProjectDocument[] selectedItems) {
182
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
183
                if (xml == null) {
184
                        JOptionPane.showMessageDialog(
185
                                        (Component)PluginServices.getMainFrame(),
186
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
187
                                        PluginServices.getText(this,"pegar"),//titulo
188
                                        JOptionPane.ERROR_MESSAGE
189
                                        );
190
                        return;
191
                }
192

    
193
                String data = this.utiles.marshallXMLEntity(xml);
194
                if (data == null) {
195
                        JOptionPane.showMessageDialog(
196
                                        (Component)PluginServices.getMainFrame(),
197
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
198
                                        PluginServices.getText(this,"pegar"),//titulo
199
                                        JOptionPane.ERROR_MESSAGE
200
                                        );
201
                        return;
202
                }
203
                this.utiles.putInClipboard(data);
204
        }
205

    
206
        public String getText() {
207
                return PluginServices.getText(this, "copiar");
208
        }
209

    
210
}
211

    
212
class CutProjectElement extends MyDocumentAction {
213
        public String getDescription() {
214
                //FIXME: Falta claves
215
                //return PluginServices.getText(this,"tooltip_cortar_al_portapapeles");
216
                return null;
217
        }
218

    
219
        public int getOrder() {
220
                return 1;
221
        }
222

    
223
        public boolean isVisible(ProjectDocument item, ProjectDocument[] selectedItems) {
224
                return true;
225
        }
226

    
227
        public boolean isEnabled(ProjectDocument item, ProjectDocument[] selectedItems) {
228
                return selectedItems.length > 0;
229
        }
230

    
231

    
232
        public void execute(ProjectDocument item, ProjectDocument[] selectedItems) {
233
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
234
                if (xml == null) {
235
                        JOptionPane.showMessageDialog(
236
                                        (Component)PluginServices.getMainFrame(),
237
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
238
                                        PluginServices.getText(this,"cortar"),//titulo
239
                                        JOptionPane.ERROR_MESSAGE
240
                                        );
241
                        return;
242
                }
243

    
244
                String data = this.utiles.marshallXMLEntity(xml);
245
                if (data == null) {
246
                        JOptionPane.showMessageDialog(
247
                                        (Component)PluginServices.getMainFrame(),
248
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
249
                                        PluginServices.getText(this,"cortar"),//titulo
250
                                        JOptionPane.ERROR_MESSAGE
251
                                        );
252
                        return;
253
                }
254
                this.utiles.putInClipboard(data);
255

    
256

    
257
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_el_documento"));
258
            if (option!=JOptionPane.OK_OPTION) {
259
                    return;
260
            }
261

    
262

    
263
                this.utiles.removeDocuments(selectedItems);
264

    
265
        }
266

    
267
        public String getText() {
268
                return PluginServices.getText(this, "cortar");
269
        }
270

    
271
}
272

    
273
class PasteProjectElement extends MyDocumentAction {
274
        private String type;
275

    
276
        public String getDescription() {
277
                //FIXME: Falta claves
278
                //return PluginServices.getText(this,"tooltip_pegar_desde_el_portapapeles");
279
                return null;
280
        }
281

    
282
        public int getOrder() {
283
                return 2;
284
        }
285

    
286
        public void setType(String type) {
287
                this.type = type;
288
        }
289

    
290
        public String getType(String type) {
291
                return this.type;
292
        }
293

    
294
        public boolean isVisible(ProjectDocument item, ProjectDocument[] selectedItems) {
295
                return true;
296
        }
297

    
298
        public boolean isEnabled(ProjectDocument item, ProjectDocument[] selectedItems) {
299
                String sourceString = this.utiles.getFromClipboard();
300
                if (sourceString == null) return false;
301

    
302
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
303
                if (xml == null) return false;
304

    
305
                if (!this.utiles.checkXMLRootNode(xml)) return false;
306

    
307
                if (this.utiles.getXMLEntityChildOfType(xml,this.type) == null) return false;
308
                return true;
309
        }
310

    
311

    
312
        public void execute(ProjectDocument item, ProjectDocument[] selectedItems) {
313
                String sourceString = this.utiles.getFromClipboard();
314
                if (sourceString == null) return;
315

    
316
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
317
                if (xml == null) return;
318

    
319
                if (!this.utiles.checkXMLRootNode(xml)) return;
320

    
321
                if (this.type.equals("views")) {
322
                        this.utiles.loadViewsFromXML(xml);
323
                } else if (this.type.equals("tables")) {
324
                        this.utiles.loadTablesFromXML(xml);
325
                } else if (this.type.equals("maps")) {
326
                        this.utiles.loadMapsFromXML(xml);
327
                } else {
328
                        //TODO que hacer aqui??
329
                        return;
330
                }
331

    
332
        }
333

    
334
        public String getText() {
335
                return PluginServices.getText(this, "pegar");
336
        }
337

    
338
}
339

    
340

    
341

    
342
abstract class  MyTocMenuEntry extends AbstractTocContextMenuAction {
343
        protected Utiles utiles;
344

    
345
        public void setUtiles(Utiles utiles) {
346
                this.utiles = utiles;
347
        }
348

    
349
        public String getGroup() {
350
                return "copyPasteLayer";
351
        }
352

    
353
        public int getGroupOrder() {
354
                return 60;
355
        }
356

    
357
}
358

    
359
class CopyTocMenuEntry extends MyTocMenuEntry{
360
        public int getOrder() {
361
                return 0;
362
        }
363

    
364
        public String getText() {
365
                return PluginServices.getText(this, "copiar");
366
        }
367

    
368
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
369
                return selectedItems.length >= 1 && isTocItemBranch(item);
370
        }
371

    
372

    
373
        public void execute(ITocItem item, FLayer[] selectedItems) {
374
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
375
                if (xml == null) {
376
                        JOptionPane.showMessageDialog(
377
                                        (Component)PluginServices.getMainFrame(),
378
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
379
                                        PluginServices.getText(this,"copiar"),//titulo
380
                                        JOptionPane.ERROR_MESSAGE
381
                                        );
382
                        return;
383
                }
384

    
385
                String data = this.utiles.marshallXMLEntity(xml);
386
                if (data == null) {
387
                        JOptionPane.showMessageDialog(
388
                                        (Component)PluginServices.getMainFrame(),
389
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
390
                                        PluginServices.getText(this,"copiar"),//titulo
391
                                        JOptionPane.ERROR_MESSAGE
392
                                        );
393
                        return;
394
                }
395

    
396
                this.utiles.putInClipboard(data);
397

    
398
        }
399

    
400

    
401
}
402

    
403
class CutTocMenuEntry extends MyTocMenuEntry{
404
        public int getOrder() {
405
                return 1;
406
        }
407

    
408
        public String getText() {
409
                return PluginServices.getText(this, "cortar");
410
        }
411

    
412
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
413
                return selectedItems.length >= 1 && isTocItemBranch(item);
414
        }
415

    
416

    
417
        public void execute(ITocItem item, FLayer[] selectedItems) {
418
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
419
                if (xml == null) {
420
                        JOptionPane.showMessageDialog(
421
                                        (Component)PluginServices.getMainFrame(),
422
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
423
                                        PluginServices.getText(this,"cortar"),//titulo
424
                                        JOptionPane.ERROR_MESSAGE
425
                                        );
426
                        return;
427
                }
428

    
429
                String data = this.utiles.marshallXMLEntity(xml);
430
                if (data == null) {
431
                        JOptionPane.showMessageDialog(
432
                                        (Component)PluginServices.getMainFrame(),
433
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
434
                                        PluginServices.getText(this,"cortar"),//titulo
435
                                        JOptionPane.ERROR_MESSAGE
436
                                        );
437
                        return;
438
                }
439

    
440

    
441
                this.utiles.putInClipboard(data);
442

    
443

    
444
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
445
            if (option!=JOptionPane.OK_OPTION) {
446
                    return;
447
            }
448
                getMapContext().beginAtomicEvent();
449

    
450

    
451
                boolean isOK =this.utiles.removeLayers(selectedItems);
452

    
453
                getMapContext().endAtomicEvent();
454

    
455
                if (isOK) {
456
                        getMapContext().invalidate();
457
                        if (getMapContext().getLayers().getLayersCount()==0) {
458
                                PluginServices.getMainFrame().enableControls();
459
                        }
460
                }
461

    
462
        }
463
}
464

    
465

    
466
class PasteTocMenuEntry extends MyTocMenuEntry{
467
        private XMLEntity xml=null;
468

    
469
        public int getOrder() {
470
                return 2;
471
        }
472

    
473
        public String getText() {
474
                return PluginServices.getText(this, "pegar");
475
        }
476

    
477
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
478
                if (isTocItemBranch(item)) {
479
                        FLayer lyr = getNodeLayer(item);
480
                        if (lyr instanceof FLayers) {
481
                                this.xml = this.getCheckedXMLFromClipboard();
482
                                return true;
483
                        }
484

    
485
                } else if (!isTocItemLeaf(item)) {
486
                        if (getNodeLayer(item) == null) {
487
                                this.xml = this.getCheckedXMLFromClipboard();
488
                                return this.xml != null;
489
                        }
490
                }
491
                return false;
492
        }
493

    
494
        private XMLEntity getCheckedXMLFromClipboard() {
495
                String sourceString = this.utiles.getFromClipboard();
496
                if (sourceString == null) return null;
497

    
498
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
499
                if (xml == null) return null;
500

    
501
                if (!this.utiles.checkXMLRootNode(xml)) return null;
502

    
503
                if (this.utiles.getXMLEntityChildOfType(xml,"layers") == null) return null;
504

    
505
                return  xml;
506
        }
507

    
508
        public void execute(ITocItem item, FLayer[] selectedItems) {
509
                FLayers root;
510

    
511
                if (this.xml == null) return;
512

    
513
                if (isTocItemBranch(item)) {
514
                        root = (FLayers)getNodeLayer(item);
515
                } else if (getNodeLayer(item) == null){
516
                        root = getMapContext().getLayers();
517
                } else {
518
                        return;
519
                }
520
                getMapContext().beginAtomicEvent();
521

    
522
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
523

    
524
                getMapContext().endAtomicEvent();
525

    
526
                if (isOK) getMapContext().invalidate();
527
        }
528

    
529
}
530

    
531

    
532
class Utiles {
533

    
534
        /*
535
         *
536
======================================
537
 Comportamiento del Pegar documentos:
538
======================================
539

540
?Pegar vista.
541
        Si ya existe una vista en el proyecto con el mismo nombre.
542
                1. Abortar
543
                2. Pedir nuevo nombre.
544
                        Que hacemos con las tablas asociadas.
545
                                No se pegan
546
        Si alguna de las tablas a pegar de las que van en
547
        el portapapeles ya existen en el proyecto.
548
                1. abortamos
549
                2. Informamos al usuario y no se pegan las tablas.
550

551
Pegar tabla.
552
        Si alguna de las tablas existe.
553
                Se pega igualmente (apareceran tablas duplicadas)
554

555
Pegar mapa.
556
        Si el mapa ya existe en el proyecto.
557
                1. Abortar
558
                2. renombrar el mapa
559
        Si alguna vista ya existe en el proyecto.
560
                1. Abortar
561
                2. Usar la vista que ya existe en el proyecto y no
562
                   pegar la nueva vista.
563
        Si alguna de las tablas a pegar de las que van en
564
                el portapapeles ya existen en el proyecto.
565
                        1. abortamos
566
                        2. Informamos al usuario y no se pegan las tablas.
567

568

569
         */
570

    
571

    
572
        /*
573
         *
574
         *
575
         *
576
         * Funciones Publicas para generar XML (copiar)
577
         *
578
         *
579
        */
580

    
581
        /**
582
         * Genera un XMLEntity con la informacion necesaria
583
         * para copiar los elementos de selectedItems en
584
         * otro proyecto
585
         */
586
        public XMLEntity generateXMLCopyDocuments(ProjectDocument[] selectedItems) {
587
                if (selectedItems.length == 0) return null;
588

    
589
                if (selectedItems[0] instanceof ProjectView) {
590
                        ProjectView[] views = new ProjectView[selectedItems.length];
591
                        System.arraycopy(selectedItems,0,views,0,selectedItems.length);
592
                        return this.generateXMLCopyViews(views);
593
                } else if (selectedItems[0] instanceof ProjectMap) {
594
                        ProjectMap[] maps = new ProjectMap[selectedItems.length];
595
                        System.arraycopy(selectedItems,0,maps,0,selectedItems.length);
596
                        return this.generateXMLCopyMaps(maps);
597
                } else if (selectedItems[0] instanceof ProjectTable) {
598
                        ProjectTable[] tables = new ProjectTable[selectedItems.length];
599
                        System.arraycopy(selectedItems,0,tables,0,selectedItems.length);
600
                        return this.generateXMLCopyTables(tables);
601
                } else {
602
                        //FIXME:????
603
                        return null;
604
                }
605
        }
606

    
607
        public XMLEntity generateXMLCopyViews(ProjectView[] selectedItems) {
608
                XMLEntity xml = this.newRootNode();
609

    
610
                XMLEntity xmlTables = this.newTablesNode();
611
                XMLEntity xmlDataSources = this.newDataSourcesNode();
612
                XMLEntity xmlViews = this.newViewsNode();
613

    
614
                for (int i=0;i < selectedItems.length; i++) {
615
                        if (!this.addToXMLView(selectedItems[i],xmlViews,xmlTables,xmlDataSources)) return null;
616

    
617
                }
618

    
619

    
620
                if (xmlDataSources.getChildrenCount() > 0) {
621
                        xml.addChild(xmlDataSources);
622
                }
623
                if (xmlViews.getChildrenCount() > 0) {
624
                        xml.addChild(xmlViews);
625
                }
626
                if (xmlTables.getChildrenCount() > 0) {
627
                        xml.addChild(xmlTables);
628
                }
629

    
630
                return xml;
631

    
632
        }
633

    
634

    
635
        public XMLEntity generateXMLCopyMaps(ProjectMap[] selectedItems) {
636
                XMLEntity xml = this.newRootNode();
637

    
638
                XMLEntity xmlTables = this.newTablesNode();
639
                XMLEntity xmlDataSources = this.newDataSourcesNode();
640
                XMLEntity xmlViews = this.newViewsNode();
641
                XMLEntity xmlMaps = this.newMapsNode();
642

    
643
                for (int i=0;i < selectedItems.length; i++) {
644
                        if (!this.addToXMLMap(selectedItems[i],xmlMaps,xmlViews,xmlTables,xmlDataSources)) return null;
645

    
646
                }
647

    
648

    
649
                if (xmlDataSources.getChildrenCount() > 0) {
650
                        xml.addChild(xmlDataSources);
651
                }
652
                if (xmlViews.getChildrenCount() > 0) {
653
                        xml.addChild(xmlViews);
654
                }
655
                if (xmlTables.getChildrenCount() > 0) {
656
                        xml.addChild(xmlTables);
657
                }
658
                if (xmlMaps.getChildrenCount() > 0) {
659
                        xml.addChild(xmlMaps);
660
                }
661

    
662

    
663
                return xml;
664
        }
665

    
666
        public XMLEntity generateXMLCopyTables(ProjectTable[] selectedItems) {
667
                XMLEntity xml = this.newRootNode();
668

    
669
                XMLEntity xmlTables = this.newTablesNode();
670
                XMLEntity xmlDataSources = this.newDataSourcesNode();
671

    
672
                for (int i=0;i < selectedItems.length; i++) {
673
                        if (!this.addToXMLTable(selectedItems[i],xmlTables,xmlDataSources,null)) return null;
674
                }
675

    
676

    
677
                if (xmlDataSources.getChildrenCount() > 0) {
678
                        xml.addChild(xmlDataSources);
679
                }
680
                if (xmlTables.getChildrenCount() > 0) {
681
                        xml.addChild(xmlTables);
682
                }
683

    
684
                return xml;
685
        }
686

    
687

    
688
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
689

    
690
                XMLEntity xml = this.newRootNode();
691
                XMLEntity xmlLayers = this.newLayersNode();
692
                XMLEntity xmlTables = this.newTablesNode();
693
                XMLEntity xmlDataSources = this.newDataSourcesNode();
694

    
695
                for (int i=0;i < actives.length; i++) {
696
                        if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) return null;
697

    
698
                }
699

    
700
                if (xmlDataSources.getChildrenCount() > 0) {
701
                        xml.addChild(xmlDataSources);
702
                }
703
                if (xmlLayers.getChildrenCount() > 0) {
704
                        xml.addChild(xmlLayers);
705
                }
706
                if (xmlTables.getChildrenCount() > 0) {
707
                        xml.addChild(xmlTables);
708
                }
709

    
710
                return xml;
711

    
712
        }
713

    
714

    
715

    
716
        /*
717
         *
718
         *
719
         *
720
         * Funciones Publicas de carga de un XML (pegar)
721
         *
722
         *
723
         *
724
        */
725

    
726
        public boolean loadLayersFromXML(XMLEntity xml, FLayers root) {
727
                XMLEntity xmlLayers = this.getXMLEntityChildOfType(xml,"layers");
728
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
729
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
730

    
731
                if (xmlLayers == null ) return false;
732

    
733
                // Se pegan las tablas igualmente
734
                /*
735
                Project project = this.getProject();
736

737
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
738
                */
739

    
740

    
741
                if (xmlDataSources != null)  {
742
                        if (!this.registerDataSources(xmlDataSources)) return false;
743
                }
744

    
745
                if (!this.addLayers(xmlLayers,root)) return false;
746

    
747
                if (xmlTables != null)  {
748
                        if (!this.addTables(xmlTables)) return false;
749
                }
750

    
751
                return true;
752

    
753
        }
754

    
755

    
756
        public boolean loadViewsFromXML(XMLEntity xml) {
757
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
758
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
759
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
760

    
761
                if (xmlViews == null ) return false;
762

    
763
                Project project = this.getProject();
764

    
765
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getDocumentsByType(ProjectViewFactory.registerName));
766

    
767
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getDocumentsByType(ProjectTableFactory.registerName));
768

    
769
                if (viewsConflits != null && viewsConflits.size() > 0) {
770
                        int option = JOptionPane.showConfirmDialog(
771
                                        (Component)PluginServices.getMainFrame(),
772
                                        "<html>"+
773
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
774
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
775
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
776
                                                PluginServices.getText(this,"desea_continuar") +
777
                                        "</html>",
778
                                        PluginServices.getText(this,"pegar_vistas"),
779
                                        JOptionPane.YES_NO_OPTION
780
                                        );
781
                        if (option != JOptionPane.YES_OPTION) {
782
                                return false;
783
                        }
784
                        Enumeration en = viewsConflits.elements();
785
                        while (en.hasMoreElements()) {
786
                                XMLEntity view = (XMLEntity)en.nextElement();
787
                                String newName = JOptionPane.showInputDialog(
788
                                                (Component)PluginServices.getMainFrame(),
789
                                                "<html>"+
790
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
791
                                                "</html>", //Mensaje
792
                                                view.getStringProperty("name") //Valor por defecto
793
                                                );
794
                                if (newName == null) {
795
                                        JOptionPane.showMessageDialog(
796
                                                        (Component)PluginServices.getMainFrame(),
797
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
798
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
799
                                                        JOptionPane.ERROR_MESSAGE
800
                                                        );
801
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
802
                                        JOptionPane.showMessageDialog(
803
                                                        (Component)PluginServices.getMainFrame(),
804
                                                        "<html>"+
805
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
806
                                                                PluginServices.getText(this,"nombre_no_valido")+
807
                                                        "</html>",//Mensaje
808
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
809
                                                        JOptionPane.ERROR_MESSAGE
810
                                                        );
811
                                        return false;
812
                                }
813
                                view.setName(newName);
814
                        }
815
                        if (xmlTables != null) xmlTables.removeAllChildren();
816
                        tablesConflits = null;
817
                }
818

    
819
                if (tablesConflits != null && tablesConflits.size() > 0) {
820
                        int option = JOptionPane.showConfirmDialog(
821
                                        (Component)PluginServices.getMainFrame(),
822
                                        "<html>" +
823
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
824
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
825
                                                PluginServices.getText(this,"desea_continuar") +
826
                                        "</html>", //Mensaje
827
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
828
                                        JOptionPane.YES_NO_OPTION
829
                                        );
830
                        if (option != JOptionPane.YES_OPTION) {
831
                                return false;
832
                        }
833
                        xmlTables.removeAllChildren();
834
                }
835

    
836

    
837
                if (xmlDataSources != null)  {
838
                        if (!this.registerDataSources(xmlDataSources)) return false;
839
                }
840

    
841
                if (!this.addViews(xmlViews)) return false;
842

    
843
                if (xmlTables != null)  {
844
                        if (!this.addTables(xmlTables)) return false;
845
                }
846

    
847
                return true;
848
        }
849

    
850
        public boolean loadTablesFromXML(XMLEntity xml) {
851
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
852
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
853

    
854

    
855
                if (xmlTables == null ) return false;
856

    
857
                /*
858
                Project project = this.getProject();
859

860
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
861
                */
862

    
863
                if (xmlDataSources != null)  {
864
                        if (!this.registerDataSources(xmlDataSources)) return false;
865
                }
866

    
867

    
868

    
869
                return this.addTables(xmlTables);
870
        }
871

    
872
        public boolean loadMapsFromXML(XMLEntity xml) {
873
                XMLEntity xmlMaps = this.getXMLEntityChildOfType(xml,"Maps");
874
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
875
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
876
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
877

    
878
                if (xmlMaps == null ) return false;
879

    
880
                Project project = this.getProject();
881

    
882
                Hashtable mapsConflits = this.getConflicts(xmlMaps,project.getDocumentsByType(ProjectMapFactory.registerName));
883

    
884
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getDocumentsByType(ProjectViewFactory.registerName));
885

    
886
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getDocumentsByType(ProjectTableFactory.registerName));
887

    
888

    
889
                if (mapsConflits != null && mapsConflits.size() > 0) {
890
                        int option = JOptionPane.showConfirmDialog(
891
                                        (Component)PluginServices.getMainFrame(),
892
                                        "<html>"+
893
                                                PluginServices.getText(this,"conflicto_de_nombres_de_mapas_al_pegar") + "<br>" +
894
                                                PluginServices.getText(this,"debera_introducir_nombres_para_los_mapas_a_pegar") + "<br>" +
895
                                        "</html>", //Mensaje
896
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
897
                                        JOptionPane.YES_NO_OPTION
898
                                        );
899
                        if (option != JOptionPane.YES_OPTION) {
900
                                return false;
901
                        }
902
                        Enumeration en = mapsConflits.elements();
903
                        while (en.hasMoreElements()) {
904
                                XMLEntity map = (XMLEntity)en.nextElement();
905
                                String newName = JOptionPane.showInputDialog(
906
                                                (Component)PluginServices.getMainFrame(),
907
                                                "<html>"+
908
                                                        PluginServices.getText(this,"nuevo_nombre_para_el_mapa") +" "+  map.getStringProperty("name") + ":" +
909
                                            "</html>", //Mensaje
910
                                                map.getStringProperty("name") //Valor por defecto
911
                                                );
912
                                if (newName == null) {
913
                                        JOptionPane.showMessageDialog(
914
                                                        (Component)PluginServices.getMainFrame(),
915
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
916
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
917
                                                        JOptionPane.ERROR_MESSAGE
918
                                                        );
919
                                } else if (newName.equalsIgnoreCase(map.getStringProperty("name")) ) {
920
                                        JOptionPane.showMessageDialog(
921
                                                        (Component)PluginServices.getMainFrame(),
922
                                                        "<html>"+
923
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
924
                                                                PluginServices.getText(this,"nombre_no_valido")+
925
                                                        "</html>",//Mensaje
926
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
927
                                                        JOptionPane.ERROR_MESSAGE
928
                                                        );
929
                                        return false;
930
                                }
931
                                map.setName(newName);
932
                        }
933
                }
934

    
935
                if (viewsConflits != null && viewsConflits.size() > 0) {
936
                        int option = JOptionPane.showConfirmDialog(
937
                                        (Component)PluginServices.getMainFrame(),
938
                                        "<html>"+
939
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
940
                                                PluginServices.getText(this,"no_se_pegaran_las_vistas_del_conflicto") + "<br>" +
941
                                                PluginServices.getText(this,"desea_continuar") +
942
                                        "</html>",
943
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
944
                                        JOptionPane.YES_NO_OPTION
945
                                        );
946
                        if (option != JOptionPane.YES_OPTION) {
947
                                return false;
948
                        }
949
                        // Eliminamos las vistas del xml que no vamos a importar
950

    
951
                        // Esto me devuelve los indices en orden inverso
952
                        int[] indexes = this.getIndexOfConflict(viewsConflits);
953
                        for (int i=0;i < indexes.length;i++) {
954
                                xmlViews.removeChild(indexes[i]);
955
                        }
956
                        viewsConflits = null;
957

    
958
                }
959

    
960

    
961
                if (tablesConflits != null && tablesConflits.size() > 0) {
962
                        int option = JOptionPane.showConfirmDialog(
963
                                        (Component)PluginServices.getMainFrame(),
964
                                        "<html>" +
965
                                                PluginServices.getText(this,"conflito_de_nombres_de_tablas_al_pegar") + "<br>" +
966
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
967
                                                PluginServices.getText(this,"desea_continuar") +
968
                                        "</html>", //Mensaje
969
                                        PluginServices.getText(this,"pegar_mapas"),
970
                                        JOptionPane.YES_NO_OPTION
971
                                        );
972
                        if (option != JOptionPane.YES_OPTION) {
973
                                return false;
974
                        }
975
                        xmlTables.removeAllChildren();
976
                }
977

    
978

    
979
                if (xmlDataSources != null)  {
980
                        if (!this.registerDataSources(xmlDataSources)) return false;
981
                }
982

    
983
                if (xmlViews != null)  {
984
                        if (!this.addViews(xmlViews)) return false;
985
                }
986

    
987
                if (xmlTables != null)  {
988
                        if (!this.addTables(xmlTables)) return false;
989
                }
990

    
991
                return this.addMaps(xmlMaps);
992

    
993
        }
994

    
995

    
996

    
997

    
998

    
999

    
1000

    
1001

    
1002

    
1003

    
1004

    
1005
        /**
1006
         * Devuelve las claves de conflits ordenados
1007
         * en orden inverso. Las claves se esperan que
1008
         * sean instancias de Integer
1009
         */
1010
        private int[] getIndexOfConflict(Hashtable conflits) {
1011
                Object[] tmpArray = conflits.keySet().toArray();
1012
                Arrays.sort(tmpArray,new Comparator() {
1013
                        public int compare(Object o1, Object o2) {
1014
                                return ((Integer)o2).intValue() - ((Integer)o1).intValue();
1015
                        }
1016
                }
1017
                );
1018
                int[] indexes = new int[] {tmpArray.length};
1019
                for (int i = 0;i< tmpArray.length;i++) {
1020
                        indexes[i] = ((Integer)tmpArray[i]).intValue();
1021
                }
1022
                return indexes;
1023

    
1024

    
1025
        }
1026

    
1027

    
1028
        private boolean addToXMLMapDependencies(ProjectMap map, XMLEntity xmlViews,XMLEntity xmlTables, XMLEntity xmlDataSources) {
1029
                IFFrame[] components = map.getModel().getLayoutContext().getFFrames();
1030
                for (int i=0; i < components.length; i++) {
1031
                        if (components[i] instanceof FFrameView) {
1032
                                ProjectView view = ((FFrameView)components[i]).getView();
1033
                                if (findChildInXML(xmlViews,"name",view.getName())==null) {
1034
                                        if (!this.addToXMLView(view,xmlViews,xmlTables,xmlDataSources)) return false;
1035
                                }
1036
                        }
1037
                }
1038

    
1039
                return true;
1040
        }
1041

    
1042
        private boolean addToXMLMap(ProjectMap map,XMLEntity xmlMaps,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1043
                try {
1044
                        xmlMaps.addChild(map.getXMLEntity());
1045

    
1046
                        return this.addToXMLMapDependencies(map,xmlViews,xmlTables,xmlDataSources);
1047

    
1048
                } catch (SaveException e) {
1049
                        // TODO Auto-generated catch block
1050
                        e.printStackTrace();
1051
                        return false;
1052
                }
1053
        }
1054

    
1055
        private boolean addToXMLView(ProjectView view,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1056
                try {
1057
                        xmlViews.addChild(view.getXMLEntity());
1058

    
1059
                        if (!this.addToXMLLayerDependencies(view.getMapContext().getLayers(),xmlTables,xmlDataSources)) return false;
1060

    
1061
                        if (view.getMapOverViewContext() != null) {
1062
                                return this.addToXMLLayerDependencies(view.getMapOverViewContext().getLayers(),xmlTables,xmlDataSources);
1063
                        } else {
1064
                                return true;
1065
                        }
1066

    
1067

    
1068

    
1069
                } catch (SaveException e) {
1070
                        // TODO Auto-generated catch block
1071
                        e.printStackTrace();
1072
                        return false;
1073
                }
1074
        }
1075

    
1076

    
1077

    
1078
        public boolean checkXMLRootNode(XMLEntity xml) {
1079
                if (!xml.contains("applicationName")) return false;
1080
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) return false;
1081

    
1082
                if (!xml.contains("version")) return false;
1083
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false;
1084

    
1085
                return true;
1086
        }
1087

    
1088
        private void fillXMLRootNode(XMLEntity xml) {
1089
                xml.putProperty("applicationName","gvSIG");
1090
                xml.putProperty("version",Version.format());
1091
        }
1092

    
1093
        public XMLEntity getXMLEntityChildOfType(XMLEntity xml,String type) {
1094
                int childCount = xml.getChildrenCount();
1095
                XMLEntity child;
1096
                for (int i=0; i < childCount; i++  ) {
1097
                        child = xml.getChild(i);
1098
                        if (child.contains("type")) {
1099
                                if (child.getStringProperty("type").equalsIgnoreCase(type)) {
1100
                                        return child;
1101
                                }
1102
                        }
1103
                }
1104
                return null;
1105

    
1106
        }
1107

    
1108
        private Hashtable getConflicts(XMLEntity xml,ArrayList elements) {
1109
                if (xml == null || xml.getChildrenCount() < 1) return null;
1110
                Hashtable conflits = new Hashtable();
1111
                for (int iXML=0;iXML < xml.getChildrenCount();iXML++) {
1112
                        XMLEntity child = xml.getChild(iXML);
1113
                        Iterator iter = elements.iterator();
1114
                        while (iter.hasNext()) {
1115
                                ProjectDocument element = (ProjectDocument)iter.next();
1116
                                if (element.getName().equalsIgnoreCase(child.getStringProperty("name"))) {
1117
                                        conflits.put(new Integer(iXML),child);
1118
                                        break;
1119
                                }
1120

    
1121
                        }
1122
                }
1123
                return conflits;
1124
        }
1125

    
1126

    
1127

    
1128
        private boolean registerDataSources(XMLEntity xmlDataSources) {
1129
                try {
1130
                        int numDataSources = xmlDataSources.getChildrenCount();
1131

    
1132
                        if (numDataSources == 0) return true;
1133
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
1134

    
1135
                        for (int i = 0; i < numDataSources; i++) {
1136
                                XMLEntity child = xmlDataSources.getChild(i);
1137
                                String name = child.getStringProperty("gdbmsname");
1138

    
1139
                                if (dsFactory.getDriverInfo(name) == null) {
1140
                                        if (child.getStringProperty("type").equals("otherDriverFile")) {
1141
                                                LayerFactory.getDataSourceFactory().addFileDataSource(
1142
                                                                child.getStringProperty("driverName"),
1143
                                                                name,
1144
                                                                child.getStringProperty("file")
1145
                                                );
1146

    
1147

    
1148
                                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
1149
                                                /*                                String layerName = child.getStringProperty("layerName");
1150
                                                 ProjectView vista = project.getViewByName(child.getStringProperty(
1151
                                                 "viewName"));
1152
                                                 FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
1153

1154
                                                 modelo = ((AlphanumericData) layer).getRecordset();
1155
                                                 associatedTable = (AlphanumericData) layer;
1156
                                                 */
1157
                                        } else if (child.getStringProperty("type").equals("db")) {
1158
                                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
1159
                                                                name,
1160
                                                                child.getStringProperty("host"),
1161
                                                                child.getIntProperty("port"),
1162
                                                                child.getStringProperty("user"),
1163
                                                                child.getStringProperty("password"),
1164
                                                                child.getStringProperty("dbName"),
1165
                                                                child.getStringProperty("tableName"),
1166
                                                                child.getStringProperty("driverInfo")
1167
                                                );
1168
                                        }
1169
                                }
1170
                        }
1171

    
1172
                        return true;
1173
                } catch (Exception e) {
1174
                        e.printStackTrace();
1175
                        return false;
1176
                }
1177
        }
1178

    
1179
        private boolean addTables(XMLEntity xmlTables) {
1180
                try {
1181
                        int numTables = xmlTables.getChildrenCount();
1182
                        if (numTables == 0) return true;
1183

    
1184
                        Project project = this.getProject();
1185

    
1186
                        for (int i = 0; i < numTables; i++) {
1187
                                try{
1188
                                        ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project);
1189
                                        project.addDocument(ptable);
1190
                                        /*
1191
                                        if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
1192
                                                PluginServices.getMDIManager().addView(ptable.getAndamiView());
1193
                                                PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
1194
                                        }
1195
                                        */
1196
                                }catch(OpenException e){
1197
                                        e.printStackTrace();
1198
                                        return false;
1199
                                }
1200
                        }
1201

    
1202
                        project.setLinkTable();
1203

    
1204
                        return true;
1205
                } catch (Exception e) {
1206
                        e.printStackTrace();
1207
                        return false;
1208
                }
1209
        }
1210

    
1211
        public XMLEntity findChildInXML(XMLEntity xml,String propName,String value) {
1212
                int num = xml.getChildrenCount();
1213
                XMLEntity child;
1214
                for (int i=0;i < num; i++) {
1215
                        child = xml.getChild(i);
1216
                        if (child.getStringProperty(propName).equals(value)) {
1217
                                return child;
1218
                        }
1219
                }
1220
                return null;
1221
        }
1222

    
1223
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
1224
                try {
1225
                        XMLEntity child;
1226
                        int numLayers = xmlLayers.getChildrenCount();
1227
                        for (int i = 0; i < numLayers; i++) {
1228
                                child = xmlLayers.getChild(i);
1229
                                if (!root.addLayerFromXMLEntity(child,null)) return false;
1230
                        }
1231
                        return true;
1232
                } catch (Exception e) {
1233
                        e.printStackTrace();
1234
                        return false;
1235
                }
1236

    
1237
        }
1238

    
1239

    
1240
        private boolean addViews(XMLEntity xmlViews) {
1241
                try {
1242
                        Project project = this.getProject();
1243
                        XMLEntity child;
1244
                        int numLayers = xmlViews.getChildrenCount();
1245
                        for (int i = 0; i < numLayers; i++) {
1246
                                child = xmlViews.getChild(i);
1247

    
1248
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(child, project);
1249
                                project.addDocument(pv);
1250

    
1251
                        }
1252
                        return true;
1253
                } catch (Exception e) {
1254
                        e.printStackTrace();
1255
                        return false;
1256
                }
1257

    
1258
        }
1259

    
1260
        private boolean addMaps(XMLEntity xmlMaps) {
1261
                try {
1262
                        Project project = this.getProject();
1263
                        XMLEntity child;
1264
                        int numLayers = xmlMaps.getChildrenCount();
1265
                        for (int i = 0; i < numLayers; i++) {
1266
                                child = xmlMaps.getChild(i);
1267

    
1268
                                ProjectMap pm = (ProjectMap) ProjectMap.createFromXML(child, project);
1269
                                project.addDocument(pm);
1270

    
1271
                        }
1272
                        return true;
1273
                } catch (Exception e) {
1274
                        e.printStackTrace();
1275
                        return false;
1276
                }
1277

    
1278
        }
1279

    
1280
        private Project getProject() {
1281
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1282
        }
1283

    
1284
        private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) {
1285
                if (project== null) {
1286
                        project = this.getProject();
1287
                }
1288
                    xmlDataSources.addChild(project.getSourceInfoXMLEntity(source));
1289

    
1290
                    return true;
1291
        }
1292

    
1293
        private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) {
1294
                if (project== null) {
1295
                        project = this.getProject();
1296
                }
1297
                if (findChildInXML(xmlTables,"name",pt.getName()) != null) return true;
1298
                XMLEntity xmlTable = null;
1299
                try {
1300
                        xmlTable = pt.getXMLEntity();
1301

    
1302
                        xmlTables.addChild(xmlTable);
1303

    
1304
                        if (pt.getAssociatedTable() != null) {
1305
                                this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
1306
                        }
1307

    
1308
                        if (pt.getLinkTable() != null) {
1309
                                if (findChildInXML(xmlTables,"name",pt.getLinkTable()) == null)  {
1310
                                        ProjectTable ptLink = project.getTable(pt.getLinkTable());
1311
                                        if (!this.addToXMLTable(ptLink,xmlTables,xmlDataSources,project)) return false;
1312
                                }
1313
                        }
1314
                } catch (SaveException e) {
1315
                        // TODO Auto-generated catch block
1316
                        e.printStackTrace();
1317
                        return false;
1318
                } catch (DriverException e) {
1319
                        // TODO Auto-generated catch block
1320
                        e.printStackTrace();
1321
                        return false;
1322
                }
1323

    
1324
                    return true;
1325
        }
1326

    
1327
        private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1328
                try {
1329
                        Project project = this.getProject();
1330

    
1331
                        if (lyr instanceof FLayers) {
1332
                                FLayers lyrs = (FLayers)lyr;
1333
                                int count = lyrs.getLayersCount();
1334
                                for (int i=0;i < count;i++) {
1335
                                        FLayer subLyr = lyrs.getLayer(i);
1336
                                        this.addToXMLLayerDependencies(subLyr,xmlTables,xmlDataSources);
1337
                                }
1338

    
1339
                    } else if (lyr instanceof AlphanumericData){
1340
                    if (!this.addToXMLDataSource(
1341
                                ((AlphanumericData)lyr).getRecordset().getSourceInfo(),
1342
                                xmlDataSources,
1343
                                project
1344

    
1345
                    )) return false;
1346

    
1347
                ProjectTable pt = project.getTable((AlphanumericData) lyr);
1348
                if (pt != null) {
1349
                        if (!this.addToXMLTable(pt,xmlTables,xmlDataSources,project)) return false;
1350
                }
1351

    
1352
            }
1353

    
1354
                } catch (DriverException e) {
1355
                        // TODO Auto-generated catch block
1356
                        e.printStackTrace();
1357
                        return false;
1358
                } catch (Exception e) {
1359
                        e.printStackTrace();
1360
                        return false;
1361

    
1362
                }
1363
                return true;
1364

    
1365
        }
1366

    
1367
        private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1368
                try {
1369
                        xmlLayers.addChild(lyr.getXMLEntity());
1370

    
1371
                        return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources);
1372

    
1373
                } catch (XMLException e) {
1374
                        e.printStackTrace();
1375
                        return false;
1376
                } catch (Exception e) {
1377
                        e.printStackTrace();
1378
                        return false;
1379
                }
1380
        }
1381

    
1382
        private XMLEntity newRootNode() {
1383
                XMLEntity xml = new XMLEntity();
1384
                fillXMLRootNode(xml);
1385
                return xml;
1386
        }
1387

    
1388
        private XMLEntity newLayersNode() {
1389
                XMLEntity xmlLayers = new XMLEntity();
1390
                xmlLayers.putProperty("type","layers");
1391
                return xmlLayers;
1392
        }
1393

    
1394
        private XMLEntity newDataSourcesNode() {
1395
                XMLEntity xmlDataSources = new XMLEntity();
1396
                xmlDataSources.putProperty("type","dataSources");
1397
                return xmlDataSources;
1398
        }
1399

    
1400
        private XMLEntity newTablesNode() {
1401
                XMLEntity xmlTables = new XMLEntity();
1402
                xmlTables.putProperty("type","tables");
1403
                return xmlTables;
1404
        }
1405

    
1406
        private XMLEntity newViewsNode() {
1407
                XMLEntity xmlTables = new XMLEntity();
1408
                xmlTables.putProperty("type","views");
1409
                return xmlTables;
1410
        }
1411

    
1412
        private XMLEntity newMapsNode() {
1413
                XMLEntity xmlTables = new XMLEntity();
1414
                xmlTables.putProperty("type","maps");
1415
                return xmlTables;
1416
        }
1417

    
1418

    
1419
        public void putInClipboard(String data) {
1420
                StringSelection ss = new StringSelection(data);
1421

    
1422
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
1423
        }
1424

    
1425
        public String marshallXMLEntity(XMLEntity xml) {
1426
                StringWriter buffer = new StringWriter();
1427

    
1428
                Marshaller m;
1429
                try {
1430
                        m = new Marshaller(buffer);
1431
                } catch (IOException e4) {
1432
                        // TODO Auto-generated catch block
1433
                        e4.printStackTrace();
1434
                        return null;
1435
                }
1436
                m.setEncoding("ISO-8859-1");
1437

    
1438
                try {
1439
                        m.marshal(xml.getXmlTag());
1440
                        //if (i < actives.length-1) buffer.write("\n##layer-separator##\n");
1441
                } catch (MarshalException e2) {
1442
                        // TODO Auto-generated catch block
1443
                        e2.printStackTrace();
1444
                        return null;
1445
                } catch (ValidationException e3) {
1446
                        // TODO Auto-generated catch block
1447
                        e3.printStackTrace();
1448
                        return null;
1449
                }
1450

    
1451
                return buffer.toString();
1452

    
1453
        }
1454

    
1455
        public XMLEntity unMarshallXMLEntity(String data) {
1456
                StringReader reader = new StringReader(data);
1457

    
1458
                XmlTag tag;
1459
                try {
1460
                        tag = (XmlTag) XmlTag.unmarshal(reader);
1461
                } catch (MarshalException e) {
1462
                        return null;
1463
                } catch (ValidationException e) {
1464
                        return null;
1465
                }
1466
                XMLEntity xml=new XMLEntity(tag);
1467

    
1468
                return xml;
1469
        }
1470

    
1471
        public String getFromClipboard() {
1472

    
1473
                 try {
1474
                        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
1475
                } catch (UnsupportedFlavorException e) {
1476
                        return null;
1477
                } catch (IOException e) {
1478
                        // TODO Auto-generated catch block
1479
                        return null;
1480
                }
1481
        }
1482

    
1483
        public boolean removeLayers(FLayer[] actives) {
1484
            for (int i = actives.length-1; i>=0; i--){
1485
                try {
1486
                                //actives[i].getParentLayer().removeLayer(actives[i]);
1487
                                //FLayers lyrs=getMapContext().getLayers();
1488
                                //lyrs.addLayer(actives[i]);
1489
                                actives[i].getParentLayer().removeLayer(actives[i]);
1490

    
1491
                if (actives[i] instanceof AlphanumericData){
1492
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1493
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
1494

    
1495
                    ArrayList tables = project.getDocumentsByType(ProjectTableFactory.registerName);
1496
                    for (int j = 0; j < tables.size(); j++) {
1497
                        if (tables.get(j) == pt){
1498
                            project.delDocument((ProjectDocument)tables.get(j));
1499
                            break;
1500
                        }
1501
                    }
1502

    
1503
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
1504
                }
1505

    
1506

    
1507
                    } catch (CancelationException e1) {
1508
                            e1.printStackTrace();
1509
                            return false;
1510
                    }
1511
            }
1512
                return true;
1513
        }
1514

    
1515
        public boolean removeDocuments(ProjectDocument[] selectedItems) {
1516
                Project p = this.getProject();
1517
                ProjectDocument element;
1518
                int index;
1519
                for (int i=selectedItems.length-1;i>=0;i--) {
1520

    
1521
                        element = selectedItems[i];
1522

    
1523
                        if (element instanceof ProjectMap) {
1524

    
1525
                                if (element.isLocked()) {
1526
                                        JOptionPane.showMessageDialog(
1527
                                                (Component)PluginServices.getMainFrame(),
1528
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1529
                                        );
1530
                                        //return false;
1531
                                } else {
1532
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1533
                                        p.delDocument(element);
1534
                                }
1535
                        } else if (element instanceof ProjectTable) {
1536
                                if (element.isLocked()) {
1537
                                        JOptionPane.showMessageDialog(
1538
                                                (Component)PluginServices.getMainFrame(),
1539
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1540
                                        );
1541

    
1542
                                        //return false;
1543
                                } else {
1544
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1545
                                        p.delDocument(element);
1546
                                }
1547
                        } else {
1548
                                if (element.isLocked()) {
1549
                                        JOptionPane.showMessageDialog(
1550
                                                (Component)PluginServices.getMainFrame(),
1551
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1552
                                        );
1553
                                        //return false;
1554
                                } else {
1555
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1556
                                        p.delDocument(element);
1557
                                }
1558
                        }
1559
                }
1560
                return true;
1561
        }
1562

    
1563
}