Statistics
| Revision:

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

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

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

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

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

568

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

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

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

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