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 6635 jmvivo
package com.iver.cit.gvsig;
2
3
import java.awt.Component;
4
import java.awt.Toolkit;
5
import java.awt.datatransfer.DataFlavor;
6
import java.awt.datatransfer.StringSelection;
7
import java.awt.datatransfer.UnsupportedFlavorException;
8
import java.awt.event.ActionEvent;
9
import java.io.IOException;
10
import java.io.StringReader;
11
import java.io.StringWriter;
12
import java.util.ArrayList;
13 6645 jmvivo
import java.util.Arrays;
14
import java.util.Comparator;
15 6635 jmvivo
import java.util.Enumeration;
16
import java.util.Hashtable;
17
import java.util.Iterator;
18
import java.util.Map;
19
20
import javax.swing.JMenuItem;
21
import javax.swing.JOptionPane;
22
23
import org.exolab.castor.xml.MarshalException;
24
import org.exolab.castor.xml.Marshaller;
25
import org.exolab.castor.xml.ValidationException;
26
27
import com.hardcode.gdbms.engine.data.DataSourceFactory;
28
import com.hardcode.gdbms.engine.data.SourceInfo;
29
import com.iver.andami.PluginServices;
30
import com.iver.andami.plugins.Extension;
31
import com.iver.cit.gvsig.fmap.DriverException;
32
import com.iver.cit.gvsig.fmap.layers.CancelationException;
33
import com.iver.cit.gvsig.fmap.layers.FLayer;
34
import com.iver.cit.gvsig.fmap.layers.FLayers;
35
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
36
import com.iver.cit.gvsig.fmap.layers.XMLException;
37
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
38
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
39
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
40
import com.iver.cit.gvsig.gui.project.OpenException;
41
import com.iver.cit.gvsig.gui.project.SaveException;
42 6676 jmvivo
import com.iver.cit.gvsig.gui.toc.AbstractTocContextMenuAction;
43 6635 jmvivo
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
44 6676 jmvivo
import com.iver.cit.gvsig.gui.toc.ITocItem;
45 6635 jmvivo
import com.iver.cit.gvsig.gui.toc.TocMenuEntry;
46 6676 jmvivo
import com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction;
47 6635 jmvivo
import com.iver.cit.gvsig.project.Project;
48
import com.iver.cit.gvsig.project.ProjectElement;
49
import com.iver.cit.gvsig.project.ProjectMap;
50
import com.iver.cit.gvsig.project.ProjectTable;
51
import com.iver.cit.gvsig.project.ProjectView;
52
import com.iver.utiles.XMLEntity;
53
import com.iver.utiles.extensionPoints.ExtensionPoints;
54
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
55
import com.iver.utiles.extensionPoints.IExtensionBuilder;
56
import com.iver.utiles.xmlEntity.generate.XmlTag;
57
58
public class CopyPasteExtension extends Extension {
59
60
        public void initialize() {
61 6676 jmvivo
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
62 6865 jaume
63
64
65 6635 jmvivo
                // TOC
66 6676 jmvivo
                MyTocMenuEntry copy = new CopyTocMenuEntry();
67
                MyTocMenuEntry cut = new CutTocMenuEntry();
68
                MyTocMenuEntry paste = new PasteTocMenuEntry();
69 6635 jmvivo
                Utiles utiles = new Utiles();
70
                copy.setUtiles(utiles);
71
                cut.setUtiles(utiles);
72
                paste.setUtiles(utiles);
73 6676 jmvivo
74
                extensionPoints.add("View_TocActions","Copy","Copy selectes layers to system clipboard",copy);
75
                extensionPoints.add("View_TocActions","Cut","Cut selectes layers to system clipboard", cut);
76
                extensionPoints.add("View_TocActions","Paste","Paste layers from system clipboard",paste);
77
78
                /*
79 6635 jmvivo
            FPopupMenu.addEntry(copy);
80
            FPopupMenu.addEntry(cut);
81
            FPopupMenu.addEntry(paste);
82 6676 jmvivo
                */
83 6865 jaume
84
            // ProjectWindow
85 6635 jmvivo
                CopyProjectElement copyProjectElement = new CopyProjectElement();
86
                CutProjectElement cutProjectElement = new CutProjectElement();
87
                PasteProjectElement pasteProjectElementView = new PasteProjectElement();
88
                PasteProjectElement pasteProjectElementTable = new PasteProjectElement();
89
                PasteProjectElement pasteProjectElementMap = new PasteProjectElement();
90 6865 jaume
91 6635 jmvivo
                copyProjectElement.setUtiles(utiles);
92
                cutProjectElement.setUtiles(utiles);
93
                pasteProjectElementView.setUtiles(utiles);
94
                pasteProjectElementTable.setUtiles(utiles);
95
                pasteProjectElementMap.setUtiles(utiles);
96 6865 jaume
97 6635 jmvivo
                pasteProjectElementView.setType("views");
98
                pasteProjectElementTable.setType("tables");
99
                pasteProjectElementMap.setType("maps");
100 6865 jaume
101
102 6676 jmvivo
                extensionPoints.add("DocumentActions_View","Copy","Copy selectes documento to system clipboard",copyProjectElement);
103
                extensionPoints.add("DocumentActions_View","Cut","Cut selectes documento to system clipboard", cutProjectElement);
104
                extensionPoints.add("DocumentActions_View","Paste","Paste views from system clipboard",pasteProjectElementView);
105 6635 jmvivo
106
107 6676 jmvivo
                extensionPoints.add("DocumentActions_Table","Copy","Copy selectes documento to system clipboard",copyProjectElement);
108
                extensionPoints.add("DocumentActions_Table","Cut","Cut selectes documento to system clipboard", cutProjectElement);
109
                extensionPoints.add("DocumentActions_Table","Paste","Paste tables from system clipboard",pasteProjectElementTable);
110 6635 jmvivo
111 6676 jmvivo
                extensionPoints.add("DocumentActions_Map","Copy","Copy selectes documento to system clipboard",copyProjectElement);
112
                extensionPoints.add("DocumentActions_Map","Cut","Cut selectes documento to system clipboard", cutProjectElement);
113
                extensionPoints.add("DocumentActions_Map","Paste","Paste maps from system clipboard",pasteProjectElementMap);
114 6635 jmvivo
115
        }
116
117
        public void execute(String actionCommand) {
118
                // TODO Auto-generated method stub
119 6865 jaume
120 6635 jmvivo
        }
121
122
        public boolean isEnabled() {
123
                return false;
124
        }
125
126
        public boolean isVisible() {
127
                return false;
128
        }
129
130
131
}
132
133 6676 jmvivo
abstract class MyDocumentAction extends AbstractDocumentContextMenuAction implements IExtensionBuilder {
134 6635 jmvivo
        protected Utiles utiles;
135
136
        public void setUtiles(Utiles utiles) {
137
                this.utiles = utiles;
138
        }
139 6865 jaume
140 6635 jmvivo
        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 6865 jaume
                return this;
157
        }
158 6635 jmvivo
}
159
160
161 6676 jmvivo
class CopyProjectElement extends MyDocumentAction{
162
        public String getDescription() {
163
                //FIXME: Falta claves
164
                //return PluginServices.getText(this,"tooltip_copiar_al_portapapeles");
165
                return null;
166
        }
167 6865 jaume
168 6635 jmvivo
        public int getOrder() {
169 6865 jaume
170 6635 jmvivo
                return 0;
171
        }
172 6865 jaume
173 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
174 6635 jmvivo
                return true;
175
        }
176 6865 jaume
177 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
178 6635 jmvivo
                return selectedItems.length > 0;
179
        }
180
181
182 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
183 6635 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
184
                if (xml == null) {
185 6782 jmvivo
                        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 6635 jmvivo
                        return;
192
                }
193 6865 jaume
194 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
195
                if (data == null) {
196 6782 jmvivo
                        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 6635 jmvivo
                        return;
203
                }
204 6865 jaume
                this.utiles.putInClipboard(data);
205 6635 jmvivo
        }
206
207
        public String getText() {
208
                return PluginServices.getText(this, "copiar");
209
        }
210 6865 jaume
211 6635 jmvivo
}
212
213 6676 jmvivo
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 6635 jmvivo
        public int getOrder() {
221
                return 1;
222
        }
223 6865 jaume
224 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
225 6635 jmvivo
                return true;
226
        }
227 6865 jaume
228 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
229 6635 jmvivo
                return selectedItems.length > 0;
230
        }
231
232
233 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
234 6635 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyDocuments(selectedItems);
235
                if (xml == null) {
236 6782 jmvivo
                        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 6635 jmvivo
                        return;
243
                }
244 6865 jaume
245 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
246
                if (data == null) {
247 6782 jmvivo
                        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 6635 jmvivo
                        return;
254 6865 jaume
                }
255 6635 jmvivo
                this.utiles.putInClipboard(data);
256 6865 jaume
257
258 6635 jmvivo
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_el_documento"));
259
            if (option!=JOptionPane.OK_OPTION) {
260
                    return;
261 6865 jaume
            }
262
263
264
                this.utiles.removeDocuments(selectedItems);
265
266 6635 jmvivo
        }
267
268
        public String getText() {
269
                return PluginServices.getText(this, "cortar");
270
        }
271 6865 jaume
272 6635 jmvivo
}
273
274
class PasteProjectElement extends MyDocumentAction {
275
        private String type;
276 6865 jaume
277 6676 jmvivo
        public String getDescription() {
278
                //FIXME: Falta claves
279
                //return PluginServices.getText(this,"tooltip_pegar_desde_el_portapapeles");
280
                return null;
281
        }
282
283 6635 jmvivo
        public int getOrder() {
284
                return 2;
285
        }
286 6865 jaume
287 6635 jmvivo
        public void setType(String type) {
288
                this.type = type;
289
        }
290 6865 jaume
291 6635 jmvivo
        public String getType(String type) {
292
                return this.type;
293
        }
294 6865 jaume
295 6676 jmvivo
        public boolean isVisible(ProjectElement item, ProjectElement[] selectedItems) {
296 6635 jmvivo
                return true;
297
        }
298 6865 jaume
299 6676 jmvivo
        public boolean isEnabled(ProjectElement item, ProjectElement[] selectedItems) {
300 6635 jmvivo
                String sourceString = this.utiles.getFromClipboard();
301
                if (sourceString == null) return false;
302 6865 jaume
303 6635 jmvivo
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
304
                if (xml == null) return false;
305 6865 jaume
306 6635 jmvivo
                if (!this.utiles.checkXMLRootNode(xml)) return false;
307 6865 jaume
308 6635 jmvivo
                if (this.utiles.getXMLEntityChildOfType(xml,this.type) == null) return false;
309
                return true;
310
        }
311
312
313 6676 jmvivo
        public void execute(ProjectElement item, ProjectElement[] selectedItems) {
314 6635 jmvivo
                String sourceString = this.utiles.getFromClipboard();
315
                if (sourceString == null) return;
316 6865 jaume
317 6635 jmvivo
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
318
                if (xml == null) return;
319 6865 jaume
320 6635 jmvivo
                if (!this.utiles.checkXMLRootNode(xml)) return;
321 6865 jaume
322 6635 jmvivo
                if (this.type.equals("views")) {
323
                        this.utiles.loadViewsFromXML(xml);
324
                } else if (this.type.equals("tables")) {
325 6865 jaume
                        this.utiles.loadTablesFromXML(xml);
326 6635 jmvivo
                } else if (this.type.equals("maps")) {
327
                        this.utiles.loadMapsFromXML(xml);
328
                } else {
329
                        //TODO que hacer aqui??
330
                        return;
331
                }
332 6865 jaume
333 6635 jmvivo
        }
334
335
        public String getText() {
336
                return PluginServices.getText(this, "pegar");
337
        }
338 6865 jaume
339 6635 jmvivo
}
340
341
342
343 6865 jaume
abstract class  MyTocMenuEntry extends AbstractTocContextMenuAction {
344 6635 jmvivo
        protected Utiles utiles;
345 6865 jaume
346 6635 jmvivo
        public void setUtiles(Utiles utiles) {
347
                this.utiles = utiles;
348
        }
349 6676 jmvivo
350
        public String getGroup() {
351
                return "copyPasteLayer";
352
        }
353
354
        public int getGroupOrder() {
355
                return 60;
356
        }
357 6865 jaume
358 6635 jmvivo
}
359
360 6676 jmvivo
class CopyTocMenuEntry extends MyTocMenuEntry{
361
        public int getOrder() {
362
                return 0;
363
        }
364 6865 jaume
365 6676 jmvivo
        public String getText() {
366 6865 jaume
                return PluginServices.getText(this, "copiar");
367 6676 jmvivo
        }
368 6865 jaume
369 6676 jmvivo
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
370 7824 jmvivo
                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 6676 jmvivo
        }
381 6635 jmvivo
382
383 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
384 6676 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
385 6635 jmvivo
                if (xml == null) {
386 6782 jmvivo
                        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 6635 jmvivo
                        return;
393
                }
394 6865 jaume
395 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
396
                if (data == null) {
397 6782 jmvivo
                        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 6635 jmvivo
                        return;
404
                }
405 6865 jaume
406
                this.utiles.putInClipboard(data);
407
408 6635 jmvivo
        }
409
410
411 6676 jmvivo
}
412 6635 jmvivo
413 6676 jmvivo
class CutTocMenuEntry extends MyTocMenuEntry{
414
        public int getOrder() {
415
                return 1;
416 6635 jmvivo
        }
417 6865 jaume
418 6676 jmvivo
        public String getText() {
419 6865 jaume
                return PluginServices.getText(this, "cortar");
420 6676 jmvivo
        }
421 6865 jaume
422 6676 jmvivo
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
423 7824 jmvivo
                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 6676 jmvivo
        }
434 6635 jmvivo
435 6676 jmvivo
436 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
437 6676 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
438 6635 jmvivo
                if (xml == null) {
439 6782 jmvivo
                        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 6635 jmvivo
                        return;
446
                }
447 6865 jaume
448 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
449
                if (data == null) {
450 6782 jmvivo
                        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 6635 jmvivo
                        return;
457
                }
458
459 6865 jaume
460 6635 jmvivo
                this.utiles.putInClipboard(data);
461 6865 jaume
462
463 6635 jmvivo
            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 6865 jaume
469
470
                boolean isOK =this.utiles.removeLayers(selectedItems);
471
472 6635 jmvivo
                getMapContext().endAtomicEvent();
473 6865 jaume
474 6635 jmvivo
                if (isOK) {
475
                        getMapContext().invalidate();
476
                        if (getMapContext().getLayers().getLayersCount()==0) {
477
                                PluginServices.getMainFrame().enableControls();
478
                        }
479
                }
480
481
        }
482
}
483
484
485 6676 jmvivo
class PasteTocMenuEntry extends MyTocMenuEntry{
486 6635 jmvivo
        private XMLEntity xml=null;
487 6865 jaume
488 6676 jmvivo
        public int getOrder() {
489
                return 2;
490
        }
491 6865 jaume
492 6676 jmvivo
        public String getText() {
493 6865 jaume
                return PluginServices.getText(this, "pegar");
494 6676 jmvivo
        }
495 6865 jaume
496 6676 jmvivo
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
497
                if (isTocItemBranch(item)) {
498
                        FLayer lyr = getNodeLayer(item);
499 6635 jmvivo
                        if (lyr instanceof FLayers) {
500
                                this.xml = this.getCheckedXMLFromClipboard();
501 6865 jaume
                                return true;
502 6635 jmvivo
                        }
503 6865 jaume
504 6676 jmvivo
                } else if (!isTocItemLeaf(item)) {
505
                        if (getNodeLayer(item) == null) {
506 6635 jmvivo
                                this.xml = this.getCheckedXMLFromClipboard();
507 6676 jmvivo
                                return this.xml != null;
508 6865 jaume
                        }
509
                }
510 6676 jmvivo
                return false;
511 6635 jmvivo
        }
512 6865 jaume
513 6635 jmvivo
        private XMLEntity getCheckedXMLFromClipboard() {
514
                String sourceString = this.utiles.getFromClipboard();
515
                if (sourceString == null) return null;
516 6865 jaume
517 6635 jmvivo
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
518
                if (xml == null) return null;
519 6865 jaume
520 6635 jmvivo
                if (!this.utiles.checkXMLRootNode(xml)) return null;
521 6865 jaume
522 6635 jmvivo
                if (this.utiles.getXMLEntityChildOfType(xml,"layers") == null) return null;
523 6865 jaume
524 6635 jmvivo
                return  xml;
525
        }
526
527 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
528 6635 jmvivo
                FLayers root;
529 6865 jaume
530 6635 jmvivo
                if (this.xml == null) return;
531 6865 jaume
532 6676 jmvivo
                if (isTocItemBranch(item)) {
533
                        root = (FLayers)getNodeLayer(item);
534
                } else if (getNodeLayer(item) == null){
535 6635 jmvivo
                        root = getMapContext().getLayers();
536
                } else {
537
                        return;
538
                }
539
                getMapContext().beginAtomicEvent();
540 6865 jaume
541 6635 jmvivo
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
542 6865 jaume
543 6635 jmvivo
                getMapContext().endAtomicEvent();
544 6865 jaume
545 6635 jmvivo
                if (isOK) getMapContext().invalidate();
546
        }
547 6865 jaume
548 6635 jmvivo
}
549
550
551
class Utiles {
552 6865 jaume
553 6635 jmvivo
        /*
554
         *
555
======================================
556
 Comportamiento del Pegar documentos:
557
======================================
558 6865 jaume

559 6635 jmvivo
?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 6865 jaume
        Si alguna de las tablas a pegar de las que van en
566 6635 jmvivo
        el portapapeles ya existen en el proyecto.
567
                1. abortamos
568
                2. Informamos al usuario y no se pegan las tablas.
569 6865 jaume

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

574 6635 jmvivo
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 6865 jaume
        Si alguna de las tablas a pegar de las que van en
583
                el portapapeles ya existen en el proyecto.
584 6635 jmvivo
                        1. abortamos
585
                        2. Informamos al usuario y no se pegan las tablas.
586 6865 jaume

587

588 6635 jmvivo
         */
589 6865 jaume
590
591 6635 jmvivo
        /*
592 6865 jaume
         *
593
         *
594
         *
595 6635 jmvivo
         * Funciones Publicas para generar XML (copiar)
596 6865 jaume
         *
597
         *
598 6635 jmvivo
        */
599 6865 jaume
600 6635 jmvivo
        /**
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 6865 jaume
608 6635 jmvivo
                if (selectedItems[0] instanceof ProjectView) {
609
                        ProjectView[] views = new ProjectView[selectedItems.length];
610
                        System.arraycopy(selectedItems,0,views,0,selectedItems.length);
611 6865 jaume
                        return this.generateXMLCopyViews(views);
612 6635 jmvivo
                } 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 6865 jaume
                        return this.generateXMLCopyTables(tables);
620 6635 jmvivo
                } else {
621
                        //FIXME:????
622
                        return null;
623
                }
624
        }
625 6865 jaume
626 6635 jmvivo
        public XMLEntity generateXMLCopyViews(ProjectView[] selectedItems) {
627
                XMLEntity xml = this.newRootNode();
628 6865 jaume
629 6635 jmvivo
                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 6865 jaume
636 6635 jmvivo
                }
637 6865 jaume
638
639
                if (xmlDataSources.getChildrenCount() > 0) {
640 6635 jmvivo
                        xml.addChild(xmlDataSources);
641 6865 jaume
                }
642
                if (xmlViews.getChildrenCount() > 0) {
643 6635 jmvivo
                        xml.addChild(xmlViews);
644
                }
645 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
646 6635 jmvivo
                        xml.addChild(xmlTables);
647
                }
648 6865 jaume
649 6635 jmvivo
                return xml;
650 6865 jaume
651 6635 jmvivo
        }
652
653 6865 jaume
654 6635 jmvivo
        public XMLEntity generateXMLCopyMaps(ProjectMap[] selectedItems) {
655
                XMLEntity xml = this.newRootNode();
656 6865 jaume
657 6635 jmvivo
                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 6865 jaume
665 6635 jmvivo
                }
666 6865 jaume
667
668
                if (xmlDataSources.getChildrenCount() > 0) {
669 6635 jmvivo
                        xml.addChild(xmlDataSources);
670 6865 jaume
                }
671
                if (xmlViews.getChildrenCount() > 0) {
672 6635 jmvivo
                        xml.addChild(xmlViews);
673
                }
674 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
675 6635 jmvivo
                        xml.addChild(xmlTables);
676
                }
677 6865 jaume
                if (xmlMaps.getChildrenCount() > 0) {
678 6635 jmvivo
                        xml.addChild(xmlMaps);
679
                }
680
681 6865 jaume
682
                return xml;
683 6635 jmvivo
        }
684 6865 jaume
685 6635 jmvivo
        public XMLEntity generateXMLCopyTables(ProjectTable[] selectedItems) {
686
                XMLEntity xml = this.newRootNode();
687 6865 jaume
688 6635 jmvivo
                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 6865 jaume
695
696
                if (xmlDataSources.getChildrenCount() > 0) {
697 6635 jmvivo
                        xml.addChild(xmlDataSources);
698 6865 jaume
                }
699
                if (xmlTables.getChildrenCount() > 0) {
700 6635 jmvivo
                        xml.addChild(xmlTables);
701
                }
702 6865 jaume
703
                return xml;
704 6635 jmvivo
        }
705
706 6865 jaume
707 6635 jmvivo
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
708 6865 jaume
709 6635 jmvivo
                XMLEntity xml = this.newRootNode();
710
                XMLEntity xmlLayers = this.newLayersNode();
711
                XMLEntity xmlTables = this.newTablesNode();
712
                XMLEntity xmlDataSources = this.newDataSourcesNode();
713 6865 jaume
714 6635 jmvivo
                for (int i=0;i < actives.length; i++) {
715
                        if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) return null;
716 6865 jaume
717 6635 jmvivo
                }
718 6865 jaume
719
                if (xmlDataSources.getChildrenCount() > 0) {
720 6635 jmvivo
                        xml.addChild(xmlDataSources);
721 6865 jaume
                }
722
                if (xmlLayers.getChildrenCount() > 0) {
723 6635 jmvivo
                        xml.addChild(xmlLayers);
724
                }
725 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
726 6635 jmvivo
                        xml.addChild(xmlTables);
727
                }
728 6865 jaume
729 6635 jmvivo
                return xml;
730 6865 jaume
731 6635 jmvivo
        }
732
733
734 6865 jaume
735 6635 jmvivo
        /*
736 6865 jaume
         *
737
         *
738
         *
739 6635 jmvivo
         * Funciones Publicas de carga de un XML (pegar)
740 6865 jaume
         *
741
         *
742
         *
743 6635 jmvivo
        */
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 6865 jaume
750 6635 jmvivo
                if (xmlLayers == null ) return false;
751 6865 jaume
752
                // Se pegan las tablas igualmente
753 6635 jmvivo
                /*
754
                Project project = this.getProject();
755 6865 jaume

756 6635 jmvivo
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
757
                */
758 6865 jaume
759
760 6635 jmvivo
                if (xmlDataSources != null)  {
761
                        if (!this.registerDataSources(xmlDataSources)) return false;
762
                }
763 6865 jaume
764 6635 jmvivo
                if (!this.addLayers(xmlLayers,root)) return false;
765 6865 jaume
766 6635 jmvivo
                if (xmlTables != null)  {
767
                        if (!this.addTables(xmlTables)) return false;
768
                }
769 6865 jaume
770 6635 jmvivo
                return true;
771 6865 jaume
772 6635 jmvivo
        }
773 6865 jaume
774
775 6635 jmvivo
        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 6865 jaume
780 6635 jmvivo
                if (xmlViews == null ) return false;
781
782
                Project project = this.getProject();
783
784
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
785 6865 jaume
786 6635 jmvivo
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
787 6865 jaume
788 6635 jmvivo
                if (viewsConflits != null && viewsConflits.size() > 0) {
789
                        int option = JOptionPane.showConfirmDialog(
790
                                        (Component)PluginServices.getMainFrame(),
791
                                        "<html>"+
792 6645 jmvivo
                                                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 6635 jmvivo
                                                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 6645 jmvivo
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
810 6635 jmvivo
                                                "</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 6865 jaume
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
825 6635 jmvivo
                                                                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 6865 jaume
                        }
834 6635 jmvivo
                        if (xmlTables != null) xmlTables.removeAllChilds();
835
                        tablesConflits = null;
836
                }
837 6865 jaume
838 6635 jmvivo
                if (tablesConflits != null && tablesConflits.size() > 0) {
839
                        int option = JOptionPane.showConfirmDialog(
840
                                        (Component)PluginServices.getMainFrame(),
841
                                        "<html>" +
842 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
843 6635 jmvivo
                                                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 6865 jaume
                        xmlTables.removeAllChilds();
853 6635 jmvivo
                }
854
855
856
                if (xmlDataSources != null)  {
857
                        if (!this.registerDataSources(xmlDataSources)) return false;
858
                }
859 6865 jaume
860 6635 jmvivo
                if (!this.addViews(xmlViews)) return false;
861 6865 jaume
862 6635 jmvivo
                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 6865 jaume
873
874 6635 jmvivo
                if (xmlTables == null ) return false;
875 6865 jaume
876 6635 jmvivo
                /*
877
                Project project = this.getProject();
878 6865 jaume

879 6635 jmvivo
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
880
                */
881
882
                if (xmlDataSources != null)  {
883
                        if (!this.registerDataSources(xmlDataSources)) return false;
884
                }
885 6865 jaume
886
887
888 6635 jmvivo
                return this.addTables(xmlTables);
889
        }
890 6865 jaume
891 6635 jmvivo
        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 6865 jaume
897 6635 jmvivo
                if (xmlMaps == null ) return false;
898 6865 jaume
899 6635 jmvivo
                Project project = this.getProject();
900
901
                Hashtable mapsConflits = this.getConflicts(xmlMaps,project.getMaps());
902 6865 jaume
903 6635 jmvivo
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
904 6865 jaume
905 6635 jmvivo
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
906 6865 jaume
907
908 6635 jmvivo
                if (mapsConflits != null && mapsConflits.size() > 0) {
909
                        int option = JOptionPane.showConfirmDialog(
910
                                        (Component)PluginServices.getMainFrame(),
911
                                        "<html>"+
912 6645 jmvivo
                                                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 6635 jmvivo
                                        "</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 6865 jaume
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
943 6635 jmvivo
                                                                PluginServices.getText(this,"nombre_no_valido")+
944 6865 jaume
                                                        "</html>",//Mensaje
945 6635 jmvivo
                                                        PluginServices.getText(this,"pegar_mapas"),//titulo
946
                                                        JOptionPane.ERROR_MESSAGE
947
                                                        );
948
                                        return false;
949
                                }
950
                                map.setName(newName);
951 6865 jaume
                        }
952 6635 jmvivo
                }
953 6865 jaume
954 6635 jmvivo
                if (viewsConflits != null && viewsConflits.size() > 0) {
955
                        int option = JOptionPane.showConfirmDialog(
956
                                        (Component)PluginServices.getMainFrame(),
957
                                        "<html>"+
958 6865 jaume
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
959 6635 jmvivo
                                                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 6645 jmvivo
                        // Eliminamos las vistas del xml que no vamos a importar
969 6865 jaume
970 6645 jmvivo
                        // 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 6865 jaume
                        viewsConflits = null;
976
977 6635 jmvivo
                }
978
979 6865 jaume
980 6635 jmvivo
                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 6865 jaume
                        xmlTables.removeAllChilds();
995 6635 jmvivo
                }
996
997
998
                if (xmlDataSources != null)  {
999
                        if (!this.registerDataSources(xmlDataSources)) return false;
1000
                }
1001 6865 jaume
1002 6635 jmvivo
                if (xmlViews != null)  {
1003
                        if (!this.addViews(xmlViews)) return false;
1004
                }
1005
1006
                if (xmlTables != null)  {
1007
                        if (!this.addTables(xmlTables)) return false;
1008 6865 jaume
                }
1009
1010 6635 jmvivo
                return this.addMaps(xmlMaps);
1011
1012
        }
1013
1014 6865 jaume
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 6645 jmvivo
        /**
1025
         * Devuelve las claves de conflits ordenados
1026
         * en orden inverso. Las claves se esperan que
1027
         * sean instancias de Integer
1028 6865 jaume
         */
1029 6645 jmvivo
        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 6865 jaume
                return indexes;
1042
1043
1044 6645 jmvivo
        }
1045 6865 jaume
1046
1047 6635 jmvivo
        private boolean addToXMLMapDependencies(ProjectMap map, XMLEntity xmlViews,XMLEntity xmlTables, XMLEntity xmlDataSources) {
1048
                IFFrame[] components = map.getModel().getFFrames();
1049 6865 jaume
                for (int i=0; i < components.length; i++) {
1050
                        if (components[i] instanceof FFrameView) {
1051 6635 jmvivo
                                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 6865 jaume
1058 6635 jmvivo
                return true;
1059
        }
1060 6865 jaume
1061 6635 jmvivo
        private boolean addToXMLMap(ProjectMap map,XMLEntity xmlMaps,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1062
                try {
1063
                        xmlMaps.addChild(map.getXMLEntity());
1064 6865 jaume
1065 6635 jmvivo
                        return this.addToXMLMapDependencies(map,xmlViews,xmlTables,xmlDataSources);
1066 6865 jaume
1067 6635 jmvivo
                } catch (SaveException e) {
1068
                        // TODO Auto-generated catch block
1069
                        e.printStackTrace();
1070
                        return false;
1071
                }
1072
        }
1073 6865 jaume
1074 6635 jmvivo
        private boolean addToXMLView(ProjectView view,XMLEntity xmlViews,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1075
                try {
1076
                        xmlViews.addChild(view.getXMLEntity());
1077 6865 jaume
1078 6635 jmvivo
                        if (!this.addToXMLLayerDependencies(view.getMapContext().getLayers(),xmlTables,xmlDataSources)) return false;
1079 6865 jaume
1080 6701 jmvivo
                        if (view.getMapOverViewContext() != null) {
1081
                                return this.addToXMLLayerDependencies(view.getMapOverViewContext().getLayers(),xmlTables,xmlDataSources);
1082
                        } else {
1083
                                return true;
1084
                        }
1085 6865 jaume
1086
1087
1088 6635 jmvivo
                } 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 6865 jaume
1101 6635 jmvivo
                if (!xml.contains("version")) return false;
1102
                if (!xml.getStringProperty("version").equalsIgnoreCase(Version.format())) return false;
1103 6865 jaume
1104 6635 jmvivo
                return true;
1105
        }
1106 6865 jaume
1107 6635 jmvivo
        private void fillXMLRootNode(XMLEntity xml) {
1108
                xml.putProperty("applicationName","gvSIG");
1109 6865 jaume
                xml.putProperty("version",Version.format());
1110 6635 jmvivo
        }
1111 6865 jaume
1112 6635 jmvivo
        public XMLEntity getXMLEntityChildOfType(XMLEntity xml,String type) {
1113 6865 jaume
                int childCount = xml.getChildrenCount();
1114 6635 jmvivo
                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 6865 jaume
1125 6635 jmvivo
        }
1126 6865 jaume
1127
        private Hashtable getConflicts(XMLEntity xml,ArrayList elements) {
1128
                if (xml == null || xml.getChildrenCount() < 1) return null;
1129 6635 jmvivo
                Hashtable conflits = new Hashtable();
1130 6865 jaume
                for (int iXML=0;iXML < xml.getChildrenCount();iXML++) {
1131 6635 jmvivo
                        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 6865 jaume
1140 6635 jmvivo
                        }
1141
                }
1142
                return conflits;
1143
        }
1144
1145
1146
1147
        private boolean registerDataSources(XMLEntity xmlDataSources) {
1148
                try {
1149 6865 jaume
                        int numDataSources = xmlDataSources.getChildrenCount();
1150
1151 6635 jmvivo
                        if (numDataSources == 0) return true;
1152
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
1153 6865 jaume
1154 6635 jmvivo
                        for (int i = 0; i < numDataSources; i++) {
1155
                                XMLEntity child = xmlDataSources.getChild(i);
1156
                                String name = child.getStringProperty("gdbmsname");
1157 6865 jaume
1158 6635 jmvivo
                                if (dsFactory.getDriverInfo(name) == null) {
1159 6865 jaume
                                        if (child.getStringProperty("type").equals("otherDriverFile")) {
1160 6635 jmvivo
                                                LayerFactory.getDataSourceFactory().addFileDataSource(
1161 6865 jaume
                                                                child.getStringProperty("driverName"),
1162 6635 jmvivo
                                                                name,
1163
                                                                child.getStringProperty("file")
1164
                                                );
1165 6865 jaume
1166
1167 6635 jmvivo
                                        } 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 6865 jaume

1173 6635 jmvivo
                                                 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 6865 jaume
1191 6635 jmvivo
                        return true;
1192
                } catch (Exception e) {
1193
                        e.printStackTrace();
1194
                        return false;
1195
                }
1196
        }
1197 6865 jaume
1198 6635 jmvivo
        private boolean addTables(XMLEntity xmlTables) {
1199
                try {
1200 6865 jaume
                        int numTables = xmlTables.getChildrenCount();
1201 6635 jmvivo
                        if (numTables == 0) return true;
1202 6865 jaume
1203 6635 jmvivo
                        Project project = this.getProject();
1204 6865 jaume
1205 6635 jmvivo
                        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 6865 jaume
1221 6635 jmvivo
                        project.setLinkTable();
1222
1223
                        return true;
1224
                } catch (Exception e) {
1225
                        e.printStackTrace();
1226 6865 jaume
                        return false;
1227 6635 jmvivo
                }
1228
        }
1229 6865 jaume
1230 6635 jmvivo
        public XMLEntity findChildInXML(XMLEntity xml,String propName,String value) {
1231 6865 jaume
                int num = xml.getChildrenCount();
1232 6635 jmvivo
                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 6865 jaume
1242 6635 jmvivo
        private boolean addLayers(XMLEntity xmlLayers,FLayers root) {
1243
                try {
1244
                        XMLEntity child;
1245 6865 jaume
                        int numLayers = xmlLayers.getChildrenCount();
1246 6635 jmvivo
                        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 6865 jaume
                        return false;
1254 6635 jmvivo
                }
1255 6865 jaume
1256 6635 jmvivo
        }
1257 6865 jaume
1258
1259 6635 jmvivo
        private boolean addViews(XMLEntity xmlViews) {
1260
                try {
1261
                        Project project = this.getProject();
1262
                        XMLEntity child;
1263 6865 jaume
                        int numLayers = xmlViews.getChildrenCount();
1264 6635 jmvivo
                        for (int i = 0; i < numLayers; i++) {
1265
                                child = xmlViews.getChild(i);
1266
1267
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(child, project);
1268 6865 jaume
                                project.addView(pv);
1269
1270 6635 jmvivo
                        }
1271
                        return true;
1272
                } catch (Exception e) {
1273
                        e.printStackTrace();
1274 6865 jaume
                        return false;
1275 6635 jmvivo
                }
1276 6865 jaume
1277 6635 jmvivo
        }
1278 6865 jaume
1279 6635 jmvivo
        private boolean addMaps(XMLEntity xmlMaps) {
1280
                try {
1281
                        Project project = this.getProject();
1282
                        XMLEntity child;
1283 6865 jaume
                        int numLayers = xmlMaps.getChildrenCount();
1284 6635 jmvivo
                        for (int i = 0; i < numLayers; i++) {
1285
                                child = xmlMaps.getChild(i);
1286
1287
                                ProjectMap pm = (ProjectMap) ProjectMap.createFromXML(child, project);
1288 6865 jaume
                                project.addMap(pm);
1289
1290 6635 jmvivo
                        }
1291
                        return true;
1292
                } catch (Exception e) {
1293
                        e.printStackTrace();
1294 6865 jaume
                        return false;
1295 6635 jmvivo
                }
1296 6865 jaume
1297 6635 jmvivo
        }
1298 6865 jaume
1299 6635 jmvivo
        private Project getProject() {
1300
                 return ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
1301
        }
1302 6865 jaume
1303 6635 jmvivo
        private boolean addToXMLDataSource(SourceInfo source,XMLEntity xmlDataSources, Project project) {
1304
                if (project== null) {
1305
                        project = this.getProject();
1306 6865 jaume
                }
1307 6635 jmvivo
                    xmlDataSources.addChild(project.getSourceInfoXMLEntity(source));
1308 6865 jaume
1309 6635 jmvivo
                    return true;
1310
        }
1311 6865 jaume
1312
        private boolean addToXMLTable(ProjectTable pt,XMLEntity xmlTables,XMLEntity xmlDataSources,Project project) {
1313 6635 jmvivo
                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 6865 jaume
1321 6635 jmvivo
                        xmlTables.addChild(xmlTable);
1322 6865 jaume
1323 6701 jmvivo
                        if (pt.getAssociatedTable() != null) {
1324
                                this.addToXMLDataSource(pt.getAssociatedTable().getRecordset().getSourceInfo(),xmlDataSources,project);
1325
                        }
1326 6865 jaume
1327 6635 jmvivo
                        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 6865 jaume
                }
1342
1343
                    return true;
1344 6635 jmvivo
        }
1345 6865 jaume
1346 6635 jmvivo
        private boolean addToXMLLayerDependencies(FLayer lyr,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1347
                try {
1348
                        Project project = this.getProject();
1349 6865 jaume
1350 6635 jmvivo
                        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 6865 jaume
                    } else if (lyr instanceof AlphanumericData){
1359 6635 jmvivo
                    if (!this.addToXMLDataSource(
1360
                                ((AlphanumericData)lyr).getRecordset().getSourceInfo(),
1361
                                xmlDataSources,
1362
                                project
1363 6865 jaume
1364 6635 jmvivo
                    )) return false;
1365 6865 jaume
1366 6635 jmvivo
                ProjectTable pt = project.getTable((AlphanumericData) lyr);
1367 6865 jaume
                if (pt != null) {
1368 6635 jmvivo
                        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 6778 jmvivo
                } catch (Exception e) {
1378
                        e.printStackTrace();
1379 6865 jaume
                        return false;
1380
1381 6635 jmvivo
                }
1382
                return true;
1383 6865 jaume
1384 6635 jmvivo
        }
1385 6865 jaume
1386
        private boolean addToXMLLayer(FLayer lyr,XMLEntity xmlLayers,XMLEntity xmlTables,XMLEntity xmlDataSources) {
1387 6635 jmvivo
                try {
1388
                        xmlLayers.addChild(lyr.getXMLEntity());
1389 6865 jaume
1390 6635 jmvivo
                        return this.addToXMLLayerDependencies(lyr,xmlTables,xmlDataSources);
1391
1392
                } catch (XMLException e) {
1393
                        e.printStackTrace();
1394
                        return false;
1395 6778 jmvivo
                } catch (Exception e) {
1396
                        e.printStackTrace();
1397 6865 jaume
                        return false;
1398 6635 jmvivo
                }
1399
        }
1400 6865 jaume
1401 6635 jmvivo
        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 6865 jaume
1425 6635 jmvivo
        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 6865 jaume
1441
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,ss);
1442 6635 jmvivo
        }
1443 6865 jaume
1444 6635 jmvivo
        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 6865 jaume
1470 6635 jmvivo
                return buffer.toString();
1471 6865 jaume
1472 6635 jmvivo
        }
1473 6865 jaume
1474 6635 jmvivo
        public XMLEntity unMarshallXMLEntity(String data) {
1475
                StringReader reader = new StringReader(data);
1476 6865 jaume
1477 6635 jmvivo
                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 6865 jaume
1487 6635 jmvivo
                return xml;
1488
        }
1489
1490 6865 jaume
        public String getFromClipboard() {
1491
1492 6635 jmvivo
                 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 6865 jaume
                }
1500 6635 jmvivo
        }
1501 6865 jaume
1502 6635 jmvivo
        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 6880 cesar
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
1523 6635 jmvivo
                }
1524
1525
1526
                    } catch (CancelationException e1) {
1527
                            e1.printStackTrace();
1528
                            return false;
1529
                    }
1530
            }
1531
                return true;
1532
        }
1533 6865 jaume
1534 6701 jmvivo
        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 6865 jaume
1540 6701 jmvivo
                        element = selectedItems[i];
1541 6865 jaume
1542 6701 jmvivo
                        if (element instanceof ProjectMap) {
1543 6865 jaume
1544 6701 jmvivo
                                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 6880 cesar
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1552 6701 jmvivo
                                        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 6635 jmvivo
1561 6701 jmvivo
                                        //return false;
1562
                                } else {
1563 6880 cesar
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1564 6701 jmvivo
                                        p.delTable(p.getTables().indexOf(element));
1565 6865 jaume
                                }
1566 6701 jmvivo
                        } 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 6880 cesar
                                        PluginServices.getMDIManager().closeSingletonWindow(element);
1575 6701 jmvivo
                                        p.delView(p.getViews().indexOf(element));
1576
                                }
1577
                        }
1578 6865 jaume
                }
1579 6701 jmvivo
                return true;
1580
        }
1581
1582 6635 jmvivo
}