Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / Project.java @ 7633

History | View | Annotate | Download (46.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project;
42

    
43
import java.awt.Color;
44
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeListener;
46
import java.beans.PropertyChangeSupport;
47
import java.io.Serializable;
48
import java.text.DateFormat;
49
import java.util.ArrayList;
50
import java.util.Date;
51
import java.util.Hashtable;
52
import java.util.Iterator;
53
import java.util.TreeMap;
54

    
55
import org.cresques.cts.IProjection;
56
import org.cresques.cts.ProjectionPool;
57

    
58
import com.hardcode.driverManager.DriverLoadException;
59
import com.hardcode.gdbms.engine.data.DataSource;
60
import com.hardcode.gdbms.engine.data.NoSuchTableException;
61
import com.hardcode.gdbms.engine.data.SourceInfo;
62
import com.hardcode.gdbms.engine.data.db.DBSourceInfo;
63
import com.hardcode.gdbms.engine.data.db.DBTableSourceInfo;
64
import com.hardcode.gdbms.engine.data.file.FileSourceInfo;
65
import com.hardcode.gdbms.engine.data.object.ObjectSourceInfo;
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.ui.mdiManager.IWindow;
68
import com.iver.andami.ui.mdiManager.SingletonWindow;
69
import com.iver.andami.ui.mdiManager.WindowInfo;
70
import com.iver.cit.gvsig.ProjectExtension;
71
import com.iver.cit.gvsig.fmap.DriverException;
72
import com.iver.cit.gvsig.fmap.MapContext;
73
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
74
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
75
import com.iver.cit.gvsig.fmap.layers.CancelationException;
76
import com.iver.cit.gvsig.fmap.layers.FLayer;
77
import com.iver.cit.gvsig.fmap.layers.FLayers;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
80
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
81
import com.iver.cit.gvsig.fmap.layers.XMLException;
82
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
83
import com.iver.cit.gvsig.fmap.operations.selection.LinkSelectionListener;
84
import com.iver.cit.gvsig.gui.Table;
85
import com.iver.cit.gvsig.gui.View;
86
import com.iver.cit.gvsig.gui.layout.Layout;
87
import com.iver.cit.gvsig.gui.project.OpenException;
88
import com.iver.cit.gvsig.gui.project.ProjectWindow;
89
import com.iver.cit.gvsig.gui.project.SaveException;
90
import com.iver.utiles.NotExistInXMLEntity;
91
import com.iver.utiles.PostProcessSupport;
92
import com.iver.utiles.StringUtilities;
93
import com.iver.utiles.XMLEntity;
94

    
95

    
96
/**
97
 * Clase que representa un proyecto de openSIG
98
 *
99
 * @author Fernando Gonz?lez Cort?s
100
 */
101
public class Project implements Serializable, PropertyChangeListener {
102
        public static String VERSION = "1.0";
103
        static private IProjection defaultProjection = ProjectionPool.get(
104
                        "EPSG:23030");
105
        private PropertyChangeSupport change;
106
        boolean modified = false;
107
        private String name;
108
        //private String path;
109
        private String creationDate;
110
        private String modificationDate;
111
        private String owner;
112
        private String comments;
113
        private Color selectionColor = new Color(255, 255, 0);
114
        /**
115
         * Stores the list of ProjectViews in the project
116
         */
117
        private ArrayList views = new ArrayList();
118
        private ArrayList tables = new ArrayList();
119
        private ArrayList maps = new ArrayList();
120
        private ArrayList extents = new ArrayList();
121
        private int signatureAtStartup;
122
        // used to restore the position of the Project Manager window when loading a project from file
123
        private WindowInfo seedProjectWindow = null;
124
        private IProjection projection;
125

    
126

    
127
        /**
128
         * Creates a new Project object.
129
         */
130
        public Project() {
131
                change = new PropertyChangeSupport(this);
132

    
133
                //        change.addPropertyChangeListener(this);
134
                creationDate = DateFormat.getDateInstance().format(new Date());
135
                modificationDate = creationDate;
136
//                signatureAtStartup = computeSignature();
137

    
138
                /*                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
139
                   .getPluginDirectory()
140
                   .getAbsolutePath() +
141
                   File.separator + "drivers");
142
                 */
143
        }
144

    
145
        /**
146
         * Obtiene la fecha de creaci?n del proyecto
147
         *
148
         * @return
149
         */
150
        public String getCreationDate() {
151
                return creationDate;
152
        }
153

    
154
        /**
155
         * Obtiene el nombre del proyecto
156
         *
157
         * @return
158
         */
159
        public String getName() {
160
                return name;
161
        }
162

    
163
//        /**
164
//         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
165
//         * proyecto
166
//         *
167
//         * @return
168
//         */
169
//        public String getPath() {
170
//                return path;
171
//        }
172

    
173
        /**
174
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
175
         * por que al recuperar la fecha del XML hay que asignarla al objeto
176
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
177
         * deber?a de modificar nunca
178
         *
179
         * @param string
180
         */
181
        public void setCreationDate(String string) {
182
                creationDate = string;
183
                modified = true;
184
                change.firePropertyChange("", null, null);
185
        }
186

    
187
        /**
188
         * A?ade un mapa al proyecto
189
         *
190
         * @param m
191
         */
192
        public void addMap(ProjectMap m) {
193
                maps.add(m);
194
                m.addPropertyChangeListener(this);
195
                modified = true;
196
                change.firePropertyChange("", null, null);
197
                m.setProject(this, maps.size() - 1);
198
        }
199

    
200
        /**
201
         * Elimina un mapa del proyecto
202
         *
203
         * @param i indice del mapa
204
         */
205
        public void delMap(int i) {
206
                maps.remove(i);
207
                modified = true;
208
                change.firePropertyChange("", null, null);
209
        }
210

    
211
        /**
212
         * Establece el nombre del proyecto
213
         *
214
         * @param string
215
         */
216
        public void setName(String string) {
217
                name = string;
218
                modified = true;
219
                change.firePropertyChange("", null, null);
220
        }
221

    
222
//        /**
223
//         * establece la ruta completa de donde se encuentra guardado el proyecto
224
//         *
225
//         * @param string
226
//         */
227
//        public void setPath(String string) {
228
//                path = string;
229
//                modified = true;
230
//                change.firePropertyChange("", null, null);
231
//        }
232

    
233
        /**
234
         * DOCUMENT ME!
235
         *
236
         * @param co DOCUMENT ME!
237
         *
238
         * @return DOCUMENT ME!
239
         */
240
        public ProjectTable getTable(AlphanumericData co) {
241
                /**
242
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
243
                 * una capa, se puede intentar a?adir dos veces la misma tabla
244
                 */
245
                for (int i = 0; i < tables.size(); i++) {
246
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
247
                                return (ProjectTable) tables.get(i);
248
                        }
249
                }
250

    
251
                return null;
252
        }
253

    
254
        public ProjectTable getTable(String name) {
255
                /**
256
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
257
                 * una capa, se puede intentar a?adir dos veces la misma tabla
258
                 */
259
                for (int i = 0; i < tables.size(); i++) {
260
                        if (((ProjectTable) tables.get(i)).getName().equals(name)) {
261
                                return (ProjectTable) tables.get(i);
262
                        }
263
                }
264

    
265
                return null;
266
        }
267

    
268
        /**
269
         * A?ade una tabla al proyecto
270
         *
271
         * @param t
272
         */
273
        public void addTable(ProjectTable t) {
274
                tables.add(t);
275
                t.addPropertyChangeListener(this);
276
                modified = true;
277
                change.firePropertyChange("", null, null);
278
                t.setProject(this, tables.size() - 1);
279
        }
280

    
281
        /**
282
         * Elimina una tabla del proyecto
283
         *
284
         * @param i indice de la tabla
285
         */
286
        public void delTable(int i) {
287
                tables.remove(i);
288
                modified = true;
289
                change.firePropertyChange("", null, null);
290
        }
291

    
292
        /**
293
         * Opens a window for provided ProjectTable, which has been previously created,
294
         * and it's present in the project manager. 
295
         *
296
         * @return The window created to open the table
297
         */
298
        public static IWindow openTable(ProjectTable pt) {
299
                com.iver.cit.gvsig.gui.Table tableWindow = new com.iver.cit.gvsig.gui.Table();
300
                tableWindow.setModel(pt);
301
                pt.setAndamiWindow(tableWindow);
302
                PluginServices.getMDIManager().addWindow(tableWindow);
303
                return tableWindow;
304
        }
305
        
306
        /**
307
         * Opens the provided ProjectView, which has been previously created, and
308
         * it's present in the project manager. 
309
         *         
310
         * @return The window created to open the view
311
         */
312
        public static IWindow openView(ProjectView pv) {
313
                com.iver.cit.gvsig.gui.View viewWindow = new com.iver.cit.gvsig.gui.View();
314
                viewWindow.setModel(pv);
315
                PluginServices.getMDIManager().addWindow(viewWindow);
316
                return viewWindow;
317
        }
318
        
319
        /**
320
         * Opens a window for provided ProjectMap, which has been previously created,
321
         * and it's present in the project manager. 
322
         * 
323
         * @return The window created to open the map
324
         */
325
        public static IWindow openMap(ProjectMap pm) {
326
                pm.getModel().setProjectMap(pm);
327
                PluginServices.getMDIManager().addWindow(pm.getModel());
328

    
329
                return pm.getModel();
330
        }
331

    
332
        /**
333
         * A?ade una vista al proyecto
334
         *
335
         * @param v
336
         */
337
        public void addView(ProjectView v) {
338
                views.add(v);
339
                v.addPropertyChangeListener(this);
340
                modified = true;
341
                change.firePropertyChange("", null, null);
342
                v.setProject(this, views.size() - 1);
343
        }
344

    
345
        /**
346
         * Elimina una tabla del proyecto
347
         *
348
         * @param i indice del proyecto
349
         */
350
        public void delView(int k) {
351
                ProjectView pv=(ProjectView)views.get(k);
352
                FLayers layers=pv.getMapContext().getLayers();
353

    
354
                for (int i = layers.getLayersCount()-1; i>=0; i--){
355
                try {
356
                        if (layers.getLayer(i) instanceof AlphanumericData){
357
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
358
                    ProjectTable pt = project.getTable((AlphanumericData) layers.getLayer(i));
359

    
360
                    ArrayList tables = project.getTables();
361
                    for (int j = 0; j < tables.size(); j++) {
362
                        if (tables.get(j) == pt){
363
                            project.delTable(j);
364
                            break;
365
                        }
366
                    }
367

    
368
                    PluginServices.getMDIManager().closeSingletonWindow(pt);
369
                }
370
                layers.getLayer(i).getParentLayer().removeLayer(layers.getLayer(i));
371
                        } catch (CancelationException e1) {
372
                            e1.printStackTrace();
373
                    }
374
            }
375
                views.remove(k);
376
                modified = true;
377
                change.firePropertyChange("", null, null);
378
        }
379

    
380
        /**
381
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
382
         * fue modificado
383
         *
384
         * @return
385
         */
386
        public boolean isModified() {
387
                if (this.getMaps().size()==0 && this.getViews().size()==0 && this.getTables().size() == 0){
388
                        return false;
389
                }
390
                return true;
391
                ///return modified;    TODO El atributo modified solo detecta cuando se elimina o a?ade una vista,
392
                ///mapa o tabla pero no cuando se modifican.
393
        }
394

    
395
        /**
396
         * Obtiene los comentarios
397
         *
398
         * @return
399
         */
400
        public String getComments() {
401
                return comments;
402
        }
403

    
404
        /**
405
         * Obtiene la fecha de la ?ltima modificaci?n
406
         *
407
         * @return
408
         */
409
        public String getModificationDate() {
410
                return modificationDate;
411
        }
412

    
413
        /**
414
         * Obtiene el propietario del proyecto
415
         *
416
         * @return
417
         */
418
        public String getOwner() {
419
                return owner;
420
        }
421

    
422
        /**
423
         * Establece una cadena como comentarios al proyecto
424
         *
425
         * @param string
426
         */
427
        public void setComments(String string) {
428
                comments = string;
429
                modified = true;
430
                change.firePropertyChange("", null, null);
431
        }
432

    
433
        /**
434
         * Establece la fecha de la ?ltima modificaci?n
435
         *
436
         * @param string
437
         */
438
        public void setModificationDate(String string) {
439
                modificationDate = string;
440
                modified = true;
441
                change.firePropertyChange("", null, null);
442
        }
443

    
444
        /**
445
         * Establece el propietario del proyecto
446
         *
447
         * @param string
448
         */
449
        public void setOwner(String string) {
450
                owner = string;
451
                modified = true;
452
                change.firePropertyChange("", null, null);
453
        }
454

    
455
        /**
456
         * Establece el flag de modificado del proyecto
457
         *
458
         * @param b
459
         */
460
        public void setModified(boolean b) {
461
                modified = b;
462
        }
463

    
464
        /**
465
         * Obtiene el color de selecci?n que se usar? en el proyecto
466
         *
467
         * @return
468
         */
469
        public Color getSelectionColor() {
470
                return selectionColor;
471
        }
472

    
473
        /**
474
         * Establece el color de selecci?n
475
         *
476
         * @param color
477
         */
478
        public void setSelectionColor(Color color) {
479
                selectionColor = color;
480
                FSymbol.setSelectionColor(color);
481
                modified = true;
482
                change.firePropertyChange("selectionColor", null, color);
483
        }
484

    
485
        /**
486
         * Obtiene el color como un entero para su serializaci?n a XML
487
         *
488
         * @return
489
         */
490
        public String getColor() {
491
                return StringUtilities.color2String(selectionColor);
492
        }
493

    
494
        /**
495
         * M?todo invocado al recuperar de XML para establecer el color de
496
         * seleccion del proyecto
497
         *
498
         * @param color Entero que representa un color
499
         */
500
        public void setColor(String color) {
501
                modified = true;
502
                selectionColor = StringUtilities.string2Color(color);
503
        }
504

    
505
        /* (non-Javadoc)
506
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
507
         */
508
        public void propertyChange(PropertyChangeEvent evt) {
509
                this.modified = true;
510
                change.firePropertyChange(evt);
511
        }
512

    
513
        /**
514
         * DOCUMENT ME!
515
         *
516
         * @param arg1
517
         */
518
        public void addExtent(ProjectExtent arg1) {
519
                extents.add(arg1);
520
                modified = true;
521
                change.firePropertyChange("addExtent", null, null);
522
        }
523

    
524
        /**
525
         * DOCUMENT ME!
526
         *
527
         * @param arg0
528
         *
529
         * @return
530
         */
531
        public Object removeExtent(int arg0) {
532
                modified = true;
533
                change.firePropertyChange("delExtent", null, null);
534

    
535
                return extents.remove(arg0);
536
        }
537

    
538
        /**
539
         * DOCUMENT ME!
540
         *
541
         * @return DOCUMENT ME!
542
         */
543
        public ProjectExtent[] getExtents() {
544
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
545
        }
546

    
547
        /**
548
         * DOCUMENT ME!
549
         *
550
         * @param arg0
551
         */
552
        public synchronized void addPropertyChangeListener(
553
                PropertyChangeListener arg0) {
554
                change.addPropertyChangeListener(arg0);
555
        }
556

    
557
        /**
558
         * Get the list of maps in the project.
559
         *
560
         * @return
561
         */
562
        public ArrayList getMaps() {
563
                return maps;
564
        }
565

    
566
        /**
567
         * Get the list of tables in the project.
568
         *
569
         * @return
570
         */
571
        public ArrayList getTables() {
572
                return tables;
573
        }
574

    
575
        /**
576
         * Get the list of ProjectViews in the project.
577
         *
578
         * @return
579
         */
580
        public ArrayList getViews() {
581
                return views;
582
        }
583

    
584
        /**
585
         * DOCUMENT ME!
586
         *
587
         * @return DOCUMENT ME!
588
         *
589
         * @throws DriverException
590
         * @throws XMLException
591
         */
592
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
593
                XMLEntity xml = new XMLEntity();
594
                xml.putProperty("className", this.getClass().getName());
595
                xml.putProperty("VERSION", VERSION);
596
                xml.putProperty("comments", comments);
597
                xml.putProperty("creationDate", creationDate);
598

    
599
                int numExtents = extents.size();
600

    
601
                /*double[] xs = new double[size];
602
                   double[] ys = new double[size];
603
                   double[] ws = new double[size];
604
                   double[] hs = new double[size];
605
                 */
606
                xml.putProperty("numExtents", numExtents);
607

    
608
                for (int i = 0; i < numExtents; i++) {
609
                        /*        Rectangle2D rect = (Rectangle2D) extents.get(i);
610
                           xs[i] = rect.getX();
611
                           ys[i] = rect.getY();
612
                           ws[i] = rect.getWidth();
613
                           hs[i] = rect.getHeight();
614
                         */
615
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
616
                }
617

    
618
        // NUEVO: ESTO ESTA EN PRUEBAS. SIRVE PARA
619
        // BORRAR LAS REFERENCIAS A DATASOURCES QUE HEMOS
620
        // BORRADO. Hay que probar a borrarlos cuando se
621
        // borra una tabla y cuando se borra una capa.
622
        cleanBadReferences();
623
                SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
624
                xml.putProperty("data-source-count", infos.length);
625

    
626
                for (int i = 0; i < infos.length; i++) {
627
                        SourceInfo di = infos[i];
628
                        XMLEntity child = this.getSourceInfoXMLEntity(di);
629
                        xml.addChild(child);
630
                }
631

    
632
                /*
633
                   xml.putProperty("extentsX", xs);
634
                   xml.putProperty("extentsY", ys);
635
                   xml.putProperty("extentsW", ws);
636
                   xml.putProperty("extentsH", hs);
637
                 */
638

    
639
                int numViews=0;
640
                for (int i = 0; i < views.size(); i++) {
641
                        try {
642
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
643
                                xml.addChild(xmlchild);
644
                                numViews++;
645
                        } catch (SaveException e) {
646
                                e.showError();
647
                        }
648
                }
649
                xml.putProperty("numViews", numViews);
650

    
651
                int numMaps=0;
652
                for (int i = 0; i < maps.size(); i++) {
653
                        try {
654
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
655
                                xml.addChild(xmlchild);
656
                                numMaps++;
657
                        } catch (SaveException e) {
658
                                e.showError();
659
                        }
660
                }
661
                xml.putProperty("numMaps", numMaps);
662
                xml.putProperty("modificationDate", modificationDate);
663
                xml.putProperty("modified", modified);
664
                xml.putProperty("name", name);
665
                xml.putProperty("owner", owner);
666
                //xml.putProperty("path", path);
667
                xml.putProperty("selectionColor",
668
                        StringUtilities.color2String(selectionColor));
669

    
670
                int numTables=0;
671
                for (int i = 0; i < tables.size(); i++) {
672
                        try {
673
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
674
                                xml.addChild(xmlchild);
675
                                numTables++;
676
                        } catch (SaveException e) {
677

    
678
                                e.showError();
679
                        }
680
                }
681
                xml.putProperty("numTables", numTables);
682
                xml.putProperty("projection", defaultProjection.getAbrev());
683

    
684
                saveWindowProperties(xml);
685
                
686
                return xml;
687
        }
688
        
689
        private void saveWindowProperties(XMLEntity xml) {
690
                XMLEntity propertyList = new XMLEntity();
691
                
692
                propertyList.setName("AndamiPersistence");
693
                propertyList.putProperty("className", Project.class.getName());
694
                
695
                boolean projectWindowSaved = false;
696
                
697
                IWindow[] windowList = PluginServices.getMDIManager().getOrderedWindows();
698
                WindowInfo wi;
699
                XMLEntity windowProperties;
700
                for (int winIndex=windowList.length-1; winIndex>=0; winIndex--) {
701
                        wi = PluginServices.getMDIManager().getWindowInfo(windowList[winIndex]);
702
                        if (wi!=null && wi.checkPersistence()) {
703
                                if (windowList[winIndex] instanceof View) {
704
                                        View viewWindow = (View) windowList[winIndex];
705
                                        windowProperties = wi.getXMLEntity();
706
                                        windowProperties.putProperty("documentType", "ProjectView");
707
                                        windowProperties.putProperty("documentName", viewWindow.getModel().getName());
708
                                        windowProperties.putProperty("zPosition", winIndex);
709
                                        propertyList.addChild(windowProperties);
710
                                }
711
                                else if (windowList[winIndex] instanceof Table) {
712
                                        Table tableWindow = (Table) windowList[winIndex];
713
                                        windowProperties = wi.getXMLEntity();
714
                                        windowProperties.putProperty("documentType", "ProjectTable");
715
                                        windowProperties.putProperty("documentName", tableWindow.getModel().getName());
716
                                        windowProperties.putProperty("zPosition", winIndex);
717
                                        propertyList.addChild(windowProperties);
718
                                }
719
                                else if (windowList[winIndex] instanceof Layout) {
720
                                        Layout layoutWindow = (Layout) windowList[winIndex];
721
                                        windowProperties = wi.getXMLEntity();
722
                                        windowProperties.putProperty("documentType", "ProjectMap");
723
                                        windowProperties.putProperty("documentName", layoutWindow.getName());
724
                                        windowProperties.putProperty("zPosition", winIndex);
725
                                        propertyList.addChild(windowProperties);
726
                                }
727
                                else if (windowList[winIndex] instanceof ProjectWindow) {
728
                                        projectWindowSaved = true;
729
                                        windowProperties = wi.getXMLEntity();        
730
                                        windowProperties.putProperty("className", "com.iver.cit.gvsig.project.document.gui.ProjectWindow");
731
                                        windowProperties.putProperty("zPosition", winIndex);
732
                                        propertyList.addChild(windowProperties);
733
                                }
734
                        }
735
                }
736
                
737
                if (projectWindowSaved == false) {
738
                        // If the Project Manager was closed, it was not in the
739
                        // previous window list. Save it now
740
                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
741
                        
742
                        if (pe!=null) {
743
                                IWindow projectWindow = pe.getProjectWindow();
744
                                wi = PluginServices.getMDIManager().getWindowInfo(projectWindow);
745
                                if (wi!=null && wi.checkPersistence()) {
746
                                        windowProperties = wi.getXMLEntity();
747
                                        if (windowProperties!=null) {
748
                                                windowProperties.putProperty("className", "com.iver.cit.gvsig.project.document.gui.ProjectWindow");
749
                                                propertyList.addChild(windowProperties);
750
                                        }
751
                                }
752
                        }
753
                }
754
                
755
                xml.addChild(propertyList);
756
        }
757
        
758
        /**
759
         * Restores the position ans size of windows from projects
760
         * (gvSIG > 1.0rc1). It also restores the order of the windows
761
         * (which windows are on the foreground).
762
         *  
763
         * @param xml An XMLEntity object containing the properties of the
764
         * windows
765
         */
766
        private void restoreWindowProperties(XMLEntity xml) {
767
                XMLEntity child;
768
                int childNumb;
769
                
770
                boolean projectWindowRestored = false;
771
                
772
                if (xml.contains("className") // the current window persistence method (>1.0rc2)
773
                                && xml.getStringProperty("className").equals(Project.class.getName())
774
                                && xml.contains("name")
775
                                && xml.getStringProperty("name").equals("AndamiPersistence")) {
776
                        
777
                        // order the window properties before restoring them, so that we also
778
                        // restore the zPosition
779
                        TreeMap orderedProperties = new TreeMap();
780
                        int maximum = 1;
781
                        for (childNumb=xml.getChildrenCount()-1; childNumb>=0; childNumb--) {
782
                                child = xml.getChild(childNumb);
783
                                if (child.contains("zPosition")) {
784
                                        orderedProperties.put(new Integer(-child.getIntProperty("zPosition")), child); // reverse the order, so that we add the background windows first
785
                                }
786
                                else {
787
                                        orderedProperties.put(new Integer(maximum++), child); // the windows without zPosition will be on the foreground
788
                                }
789
                        }
790
                        
791
                        Iterator propertiesIterator = orderedProperties.values().iterator();
792
                        while (propertiesIterator.hasNext()) {
793
                                child = (XMLEntity) propertiesIterator.next();
794
                                 if (child.contains("documentType") // restore the position of the layout windows
795
                                                        && child.getStringProperty("documentType").equals("ProjectView")
796
                                                        && child.contains("name")
797
                                                        && child.getStringProperty("name").equals("ViewInfoProperties")) {
798
                                                boolean isClosed = true;
799
                                                if (child.contains("isClosed"))
800
                                                        isClosed = child.getBooleanProperty("isClosed");
801
                                                if (isClosed==false) {
802
                                                        WindowInfo windowProps = WindowInfo.createFromXMLEntity(child);
803
                                                        String documentName = child.getStringProperty("documentName");
804
                                                        ProjectView pv = this.getViewByName(documentName);
805
                                                        IWindow win = Project.openView(pv);
806
                                                        PluginServices.getMDIManager().changeWindowInfo(win, windowProps);
807
                                                }
808
                                }
809
                                else if (child.contains("documentType") // restore the position of the project manager window
810
                                                && child.getStringProperty("documentType").equals("com.iver.cit.gvsig.project.document.gui.ProjectWindow")
811
                                                && child.contains("name")
812
                                                && child.getStringProperty("name").equals("ViewInfoProperties")) {
813
                                        this.seedProjectWindow = WindowInfo.createFromXMLEntity(child);
814

    
815
                                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
816
                                        
817
                                        if (pe!=null) {
818
                                                pe.setProject(this);
819
                                                pe.showProjectWindow();
820
                                                projectWindowRestored = true;
821
                                        }
822
                                }
823
                                else if (child.contains("documentType") // restore the position of the layout windows
824
                                                && child.getStringProperty("documentType").equals("ProjectMap")
825
                                                && child.contains("name")
826
                                                && child.getStringProperty("name").equals("ViewInfoProperties")) {
827
                                        boolean isClosed = true;
828
                                        if (child.contains("isClosed"))
829
                                                isClosed = child.getBooleanProperty("isClosed");
830
                                        if (isClosed==false) {
831
                                                WindowInfo windowProps = WindowInfo.createFromXMLEntity(child);
832
                                                String documentName = child.getStringProperty("documentName");
833
                                                ProjectMap pm = this.getLayoutByName(documentName);
834
                                                IWindow win = Project.openMap(pm);
835
                                                PluginServices.getMDIManager().changeWindowInfo(win, windowProps);
836
                                        }
837
                                }
838
                                else if (child.contains("documentType") // restore the position of the table windows
839
                                                && child.getStringProperty("documentType").equals("ProjectTable")
840
                                                && child.contains("name")
841
                                                && child.getStringProperty("name").equals("ViewInfoProperties")) {
842
                                        boolean isClosed = true;
843
                                        if (child.contains("isClosed"))
844
                                                isClosed = child.getBooleanProperty("isClosed");
845
                                        if (isClosed==false) {
846
                                                WindowInfo windowProps = WindowInfo.createFromXMLEntity(child);
847
                                                String documentName = child.getStringProperty("documentName");
848
                                                ProjectTable pt = this.getTableByName(documentName);
849
                                                IWindow win = Project.openTable(pt);
850
                                                PluginServices.getMDIManager().changeWindowInfo(win, windowProps);
851
                                        }
852
                                }
853

    
854
                        }
855
                }
856
                
857
                if (!projectWindowRestored) { // if the project manager window was not stored in the project, just open it
858
                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
859
                        
860
                        if (pe!=null) {
861
                                pe.setProject(this);
862
                                pe.showProjectWindow();
863
                        }
864
                }
865
        }
866

    
867
        /**
868
         * Restores the position ans size of windows from old projects
869
         * (gvSIG 0.6.1, 1.0alpha, 1.0rc1). Previous versions did not
870
         * store any information about the window properties. Later
871
         * versions use a new system which also stores the window order.
872
         * 
873
         * It just restores positions of the Project Manager window, the
874
         * Views and the Maps (not the tables, they were not stored).
875
         * The window order (which windows are on the foreground)
876
         * is not restored (it was not stored).
877
         *  
878
         * @param xml An XMLEntity object containing the whole project.
879
         */
880
        private void restoreWindowProperties061(XMLEntity xml) {
881
                XMLEntity child;
882
                int childNumb;
883
                
884
                child = xml.getChild(xml.getChildrenCount()-1);
885
                if (child.contains("className")
886
                                && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.Project")
887
                                && child.contains("name")
888
                                && child.getStringProperty("name").equals("ViewInfoProperties")) {
889
                        this.seedProjectWindow = WindowInfo.createFromXMLEntity(child);
890
                        
891
                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
892
                        
893
                        if (pe!=null) {
894
                                pe.setProject(this);
895
                                pe.showProjectWindow();
896
                        }
897
                        
898
                }
899
                else  { // if the project manager window was not stored in the project, just open it
900
                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
901
                        
902
                        if (pe!=null) {
903
                                pe.setProject(this);
904
                                pe.showProjectWindow();
905
                        }
906
                }
907
                
908
                // try to open the views
909
                childNumb = xml.getIntProperty("numExtents") + xml.getIntProperty("data-source-count");
910
                int limit = (xml.getIntProperty("numViews") + childNumb);
911
                WindowInfo winProps;
912
                XMLEntity view;
913
                for (int i = childNumb; i < limit; i++) {
914
                        view = xml.getChild(i);
915
                        child = view.getChild(view.getChildrenCount()-1);
916
                        if (child.contains("className") && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.ProjectView") && child.contains("name") && child.getStringProperty("name").equals("ViewInfoProperties")) {
917
                                winProps = WindowInfo.createFromXMLEntity(child);
918
                                String documentName = view.getStringProperty("name");
919
                                ProjectView pv = this.getViewByName(documentName);
920
                                IWindow win = Project.openView(pv);
921
                                PluginServices.getMDIManager().changeWindowInfo(win, winProps);
922
                        }
923
                }
924
                
925
                childNumb += xml.getIntProperty("numViews");
926
                limit = childNumb + xml.getIntProperty("numMaps"); 
927

    
928
                // try to open the maps
929
                XMLEntity map;
930
                for (int i = childNumb; i < limit; i++) {
931
                        map = xml.getChild(i);
932
                        for (int j=0; j<map.getChildrenCount(); j++)
933
                        {
934
                                child = map.getChild(j);
935
                                if (child.contains("className") && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.ProjectMap") && child.contains("name") && child.getStringProperty("name").equals("ViewInfoProperties")) {
936
                                        winProps = WindowInfo.createFromXMLEntity(child);
937
                                        String documentName = map.getStringProperty("name");
938
                                        ProjectMap pm = this.getLayoutByName(documentName);
939
                                        IWindow win = Project.openMap(pm);
940
                                        PluginServices.getMDIManager().changeWindowInfo(win, winProps);
941
                                }
942
                        }
943
                }
944
        }
945

    
946
        
947
        /**
948
         * DOCUMENT ME!
949
         *
950
         * @param xml DOCUMENT ME!
951
         *
952
         * @return DOCUMENT ME!
953
         * @throws XMLException
954
         * @throws DriverException
955
         * @throws DriverIOException
956
         * @throws OpenException
957
         */
958
        public static Project createFromXML03(XMLEntity xml)
959
                throws OpenException {
960
                Project p = new Project();
961
                try{
962
                p.comments = xml.getStringProperty("comments");
963
                p.creationDate = xml.getStringProperty("creationDate");
964
/*
965
                double[] xs = xml.getDoubleArrayProperty("extentsX");
966
                double[] ys = xml.getDoubleArrayProperty("extentsY");
967
                double[] ws = xml.getDoubleArrayProperty("extentsW");
968
                double[] hs = xml.getDoubleArrayProperty("extentsH");
969
*/
970
                int numExtents=xml.getIntProperty("numExtents");
971
                for (int i = 0; i < numExtents; i++) {
972
                        ProjectExtent pe=ProjectExtent.createFromXML03(xml.getChild(i));
973
                        p.extents.add(pe);
974
                }
975

    
976
                int numViews = xml.getIntProperty("numViews");
977

    
978
                for (int i = numExtents; i < numViews+numExtents; i++) {
979
                        p.views.add(ProjectView.createFromXML03(xml.getChild(i), p));
980
                }
981

    
982
                int numMaps = xml.getIntProperty("numMaps");
983

    
984
                for (int i = numViews+numExtents; i < (numMaps + numViews+numExtents); i++) {
985
                        p.maps.add(ProjectMap.createFromXML03(xml.getChild(i), p));
986
                }
987

    
988
                p.modificationDate = xml.getStringProperty("modificationDate");
989
                p.modified = xml.getBooleanProperty("modified");
990
                p.name = xml.getStringProperty("name");
991
                p.owner = xml.getStringProperty("owner");
992
//                p.path = xml.getStringProperty("path");
993
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
994
                                        "selectionColor"));
995

    
996
                int numTables = xml.getIntProperty("numTables");
997

    
998
                for (int i = numExtents+numMaps + numViews; i < (numExtents+numTables + numMaps + numViews);
999
                                i++) {
1000
                        p.tables.add(ProjectTable.createFromXML03(xml.getChild(i), p));
1001
                }
1002
                String strProj = xml.getStringProperty("projection");
1003
                if (strProj != null)
1004
                        p.setProjection(ProjectionPool.get(strProj));
1005
                }catch (Exception e) {
1006
                        throw new OpenException(e,p.getClass().getName());
1007
                }
1008
                return p;
1009
        }
1010

    
1011
        /**
1012
         * DOCUMENT ME!
1013
         *
1014
         * @param xml DOCUMENT ME!
1015
         *
1016
         * @return DOCUMENT ME!
1017
         *
1018
         * @throws XMLException
1019
         * @throws DriverException
1020
         * @throws DriverIOException
1021
         * @throws OpenException
1022
         * @throws VersionException
1023
         */
1024
        public static Project createFromXML(XMLEntity xml)
1025
                throws OpenException {
1026

    
1027
                int childNumber = 0;
1028
                Project p = new Project();
1029
                try{
1030
                p.comments = xml.getStringProperty("comments");
1031
                p.creationDate = xml.getStringProperty("creationDate");
1032

    
1033
                /*
1034
                   double[] xs = xml.getDoubleArrayProperty("extentsX");
1035
                   double[] ys = xml.getDoubleArrayProperty("extentsY");
1036
                   double[] ws = xml.getDoubleArrayProperty("extentsW");
1037
                   double[] hs = xml.getDoubleArrayProperty("extentsH");
1038
                 */
1039
                int numExtents = xml.getIntProperty("numExtents");
1040

    
1041
                for (int i = 0; i < numExtents; i++) {
1042
                        ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
1043
                        p.extents.add(pe);
1044
                }
1045

    
1046
                childNumber = numExtents;
1047

    
1048
                int numDataSources = xml.getIntProperty("data-source-count");
1049

    
1050
                for (int i = childNumber; i < (childNumber + numDataSources); i++) {
1051
                        XMLEntity child = xml.getChild(i);
1052

    
1053
                        if (child.getStringProperty("type").equals("otherDriverFile")) {
1054
                                LayerFactory.getDataSourceFactory().addFileDataSource(child.getStringProperty(
1055
                                                "driverName"), child.getStringProperty("gdbmsname"),
1056
                                        child.getStringProperty("file"));
1057
                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
1058
                                /*                                String layerName = child.getStringProperty("layerName");
1059
                                   ProjectView vista = project.getViewByName(child.getStringProperty(
1060
                                   "viewName"));
1061
                                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
1062

1063
                                   modelo = ((AlphanumericData) layer).getRecordset();
1064
                                   associatedTable = (AlphanumericData) layer;
1065
                                 */
1066
                        } else if (child.getStringProperty("type").equals("db")) {
1067
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(child.getStringProperty(
1068
                                                "gdbmsname"), child.getStringProperty("host"),
1069
                                        child.getIntProperty("port"),
1070
                                        child.getStringProperty("user"),
1071
                                        child.getStringProperty("password"),
1072
                                        child.getStringProperty("dbName"),
1073
                                        child.getStringProperty("tableName"),
1074
                                        child.getStringProperty("driverInfo"));
1075
                        }
1076
                }
1077

    
1078
                childNumber += numDataSources;
1079

    
1080
                int numViews = xml.getIntProperty("numViews");
1081

    
1082
                for (int i = childNumber; i < (numViews + childNumber); i++) {
1083
                        try{
1084
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(xml.getChild(i), p);
1085
                                p.addView(pv);
1086
                        }catch(OpenException e){
1087
                                e.showError();
1088
                        }
1089
                }
1090

    
1091
                childNumber += numViews;
1092

    
1093
                int numMaps = xml.getIntProperty("numMaps");
1094

    
1095
                for (int i = childNumber; i < (numMaps + childNumber); i++) {
1096
                        try{
1097
                                ProjectMap map = (ProjectMap) ProjectMap.createFromXML(xml.getChild(i), p);
1098
                                p.maps.add(map);
1099
                        }catch(OpenException e){
1100
                                e.showError();
1101
                        }
1102
                }
1103

    
1104
                childNumber += numMaps;
1105
                p.modificationDate = xml.getStringProperty("modificationDate");
1106
                p.modified = xml.getBooleanProperty("modified");
1107
                p.name = xml.getStringProperty("name");
1108
                p.owner = xml.getStringProperty("owner");
1109
//                p.path = xml.getStringProperty("path");
1110
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
1111
                                        "selectionColor"));
1112

    
1113
                int numTables = xml.getIntProperty("numTables");
1114

    
1115
                for (int i = childNumber; i < (childNumber + numTables); i++) {
1116
                        try{
1117
                                ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xml.getChild(i), p);
1118
                                p.tables.add(ptable);
1119
                        }catch(OpenException e){
1120
                                e.showError();
1121
                        }
1122
                }
1123

    
1124
                childNumber += numTables;
1125
                
1126
                if (childNumber < xml.getChildrenCount()) { // restore the position of the windows
1127
                        XMLEntity child = xml.getChild(childNumber);
1128
                        if (child.contains("className")
1129
                                        && child.getStringProperty("className").equals(Project.class.getName())
1130
                                        && child.contains("name")
1131
                                        && child.getStringProperty("name").equals("AndamiPersistence")) {
1132
                                p.restoreWindowProperties(child);
1133
                        }
1134
                        else if (child.contains("className")
1135
                                        && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.Project")
1136
                                        && child.contains("name")
1137
                                        && child.getStringProperty("name").equals("ViewInfoProperties")) {
1138
                                p.restoreWindowProperties061(xml); 
1139
                        }
1140
                }
1141
                else { // if there was no info about the windows, just open the project manager window
1142
                        ProjectExtension pe =(ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);                        
1143
                        if (pe!=null) {
1144
                                pe.setProject(p);
1145
                                pe.showProjectWindow();
1146
                        }
1147
                }
1148

    
1149
                p.setLinkTable();
1150

    
1151
                String strProj = xml.getStringProperty("projection");
1152

    
1153
                if (strProj != null) {
1154
                        p.setProjection(ProjectionPool.get(strProj));
1155
                }
1156
        PostProcessSupport.executeCalls();
1157
                }catch (Exception e) {
1158
                        throw new OpenException(e,p.getClass().getName());
1159
                }
1160
//                p.setSignature( p.hashCode());
1161
                return p;
1162

    
1163
        }
1164
        
1165
        
1166
//        private void setSignature(int signature) {
1167
//                this.signatureAtStartup = computeSignature();
1168
//        }
1169

    
1170
        /**
1171
         * Reestablece los link que ten?a cada tabla con las dem?s.
1172
         */
1173
        public void setLinkTable() {
1174
                ArrayList tables = getTables();
1175

    
1176
                for (int i = 0; i < tables.size(); i++) {
1177
                        for (int j = 0; j < tables.size(); j++) {
1178
                                /*System.out.println("name = " +
1179
                                        ((ProjectTable) tables.get(j)).getModelo().getName());
1180
                                System.out.println("linktable = " +
1181
                                        ((ProjectTable) tables.get(i)).getLinkTable());
1182
*/
1183
                                try {
1184
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
1185
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
1186
                                                                                                                                                                  .get(
1187
                                                                j)).getModelo().getRecordset().getName())) {
1188
                                        LinkSelectionListener lsl;
1189

    
1190
                                                lsl = new LinkSelectionListener((SelectableDataSource)((ProjectTable) tables.get(
1191
                                                                        i)).getModelo().getRecordset(),
1192
                                                                        (SelectableDataSource)((ProjectTable) tables.get(j)).getModelo().getRecordset(),
1193
                                                                ((ProjectTable) tables.get(i)).getField1(),
1194
                                                                ((ProjectTable) tables.get(i)).getField2());
1195

    
1196
                                                ((SelectableDataSource)((ProjectTable) tables.get(i)).getModelo().getRecordset())
1197
                                         .addSelectionListener(lsl);
1198

    
1199
                                }
1200
                                } catch (DriverLoadException e) {
1201
                                        // TODO Auto-generated catch block
1202
                                        e.printStackTrace();
1203
                                }
1204
                        }
1205
                }
1206
        }
1207

    
1208
        /**
1209
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
1210
         *
1211
         * @param layer Capa cuya vista se quiere obtener
1212
         *
1213
         * @return
1214
         *
1215
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
1216
         *                    encuentra en ninguna vista
1217
         */
1218
        public String getView(FLayer layer) {
1219
                for (int v = 0; v < views.size(); v++) {
1220
                        ProjectView pView = (ProjectView) views.get(v);
1221
                        FLayers layers = pView.getMapContext().getLayers();
1222
                        if (isView(layers,layer))return pView.getName();
1223
                }
1224

    
1225
                throw new RuntimeException("The layer is not in a view");
1226
        }
1227

    
1228
        public boolean isView(FLayers layers, FLayer layer) {
1229
                for (int i = 0; i < layers.getLayersCount(); i++) {
1230
                        if (layers.getLayer(i) instanceof FLayers){
1231
                                return isView((FLayers)layers.getLayer(i),layer);
1232
                        }else{
1233
                                if (layers.getLayer(i) == layer) {
1234
                                        return true;
1235
                                }
1236
                        }
1237
                }
1238
                return false;
1239
        }
1240

    
1241
        /**
1242
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas) con el
1243
         * que se pasa como par?metro. Devuelve null si no hay ninguna vista con
1244
         * ese nombre
1245
         *
1246
         * @param viewName Nombre de la vista que se quiere obtener
1247
         *
1248
         * @return DOCUMENT ME!
1249
         */
1250
        public ProjectView getViewByName(String viewName) {
1251
                Object o = getProjectElementByName(viewName, views);
1252

    
1253
                if (o == null) {
1254
                        return null;
1255
                }
1256

    
1257
                return (ProjectView) o;
1258
        }
1259

    
1260
        /**
1261
         * DOCUMENT ME!
1262
         *
1263
         * @return DOCUMENT ME!
1264
         */
1265
        public IProjection getProjection() {
1266
                if (projection == null)
1267
                        projection = defaultProjection;
1268
                return defaultProjection;
1269
        }
1270

    
1271
        /**
1272
         * DOCUMENT ME!
1273
         *
1274
         * @param defaultProjection DOCUMENT ME!
1275
         */
1276
        public void setProjection(IProjection defaultProjection) {
1277
                Project.defaultProjection = defaultProjection;
1278
        }
1279

    
1280
        /**
1281
         * DOCUMENT ME!
1282
         *
1283
         * @param name DOCUMENT ME!
1284
         * @param l DOCUMENT ME!
1285
         *
1286
         * @return DOCUMENT ME!
1287
         */
1288
        private Object getProjectElementByName(String name, ArrayList l) {
1289
                for (Iterator iter = l.iterator(); iter.hasNext();) {
1290
                        ProjectElement elem = (ProjectElement) iter.next();
1291

    
1292
                        if (elem.getName().equals(name)) {
1293
                                return elem;
1294
                        }
1295
                }
1296

    
1297
                return null;
1298
        }
1299

    
1300
        /**
1301
         * DOCUMENT ME!
1302
         *
1303
         * @param name
1304
         *
1305
         * @return
1306
         */
1307
        public ProjectTable getTableByName(String name) {
1308
                Object o = getProjectElementByName(name, tables);
1309

    
1310
                if (o == null) {
1311
                        return null;
1312
                }
1313

    
1314
                return (ProjectTable) o;
1315
        }
1316

    
1317
        /**
1318
         * DOCUMENT ME!
1319
         *
1320
         * @param name
1321
         *
1322
         * @return
1323
         */
1324
        public ProjectMap getLayoutByName(String name) {
1325
                Object o = getProjectElementByName(name, maps);
1326

    
1327
                if (o == null) {
1328
                        return null;
1329
                }
1330

    
1331
                return (ProjectMap) o;
1332
        }
1333

    
1334
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
1335
        ArrayList tables = getTables();
1336
        SelectableDataSource dataSource = null;
1337
        try {
1338
        for (int i = 0; i < tables.size(); i++) {
1339
            ProjectTable pt = (ProjectTable) tables.get(i);
1340
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
1341
                dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1342
                break;
1343
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
1344

    
1345
                                        dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1346

    
1347
                break;
1348
            }
1349
        }
1350
        } catch (DriverLoadException e) {
1351
                        // TODO Auto-generated catch block
1352
                        e.printStackTrace();
1353
                }
1354

    
1355
        if (dataSource == null){
1356
            //No se ha creado la tabla asociada al tema
1357
            dataSource = ((AlphanumericData) layer).getRecordset();
1358
        }
1359

    
1360
        return dataSource;
1361

    
1362
    }
1363

    
1364
    /**
1365
     * Recorremos las capas y las tablas del proyecto, y creamos una lista
1366
     * con todos los datasources de GDBMS que estamos usando.
1367
     * Luego recorremos los que est?n registrados, y borramos
1368
     * aquellos que no est?n siendo usados, es decir, aquellos
1369
     * que no est?n en nuestra lista (un Hash con clave el nombre
1370
     * del GDBMS)
1371
     *
1372
     */
1373
    private void cleanBadReferences()
1374
    {
1375
        Hashtable usedDataSources = new Hashtable();
1376
        // Primero las tablas
1377
        int i, j;
1378
        try {
1379
        for (i=0; i<tables.size(); i++)
1380
        {
1381
            ProjectTable t = (ProjectTable) tables.get(i);
1382
            SelectableDataSource ds;
1383

    
1384
                                ds = (SelectableDataSource)t.getModelo().getRecordset();
1385

    
1386
            if (t.getOriginal() != null)
1387
                usedDataSources.put(t.getOriginal().getRecordset().getName(), t.getOriginal());
1388
            usedDataSources.put(ds.getName(), ds);
1389
        }
1390
        } catch (DriverLoadException e) {
1391
                        // TODO Auto-generated catch block
1392
                        e.printStackTrace();
1393
                }
1394
        // Ahora las vistas
1395
        try {
1396
            for (i=0; i<views.size(); i++)
1397
            {
1398
                ProjectView pv = (ProjectView) views.get(i);
1399

    
1400
                FLayers lyrs = pv.getMapContext().getLayers();
1401

    
1402
                for (j=0; j<lyrs.getLayersCount(); j++)
1403
                {
1404
                    FLayer lyr = lyrs.getLayer(j);
1405
                    if (lyr instanceof FLyrVect)
1406
                    {
1407
                        FLyrVect lyrVect = (FLyrVect) lyr;
1408
                        if (lyrVect.isAvailable())
1409
                                usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getSource().getRecordset());
1410
                    }
1411
                }
1412
                MapContext aux = pv.getMapOverViewContext();
1413
                if (aux != null)
1414
                {
1415
                    FLayers lyrsOverview = aux.getLayers();
1416
                    for (j=0; j<lyrsOverview.getLayersCount(); j++)
1417
                    {
1418
                        FLayer lyr = lyrsOverview.getLayer(j);
1419
                        if (lyr instanceof FLyrVect)
1420
                        {
1421
                            FLyrVect lyrVect = (FLyrVect) lyr;
1422
                            usedDataSources.put(lyrVect.getSource().getRecordset().getName(), lyrVect.getSource().getRecordset());
1423
                        }
1424
                    }
1425
                }
1426

    
1427
            } // for i
1428
        } catch (DriverLoadException e) {
1429
                        // TODO Auto-generated catch block
1430
                        e.printStackTrace();
1431
                } catch (DriverException e) {
1432
                        // TODO Auto-generated catch block
1433
                        e.printStackTrace();
1434
                }
1435
        // Recorremos los dataSources y los borramos si no
1436
        // los estamos usando.
1437
        SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
1438
        try {
1439
            for (i=0; i < infos.length; i++)
1440
            {
1441
                if (!usedDataSources.containsKey(infos[i].name))
1442
                {
1443
                    DataSource ds;
1444
                    ds = LayerFactory.getDataSourceFactory().createRandomDataSource(infos[i].name);
1445
                    ds.remove();
1446
                }
1447
            }
1448
        } catch (DriverLoadException e) {
1449
            // TODO Auto-generated catch block
1450
            e.printStackTrace();
1451
        } catch (NoSuchTableException e) {
1452
            // TODO Auto-generated catch block
1453
            e.printStackTrace();
1454
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1455
            // TODO Auto-generated catch block
1456
            e.printStackTrace();
1457
        }
1458
    }
1459

    
1460
        public WindowInfo getSeedProjectWindow() {
1461
                return seedProjectWindow;
1462
        }
1463
        
1464
        public void setSeedProjectWindow(WindowInfo projectWindow) {
1465
                seedProjectWindow = projectWindow;
1466
        }
1467

    
1468
        public XMLEntity getSourceInfoXMLEntity(SourceInfo di) {
1469
                XMLEntity child = new XMLEntity();
1470

    
1471

    
1472
                if (di instanceof ObjectSourceInfo) {
1473
                        ObjectSourceInfo driver = (ObjectSourceInfo) di;
1474
                        child.putProperty("type", "sameDriverFile");
1475
                        child.putProperty("gdbmsname", driver.name);
1476
                } else if (di instanceof FileSourceInfo) {
1477
                        FileSourceInfo vfdi = (FileSourceInfo) di;
1478
                        child.putProperty("type", "otherDriverFile");
1479
                        child.putProperty("gdbmsname", vfdi.name);
1480
                        child.putProperty("file", vfdi.file);
1481
                        child.putProperty("driverName", vfdi.driverName);
1482
                } else if (di instanceof DBSourceInfo) {
1483
                        DBTableSourceInfo dbdi = (DBTableSourceInfo) di;
1484
                        child.putProperty("type", "db");
1485
                        child.putProperty("gdbmsname", dbdi.name);
1486
                        child.putProperty("dbms", dbdi.dbms);
1487
                        child.putProperty("host", dbdi.host);
1488
                        child.putProperty("port", dbdi.port);
1489
                        child.putProperty("user", dbdi.user);
1490
                        child.putProperty("password", dbdi.password);
1491
                        child.putProperty("dbName", dbdi.dbName);
1492
                        child.putProperty("tableName", dbdi.tableName);
1493
                        child.putProperty("driverInfo", dbdi.driverName);
1494
                }
1495

    
1496
                return child;
1497
        }
1498

    
1499

    
1500
        /**
1501
         * Sets the projection used when no projection is defined
1502
         * @param defaultProjection DOCUMENT ME!
1503
         */
1504
        public static void setDefaultProjection(IProjection defaultProjection) {
1505
                Project.defaultProjection = defaultProjection;
1506
        }
1507

    
1508
        public static IProjection getDefaultProjection() {
1509
                return Project.defaultProjection;
1510
        }
1511

    
1512
//        public boolean hasChanged() {
1513
//                return signatureAtStartup != computeSignature();
1514
//        }
1515

    
1516
//        public int computeSignature() {
1517
//
1518
////                /* TODO method also present in ProjectMap, ProjectTable,
1519
////                 *  ProjectView and ProjectExtent
1520
////                 */
1521
//                int result = 17;
1522
////
1523
////                Class clazz = getClass();
1524
////                Field[] fields = clazz.getDeclaredFields();
1525
////                for (int i = 0; i < fields.length; i++) {
1526
////                        try {
1527
////                                // don't compute following fields
1528
////                                if (fields[i].getName().equals("modificationDate")
1529
////                                                || fields[i].getName().equals("creationDate")
1530
////                                                || fields[i].getName().equals("signatureAtStartup")
1531
////                                                || fields[i].getName().equals("change")
1532
////                                                || Modifier.isStatic(fields[i].getModifiers()))
1533
////                                        continue;
1534
////
1535
////
1536
////                                String type = fields[i].getType().getName();
1537
////                                if (type.equals("boolean")) {
1538
////                                        result += 37 + ((fields[i].getBoolean(this)) ? 1 : 0);
1539
////                                } else if (type.equals("java.lang.String")) {
1540
////                                        Object v = fields[i].get(this);
1541
////                                        if (v == null) {
1542
////                                                result += 37;
1543
////                                                continue;
1544
////                                        }
1545
////                                        char[] chars = ((String) v).toCharArray();
1546
////                                        for (int j = 0; j < chars.length; j++) {
1547
////                                                result += 37 + (int) chars[i];
1548
////                                        }
1549
////                                } else if (type.equals("byte")) {
1550
////                                        result += 37 + (int) fields[i].getByte(this);
1551
////                                } else if (type.equals("char")) {
1552
////                                        result += 37 + (int) fields[i].getChar(this);
1553
////                                } else if (type.equals("short")) {
1554
////                                        result += 37 + (int) fields[i].getShort(this);
1555
////                                } else if (type.equals("int")) {
1556
////                                        result += 37 + fields[i].getInt(this);
1557
////                                } else if (type.equals("long")) {
1558
////                                        long f = fields[i].getLong(this) ;
1559
////                                        result += 37 + (f ^ (f >>> 32));
1560
////                                } else if (type.equals("float")) {
1561
////                                        result += 37 + Float.floatToIntBits(fields[i].getFloat(this));
1562
////                                } else if (type.equals("double")) {
1563
////                                        long f = Double.doubleToLongBits(fields[i].getDouble(this));
1564
////                                        result += 37 + (f ^ (f >>> 32));
1565
////                                } else {
1566
//////                                        Object obj = fields[i].get(this);
1567
//////                                        result += 37 + ((obj != null)? obj.hashCode() : 0);
1568
////                                }
1569
////                        } catch (Exception e) { e.printStackTrace(); }
1570
////
1571
////                }
1572
////
1573
////                for (int i = 0; i < views.size(); i++) {
1574
////                        ProjectView e = (ProjectView) views.get(i);
1575
////                        result += 37 + e.computeSignature();
1576
////                }
1577
////
1578
////                for (int i = 0; i < tables.size(); i++) {
1579
////                        ProjectTable e = (ProjectTable) tables.get(i);
1580
////                        result += 37 + e.computeSignature();
1581
////                }
1582
////
1583
////                for (int i = 0; i < maps.size(); i++) {
1584
////                        ProjectMap e = (ProjectMap) maps.get(i);
1585
////                        result += 37 + e.computeSignature();
1586
////                }
1587
////
1588
////                for (int i = 0; i < extents.size(); i++) {
1589
////                        ProjectExtent e = (ProjectExtent) extents.get(i);
1590
////                        result += 37 + e.computeSignature();
1591
////                }
1592
//
1593
//
1594
//                return result;
1595
//        }
1596

    
1597
}