Statistics
| Revision:

svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / applications / appgvSIG / src / com / iver / cit / gvsig / project / Project.java @ 5222

History | View | Annotate | Download (31.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

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

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

    
86

    
87
/**
88
 * Clase que representa un proyecto de openSIG
89
 *
90
 * @author Fernando Gonz?lez Cort?s
91
 */
92
public class Project implements Serializable, PropertyChangeListener {
93
        public static String VERSION = "0.6";
94
        static private IProjection defaultProjection = ProjectionPool.get(
95
                        "EPSG:23030");
96
        private PropertyChangeSupport change;
97
        boolean modified = false;
98
        private String name;
99
        private String path;
100
        private String creationDate;
101
        private String modificationDate;
102
        private String owner;
103
        private String comments;
104
        private Color selectionColor = new Color(255, 255, 0);
105
        private ArrayList views = new ArrayList();
106
        private ArrayList tables = new ArrayList();
107
        private ArrayList maps = new ArrayList();
108
        private ArrayList extents = new ArrayList();
109
        // used to restore the position of the Project Manager window when loading a project from file
110
        private static ViewInfo seedProjectWindow = null;
111

    
112
        /**
113
         * Creates a new Project object.
114
         */
115
        public Project() {
116
                change = new PropertyChangeSupport(this);
117

    
118
                //        change.addPropertyChangeListener(this);
119
                creationDate = DateFormat.getDateInstance().format(new Date());
120
                modificationDate = creationDate;
121

    
122
                /*                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
123
                   .getPluginDirectory()
124
                   .getAbsolutePath() +
125
                   File.separator + "drivers");
126
                 */
127
        }
128

    
129
        /**
130
         * Obtiene la fecha de creaci?n del proyecto
131
         *
132
         * @return
133
         */
134
        public String getCreationDate() {
135
                return creationDate;
136
        }
137

    
138
        /**
139
         * Obtiene el nombre del proyecto
140
         *
141
         * @return
142
         */
143
        public String getName() {
144
                return name;
145
        }
146

    
147
        /**
148
         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
149
         * proyecto
150
         *
151
         * @return
152
         */
153
        public String getPath() {
154
                return path;
155
        }
156

    
157
        /**
158
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
159
         * por que al recuperar la fecha del XML hay que asignarla al objeto
160
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
161
         * deber?a de modificar nunca
162
         *
163
         * @param string
164
         */
165
        public void setCreationDate(String string) {
166
                creationDate = string;
167
                modified = true;
168
                change.firePropertyChange("", null, null);
169
        }
170

    
171
        /**
172
         * A?ade un mapa al proyecto
173
         *
174
         * @param m
175
         */
176
        public void addMap(ProjectMap m) {
177
                maps.add(m);
178
                m.addPropertyChangeListener(this);
179
                modified = true;
180
                change.firePropertyChange("", null, null);
181
                m.setProject(this, maps.size() - 1);
182
        }
183

    
184
        /**
185
         * Elimina un mapa del proyecto
186
         *
187
         * @param i indice del mapa
188
         */
189
        public void delMap(int i) {
190
                maps.remove(i);
191
                modified = true;
192
                change.firePropertyChange("", null, null);
193
        }
194

    
195
        /**
196
         * Establece el nombre del proyecto
197
         *
198
         * @param string
199
         */
200
        public void setName(String string) {
201
                name = string;
202
                modified = true;
203
                change.firePropertyChange("", null, null);
204
        }
205

    
206
        /**
207
         * establece la ruta completa de donde se encuentra guardado el proyecto
208
         *
209
         * @param string
210
         */
211
        public void setPath(String string) {
212
                path = string;
213
                modified = true;
214
                change.firePropertyChange("", null, null);
215
        }
216

    
217
        /**
218
         * DOCUMENT ME!
219
         *
220
         * @param co DOCUMENT ME!
221
         *
222
         * @return DOCUMENT ME!
223
         */
224
        public ProjectTable getTable(AlphanumericData co) {
225
                /**
226
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
227
                 * una capa, se puede intentar a?adir dos veces la misma tabla
228
                 */
229
                for (int i = 0; i < tables.size(); i++) {
230
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
231
                                return (ProjectTable) tables.get(i);
232
                        }
233
                }
234

    
235
                return null;
236
        }
237

    
238
        /**
239
         * A?ade una tabla al proyecto
240
         *
241
         * @param t
242
         */
243
        public void addTable(ProjectTable t) {
244
                tables.add(t);
245
                t.addPropertyChangeListener(this);
246
                modified = true;
247
                change.firePropertyChange("", null, null);
248
                t.setProject(this, tables.size() - 1);
249
        }
250

    
251
        /**
252
         * Elimina una tabla del proyecto
253
         *
254
         * @param i indice de la tabla
255
         */
256
        public void delTable(int i) {
257
                tables.remove(i);
258
                modified = true;
259
                change.firePropertyChange("", null, null);
260
        }
261

    
262
        /**
263
         * A?ade una vista al proyecto
264
         *
265
         * @param v
266
         */
267
        public void addView(ProjectView v) {
268
                views.add(v);
269
                v.addPropertyChangeListener(this);
270
                modified = true;
271
                change.firePropertyChange("", null, null);
272
                v.setProject(this, views.size() - 1);
273
        }
274

    
275
        /**
276
         * Elimina una tabla del proyecto
277
         *
278
         * @param i indice del proyecto
279
         */
280
        public void delView(int i) {
281
                views.remove(i);
282
                modified = true;
283
                change.firePropertyChange("", null, null);
284
        }
285

    
286
        /**
287
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
288
         * fue modificado
289
         *
290
         * @return
291
         */
292
        public boolean isModified() {
293
                return modified;
294
        }
295

    
296
        /**
297
         * Obtiene los comentarios
298
         *
299
         * @return
300
         */
301
        public String getComments() {
302
                return comments;
303
        }
304

    
305
        /**
306
         * Obtiene la fecha de la ?ltima modificaci?n
307
         *
308
         * @return
309
         */
310
        public String getModificationDate() {
311
                return modificationDate;
312
        }
313

    
314
        /**
315
         * Obtiene el propietario del proyecto
316
         *
317
         * @return
318
         */
319
        public String getOwner() {
320
                return owner;
321
        }
322

    
323
        /**
324
         * Establece una cadena como comentarios al proyecto
325
         *
326
         * @param string
327
         */
328
        public void setComments(String string) {
329
                comments = string;
330
                modified = true;
331
                change.firePropertyChange("", null, null);
332
        }
333

    
334
        /**
335
         * Establece la fecha de la ?ltima modificaci?n
336
         *
337
         * @param string
338
         */
339
        public void setModificationDate(String string) {
340
                modificationDate = string;
341
                modified = true;
342
                change.firePropertyChange("", null, null);
343
        }
344

    
345
        /**
346
         * Establece el propietario del proyecto
347
         *
348
         * @param string
349
         */
350
        public void setOwner(String string) {
351
                owner = string;
352
                modified = true;
353
                change.firePropertyChange("", null, null);
354
        }
355

    
356
        /**
357
         * Establece el flag de modificado del proyecto
358
         *
359
         * @param b
360
         */
361
        public void setModified(boolean b) {
362
                modified = b;
363
        }
364

    
365
        /**
366
         * Obtiene el color de selecci?n que se usar? en el proyecto
367
         *
368
         * @return
369
         */
370
        public Color getSelectionColor() {
371
                return selectionColor;
372
        }
373

    
374
        /**
375
         * Establece el color de selecci?n
376
         *
377
         * @param color
378
         */
379
        public void setSelectionColor(Color color) {
380
                selectionColor = color;
381
                FSymbol.setSelectionColor(color);
382
                modified = true;
383
                change.firePropertyChange("selectionColor", null, color);
384
        }
385

    
386
        /**
387
         * Obtiene el color como un entero para su serializaci?n a XML
388
         *
389
         * @return
390
         */
391
        public String getColor() {
392
                return StringUtilities.color2String(selectionColor);
393
        }
394

    
395
        /**
396
         * M?todo invocado al recuperar de XML para establecer el color de
397
         * seleccion del proyecto
398
         *
399
         * @param color Entero que representa un color
400
         */
401
        public void setColor(String color) {
402
                modified = true;
403
                selectionColor = StringUtilities.string2Color(color);
404
        }
405

    
406
        /* (non-Javadoc)
407
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
408
         */
409
        public void propertyChange(PropertyChangeEvent evt) {
410
                this.modified = true;
411
                change.firePropertyChange(evt);
412
        }
413

    
414
        /**
415
         * DOCUMENT ME!
416
         *
417
         * @param arg1
418
         */
419
        public void addExtent(ProjectExtent arg1) {
420
                extents.add(arg1);
421
                modified = true;
422
                change.firePropertyChange("addExtent", null, null);
423
        }
424

    
425
        /**
426
         * DOCUMENT ME!
427
         *
428
         * @param arg0
429
         *
430
         * @return
431
         */
432
        public Object removeExtent(int arg0) {
433
                modified = true;
434
                change.firePropertyChange("delExtent", null, null);
435

    
436
                return extents.remove(arg0);
437
        }
438

    
439
        /**
440
         * DOCUMENT ME!
441
         *
442
         * @return DOCUMENT ME!
443
         */
444
        public ProjectExtent[] getExtents() {
445
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
446
        }
447

    
448
        /**
449
         * DOCUMENT ME!
450
         *
451
         * @param arg0
452
         */
453
        public synchronized void addPropertyChangeListener(
454
                PropertyChangeListener arg0) {
455
                change.addPropertyChangeListener(arg0);
456
        }
457

    
458
        /**
459
         * DOCUMENT ME!
460
         *
461
         * @return
462
         */
463
        public ArrayList getMaps() {
464
                return maps;
465
        }
466

    
467
        /**
468
         * DOCUMENT ME!
469
         *
470
         * @return
471
         */
472
        public ArrayList getTables() {
473
                return tables;
474
        }
475

    
476
        /**
477
         * DOCUMENT ME!
478
         *
479
         * @return
480
         */
481
        public ArrayList getViews() {
482
                return views;
483
        }
484

    
485
        /**
486
         * DOCUMENT ME!
487
         *
488
         * @return DOCUMENT ME!
489
         *
490
         * @throws DriverException
491
         * @throws XMLException
492
         */
493
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
494
                XMLEntity xml = new XMLEntity();
495
                xml.putProperty("className", this.getClass().getName());
496
                xml.putProperty("VERSION", VERSION);
497
                xml.putProperty("comments", comments);
498
                xml.putProperty("creationDate", creationDate);
499

    
500
                int numExtents = extents.size();
501

    
502
                /*double[] xs = new double[size];
503
                   double[] ys = new double[size];
504
                   double[] ws = new double[size];
505
                   double[] hs = new double[size];
506
                 */
507
                xml.putProperty("numExtents", numExtents);
508

    
509
                for (int i = 0; i < numExtents; i++) {
510
                        /*        Rectangle2D rect = (Rectangle2D) extents.get(i);
511
                           xs[i] = rect.getX();
512
                           ys[i] = rect.getY();
513
                           ws[i] = rect.getWidth();
514
                           hs[i] = rect.getHeight();
515
                         */
516
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
517
                }
518

    
519
        // NUEVO: ESTO ESTA EN PRUEBAS. SIRVE PARA
520
        // BORRAR LAS REFERENCIAS A DATASOURCES QUE HEMOS
521
        // BORRADO. Hay que probar a borrarlos cuando se
522
        // borra una tabla y cuando se borra una capa.
523
        cleanBadReferences();
524
                SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
525
                xml.putProperty("data-source-count", infos.length);
526

    
527
                for (int i = 0; i < infos.length; i++) {
528
                        XMLEntity child = new XMLEntity();
529
                        SourceInfo di = infos[i];
530

    
531
                        if (di instanceof ObjectSourceInfo) {
532
                                ObjectSourceInfo driver = (ObjectSourceInfo) di;
533
                                child.putProperty("type", "sameDriverFile");
534
                                child.putProperty("gdbmsname", driver.name);
535
                        } else if (di instanceof FileSourceInfo) {
536
                                FileSourceInfo vfdi = (FileSourceInfo) di;
537
                                child.putProperty("type", "otherDriverFile");
538
                                child.putProperty("gdbmsname", vfdi.name);
539
                                child.putProperty("file", vfdi.file);
540
                                child.putProperty("driverName", vfdi.driverName);
541
                        } else if (di instanceof DBSourceInfo) {
542
                                DBTableSourceInfo dbdi = (DBTableSourceInfo) di;
543
                                child.putProperty("type", "db");
544
                                child.putProperty("gdbmsname", dbdi.name);
545
                                child.putProperty("dbms", dbdi.dbms);
546
                                child.putProperty("host", dbdi.host);
547
                                child.putProperty("port", dbdi.port);
548
                                child.putProperty("user", dbdi.user);
549
                                child.putProperty("password", dbdi.password);
550
                                child.putProperty("dbName", dbdi.dbName);
551
                                child.putProperty("tableName", dbdi.tableName);
552
                                child.putProperty("driverInfo", dbdi.driverName);
553
                        }
554

    
555
                        xml.addChild(child);
556
                }
557

    
558
                /*
559
                   xml.putProperty("extentsX", xs);
560
                   xml.putProperty("extentsY", ys);
561
                   xml.putProperty("extentsW", ws);
562
                   xml.putProperty("extentsH", hs);
563
                 */
564
                
565
                int numViews=0;
566
                for (int i = 0; i < views.size(); i++) {
567
                        try {
568
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
569
                                xml.addChild(xmlchild);
570
                                numViews++;
571
                        } catch (SaveException e) {
572
                                e.showError();
573
                        }
574
                }
575
                xml.putProperty("numViews", numViews);
576
                
577
                int numMaps=0;
578
                for (int i = 0; i < maps.size(); i++) {
579
                        try {
580
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
581
                                xml.addChild(xmlchild);
582
                                numMaps++;
583
                        } catch (SaveException e) {
584
                                e.showError();
585
                        }
586
                }
587
                xml.putProperty("numMaps", numMaps);
588
                xml.putProperty("modificationDate", modificationDate);
589
                xml.putProperty("modified", modified);
590
                xml.putProperty("name", name);
591
                xml.putProperty("owner", owner);
592
                xml.putProperty("path", path);
593
                xml.putProperty("selectionColor",
594
                        StringUtilities.color2String(selectionColor));
595
                
596
                int numTables=0;
597
                for (int i = 0; i < tables.size(); i++) {
598
                        try {
599
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
600
                                xml.addChild(xmlchild);
601
                                numTables++;
602
                        } catch (SaveException e) {
603
                                
604
                                e.showError();
605
                        }
606
                }
607
                xml.putProperty("numTables", numTables);
608
                xml.putProperty("projection", defaultProjection.getAbrev());
609
                // save the properties of the Project Manager window
610
                View []andamiViews = PluginServices.getMDIManager().getAllViews();
611
                for (int i=0; i<andamiViews.length; i++) {
612
                        if (andamiViews[i] instanceof ProjectWindow) {
613
                                ProjectWindow pw = (ProjectWindow) andamiViews[i];
614
                                try {
615
                                        XMLEntity xmlchild = null;
616
                                        xmlchild = getViewInfoXMLEntity(pw);
617
                                        if (xmlchild!=null) {
618
                                                xml.addChild(xmlchild);
619
                                        }
620
                                }
621
                                catch (SaveException e){
622
                                        e.showError();
623
                                }
624
                        }
625
                }
626
                return xml;
627
        }
628
        /**
629
         * DOCUMENT ME!
630
         *
631
         * @param xml DOCUMENT ME!
632
         *
633
         * @return DOCUMENT ME!
634
         * @throws XMLException
635
         * @throws DriverException
636
         * @throws DriverIOException
637
         * @throws OpenException 
638
         */
639
        public static Project createFromXML03(XMLEntity xml)
640
                throws OpenException {
641
                Project p = new Project();
642
                try{
643
                p.comments = xml.getStringProperty("comments");
644
                p.creationDate = xml.getStringProperty("creationDate");
645
/*
646
                double[] xs = xml.getDoubleArrayProperty("extentsX");
647
                double[] ys = xml.getDoubleArrayProperty("extentsY");
648
                double[] ws = xml.getDoubleArrayProperty("extentsW");
649
                double[] hs = xml.getDoubleArrayProperty("extentsH");
650
*/
651
                int numExtents=xml.getIntProperty("numExtents");
652
                for (int i = 0; i < numExtents; i++) {
653
                        ProjectExtent pe=ProjectExtent.createFromXML03(xml.getChild(i));
654
                        p.extents.add(pe);
655
                }
656

    
657
                int numViews = xml.getIntProperty("numViews");
658

    
659
                for (int i = numExtents; i < numViews+numExtents; i++) {
660
                        p.views.add(ProjectView.createFromXML03(xml.getChild(i), p));
661
                }
662

    
663
                int numMaps = xml.getIntProperty("numMaps");
664

    
665
                for (int i = numViews+numExtents; i < (numMaps + numViews+numExtents); i++) {
666
                        p.maps.add(ProjectMap.createFromXML03(xml.getChild(i), p));
667
                }
668

    
669
                p.modificationDate = xml.getStringProperty("modificationDate");
670
                p.modified = xml.getBooleanProperty("modified");
671
                p.name = xml.getStringProperty("name");
672
                p.owner = xml.getStringProperty("owner");
673
                p.path = xml.getStringProperty("path");
674
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
675
                                        "selectionColor"));
676

    
677
                int numTables = xml.getIntProperty("numTables");
678

    
679
                for (int i = numExtents+numMaps + numViews; i < (numExtents+numTables + numMaps + numViews);
680
                                i++) {
681
                        p.tables.add(ProjectTable.createFromXML03(xml.getChild(i), p));
682
                }
683
                String strProj = xml.getStringProperty("projection");
684
                if (strProj != null)
685
                        Project.setProjection(ProjectionPool.get(strProj));
686
                }catch (Exception e) {
687
                        throw new OpenException(e,p.getClass().getName());
688
                }
689
                return p;
690
        }
691

    
692
        /**
693
         * DOCUMENT ME!
694
         *
695
         * @param xml DOCUMENT ME!
696
         *
697
         * @return DOCUMENT ME!
698
         *
699
         * @throws XMLException
700
         * @throws DriverException
701
         * @throws DriverIOException
702
         * @throws OpenException 
703
         * @throws VersionException 
704
         */
705
        public static Project createFromXML(XMLEntity xml)
706
                throws OpenException {
707
                
708
                int childNumber = 0;
709
                Project p = new Project();
710
                try{
711
                p.comments = xml.getStringProperty("comments");
712
                p.creationDate = xml.getStringProperty("creationDate");
713

    
714
                /*
715
                   double[] xs = xml.getDoubleArrayProperty("extentsX");
716
                   double[] ys = xml.getDoubleArrayProperty("extentsY");
717
                   double[] ws = xml.getDoubleArrayProperty("extentsW");
718
                   double[] hs = xml.getDoubleArrayProperty("extentsH");
719
                 */
720
                int numExtents = xml.getIntProperty("numExtents");
721

    
722
                for (int i = 0; i < numExtents; i++) {
723
                        ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
724
                        p.extents.add(pe);
725
                }
726

    
727
                childNumber = numExtents;
728

    
729
                int numDataSources = xml.getIntProperty("data-source-count");
730

    
731
                for (int i = childNumber; i < (childNumber + numDataSources); i++) {
732
                        XMLEntity child = xml.getChild(i);
733

    
734
                        if (child.getStringProperty("type").equals("otherDriverFile")) {
735
                                LayerFactory.getDataSourceFactory().addFileDataSource(child.getStringProperty(
736
                                                "driverName"), child.getStringProperty("gdbmsname"),
737
                                        child.getStringProperty("file"));
738
                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
739
                                /*                                String layerName = child.getStringProperty("layerName");
740
                                   ProjectView vista = project.getViewByName(child.getStringProperty(
741
                                   "viewName"));
742
                                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
743
                                
744
                                   modelo = ((AlphanumericData) layer).getRecordset();
745
                                   associatedTable = (AlphanumericData) layer;
746
                                 */
747
                        } else if (child.getStringProperty("type").equals("db")) {
748
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(child.getStringProperty(
749
                                                "gdbmsname"), child.getStringProperty("host"),
750
                                        child.getIntProperty("port"),
751
                                        child.getStringProperty("user"),
752
                                        child.getStringProperty("password"),
753
                                        child.getStringProperty("dbName"),
754
                                        child.getStringProperty("tableName"),
755
                                        child.getStringProperty("driverInfo"));
756
                        }
757
                }
758

    
759
                childNumber += numDataSources;
760

    
761
                int numViews = xml.getIntProperty("numViews");
762

    
763
                for (int i = childNumber; i < (numViews + childNumber); i++) {
764
                        try{
765
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(xml.getChild(i), p);
766
                                p.addView(pv);
767
                                if (pv.getSeedViewInfo()!=null) { // open the view, if it was open, and restore its dimensions
768
                                        com.iver.cit.gvsig.gui.View vista = new com.iver.cit.gvsig.gui.View();
769
                                        vista.setModel(pv);
770
                                        PluginServices.getMDIManager().addView(vista);  
771
                                        PluginServices.getMDIManager().changeViewInfo(vista, pv.getSeedViewInfo());
772
                                }
773
                        }catch(OpenException e){
774
                                e.showError();
775
                        }
776
                }
777

    
778
                childNumber += numViews;
779

    
780
                int numMaps = xml.getIntProperty("numMaps");
781

    
782
                for (int i = childNumber; i < (numMaps + childNumber); i++) {
783
                        try{
784
                                // p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
785
                                ProjectMap map = (ProjectMap) ProjectMap.createFromXML(xml.getChild(i), p);
786
                                p.maps.add(map);
787
                                if (map.getSeedViewInfo()!=null) { // open the view, if it was open, and restore its dimensions
788
                                        map.getModel().setProjectMap(map);
789
                                        PluginServices.getMDIManager().addView(map.getModel());
790
                                        PluginServices.getMDIManager().changeViewInfo(map.getModel(), map.getSeedViewInfo());
791
                                }
792
                        }catch(OpenException e){
793
                                e.showError();
794
                        }
795
                }
796

    
797
                childNumber += numMaps;
798
                p.modificationDate = xml.getStringProperty("modificationDate");
799
                p.modified = xml.getBooleanProperty("modified");
800
                p.name = xml.getStringProperty("name");
801
                p.owner = xml.getStringProperty("owner");
802
                p.path = xml.getStringProperty("path");
803
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
804
                                        "selectionColor"));
805

    
806
                int numTables = xml.getIntProperty("numTables");
807

    
808
                for (int i = childNumber; i < (childNumber + numTables); i++) {
809
                        try{
810
                                ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xml.getChild(i), p);
811
                                p.tables.add(ptable);
812
                                if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
813
                                        PluginServices.getMDIManager().addView(ptable.getAndamiView());
814
                                        PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
815
                                }
816
                        }catch(OpenException e){
817
                                e.showError();
818
                        }
819
                }
820
                
821
                childNumber += numTables;
822
                if (childNumber < xml.getNumChild()) { // restore the position of the project manager window
823
                        XMLEntity child = xml.getChild(childNumber);
824
                        if (child.contains("className") 
825
                                        && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.Project")
826
                                        && child.contains("name")
827
                                        && child.getStringProperty("name").equals("ViewInfoProperties")) {
828
                                seedProjectWindow = Project.createViewInfoFromXMLEntity(xml.getChild(childNumber));
829
                        }
830
                }
831

    
832
                p.setLinkTable();
833

    
834
                String strProj = xml.getStringProperty("projection");
835

    
836
                if (strProj != null) {
837
                        Project.setProjection(ProjectionPool.get(strProj));
838
                }
839
        PostProcessSupport.executeCalls();
840
                }catch (Exception e) {
841
                        throw new OpenException(e,p.getClass().getName());
842
                }
843
                return p;
844
                
845
        }
846

    
847
        /**
848
         * Reestablece los link que ten?a cada tabla con las dem?s.
849
         */
850
        public void setLinkTable() {
851
                ArrayList tables = getTables();
852

    
853
                for (int i = 0; i < tables.size(); i++) {
854
                        for (int j = 0; j < tables.size(); j++) {
855
                                /*System.out.println("name = " +
856
                                        ((ProjectTable) tables.get(j)).getModelo().getName());
857
                                System.out.println("linktable = " +
858
                                        ((ProjectTable) tables.get(i)).getLinkTable());
859
*/
860
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
861
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
862
                                                                                                                                                                  .get(
863
                                                                j)).getModelo().getName())) {
864
                                        LinkSelectionListener lsl = new LinkSelectionListener(((ProjectTable) tables.get(
865
                                                                i)).getModelo(),
866
                                                        ((ProjectTable) tables.get(j)).getModelo(),
867
                                                        ((ProjectTable) tables.get(i)).getField1(),
868
                                                        ((ProjectTable) tables.get(i)).getField2());
869
                                        ((ProjectTable) tables.get(i)).getModelo()
870
                                         .addSelectionListener(lsl);
871
                                }
872
                        }
873
                }
874
        }
875

    
876
        /**
877
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
878
         *
879
         * @param layer Capa cuya vista se quiere obtener
880
         *
881
         * @return
882
         *
883
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
884
         *                    encuentra en ninguna vista
885
         */
886
        public String getView(FLayer layer) {
887
                for (int v = 0; v < views.size(); v++) {
888
                        ProjectView pView = (ProjectView) views.get(v);
889
                        FLayers layers = pView.getMapContext().getLayers();
890
                        if (isView(layers,layer))return pView.getName();
891
                }
892

    
893
                throw new RuntimeException("The layer is not in a view");
894
        }
895
        public boolean isView(FLayers layers,FLayer layer) {
896
                        for (int i = 0; i < layers.getLayersCount(); i++) {
897
                                if (layers.getLayer(i) instanceof FLayers){
898
                                        return isView((FLayers)layers.getLayer(i),layer);
899
                                }else{
900
                                if (layers.getLayer(i) == layer) {
901
                                        return true;
902
                                }
903
                                }
904
                        }
905
                        return false;
906
        }
907
        /**
908
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas) con el
909
         * que se pasa como par?metro. Devuelve null si no hay ninguna vista con
910
         * ese nombre
911
         *
912
         * @param viewName Nombre de la vista que se quiere obtener
913
         *
914
         * @return DOCUMENT ME!
915
         */
916
        public ProjectView getViewByName(String viewName) {
917
                Object o = getProjectElementByName(viewName, views);
918

    
919
                if (o == null) {
920
                        return null;
921
                }
922

    
923
                return (ProjectView) o;
924
        }
925

    
926
        /**
927
         * DOCUMENT ME!
928
         *
929
         * @return DOCUMENT ME!
930
         */
931
        public static IProjection getProjection() {
932
                return defaultProjection;
933
        }
934

    
935
        /**
936
         * DOCUMENT ME!
937
         *
938
         * @param defaultProjection DOCUMENT ME!
939
         */
940
        public static void setProjection(IProjection defaultProjection) {
941
                Project.defaultProjection = defaultProjection;
942
        }
943

    
944
        /**
945
         * DOCUMENT ME!
946
         *
947
         * @param name DOCUMENT ME!
948
         * @param l DOCUMENT ME!
949
         *
950
         * @return DOCUMENT ME!
951
         */
952
        private Object getProjectElementByName(String name, ArrayList l) {
953
                for (Iterator iter = l.iterator(); iter.hasNext();) {
954
                        ProjectElement elem = (ProjectElement) iter.next();
955

    
956
                        if (elem.getName().equals(name)) {
957
                                return elem;
958
                        }
959
                }
960

    
961
                return null;
962
        }
963

    
964
        /**
965
         * DOCUMENT ME!
966
         *
967
         * @param name
968
         *
969
         * @return
970
         */
971
        public ProjectTable getTableByName(String name) {
972
                Object o = getProjectElementByName(name, tables);
973

    
974
                if (o == null) {
975
                        return null;
976
                }
977

    
978
                return (ProjectTable) o;
979
        }
980

    
981
        /**
982
         * DOCUMENT ME!
983
         *
984
         * @param name
985
         *
986
         * @return
987
         */
988
        public ProjectMap getLayoutByName(String name) {
989
                Object o = getProjectElementByName(name, maps);
990

    
991
                if (o == null) {
992
                        return null;
993
                }
994

    
995
                return (ProjectMap) o;
996
        }
997
    
998
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
999
        ArrayList tables = getTables();
1000
        SelectableDataSource dataSource = null;
1001
        for (int i = 0; i < tables.size(); i++) {
1002
            ProjectTable pt = (ProjectTable) tables.get(i);
1003
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
1004
                dataSource = pt.getModelo();
1005
                break;
1006
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
1007
                dataSource = pt.getModelo();
1008
                break;
1009
            }
1010
        }
1011
        
1012
        if (dataSource == null){
1013
            //No se ha creado la tabla asociada al tema
1014
            dataSource = ((AlphanumericData) layer).getRecordset();
1015
        }
1016
        
1017
        return dataSource;
1018
        
1019
    }
1020
    
1021
    /**
1022
     * Recorremos las capas y las tablas del proyecto, y creamos una lista
1023
     * con todos los datasources de GDBMS que estamos usando.
1024
     * Luego recorremos los que est?n registrados, y borramos
1025
     * aquellos que no est?n siendo usados, es decir, aquellos
1026
     * que no est?n en nuestra lista (un Hash con clave el nombre
1027
     * del GDBMS)
1028
     * 
1029
     */
1030
    private void cleanBadReferences()
1031
    {
1032
        Hashtable usedDataSources = new Hashtable();
1033
        // Primero las tablas
1034
        int i, j;
1035
        for (i=0; i<tables.size(); i++)
1036
        {
1037
            ProjectTable t = (ProjectTable) tables.get(i);
1038
            SelectableDataSource ds = t.getModelo();
1039
            if (t.getOriginal() != null)
1040
                usedDataSources.put(t.getOriginal().getName(), t.getOriginal());
1041
            usedDataSources.put(ds.getName(), ds);
1042
        }
1043
        
1044
        // Ahora las vistas
1045
        try {
1046
            for (i=0; i<views.size(); i++)
1047
            {
1048
                ProjectView pv = (ProjectView) views.get(i);
1049
                
1050
                FLayers lyrs = pv.getMapContext().getLayers();
1051
                FLayers lyrsOverview = pv.getMapOverViewContext().getLayers();
1052
                for (j=0; j<lyrs.getLayersCount(); j++)
1053
                {
1054
                    FLayer lyr = lyrs.getLayer(j);
1055
                    if (lyr instanceof FLyrVect)
1056
                    {
1057
                        FLyrVect lyrVect = (FLyrVect) lyr;
1058
                        usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getRecordset());
1059
                    }
1060
                }
1061
                for (j=0; j<lyrsOverview.getLayersCount(); j++)
1062
                {
1063
                    FLayer lyr = lyrsOverview.getLayer(j);
1064
                    if (lyr instanceof FLyrVect)
1065
                    {
1066
                        FLyrVect lyrVect = (FLyrVect) lyr;
1067
                        usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getRecordset());
1068
                    }
1069
                }
1070
                
1071
            } // for i
1072
        } catch (DriverException e) {
1073
            // TODO Auto-generated catch block
1074
            e.printStackTrace();
1075
        }
1076
        // Recorremos los dataSources y los borramos si no
1077
        // los estamos usando.
1078
        SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
1079
        try {
1080
            for (i=0; i < infos.length; i++)
1081
            {
1082
                if (!usedDataSources.containsKey(infos[i].name))
1083
                {
1084
                    DataSource ds;
1085
                    ds = LayerFactory.getDataSourceFactory().createRandomDataSource(infos[i].name);
1086
                    ds.remove();
1087
                }
1088
            }
1089
        } catch (DriverLoadException e) {
1090
            // TODO Auto-generated catch block
1091
            e.printStackTrace();
1092
        } catch (NoSuchTableException e) {
1093
            // TODO Auto-generated catch block
1094
            e.printStackTrace();
1095
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1096
            // TODO Auto-generated catch block
1097
            e.printStackTrace();
1098
        }
1099
    }
1100
    
1101
    
1102
    /**
1103
         * DOCUMENT ME!
1104
         *
1105
         * @return DOCUMENT ME!
1106
     * @throws SaveException 
1107
         * @throws XMLException
1108
         * @throws SaveException 
1109
         */
1110
        public XMLEntity getViewInfoXMLEntity(com.iver.andami.ui.mdiManager.View view) throws SaveException {
1111
                if (view!=null && PluginServices.getMDIManager().getViewInfo(view)!=null) {
1112
                        ViewInfo vi = PluginServices.getMDIManager().getViewInfo(view);
1113
                        XMLEntity xml = new XMLEntity();
1114
                        //xml.putProperty("nameClass", this.getClass().getName());
1115
                        try{
1116
                                xml.setName("ViewInfoProperties");
1117
                                xml.putProperty("className", this.getClass().getName());
1118
                                xml.putProperty("X", vi.getX());
1119
                                xml.putProperty("Y", vi.getY());
1120
                                xml.putProperty("Width", vi.getWidth());
1121
                                xml.putProperty("Height", vi.getHeight());
1122
                                xml.putProperty("isVisible", vi.isVisible());
1123
                                xml.putProperty("isResizable", vi.isResizable());
1124
                                xml.putProperty("isMaximizable", vi.isMaximizable());
1125
                                xml.putProperty("isModal", vi.isModal());
1126
                                xml.putProperty("isModeless", vi.isModeless());
1127
                                xml.putProperty("isClosed", vi.isClosed());
1128
                                if (vi.isMaximized()==true) {
1129
                                        xml.putProperty("isMaximized", vi.isMaximized());
1130
                                        xml.putProperty("normalX", vi.getNormalX());
1131
                                        xml.putProperty("normalY", vi.getNormalY());
1132
                                        xml.putProperty("normalWidth", vi.getNormalWidth());
1133
                                        xml.putProperty("normalHeight", vi.getNormalHeight());
1134
                                }
1135
                        }catch (Exception e) {
1136
                                throw new SaveException(e,this.getClass().getName());
1137
                        }
1138
                        return xml;
1139
                }
1140
                else
1141
                        return null;
1142
        }
1143
        
1144
        public static ViewInfo createViewInfoFromXMLEntity(XMLEntity xml)
1145
        {
1146
                ViewInfo result = new ViewInfo();
1147
                result.setX(xml.getIntProperty("X"));
1148
                result.setY(xml.getIntProperty("Y"));
1149
                result.setHeight(xml.getIntProperty("Height"));
1150
                result.setWidth(xml.getIntProperty("Width"));
1151
                result.setClosed(xml.getBooleanProperty("isClosed"));
1152
                if (xml.contains("isMaximized")) {
1153
                        boolean maximized = xml.getBooleanProperty("isMaximized");
1154
                        result.setMaximized(maximized);
1155
                        if (maximized==true) {
1156
                                result.setNormalBounds(xml.getIntProperty("normalX"), xml.getIntProperty("normalY"), xml.getIntProperty("normalWidth"), xml.getIntProperty("normalHeight"));
1157
                        }
1158
                        else {
1159
                                result.setNormalBounds(result.getBounds());
1160
                        }
1161
                }
1162

    
1163
                return result;
1164
        }
1165
        
1166
        public static ViewInfo getSeedProjectWindow() {
1167
                return seedProjectWindow;
1168
        }
1169
}