Statistics
| Revision:

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

History | View | Annotate | Download (42.9 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.awt.event.ActionEvent;
9
import java.io.IOException;
10
import java.io.StringReader;
11
import java.io.StringWriter;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Comparator;
15
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
import com.iver.cit.gvsig.gui.toc.AbstractTocContextMenuAction;
43
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
44
import com.iver.cit.gvsig.gui.toc.ITocItem;
45
import com.iver.cit.gvsig.gui.toc.TocMenuEntry;
46
import com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction;
47
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
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
62

    
63

    
64

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

    
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
            FPopupMenu.addEntry(copy);
80
            FPopupMenu.addEntry(cut);
81
            FPopupMenu.addEntry(paste);
82
                */
83

    
84
            // ProjectWindow
85
                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
                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

    
106

    
107
                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

    
111
                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

    
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
abstract class MyDocumentAction extends AbstractDocumentContextMenuAction implements IExtensionBuilder {
134
        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
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
        public int getOrder() {
169

    
170
                return 0;
171
        }
172

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

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

    
181

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

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

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

    
211
}
212

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

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

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

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

    
232

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

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

    
257

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

    
263

    
264
                this.utiles.removeDocuments(selectedItems);
265

    
266
        }
267

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

    
272
}
273

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

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

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

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

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

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

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

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

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

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

    
312

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

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

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

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

    
333
        }
334

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

    
339
}
340

    
341

    
342

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

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

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

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

    
358
}
359

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

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

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

    
373

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

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

    
397
                this.utiles.putInClipboard(data);
398

    
399
        }
400

    
401

    
402
}
403

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

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

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

    
417

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

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

    
441

    
442
                this.utiles.putInClipboard(data);
443

    
444

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

    
451

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

    
454
                getMapContext().endAtomicEvent();
455

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

    
463
        }
464
}
465

    
466

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

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

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

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

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

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

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

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

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

    
506
                return  xml;
507
        }
508

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

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

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

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

    
525
                getMapContext().endAtomicEvent();
526

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

    
530
}
531

    
532

    
533
class Utiles {
534

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

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

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

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

569

570
         */
571

    
572

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

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

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

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

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

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

    
618
                }
619

    
620

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

    
631
                return xml;
632

    
633
        }
634

    
635

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

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

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

    
647
                }
648

    
649

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

    
663

    
664
                return xml;
665
        }
666

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

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

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

    
677

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

    
685
                return xml;
686
        }
687

    
688

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

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

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

    
699
                }
700

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

    
711
                return xml;
712

    
713
        }
714

    
715

    
716

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

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

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

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

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

    
741

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

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

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

    
752
                return true;
753

    
754
        }
755

    
756

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

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

    
764
                Project project = this.getProject();
765

    
766
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
767

    
768
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
769

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

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

    
837

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

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

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

    
848
                return true;
849
        }
850

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

    
855

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

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

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

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

    
868

    
869

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

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

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

    
881
                Project project = this.getProject();
882

    
883
                Hashtable mapsConflits = this.getConflicts(xmlMaps,project.getMaps());
884

    
885
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
886

    
887
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
888

    
889

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

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

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

    
959
                }
960

    
961

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

    
979

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

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

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

    
992
                return this.addMaps(xmlMaps);
993

    
994
        }
995

    
996

    
997

    
998

    
999

    
1000

    
1001

    
1002

    
1003

    
1004

    
1005

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

    
1025

    
1026
        }
1027

    
1028

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

    
1040
                return true;
1041
        }
1042

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

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

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

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

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

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

    
1068

    
1069

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

    
1077

    
1078

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

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

    
1086
                return true;
1087
        }
1088

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

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

    
1107
        }
1108

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

    
1122
                        }
1123
                }
1124
                return conflits;
1125
        }
1126

    
1127

    
1128

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

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

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

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

    
1148

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

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

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

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

    
1185
                        Project project = this.getProject();
1186

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

    
1203
                        project.setLinkTable();
1204

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

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

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

    
1238
        }
1239

    
1240

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

    
1249
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(child, project);
1250
                                project.addView(pv);
1251

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

    
1259
        }
1260

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

    
1269
                                ProjectMap pm = (ProjectMap) ProjectMap.createFromXML(child, project);
1270
                                project.addMap(pm);
1271

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

    
1279
        }
1280

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

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

    
1291
                    return true;
1292
        }
1293

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

    
1303
                        xmlTables.addChild(xmlTable);
1304

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

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

    
1325
                    return true;
1326
        }
1327

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

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

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

    
1346
                    )) return false;
1347

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

    
1353
            }
1354

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

    
1363
                }
1364
                return true;
1365

    
1366
        }
1367

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

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

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

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

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

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

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

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

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

    
1419

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

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

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

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

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

    
1452
                return buffer.toString();
1453

    
1454
        }
1455

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

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

    
1469
                return xml;
1470
        }
1471

    
1472
        public String getFromClipboard() {
1473

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

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

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

    
1496
                    ArrayList tables = project.getTables();
1497
                    for (int j = 0; j < tables.size(); j++) {
1498
                        if (tables.get(j) == pt){
1499
                            project.delTable(j);
1500
                            break;
1501
                        }
1502
                    }
1503

    
1504
                    PluginServices.getMDIManager().closeSingletonView(pt);
1505
                }
1506

    
1507

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

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

    
1522
                        element = selectedItems[i];
1523

    
1524
                        if (element instanceof ProjectMap) {
1525

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

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

    
1564
}