Statistics
| Revision:

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

History | View | Annotate | Download (42.9 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 6865 jaume
                return selectedItems.length >= 1 && isTocItemBranch(item);
371 6676 jmvivo
        }
372 6635 jmvivo
373
374 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
375 6676 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
376 6635 jmvivo
                if (xml == null) {
377 6782 jmvivo
                        JOptionPane.showMessageDialog(
378
                                        (Component)PluginServices.getMainFrame(),
379
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
380
                                        PluginServices.getText(this,"copiar"),//titulo
381
                                        JOptionPane.ERROR_MESSAGE
382
                                        );
383 6635 jmvivo
                        return;
384
                }
385 6865 jaume
386 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
387
                if (data == null) {
388 6782 jmvivo
                        JOptionPane.showMessageDialog(
389
                                        (Component)PluginServices.getMainFrame(),
390
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
391
                                        PluginServices.getText(this,"copiar"),//titulo
392
                                        JOptionPane.ERROR_MESSAGE
393
                                        );
394 6635 jmvivo
                        return;
395
                }
396 6865 jaume
397
                this.utiles.putInClipboard(data);
398
399 6635 jmvivo
        }
400
401
402 6676 jmvivo
}
403 6635 jmvivo
404 6676 jmvivo
class CutTocMenuEntry extends MyTocMenuEntry{
405
        public int getOrder() {
406
                return 1;
407 6635 jmvivo
        }
408 6865 jaume
409 6676 jmvivo
        public String getText() {
410 6865 jaume
                return PluginServices.getText(this, "cortar");
411 6676 jmvivo
        }
412 6865 jaume
413 6676 jmvivo
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
414 6865 jaume
                return selectedItems.length >= 1 && isTocItemBranch(item);
415 6676 jmvivo
        }
416 6635 jmvivo
417 6676 jmvivo
418 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
419 6676 jmvivo
                XMLEntity xml = this.utiles.generateXMLCopyLayers(selectedItems);
420 6635 jmvivo
                if (xml == null) {
421 6782 jmvivo
                        JOptionPane.showMessageDialog(
422
                                        (Component)PluginServices.getMainFrame(),
423
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
424
                                        PluginServices.getText(this,"cortar"),//titulo
425
                                        JOptionPane.ERROR_MESSAGE
426
                                        );
427 6635 jmvivo
                        return;
428
                }
429 6865 jaume
430 6635 jmvivo
                String data = this.utiles.marshallXMLEntity(xml);
431
                if (data == null) {
432 6782 jmvivo
                        JOptionPane.showMessageDialog(
433
                                        (Component)PluginServices.getMainFrame(),
434
                                        "<html>"+PluginServices.getText(this,"No_ha_sido_posible_realizar_la_operacion")+"</html>",//Mensaje
435
                                        PluginServices.getText(this,"cortar"),//titulo
436
                                        JOptionPane.ERROR_MESSAGE
437
                                        );
438 6635 jmvivo
                        return;
439
                }
440
441 6865 jaume
442 6635 jmvivo
                this.utiles.putInClipboard(data);
443 6865 jaume
444
445 6635 jmvivo
            int option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
446
            if (option!=JOptionPane.OK_OPTION) {
447
                    return;
448
            }
449
                getMapContext().beginAtomicEvent();
450 6865 jaume
451
452
                boolean isOK =this.utiles.removeLayers(selectedItems);
453
454 6635 jmvivo
                getMapContext().endAtomicEvent();
455 6865 jaume
456 6635 jmvivo
                if (isOK) {
457
                        getMapContext().invalidate();
458
                        if (getMapContext().getLayers().getLayersCount()==0) {
459
                                PluginServices.getMainFrame().enableControls();
460
                        }
461
                }
462
463
        }
464
}
465
466
467 6676 jmvivo
class PasteTocMenuEntry extends MyTocMenuEntry{
468 6635 jmvivo
        private XMLEntity xml=null;
469 6865 jaume
470 6676 jmvivo
        public int getOrder() {
471
                return 2;
472
        }
473 6865 jaume
474 6676 jmvivo
        public String getText() {
475 6865 jaume
                return PluginServices.getText(this, "pegar");
476 6676 jmvivo
        }
477 6865 jaume
478 6676 jmvivo
        public boolean isEnabled(ITocItem item, FLayer[] selectedItems) {
479
                if (isTocItemBranch(item)) {
480
                        FLayer lyr = getNodeLayer(item);
481 6635 jmvivo
                        if (lyr instanceof FLayers) {
482
                                this.xml = this.getCheckedXMLFromClipboard();
483 6865 jaume
                                return true;
484 6635 jmvivo
                        }
485 6865 jaume
486 6676 jmvivo
                } else if (!isTocItemLeaf(item)) {
487
                        if (getNodeLayer(item) == null) {
488 6635 jmvivo
                                this.xml = this.getCheckedXMLFromClipboard();
489 6676 jmvivo
                                return this.xml != null;
490 6865 jaume
                        }
491
                }
492 6676 jmvivo
                return false;
493 6635 jmvivo
        }
494 6865 jaume
495 6635 jmvivo
        private XMLEntity getCheckedXMLFromClipboard() {
496
                String sourceString = this.utiles.getFromClipboard();
497
                if (sourceString == null) return null;
498 6865 jaume
499 6635 jmvivo
                XMLEntity xml = this.utiles.unMarshallXMLEntity(sourceString);
500
                if (xml == null) return null;
501 6865 jaume
502 6635 jmvivo
                if (!this.utiles.checkXMLRootNode(xml)) return null;
503 6865 jaume
504 6635 jmvivo
                if (this.utiles.getXMLEntityChildOfType(xml,"layers") == null) return null;
505 6865 jaume
506 6635 jmvivo
                return  xml;
507
        }
508
509 6865 jaume
        public void execute(ITocItem item, FLayer[] selectedItems) {
510 6635 jmvivo
                FLayers root;
511 6865 jaume
512 6635 jmvivo
                if (this.xml == null) return;
513 6865 jaume
514 6676 jmvivo
                if (isTocItemBranch(item)) {
515
                        root = (FLayers)getNodeLayer(item);
516
                } else if (getNodeLayer(item) == null){
517 6635 jmvivo
                        root = getMapContext().getLayers();
518
                } else {
519
                        return;
520
                }
521
                getMapContext().beginAtomicEvent();
522 6865 jaume
523 6635 jmvivo
                boolean isOK = this.utiles.loadLayersFromXML(this.xml,root);
524 6865 jaume
525 6635 jmvivo
                getMapContext().endAtomicEvent();
526 6865 jaume
527 6635 jmvivo
                if (isOK) getMapContext().invalidate();
528
        }
529 6865 jaume
530 6635 jmvivo
}
531
532
533
class Utiles {
534 6865 jaume
535 6635 jmvivo
        /*
536
         *
537
======================================
538
 Comportamiento del Pegar documentos:
539
======================================
540 6865 jaume

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

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

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

569

570 6635 jmvivo
         */
571 6865 jaume
572
573 6635 jmvivo
        /*
574 6865 jaume
         *
575
         *
576
         *
577 6635 jmvivo
         * Funciones Publicas para generar XML (copiar)
578 6865 jaume
         *
579
         *
580 6635 jmvivo
        */
581 6865 jaume
582 6635 jmvivo
        /**
583
         * Genera un XMLEntity con la informacion necesaria
584
         * para copiar los elementos de selectedItems en
585
         * otro proyecto
586
         */
587
        public XMLEntity generateXMLCopyDocuments(ProjectElement[] selectedItems) {
588
                if (selectedItems.length == 0) return null;
589 6865 jaume
590 6635 jmvivo
                if (selectedItems[0] instanceof ProjectView) {
591
                        ProjectView[] views = new ProjectView[selectedItems.length];
592
                        System.arraycopy(selectedItems,0,views,0,selectedItems.length);
593 6865 jaume
                        return this.generateXMLCopyViews(views);
594 6635 jmvivo
                } else if (selectedItems[0] instanceof ProjectMap) {
595
                        ProjectMap[] maps = new ProjectMap[selectedItems.length];
596
                        System.arraycopy(selectedItems,0,maps,0,selectedItems.length);
597
                        return this.generateXMLCopyMaps(maps);
598
                } else if (selectedItems[0] instanceof ProjectTable) {
599
                        ProjectTable[] tables = new ProjectTable[selectedItems.length];
600
                        System.arraycopy(selectedItems,0,tables,0,selectedItems.length);
601 6865 jaume
                        return this.generateXMLCopyTables(tables);
602 6635 jmvivo
                } else {
603
                        //FIXME:????
604
                        return null;
605
                }
606
        }
607 6865 jaume
608 6635 jmvivo
        public XMLEntity generateXMLCopyViews(ProjectView[] selectedItems) {
609
                XMLEntity xml = this.newRootNode();
610 6865 jaume
611 6635 jmvivo
                XMLEntity xmlTables = this.newTablesNode();
612
                XMLEntity xmlDataSources = this.newDataSourcesNode();
613
                XMLEntity xmlViews = this.newViewsNode();
614
615
                for (int i=0;i < selectedItems.length; i++) {
616
                        if (!this.addToXMLView(selectedItems[i],xmlViews,xmlTables,xmlDataSources)) return null;
617 6865 jaume
618 6635 jmvivo
                }
619 6865 jaume
620
621
                if (xmlDataSources.getChildrenCount() > 0) {
622 6635 jmvivo
                        xml.addChild(xmlDataSources);
623 6865 jaume
                }
624
                if (xmlViews.getChildrenCount() > 0) {
625 6635 jmvivo
                        xml.addChild(xmlViews);
626
                }
627 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
628 6635 jmvivo
                        xml.addChild(xmlTables);
629
                }
630 6865 jaume
631 6635 jmvivo
                return xml;
632 6865 jaume
633 6635 jmvivo
        }
634
635 6865 jaume
636 6635 jmvivo
        public XMLEntity generateXMLCopyMaps(ProjectMap[] selectedItems) {
637
                XMLEntity xml = this.newRootNode();
638 6865 jaume
639 6635 jmvivo
                XMLEntity xmlTables = this.newTablesNode();
640
                XMLEntity xmlDataSources = this.newDataSourcesNode();
641
                XMLEntity xmlViews = this.newViewsNode();
642
                XMLEntity xmlMaps = this.newMapsNode();
643
644
                for (int i=0;i < selectedItems.length; i++) {
645
                        if (!this.addToXMLMap(selectedItems[i],xmlMaps,xmlViews,xmlTables,xmlDataSources)) return null;
646 6865 jaume
647 6635 jmvivo
                }
648 6865 jaume
649
650
                if (xmlDataSources.getChildrenCount() > 0) {
651 6635 jmvivo
                        xml.addChild(xmlDataSources);
652 6865 jaume
                }
653
                if (xmlViews.getChildrenCount() > 0) {
654 6635 jmvivo
                        xml.addChild(xmlViews);
655
                }
656 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
657 6635 jmvivo
                        xml.addChild(xmlTables);
658
                }
659 6865 jaume
                if (xmlMaps.getChildrenCount() > 0) {
660 6635 jmvivo
                        xml.addChild(xmlMaps);
661
                }
662
663 6865 jaume
664
                return xml;
665 6635 jmvivo
        }
666 6865 jaume
667 6635 jmvivo
        public XMLEntity generateXMLCopyTables(ProjectTable[] selectedItems) {
668
                XMLEntity xml = this.newRootNode();
669 6865 jaume
670 6635 jmvivo
                XMLEntity xmlTables = this.newTablesNode();
671
                XMLEntity xmlDataSources = this.newDataSourcesNode();
672
673
                for (int i=0;i < selectedItems.length; i++) {
674
                        if (!this.addToXMLTable(selectedItems[i],xmlTables,xmlDataSources,null)) return null;
675
                }
676 6865 jaume
677
678
                if (xmlDataSources.getChildrenCount() > 0) {
679 6635 jmvivo
                        xml.addChild(xmlDataSources);
680 6865 jaume
                }
681
                if (xmlTables.getChildrenCount() > 0) {
682 6635 jmvivo
                        xml.addChild(xmlTables);
683
                }
684 6865 jaume
685
                return xml;
686 6635 jmvivo
        }
687
688 6865 jaume
689 6635 jmvivo
        public XMLEntity generateXMLCopyLayers(FLayer[] actives) {
690 6865 jaume
691 6635 jmvivo
                XMLEntity xml = this.newRootNode();
692
                XMLEntity xmlLayers = this.newLayersNode();
693
                XMLEntity xmlTables = this.newTablesNode();
694
                XMLEntity xmlDataSources = this.newDataSourcesNode();
695 6865 jaume
696 6635 jmvivo
                for (int i=0;i < actives.length; i++) {
697
                        if (!this.addToXMLLayer(actives[i],xmlLayers ,xmlTables,xmlDataSources)) return null;
698 6865 jaume
699 6635 jmvivo
                }
700 6865 jaume
701
                if (xmlDataSources.getChildrenCount() > 0) {
702 6635 jmvivo
                        xml.addChild(xmlDataSources);
703 6865 jaume
                }
704
                if (xmlLayers.getChildrenCount() > 0) {
705 6635 jmvivo
                        xml.addChild(xmlLayers);
706
                }
707 6865 jaume
                if (xmlTables.getChildrenCount() > 0) {
708 6635 jmvivo
                        xml.addChild(xmlTables);
709
                }
710 6865 jaume
711 6635 jmvivo
                return xml;
712 6865 jaume
713 6635 jmvivo
        }
714
715
716 6865 jaume
717 6635 jmvivo
        /*
718 6865 jaume
         *
719
         *
720
         *
721 6635 jmvivo
         * Funciones Publicas de carga de un XML (pegar)
722 6865 jaume
         *
723
         *
724
         *
725 6635 jmvivo
        */
726
727
        public boolean loadLayersFromXML(XMLEntity xml, FLayers root) {
728
                XMLEntity xmlLayers = this.getXMLEntityChildOfType(xml,"layers");
729
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
730
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
731 6865 jaume
732 6635 jmvivo
                if (xmlLayers == null ) return false;
733 6865 jaume
734
                // Se pegan las tablas igualmente
735 6635 jmvivo
                /*
736
                Project project = this.getProject();
737 6865 jaume

738 6635 jmvivo
                Hashtable tablesConfits = this.getConflicts(xmlTables,project.getTables());
739
                */
740 6865 jaume
741
742 6635 jmvivo
                if (xmlDataSources != null)  {
743
                        if (!this.registerDataSources(xmlDataSources)) return false;
744
                }
745 6865 jaume
746 6635 jmvivo
                if (!this.addLayers(xmlLayers,root)) return false;
747 6865 jaume
748 6635 jmvivo
                if (xmlTables != null)  {
749
                        if (!this.addTables(xmlTables)) return false;
750
                }
751 6865 jaume
752 6635 jmvivo
                return true;
753 6865 jaume
754 6635 jmvivo
        }
755 6865 jaume
756
757 6635 jmvivo
        public boolean loadViewsFromXML(XMLEntity xml) {
758
                XMLEntity xmlViews = this.getXMLEntityChildOfType(xml,"views");
759
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
760
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
761 6865 jaume
762 6635 jmvivo
                if (xmlViews == null ) return false;
763
764
                Project project = this.getProject();
765
766
                Hashtable viewsConflits = this.getConflicts(xmlViews,project.getViews());
767 6865 jaume
768 6635 jmvivo
                Hashtable tablesConflits = this.getConflicts(xmlTables,project.getTables());
769 6865 jaume
770 6635 jmvivo
                if (viewsConflits != null && viewsConflits.size() > 0) {
771
                        int option = JOptionPane.showConfirmDialog(
772
                                        (Component)PluginServices.getMainFrame(),
773
                                        "<html>"+
774 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_vistas_al_pegar") + "<br>" +
775
                                                PluginServices.getText(this,"debera_introducir_nombres_para_las_vistas_a_pegar") + "<br>" +
776 6635 jmvivo
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
777
                                                PluginServices.getText(this,"desea_continuar") +
778
                                        "</html>",
779
                                        PluginServices.getText(this,"pegar_vistas"),
780
                                        JOptionPane.YES_NO_OPTION
781
                                        );
782
                        if (option != JOptionPane.YES_OPTION) {
783
                                return false;
784
                        }
785
                        Enumeration en = viewsConflits.elements();
786
                        while (en.hasMoreElements()) {
787
                                XMLEntity view = (XMLEntity)en.nextElement();
788
                                String newName = JOptionPane.showInputDialog(
789
                                                (Component)PluginServices.getMainFrame(),
790
                                                "<html>"+
791 6645 jmvivo
                                                        PluginServices.getText(this,"introduzca_nuevo_nombre_para_la_vista") +" "+  view.getStringProperty("name") + ":" +
792 6635 jmvivo
                                                "</html>", //Mensaje
793
                                                view.getStringProperty("name") //Valor por defecto
794
                                                );
795
                                if (newName == null) {
796
                                        JOptionPane.showMessageDialog(
797
                                                        (Component)PluginServices.getMainFrame(),
798
                                                        "<html>"+PluginServices.getText(this,"operacion_cancelada")+"</html>",//Mensaje
799
                                                        PluginServices.getText(this,"pegar_vistas"),//titulo
800
                                                        JOptionPane.ERROR_MESSAGE
801
                                                        );
802
                                } else if (newName.equalsIgnoreCase(view.getStringProperty("name")) ) {
803
                                        JOptionPane.showMessageDialog(
804
                                                        (Component)PluginServices.getMainFrame(),
805
                                                        "<html>"+
806 6865 jaume
                                                                PluginServices.getText(this,"operacion_cancelada") +":<br>" +
807 6635 jmvivo
                                                                PluginServices.getText(this,"nombre_no_valido")+
808
                                                        "</html>",//Mensaje
809
                                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
810
                                                        JOptionPane.ERROR_MESSAGE
811
                                                        );
812
                                        return false;
813
                                }
814
                                view.setName(newName);
815 6865 jaume
                        }
816 6635 jmvivo
                        if (xmlTables != null) xmlTables.removeAllChilds();
817
                        tablesConflits = null;
818
                }
819 6865 jaume
820 6635 jmvivo
                if (tablesConflits != null && tablesConflits.size() > 0) {
821
                        int option = JOptionPane.showConfirmDialog(
822
                                        (Component)PluginServices.getMainFrame(),
823
                                        "<html>" +
824 6645 jmvivo
                                                PluginServices.getText(this,"conflicto_de_nombres_de_tablas_al_pegar") + "<br>" +
825 6635 jmvivo
                                                PluginServices.getText(this,"no_se_pegaran_las_tablas") + "<br>" +
826
                                                PluginServices.getText(this,"desea_continuar") +
827
                                        "</html>", //Mensaje
828
                                        PluginServices.getText(this,"pegar_vistas"),//FIXME: getText
829
                                        JOptionPane.YES_NO_OPTION
830
                                        );
831
                        if (option != JOptionPane.YES_OPTION) {
832
                                return false;
833
                        }
834 6865 jaume
                        xmlTables.removeAllChilds();
835 6635 jmvivo
                }
836
837
838
                if (xmlDataSources != null)  {
839
                        if (!this.registerDataSources(xmlDataSources)) return false;
840
                }
841 6865 jaume
842 6635 jmvivo
                if (!this.addViews(xmlViews)) return false;
843 6865 jaume
844 6635 jmvivo
                if (xmlTables != null)  {
845
                        if (!this.addTables(xmlTables)) return false;
846
                }
847
848
                return true;
849
        }
850
851
        public boolean loadTablesFromXML(XMLEntity xml) {
852
                XMLEntity xmlTables = this.getXMLEntityChildOfType(xml,"tables");
853
                XMLEntity xmlDataSources = this.getXMLEntityChildOfType(xml,"dataSources");
854 6865 jaume
855
856 6635 jmvivo
                if (xmlTables == null ) return false;
857 6865 jaume
858 6635 jmvivo
                /*
859
                Project project = this.getProject();
860 6865 jaume

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

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