Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / CopyPasteExtension.java @ 23176

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 21911 jmvivo
import org.gvsig.fmap.data.ReadException;
25 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.CancelationException;
26
import org.gvsig.fmap.mapcontext.layers.FLayer;
27
import org.gvsig.fmap.mapcontext.layers.FLayers;
28
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
29 21893 vcaballero
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
30 6635 jmvivo
31
import com.iver.andami.PluginServices;
32
import com.iver.andami.plugins.Extension;
33 9532 caballero
import com.iver.andami.ui.mdiManager.IWindow;
34 6635 jmvivo
import com.iver.cit.gvsig.project.Project;
35 7304 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
36 7742 jmvivo
import com.iver.cit.gvsig.project.documents.contextMenu.AbstractDocumentContextMenuAction;
37 7304 caballero
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
38
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
39
import com.iver.cit.gvsig.project.documents.layout.ProjectMap;
40 7379 caballero
import com.iver.cit.gvsig.project.documents.layout.ProjectMapFactory;
41 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameView;
42 7304 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
43
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
44 7379 caballero
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
45 7304 caballero
import com.iver.cit.gvsig.project.documents.view.ProjectView;
46 7379 caballero
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
47 9532 caballero
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
48 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
49
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
50 6635 jmvivo
import com.iver.utiles.XMLEntity;
51 23176 vcaballero
import com.iver.utiles.XMLException;
52 6635 jmvivo
import com.iver.utiles.extensionPoints.IExtensionBuilder;
53
import com.iver.utiles.xmlEntity.generate.XmlTag;
54
55
public class CopyPasteExtension extends Extension {
56
57
        public void initialize() {
58 7742 jmvivo
                /*
59 6676 jmvivo
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
60 6865 jaume

61

62

63 6635 jmvivo
                // TOC
64 6676 jmvivo
                MyTocMenuEntry copy = new CopyTocMenuEntry();
65
                MyTocMenuEntry cut = new CutTocMenuEntry();
66
                MyTocMenuEntry paste = new PasteTocMenuEntry();
67 6635 jmvivo
                Utiles utiles = new Utiles();
68
                copy.setUtiles(utiles);
69
                cut.setUtiles(utiles);
70
                paste.setUtiles(utiles);
71 6676 jmvivo

72
                extensionPoints.add("View_TocActions","Copy","Copy selectes layers to system clipboard",copy);
73
                extensionPoints.add("View_TocActions","Cut","Cut selectes layers to system clipboard", cut);
74
                extensionPoints.add("View_TocActions","Paste","Paste layers from system clipboard",paste);
75

76 9392 caballero

77 7742 jmvivo
            //FPopupMenu.addEntry(copy);
78
            //FPopupMenu.addEntry(cut);
79
            //FPopupMenu.addEntry(paste);
80 6865 jaume

81 9392 caballero

82 6865 jaume
            // ProjectWindow
83 6635 jmvivo
                CopyProjectElement copyProjectElement = new CopyProjectElement();
84
                CutProjectElement cutProjectElement = new CutProjectElement();
85
                PasteProjectElement pasteProjectElementView = new PasteProjectElement();
86
                PasteProjectElement pasteProjectElementTable = new PasteProjectElement();
87
                PasteProjectElement pasteProjectElementMap = new PasteProjectElement();
88 6865 jaume

89 6635 jmvivo
                copyProjectElement.setUtiles(utiles);
90
                cutProjectElement.setUtiles(utiles);
91
                pasteProjectElementView.setUtiles(utiles);
92
                pasteProjectElementTable.setUtiles(utiles);
93
                pasteProjectElementMap.setUtiles(utiles);
94 6865 jaume

95 6635 jmvivo
                pasteProjectElementView.setType("views");
96
                pasteProjectElementTable.setType("tables");
97
                pasteProjectElementMap.setType("maps");
98 6865 jaume

99

100 6676 jmvivo
                extensionPoints.add("DocumentActions_View","Copy","Copy selectes documento to system clipboard",copyProjectElement);
101
                extensionPoints.add("DocumentActions_View","Cut","Cut selectes documento to system clipboard", cutProjectElement);
102
                extensionPoints.add("DocumentActions_View","Paste","Paste views from system clipboard",pasteProjectElementView);
103 6635 jmvivo

104

105 6676 jmvivo
                extensionPoints.add("DocumentActions_Table","Copy","Copy selectes documento to system clipboard",copyProjectElement);
106
                extensionPoints.add("DocumentActions_Table","Cut","Cut selectes documento to system clipboard", cutProjectElement);
107
                extensionPoints.add("DocumentActions_Table","Paste","Paste tables from system clipboard",pasteProjectElementTable);
108 6635 jmvivo

109 6676 jmvivo
                extensionPoints.add("DocumentActions_Map","Copy","Copy selectes documento to system clipboard",copyProjectElement);
110
                extensionPoints.add("DocumentActions_Map","Cut","Cut selectes documento to system clipboard", cutProjectElement);
111
                extensionPoints.add("DocumentActions_Map","Paste","Paste maps from system clipboard",pasteProjectElementMap);
112 6635 jmvivo

113 7742 jmvivo
                */
114 6635 jmvivo
        }
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 9532 caballero
                if (isOK) {
527
                        getMapContext().invalidate();
528
                        IWindow view=PluginServices.getMDIManager().getActiveWindow();
529
                        if (view instanceof BaseView)
530
                                ((ProjectDocument)((BaseView)view).getModel()).setModified(true);
531
                }
532 6635 jmvivo
        }
533 6865 jaume
534 6635 jmvivo
}
535
536
537
class Utiles {
538 6865 jaume
539 6635 jmvivo
        /*
540
         *
541
======================================
542
 Comportamiento del Pegar documentos:
543
======================================
544 6865 jaume

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

556 6635 jmvivo
Pegar tabla.
557
        Si alguna de las tablas existe.
558
                Se pega igualmente (apareceran tablas duplicadas)
559 6865 jaume

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

573

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

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

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

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