Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / CopyPasteExtension.java @ 8745

History | View | Annotate | Download (43.2 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
                if ( selectedItems.length >= 1 && isTocItemBranch(item)){
371
                        for (int i=0;i< selectedItems.length;i++) {
372
                                if (selectedItems[i].isEditing()){
373
                                        return false;
374
                                }
375
                        }
376
                        return true;
377
                }
378
                return false;
379
                
380
        }
381

    
382

    
383
        public void execute(ITocItem item, FLayer[] selectedItems) {
384
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
385
                if (xml == null) {
386
                        JOptionPane.showMessageDialog(
387
                                        (Component)PluginServices.getMainFrame(),
388
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
389
                                        PluginServices.getText(this,"copiar"),//titulo
390
                                        JOptionPane.ERROR_MESSAGE
391
                                        );
392
                        return;
393
                }
394

    
395
                String data = this.utiles.marshallXMLEntity(xml);
396
                if (data == null) {
397
                        JOptionPane.showMessageDialog(
398
                                        (Component)PluginServices.getMainFrame(),
399
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
400
                                        PluginServices.getText(this,"copiar"),//titulo
401
                                        JOptionPane.ERROR_MESSAGE
402
                                        );
403
                        return;
404
                }
405

    
406
                this.utiles.putInClipboard(data);
407

    
408
        }
409

    
410

    
411
}
412

    
413
class CutTocMenuEntry extends MyTocMenuEntry{
414
        public int getOrder() {
415
                return 1;
416
        }
417

    
418
        public String getText() {
419
                return PluginServices.getText(this, "cortar");
420
        }
421

    
422
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
423
                if ( selectedItems.length >= 1 && isTocItemBranch(item)){
424
                        for (int i=0;i< selectedItems.length;i++) {
425
                                if (selectedItems[i].isEditing()){
426
                                        return false;
427
                                }
428
                        }
429
                        return true;
430
                }
431
                return false;
432

    
433
        }
434

    
435

    
436
        public void execute(ITocItem item, FLayer[] selectedItems) {
437
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
438
                if (xml == null) {
439
                        JOptionPane.showMessageDialog(
440
                                        (Component)PluginServices.getMainFrame(),
441
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
442
                                        PluginServices.getText(this,"cortar"),//titulo
443
                                        JOptionPane.ERROR_MESSAGE
444
                                        );
445
                        return;
446
                }
447

    
448
                String data = this.utiles.marshallXMLEntity(xml);
449
                if (data == null) {
450
                        JOptionPane.showMessageDialog(
451
                                        (Component)PluginServices.getMainFrame(),
452
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
453
                                        PluginServices.getText(this,"cortar"),//titulo
454
                                        JOptionPane.ERROR_MESSAGE
455
                                        );
456
                        return;
457
                }
458

    
459

    
460
                this.utiles.putInClipboard(data);
461

    
462

    
463
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
464
            if (option!=JOptionPane.OK_OPTION) {
465
                    return;
466
            }
467
                getMapContext().beginAtomicEvent();
468

    
469

    
470
                boolean isOK =this.utiles.removeLayers(selectedItems);
471

    
472
                getMapContext().endAtomicEvent();
473

    
474
                if (isOK) {
475
                        getMapContext().invalidate();
476
                        if (getMapContext().getLayers().getLayersCount()==0) {
477
                                PluginServices.getMainFrame().enableControls();
478
                        }
479
                }
480

    
481
        }
482
}
483

    
484

    
485
class PasteTocMenuEntry extends MyTocMenuEntry{
486
        private XMLEntity xml=null;
487

    
488
        public int getOrder() {
489
                return 2;
490
        }
491

    
492
        public String getText() {
493
                return PluginServices.getText(this, "pegar");
494
        }
495

    
496
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
497
                if (isTocItemBranch(item)) {
498
                        FLayer lyr = getNodeLayer(item);
499
                        if (lyr instanceof FLayers) {
500
                                this.xml = this.getCheckedXMLFromClipboard();
501
                                return true;
502
                        }
503

    
504
                } else if (!isTocItemLeaf(item)) {
505
                        if (getNodeLayer(item) == null) {
506
                                this.xml = this.getCheckedXMLFromClipboard();
507
                                return this.xml != null;
508
                        }
509
                }
510
                return false;
511
        }
512

    
513
        private XMLEntity getCheckedXMLFromClipboard() {
514
                String sourceString = this.utiles.getFromClipboard();
515
                if (sourceString == null) return null;
516

    
517
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
518
                if (xml == null) return null;
519

    
520
                if (!this.utiles.checkXMLRootNode(xml)) return null;
521

    
522
                if (this.utiles.getXMLEntityChildOfType(xml,"layers") == null) return null;
523

    
524
                return  xml;
525
        }
526

    
527
        public void execute(ITocItem item, FLayer[] selectedItems) {
528
                FLayers root;
529

    
530
                if (this.xml == null) return;
531

    
532
                if (isTocItemBranch(item)) {
533
                        root = (FLayers)getNodeLayer(item);
534
                } else if (getNodeLayer(item) == null){
535
                        root = getMapContext().getLayers();
536
                } else {
537
                        return;
538
                }
539
                getMapContext().beginAtomicEvent();
540

    
541
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
542

    
543
                getMapContext().endAtomicEvent();
544

    
545
                if (isOK) getMapContext().invalidate();
546
        }
547

    
548
}
549

    
550

    
551
class Utiles {
552

    
553
        /*
554
         *
555
======================================
556
 Comportamiento del Pegar documentos:
557
======================================
558

559
?Pegar vista.
560
        Si ya existe una vista en el proyecto con el mismo nombre.
561
                1. Abortar
562
                2. Pedir nuevo nombre.
563
                        Que hacemos con las tablas asociadas.
564
                                No se pegan
565
        Si alguna de las tablas a pegar de las que van en
566
        el portapapeles ya existen en el proyecto.
567
                1. abortamos
568
                2. Informamos al usuario y no se pegan las tablas.
569

570
Pegar tabla.
571
        Si alguna de las tablas existe.
572
                Se pega igualmente (apareceran tablas duplicadas)
573

574
Pegar mapa.
575
        Si el mapa ya existe en el proyecto.
576
                1. Abortar
577
                2. renombrar el mapa
578
        Si alguna vista ya existe en el proyecto.
579
                1. Abortar
580
                2. Usar la vista que ya existe en el proyecto y no
581
                   pegar la nueva vista.
582
        Si alguna de las tablas a pegar de las que van en
583
                el portapapeles ya existen en el proyecto.
584
                        1. abortamos
585
                        2. Informamos al usuario y no se pegan las tablas.
586

587

588
         */
589

    
590

    
591
        /*
592
         *
593
         *
594
         *
595
         * Funciones Publicas para generar XML (copiar)
596
         *
597
         *
598
        */
599

    
600
        /**
601
         * Genera un XMLEntity con la informacion necesaria
602
         * para copiar los elementos de selectedItems en
603
         * otro proyecto
604
         */
605
        public XMLEntity generateXMLCopyDocuments(ProjectElement[] selectedItems) {
606
                if (selectedItems.length == 0) return null;
607

    
608
                if (selectedItems[0] instanceof ProjectView) {
609
                        ProjectView[] views = new ProjectView[selectedItems.length];
610
                        System.arraycopy(selectedItems,0,views,0,selectedItems.length);
611
                        return this.generateXMLCopyViews(views);
612
                } else if (selectedItems[0] instanceof ProjectMap) {
613
                        ProjectMap[] maps = new ProjectMap[selectedItems.length];
614
                        System.arraycopy(selectedItems,0,maps,0,selectedItems.length);
615
                        return this.generateXMLCopyMaps(maps);
616
                } else if (selectedItems[0] instanceof ProjectTable) {
617
                        ProjectTable[] tables = new ProjectTable[selectedItems.length];
618
                        System.arraycopy(selectedItems,0,tables,0,selectedItems.length);
619
                        return this.generateXMLCopyTables(tables);
620
                } else {
621
                        //FIXME:????
622
                        return null;
623
                }
624
        }
625

    
626
        public XMLEntity generateXMLCopyViews(ProjectView[] selectedItems) {
627
                XMLEntity xml = this.newRootNode();
628

    
629
                XMLEntity xmlTables = this.newTablesNode();
630
                XMLEntity xmlDataSources = this.newDataSourcesNode();
631
                XMLEntity xmlViews = this.newViewsNode();
632

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

    
636
                }
637

    
638

    
639
                if (xmlDataSources.getChildrenCount() > 0) {
640
                        xml.addChild(xmlDataSources);
641
                }
642
                if (xmlViews.getChildrenCount() > 0) {
643
                        xml.addChild(xmlViews);
644
                }
645
                if (xmlTables.getChildrenCount() > 0) {
646
                        xml.addChild(xmlTables);
647
                }
648

    
649
                return xml;
650

    
651
        }
652

    
653

    
654
        public XMLEntity generateXMLCopyMaps(ProjectMap[] selectedItems) {
655
                XMLEntity xml = this.newRootNode();
656

    
657
                XMLEntity xmlTables = this.newTablesNode();
658
                XMLEntity xmlDataSources = this.newDataSourcesNode();
659
                XMLEntity xmlViews = this.newViewsNode();
660
                XMLEntity xmlMaps = this.newMapsNode();
661

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

    
665
                }
666

    
667

    
668
                if (xmlDataSources.getChildrenCount() > 0) {
669
                        xml.addChild(xmlDataSources);
670
                }
671
                if (xmlViews.getChildrenCount() > 0) {
672
                        xml.addChild(xmlViews);
673
                }
674
                if (xmlTables.getChildrenCount() > 0) {
675
                        xml.addChild(xmlTables);
676
                }
677
                if (xmlMaps.getChildrenCount() > 0) {
678
                        xml.addChild(xmlMaps);
679
                }
680

    
681

    
682
                return xml;
683
        }
684

    
685
        public XMLEntity generateXMLCopyTables(ProjectTable[] selectedItems) {
686
                XMLEntity xml = this.newRootNode();
687

    
688
                XMLEntity xmlTables = this.newTablesNode();
689
                XMLEntity xmlDataSources = this.newDataSourcesNode();
690

    
691
                for (int i=0;i < selectedItems.length; i++) {
692
                        if (!this.addToXMLTable(selectedItems[i],xmlTables,xmlDataSources,null)) return null;
693
                }
694

    
695

    
696
                if (xmlDataSources.getChildrenCount() > 0) {
697
                        xml.addChild(xmlDataSources);
698
                }
699
                if (xmlTables.getChildrenCount() > 0) {
700
                        xml.addChild(xmlTables);
701
                }
702

    
703
                return xml;
704
        }
705

    
706

    
707
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
708

    
709
                XMLEntity xml = this.newRootNode();
710
                XMLEntity xmlLayers = this.newLayersNode();
711
                XMLEntity xmlTables = this.newTablesNode();
712
                XMLEntity xmlDataSources = this.newDataSourcesNode();
713

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

    
717
                }
718

    
719
                if (xmlDataSources.getChildrenCount() > 0) {
720
                        xml.addChild(xmlDataSources);
721
                }
722
                if (xmlLayers.getChildrenCount() > 0) {
723
                        xml.addChild(xmlLayers);
724
                }
725
                if (xmlTables.getChildrenCount() > 0) {
726
                        xml.addChild(xmlTables);
727
                }
728

    
729
                return xml;
730

    
731
        }
732

    
733

    
734

    
735
        /*
736
         *
737
         *
738
         *
739
         * Funciones Publicas de carga de un XML (pegar)
740
         *
741
         *
742
         *
743
        */
744

    
745
        public boolean loadLayersFromXML(XMLEntity xml, FLayers root) {
746
                XMLEntity xmlLayers = this.getXMLEntityChildOfType(xml,"layers");
747
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
748
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
749

    
750
                if (xmlLayers == null ) return false;
751

    
752
                // Se pegan las tablas igualmente
753
                /*
754
                Project project = this.getProject();
755

756
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
757
                */
758

    
759

    
760
                if (xmlDataSources != null)  {
761
                        if (!this.registerDataSources(xmlDataSources)) return false;
762
                }
763

    
764
                if (!this.addLayers(xmlLayers,root)) return false;
765

    
766
                if (xmlTables != null)  {
767
                        if (!this.addTables(xmlTables)) return false;
768
                }
769

    
770
                return true;
771

    
772
        }
773

    
774

    
775
        public boolean loadViewsFromXML(XMLEntity xml) {
776
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
777
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
778
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
779

    
780
                if (xmlViews == null ) return false;
781

    
782
                Project project = this.getProject();
783

    
784
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
785

    
786
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
787

    
788
                if (viewsConflits != null && viewsConflits.size() > 0) {
789
                        int option = JOptionPane.showConfirmDialog(
790
                                        (Component)PluginServices.getMainFrame(),
791
                                        "<html>"+
792
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
793
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
794
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
795
                                                PluginServices.getText(this,"desea_continuar") +
796
                                        "</html>",
797
                                        PluginServices.getText(this,"pegar_vistas"),
798
                                        JOptionPane.YES_NO_OPTION
799
                                        );
800
                        if (option != JOptionPane.YES_OPTION) {
801
                                return false;
802
                        }
803
                        Enumeration en = viewsConflits.elements();
804
                        while (en.hasMoreElements()) {
805
                                XMLEntity view = (XMLEntity)en.nextElement();
806
                                String newName = JOptionPane.showInputDialog(
807
                                                (Component)PluginServices.getMainFrame(),
808
                                                "<html>"+
809
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
810
                                                "</html>", //Mensaje
811
                                                view.getStringProperty("name") //Valor por defecto
812
                                                );
813
                                if (newName == null) {
814
                                        JOptionPane.showMessageDialog(
815
                                                        (Component)PluginServices.getMainFrame(),
816
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
817
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
818
                                                        JOptionPane.ERROR_MESSAGE
819
                                                        );
820
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
821
                                        JOptionPane.showMessageDialog(
822
                                                        (Component)PluginServices.getMainFrame(),
823
                                                        "<html>"+
824
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
825
                                                                PluginServices.getText(this,"nombre_no_valido")+
826
                                                        "</html>",//Mensaje
827
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
828
                                                        JOptionPane.ERROR_MESSAGE
829
                                                        );
830
                                        return false;
831
                                }
832
                                view.setName(newName);
833
                        }
834
                        if (xmlTables != null) xmlTables.removeAllChilds();
835
                        tablesConflits = null;
836
                }
837

    
838
                if (tablesConflits != null && tablesConflits.size() > 0) {
839
                        int option = JOptionPane.showConfirmDialog(
840
                                        (Component)PluginServices.getMainFrame(),
841
                                        "<html>" +
842
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
843
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
844
                                                PluginServices.getText(this,"desea_continuar") +
845
                                        "</html>", //Mensaje
846
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
847
                                        JOptionPane.YES_NO_OPTION
848
                                        );
849
                        if (option != JOptionPane.YES_OPTION) {
850
                                return false;
851
                        }
852
                        xmlTables.removeAllChilds();
853
                }
854

    
855

    
856
                if (xmlDataSources != null)  {
857
                        if (!this.registerDataSources(xmlDataSources)) return false;
858
                }
859

    
860
                if (!this.addViews(xmlViews)) return false;
861

    
862
                if (xmlTables != null)  {
863
                        if (!this.addTables(xmlTables)) return false;
864
                }
865

    
866
                return true;
867
        }
868

    
869
        public boolean loadTablesFromXML(XMLEntity xml) {
870
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
871
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
872

    
873

    
874
                if (xmlTables == null ) return false;
875

    
876
                /*
877
                Project project = this.getProject();
878

879
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
880
                */
881

    
882
                if (xmlDataSources != null)  {
883
                        if (!this.registerDataSources(xmlDataSources)) return false;
884
                }
885

    
886

    
887

    
888
                return this.addTables(xmlTables);
889
        }
890

    
891
        public boolean loadMapsFromXML(XMLEntity xml) {
892
                XMLEntity xmlMaps = this.getXMLEntityChildOfType(xml,"Maps");
893
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
894
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
895
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
896

    
897
                if (xmlMaps == null ) return false;
898

    
899
                Project project = this.getProject();
900

    
901
                Hashtable mapsConflits = this.getConflicts(xmlMaps,project.getMaps());
902

    
903
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
904

    
905
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
906

    
907

    
908
                if (mapsConflits != null && mapsConflits.size() > 0) {
909
                        int option = JOptionPane.showConfirmDialog(
910
                                        (Component)PluginServices.getMainFrame(),
911
                                        "<html>"+
912
                                                PluginServices.getText(this,"conflicto_de_nombres_de_mapas_al_pegar") + "<br>" +
913
                                                PluginServices.getText(this,"debera_introducir_nombres_para_los_mapas_a_pegar") + "<br>" +
914
                                        "</html>", //Mensaje
915
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
916
                                        JOptionPane.YES_NO_OPTION
917
                                        );
918
                        if (option != JOptionPane.YES_OPTION) {
919
                                return false;
920
                        }
921
                        Enumeration en = mapsConflits.elements();
922
                        while (en.hasMoreElements()) {
923
                                XMLEntity map = (XMLEntity)en.nextElement();
924
                                String newName = JOptionPane.showInputDialog(
925
                                                (Component)PluginServices.getMainFrame(),
926
                                                "<html>"+
927
                                                        PluginServices.getText(this,"nuevo_nombre_para_el_mapa") +" "+  map.getStringProperty("name") + ":" +
928
                                            "</html>", //Mensaje
929
                                                map.getStringProperty("name") //Valor por defecto
930
                                                );
931
                                if (newName == null) {
932
                                        JOptionPane.showMessageDialog(
933
                                                        (Component)PluginServices.getMainFrame(),
934
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
935
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
936
                                                        JOptionPane.ERROR_MESSAGE
937
                                                        );
938
                                } else if (newName.equalsIgnoreCase(map.getStringProperty("name")) ) {
939
                                        JOptionPane.showMessageDialog(
940
                                                        (Component)PluginServices.getMainFrame(),
941
                                                        "<html>"+
942
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
943
                                                                PluginServices.getText(this,"nombre_no_valido")+
944
                                                        "</html>",//Mensaje
945
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
946
                                                        JOptionPane.ERROR_MESSAGE
947
                                                        );
948
                                        return false;
949
                                }
950
                                map.setName(newName);
951
                        }
952
                }
953

    
954
                if (viewsConflits != null && viewsConflits.size() > 0) {
955
                        int option = JOptionPane.showConfirmDialog(
956
                                        (Component)PluginServices.getMainFrame(),
957
                                        "<html>"+
958
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
959
                                                PluginServices.getText(this,"no_se_pegaran_las_vistas_del_conflicto") + "<br>" +
960
                                                PluginServices.getText(this,"desea_continuar") +
961
                                        "</html>",
962
                                        PluginServices.getText(this,"pegar_mapas"),//titulo
963
                                        JOptionPane.YES_NO_OPTION
964
                                        );
965
                        if (option != JOptionPane.YES_OPTION) {
966
                                return false;
967
                        }
968
                        // Eliminamos las vistas del xml que no vamos a importar
969

    
970
                        // Esto me devuelve los indices en orden inverso
971
                        int[] indexes = this.getIndexOfConflict(viewsConflits);
972
                        for (int i=0;i < indexes.length;i++) {
973
                                xmlViews.removeChild(indexes[i]);
974
                        }
975
                        viewsConflits = null;
976

    
977
                }
978

    
979

    
980
                if (tablesConflits != null && tablesConflits.size() > 0) {
981
                        int option = JOptionPane.showConfirmDialog(
982
                                        (Component)PluginServices.getMainFrame(),
983
                                        "<html>" +
984
                                                PluginServices.getText(this,"conflito_de_nombres_de_tablas_al_pegar") + "<br>" +
985
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
986
                                                PluginServices.getText(this,"desea_continuar") +
987
                                        "</html>", //Mensaje
988
                                        PluginServices.getText(this,"pegar_mapas"),
989
                                        JOptionPane.YES_NO_OPTION
990
                                        );
991
                        if (option != JOptionPane.YES_OPTION) {
992
                                return false;
993
                        }
994
                        xmlTables.removeAllChilds();
995
                }
996

    
997

    
998
                if (xmlDataSources != null)  {
999
                        if (!this.registerDataSources(xmlDataSources)) return false;
1000
                }
1001

    
1002
                if (xmlViews != null)  {
1003
                        if (!this.addViews(xmlViews)) return false;
1004
                }
1005

    
1006
                if (xmlTables != null)  {
1007
                        if (!this.addTables(xmlTables)) return false;
1008
                }
1009

    
1010
                return this.addMaps(xmlMaps);
1011

    
1012
        }
1013

    
1014

    
1015

    
1016

    
1017

    
1018

    
1019

    
1020

    
1021

    
1022

    
1023

    
1024
        /**
1025
         * Devuelve las claves de conflits ordenados
1026
         * en orden inverso. Las claves se esperan que
1027
         * sean instancias de Integer
1028
         */
1029
        private int[] getIndexOfConflict(Hashtable conflits) {
1030
                Object[] tmpArray = conflits.keySet().toArray();
1031
                Arrays.sort(tmpArray,new Comparator() {
1032
                        public int compare(Object o1, Object o2) {
1033
                                return ((Integer)o2).intValue() - ((Integer)o1).intValue();
1034
                        }
1035
                }
1036
                );
1037
                int[] indexes = new int[] {tmpArray.length};
1038
                for (int i = 0;i< tmpArray.length;i++) {
1039
                        indexes[i] = ((Integer)tmpArray[i]).intValue();
1040
                }
1041
                return indexes;
1042

    
1043

    
1044
        }
1045

    
1046

    
1047
        private boolean addToXMLMapDependencies(ProjectMap map, XMLEntity xmlViews,XMLEntity xmlTables, XMLEntity xmlDataSources) {
1048
                IFFrame[] components = map.getModel().getFFrames();
1049
                for (int i=0; i < components.length; i++) {
1050
                        if (components[i] instanceof FFrameView) {
1051
                                ProjectView view = ((FFrameView)components[i]).getView();
1052
                                if (findChildInXML(xmlViews,"name",view.getName())==null) {
1053
                                        if (!this.addToXMLView(view,xmlViews,xmlTables,xmlDataSources)) return false;
1054
                                }
1055
                        }
1056
                }
1057

    
1058
                return true;
1059
        }
1060

    
1061
        private boolean addToXMLMap(ProjectMap map,XMLEntity xmlMaps,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1062
                try {
1063
                        xmlMaps.addChild(map.getXMLEntity());
1064

    
1065
                        return this.addToXMLMapDependencies(map,xmlViews,xmlTables,xmlDataSources);
1066

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

    
1074
        private boolean addToXMLView(ProjectView view,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1075
                try {
1076
                        xmlViews.addChild(view.getXMLEntity());
1077

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

    
1080
                        if (view.getMapOverViewContext() != null) {
1081
                                return this.addToXMLLayerDependencies(view.getMapOverViewContext().getLayers(),xmlTables,xmlDataSources);
1082
                        } else {
1083
                                return true;
1084
                        }
1085

    
1086

    
1087

    
1088
                } catch (SaveException e) {
1089
                        // TODO Auto-generated catch block
1090
                        e.printStackTrace();
1091
                        return false;
1092
                }
1093
        }
1094

    
1095

    
1096

    
1097
        public boolean checkXMLRootNode(XMLEntity xml) {
1098
                if (!xml.contains("applicationName")) return false;
1099
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG")) return false;
1100

    
1101
                if (!xml.contains("version")) return false;
1102
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false;
1103

    
1104
                return true;
1105
        }
1106

    
1107
        private void fillXMLRootNode(XMLEntity xml) {
1108
                xml.putProperty("applicationName","gvSIG");
1109
                xml.putProperty("version",Version.format());
1110
        }
1111

    
1112
        public XMLEntity getXMLEntityChildOfType(XMLEntity xml,String type) {
1113
                int childCount = xml.getChildrenCount();
1114
                XMLEntity child;
1115
                for (int i=0; i < childCount; i++  ) {
1116
                        child = xml.getChild(i);
1117
                        if (child.contains("type")) {
1118
                                if (child.getStringProperty("type").equalsIgnoreCase(type)) {
1119
                                        return child;
1120
                                }
1121
                        }
1122
                }
1123
                return null;
1124

    
1125
        }
1126

    
1127
        private Hashtable getConflicts(XMLEntity xml,ArrayList elements) {
1128
                if (xml == null || xml.getChildrenCount() < 1) return null;
1129
                Hashtable conflits = new Hashtable();
1130
                for (int iXML=0;iXML < xml.getChildrenCount();iXML++) {
1131
                        XMLEntity child = xml.getChild(iXML);
1132
                        Iterator iter = elements.iterator();
1133
                        while (iter.hasNext()) {
1134
                                ProjectElement element = (ProjectElement)iter.next();
1135
                                if (element.getName().equalsIgnoreCase(child.getStringProperty("name"))) {
1136
                                        conflits.put(new Integer(iXML),child);
1137
                                        break;
1138
                                }
1139

    
1140
                        }
1141
                }
1142
                return conflits;
1143
        }
1144

    
1145

    
1146

    
1147
        private boolean registerDataSources(XMLEntity xmlDataSources) {
1148
                try {
1149
                        int numDataSources = xmlDataSources.getChildrenCount();
1150

    
1151
                        if (numDataSources == 0) return true;
1152
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
1153

    
1154
                        for (int i = 0; i < numDataSources; i++) {
1155
                                XMLEntity child = xmlDataSources.getChild(i);
1156
                                String name = child.getStringProperty("gdbmsname");
1157

    
1158
                                if (dsFactory.getDriverInfo(name) == null) {
1159
                                        if (child.getStringProperty("type").equals("otherDriverFile")) {
1160
                                                LayerFactory.getDataSourceFactory().addFileDataSource(
1161
                                                                child.getStringProperty("driverName"),
1162
                                                                name,
1163
                                                                child.getStringProperty("file")
1164
                                                );
1165

    
1166

    
1167
                                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
1168
                                                /*                                String layerName = child.getStringProperty("layerName");
1169
                                                 ProjectView vista = project.getViewByName(child.getStringProperty(
1170
                                                 "viewName"));
1171
                                                 FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
1172

1173
                                                 modelo = ((AlphanumericData) layer).getRecordset();
1174
                                                 associatedTable = (AlphanumericData) layer;
1175
                                                 */
1176
                                        } else if (child.getStringProperty("type").equals("db")) {
1177
                                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
1178
                                                                name,
1179
                                                                child.getStringProperty("host"),
1180
                                                                child.getIntProperty("port"),
1181
                                                                child.getStringProperty("user"),
1182
                                                                child.getStringProperty("password"),
1183
                                                                child.getStringProperty("dbName"),
1184
                                                                child.getStringProperty("tableName"),
1185
                                                                child.getStringProperty("driverInfo")
1186
                                                );
1187
                                        }
1188
                                }
1189
                        }
1190

    
1191
                        return true;
1192
                } catch (Exception e) {
1193
                        e.printStackTrace();
1194
                        return false;
1195
                }
1196
        }
1197

    
1198
        private boolean addTables(XMLEntity xmlTables) {
1199
                try {
1200
                        int numTables = xmlTables.getChildrenCount();
1201
                        if (numTables == 0) return true;
1202

    
1203
                        Project project = this.getProject();
1204

    
1205
                        for (int i = 0; i < numTables; i++) {
1206
                                try{
1207
                                        ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xmlTables.getChild(i), project);
1208
                                        project.addTable(ptable);
1209
                                        /*
1210
                                        if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
1211
                                                PluginServices.getMDIManager().addView(ptable.getAndamiView());
1212
                                                PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
1213
                                        }
1214
                                        */
1215
                                }catch(OpenException e){
1216
                                        e.printStackTrace();
1217
                                        return false;
1218
                                }
1219
                        }
1220

    
1221
                        project.setLinkTable();
1222

    
1223
                        return true;
1224
                } catch (Exception e) {
1225
                        e.printStackTrace();
1226
                        return false;
1227
                }
1228
        }
1229

    
1230
        public XMLEntity findChildInXML(XMLEntity xml,String propName,String value) {
1231
                int num = xml.getChildrenCount();
1232
                XMLEntity child;
1233
                for (int i=0;i < num; i++) {
1234
                        child = xml.getChild(i);
1235
                        if (child.getStringProperty(propName).equals(value)) {
1236
                                return child;
1237
                        }
1238
                }
1239
                return null;
1240
        }
1241

    
1242
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
1243
                try {
1244
                        XMLEntity child;
1245
                        int numLayers = xmlLayers.getChildrenCount();
1246
                        for (int i = 0; i < numLayers; i++) {
1247
                                child = xmlLayers.getChild(i);
1248
                                if (!root.addLayerFromXMLEntity(child,null)) return false;
1249
                        }
1250
                        return true;
1251
                } catch (Exception e) {
1252
                        e.printStackTrace();
1253
                        return false;
1254
                }
1255

    
1256
        }
1257

    
1258

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

    
1267
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(child, project);
1268
                                project.addView(pv);
1269

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

    
1277
        }
1278

    
1279
        private boolean addMaps(XMLEntity xmlMaps) {
1280
                try {
1281
                        Project project = this.getProject();
1282
                        XMLEntity child;
1283
                        int numLayers = xmlMaps.getChildrenCount();
1284
                        for (int i = 0; i < numLayers; i++) {
1285
                                child = xmlMaps.getChild(i);
1286

    
1287
                                ProjectMap pm = (ProjectMap) ProjectMap.createFromXML(child, project);
1288
                                project.addMap(pm);
1289

    
1290
                        }
1291
                        return true;
1292
                } catch (Exception e) {
1293
                        e.printStackTrace();
1294
                        return false;
1295
                }
1296

    
1297
        }
1298

    
1299
        private Project getProject() {
1300
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1301
        }
1302

    
1303
        private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) {
1304
                if (project== null) {
1305
                        project = this.getProject();
1306
                }
1307
                    xmlDataSources.addChild(project.getSourceInfoXMLEntity(source));
1308

    
1309
                    return true;
1310
        }
1311

    
1312
        private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) {
1313
                if (project== null) {
1314
                        project = this.getProject();
1315
                }
1316
                if (findChildInXML(xmlTables,"name",pt.getName()) != null) return true;
1317
                XMLEntity xmlTable = null;
1318
                try {
1319
                        xmlTable = pt.getXMLEntity();
1320

    
1321
                        xmlTables.addChild(xmlTable);
1322

    
1323
                        if (pt.getAssociatedTable() != null) {
1324
                                this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
1325
                        }
1326

    
1327
                        if (pt.getLinkTable() != null) {
1328
                                if (findChildInXML(xmlTables,"name",pt.getLinkTable()) == null)  {
1329
                                        ProjectTable ptLink = project.getTable(pt.getLinkTable());
1330
                                        if (!this.addToXMLTable(ptLink,xmlTables,xmlDataSources,project)) return false;
1331
                                }
1332
                        }
1333
                } catch (SaveException e) {
1334
                        // TODO Auto-generated catch block
1335
                        e.printStackTrace();
1336
                        return false;
1337
                } catch (DriverException e) {
1338
                        // TODO Auto-generated catch block
1339
                        e.printStackTrace();
1340
                        return false;
1341
                }
1342

    
1343
                    return true;
1344
        }
1345

    
1346
        private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1347
                try {
1348
                        Project project = this.getProject();
1349

    
1350
                        if (lyr instanceof FLayers) {
1351
                                FLayers lyrs = (FLayers)lyr;
1352
                                int count = lyrs.getLayersCount();
1353
                                for (int i=0;i < count;i++) {
1354
                                        FLayer subLyr = lyrs.getLayer(i);
1355
                                        this.addToXMLLayerDependencies(subLyr,xmlTables,xmlDataSources);
1356
                                }
1357

    
1358
                    } else if (lyr instanceof AlphanumericData){
1359
                    if (!this.addToXMLDataSource(
1360
                                ((AlphanumericData)lyr).getRecordset().getSourceInfo(),
1361
                                xmlDataSources,
1362
                                project
1363

    
1364
                    )) return false;
1365

    
1366
                ProjectTable pt = project.getTable((AlphanumericData) lyr);
1367
                if (pt != null) {
1368
                        if (!this.addToXMLTable(pt,xmlTables,xmlDataSources,project)) return false;
1369
                }
1370

    
1371
            }
1372

    
1373
                } catch (DriverException e) {
1374
                        // TODO Auto-generated catch block
1375
                        e.printStackTrace();
1376
                        return false;
1377
                } catch (Exception e) {
1378
                        e.printStackTrace();
1379
                        return false;
1380

    
1381
                }
1382
                return true;
1383

    
1384
        }
1385

    
1386
        private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1387
                try {
1388
                        xmlLayers.addChild(lyr.getXMLEntity());
1389

    
1390
                        return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources);
1391

    
1392
                } catch (XMLException e) {
1393
                        e.printStackTrace();
1394
                        return false;
1395
                } catch (Exception e) {
1396
                        e.printStackTrace();
1397
                        return false;
1398
                }
1399
        }
1400

    
1401
        private XMLEntity newRootNode() {
1402
                XMLEntity xml = new XMLEntity();
1403
                fillXMLRootNode(xml);
1404
                return xml;
1405
        }
1406

    
1407
        private XMLEntity newLayersNode() {
1408
                XMLEntity xmlLayers = new XMLEntity();
1409
                xmlLayers.putProperty("type","layers");
1410
                return xmlLayers;
1411
        }
1412

    
1413
        private XMLEntity newDataSourcesNode() {
1414
                XMLEntity xmlDataSources = new XMLEntity();
1415
                xmlDataSources.putProperty("type","dataSources");
1416
                return xmlDataSources;
1417
        }
1418

    
1419
        private XMLEntity newTablesNode() {
1420
                XMLEntity xmlTables = new XMLEntity();
1421
                xmlTables.putProperty("type","tables");
1422
                return xmlTables;
1423
        }
1424

    
1425
        private XMLEntity newViewsNode() {
1426
                XMLEntity xmlTables = new XMLEntity();
1427
                xmlTables.putProperty("type","views");
1428
                return xmlTables;
1429
        }
1430

    
1431
        private XMLEntity newMapsNode() {
1432
                XMLEntity xmlTables = new XMLEntity();
1433
                xmlTables.putProperty("type","maps");
1434
                return xmlTables;
1435
        }
1436

    
1437

    
1438
        public void putInClipboard(String data) {
1439
                StringSelection ss = new StringSelection(data);
1440

    
1441
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
1442
        }
1443

    
1444
        public String marshallXMLEntity(XMLEntity xml) {
1445
                StringWriter buffer = new StringWriter();
1446

    
1447
                Marshaller m;
1448
                try {
1449
                        m = new Marshaller(buffer);
1450
                } catch (IOException e4) {
1451
                        // TODO Auto-generated catch block
1452
                        e4.printStackTrace();
1453
                        return null;
1454
                }
1455
                m.setEncoding("ISO-8859-1");
1456

    
1457
                try {
1458
                        m.marshal(xml.getXmlTag());
1459
                        //if (i < actives.length-1) buffer.write("\n##layer-separator##\n");
1460
                } catch (MarshalException e2) {
1461
                        // TODO Auto-generated catch block
1462
                        e2.printStackTrace();
1463
                        return null;
1464
                } catch (ValidationException e3) {
1465
                        // TODO Auto-generated catch block
1466
                        e3.printStackTrace();
1467
                        return null;
1468
                }
1469

    
1470
                return buffer.toString();
1471

    
1472
        }
1473

    
1474
        public XMLEntity unMarshallXMLEntity(String data) {
1475
                StringReader reader = new StringReader(data);
1476

    
1477
                XmlTag tag;
1478
                try {
1479
                        tag = (XmlTag) XmlTag.unmarshal(reader);
1480
                } catch (MarshalException e) {
1481
                        return null;
1482
                } catch (ValidationException e) {
1483
                        return null;
1484
                }
1485
                XMLEntity xml=new XMLEntity(tag);
1486

    
1487
                return xml;
1488
        }
1489

    
1490
        public String getFromClipboard() {
1491

    
1492
                 try {
1493
                        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
1494
                } catch (UnsupportedFlavorException e) {
1495
                        return null;
1496
                } catch (IOException e) {
1497
                        // TODO Auto-generated catch block
1498
                        return null;
1499
                }
1500
        }
1501

    
1502
        public boolean removeLayers(FLayer[] actives) {
1503
            for (int i = actives.length-1; i>=0; i--){
1504
                try {
1505
                                //actives[i].getParentLayer().removeLayer(actives[i]);
1506
                                //FLayers lyrs=getMapContext().getLayers();
1507
                                //lyrs.addLayer(actives[i]);
1508
                                actives[i].getParentLayer().removeLayer(actives[i]);
1509

    
1510
                if (actives[i] instanceof AlphanumericData){
1511
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1512
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
1513

    
1514
                    ArrayList tables = project.getTables();
1515
                    for (int j = 0; j < tables.size(); j++) {
1516
                        if (tables.get(j) == pt){
1517
                            project.delTable(j);
1518
                            break;
1519
                        }
1520
                    }
1521

    
1522
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
1523
                }
1524

    
1525

    
1526
                    } catch (CancelationException e1) {
1527
                            e1.printStackTrace();
1528
                            return false;
1529
                    }
1530
            }
1531
                return true;
1532
        }
1533

    
1534
        public boolean removeDocuments(ProjectElement[] selectedItems) {
1535
                Project p = this.getProject();
1536
                ProjectElement element;
1537
                int index;
1538
                for (int i=selectedItems.length-1;i>=0;i--) {
1539

    
1540
                        element = selectedItems[i];
1541

    
1542
                        if (element instanceof ProjectMap) {
1543

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

    
1561
                                        //return false;
1562
                                } else {
1563
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1564
                                        p.delTable(p.getTables().indexOf(element));
1565
                                }
1566
                        } else {
1567
                                if (element.isLocked()) {
1568
                                        JOptionPane.showMessageDialog(
1569
                                                (Component)PluginServices.getMainFrame(),
1570
                                                PluginServices.getText(this, "locked_element_it_cannot_be_deleted") + ": " +element.getName()
1571
                                        );
1572
                                        //return false;
1573
                                } else {
1574
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1575
                                        p.delView(p.getViews().indexOf(element));
1576
                                }
1577
                        }
1578
                }
1579
                return true;
1580
        }
1581

    
1582
}