Statistics
| Revision:

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

History | View | Annotate | Download (27.1 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.cit.gvsig.fmap.DriverException;
66
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
67
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLayers;
70
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
71
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
72
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
73
import com.iver.cit.gvsig.fmap.layers.XMLException;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
75
import com.iver.cit.gvsig.fmap.operations.selection.LinkSelectionListener;
76
import com.iver.cit.gvsig.gui.project.OpenException;
77
import com.iver.cit.gvsig.gui.project.SaveException;
78
import com.iver.utiles.PostProcessSupport;
79
import com.iver.utiles.StringUtilities;
80
import com.iver.utiles.XMLEntity;
81

    
82

    
83
/**
84
 * Clase que representa un proyecto de openSIG
85
 *
86
 * @author Fernando Gonz?lez Cort?s
87
 */
88
public class Project implements Serializable, PropertyChangeListener {
89
        public static String VERSION = "0.6";
90
        static private IProjection defaultProjection = ProjectionPool.get(
91
                        "EPSG:23030");
92
        private PropertyChangeSupport change;
93
        boolean modified = false;
94
        private String name;
95
        private String path;
96
        private String creationDate;
97
        private String modificationDate;
98
        private String owner;
99
        private String comments;
100
        private Color selectionColor = new Color(255, 255, 0);
101
        private ArrayList views = new ArrayList();
102
        private ArrayList tables = new ArrayList();
103
        private ArrayList maps = new ArrayList();
104
        private ArrayList extents = new ArrayList();
105

    
106
        /**
107
         * Creates a new Project object.
108
         */
109
        public Project() {
110
                change = new PropertyChangeSupport(this);
111

    
112
                //        change.addPropertyChangeListener(this);
113
                creationDate = DateFormat.getDateInstance().format(new Date());
114
                modificationDate = creationDate;
115

    
116
                /*                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
117
                   .getPluginDirectory()
118
                   .getAbsolutePath() +
119
                   File.separator + "drivers");
120
                 */
121
        }
122

    
123
        /**
124
         * Obtiene la fecha de creaci?n del proyecto
125
         *
126
         * @return
127
         */
128
        public String getCreationDate() {
129
                return creationDate;
130
        }
131

    
132
        /**
133
         * Obtiene el nombre del proyecto
134
         *
135
         * @return
136
         */
137
        public String getName() {
138
                return name;
139
        }
140

    
141
        /**
142
         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
143
         * proyecto
144
         *
145
         * @return
146
         */
147
        public String getPath() {
148
                return path;
149
        }
150

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

    
165
        /**
166
         * A?ade un mapa al proyecto
167
         *
168
         * @param m
169
         */
170
        public void addMap(ProjectMap m) {
171
                maps.add(m);
172
                m.addPropertyChangeListener(this);
173
                modified = true;
174
                change.firePropertyChange("", null, null);
175
                m.setProject(this, maps.size() - 1);
176
        }
177

    
178
        /**
179
         * Elimina un mapa del proyecto
180
         *
181
         * @param i indice del mapa
182
         */
183
        public void delMap(int i) {
184
                maps.remove(i);
185
                modified = true;
186
                change.firePropertyChange("", null, null);
187
        }
188

    
189
        /**
190
         * Establece el nombre del proyecto
191
         *
192
         * @param string
193
         */
194
        public void setName(String string) {
195
                name = string;
196
                modified = true;
197
                change.firePropertyChange("", null, null);
198
        }
199

    
200
        /**
201
         * establece la ruta completa de donde se encuentra guardado el proyecto
202
         *
203
         * @param string
204
         */
205
        public void setPath(String string) {
206
                path = string;
207
                modified = true;
208
                change.firePropertyChange("", null, null);
209
        }
210

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

    
229
                return null;
230
        }
231

    
232
        /**
233
         * A?ade una tabla al proyecto
234
         *
235
         * @param t
236
         */
237
        public void addTable(ProjectTable t) {
238
                tables.add(t);
239
                t.addPropertyChangeListener(this);
240
                modified = true;
241
                change.firePropertyChange("", null, null);
242
                t.setProject(this, tables.size() - 1);
243
        }
244

    
245
        /**
246
         * Elimina una tabla del proyecto
247
         *
248
         * @param i indice de la tabla
249
         */
250
        public void delTable(int i) {
251
                tables.remove(i);
252
                modified = true;
253
                change.firePropertyChange("", null, null);
254
        }
255

    
256
        /**
257
         * A?ade una vista al proyecto
258
         *
259
         * @param v
260
         */
261
        public void addView(ProjectView v) {
262
                views.add(v);
263
                v.addPropertyChangeListener(this);
264
                modified = true;
265
                change.firePropertyChange("", null, null);
266
                v.setProject(this, views.size() - 1);
267
        }
268

    
269
        /**
270
         * Elimina una tabla del proyecto
271
         *
272
         * @param i indice del proyecto
273
         */
274
        public void delView(int i) {
275
                views.remove(i);
276
                modified = true;
277
                change.firePropertyChange("", null, null);
278
        }
279

    
280
        /**
281
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
282
         * fue modificado
283
         *
284
         * @return
285
         */
286
        public boolean isModified() {
287
                return modified;
288
        }
289

    
290
        /**
291
         * Obtiene los comentarios
292
         *
293
         * @return
294
         */
295
        public String getComments() {
296
                return comments;
297
        }
298

    
299
        /**
300
         * Obtiene la fecha de la ?ltima modificaci?n
301
         *
302
         * @return
303
         */
304
        public String getModificationDate() {
305
                return modificationDate;
306
        }
307

    
308
        /**
309
         * Obtiene el propietario del proyecto
310
         *
311
         * @return
312
         */
313
        public String getOwner() {
314
                return owner;
315
        }
316

    
317
        /**
318
         * Establece una cadena como comentarios al proyecto
319
         *
320
         * @param string
321
         */
322
        public void setComments(String string) {
323
                comments = string;
324
                modified = true;
325
                change.firePropertyChange("", null, null);
326
        }
327

    
328
        /**
329
         * Establece la fecha de la ?ltima modificaci?n
330
         *
331
         * @param string
332
         */
333
        public void setModificationDate(String string) {
334
                modificationDate = string;
335
                modified = true;
336
                change.firePropertyChange("", null, null);
337
        }
338

    
339
        /**
340
         * Establece el propietario del proyecto
341
         *
342
         * @param string
343
         */
344
        public void setOwner(String string) {
345
                owner = string;
346
                modified = true;
347
                change.firePropertyChange("", null, null);
348
        }
349

    
350
        /**
351
         * Establece el flag de modificado del proyecto
352
         *
353
         * @param b
354
         */
355
        public void setModified(boolean b) {
356
                modified = b;
357
        }
358

    
359
        /**
360
         * Obtiene el color de selecci?n que se usar? en el proyecto
361
         *
362
         * @return
363
         */
364
        public Color getSelectionColor() {
365
                return selectionColor;
366
        }
367

    
368
        /**
369
         * Establece el color de selecci?n
370
         *
371
         * @param color
372
         */
373
        public void setSelectionColor(Color color) {
374
                selectionColor = color;
375
                FSymbol.setSelectionColor(color);
376
                modified = true;
377
                change.firePropertyChange("selectionColor", null, color);
378
        }
379

    
380
        /**
381
         * Obtiene el color como un entero para su serializaci?n a XML
382
         *
383
         * @return
384
         */
385
        public String getColor() {
386
                return StringUtilities.color2String(selectionColor);
387
        }
388

    
389
        /**
390
         * M?todo invocado al recuperar de XML para establecer el color de
391
         * seleccion del proyecto
392
         *
393
         * @param color Entero que representa un color
394
         */
395
        public void setColor(String color) {
396
                modified = true;
397
                selectionColor = StringUtilities.string2Color(color);
398
        }
399

    
400
        /* (non-Javadoc)
401
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
402
         */
403
        public void propertyChange(PropertyChangeEvent evt) {
404
                this.modified = true;
405
                change.firePropertyChange(evt);
406
        }
407

    
408
        /**
409
         * DOCUMENT ME!
410
         *
411
         * @param arg1
412
         */
413
        public void addExtent(ProjectExtent arg1) {
414
                extents.add(arg1);
415
                modified = true;
416
                change.firePropertyChange("addExtent", null, null);
417
        }
418

    
419
        /**
420
         * DOCUMENT ME!
421
         *
422
         * @param arg0
423
         *
424
         * @return
425
         */
426
        public Object removeExtent(int arg0) {
427
                modified = true;
428
                change.firePropertyChange("delExtent", null, null);
429

    
430
                return extents.remove(arg0);
431
        }
432

    
433
        /**
434
         * DOCUMENT ME!
435
         *
436
         * @return DOCUMENT ME!
437
         */
438
        public ProjectExtent[] getExtents() {
439
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
440
        }
441

    
442
        /**
443
         * DOCUMENT ME!
444
         *
445
         * @param arg0
446
         */
447
        public synchronized void addPropertyChangeListener(
448
                PropertyChangeListener arg0) {
449
                change.addPropertyChangeListener(arg0);
450
        }
451

    
452
        /**
453
         * DOCUMENT ME!
454
         *
455
         * @return
456
         */
457
        public ArrayList getMaps() {
458
                return maps;
459
        }
460

    
461
        /**
462
         * DOCUMENT ME!
463
         *
464
         * @return
465
         */
466
        public ArrayList getTables() {
467
                return tables;
468
        }
469

    
470
        /**
471
         * DOCUMENT ME!
472
         *
473
         * @return
474
         */
475
        public ArrayList getViews() {
476
                return views;
477
        }
478

    
479
        /**
480
         * DOCUMENT ME!
481
         *
482
         * @return DOCUMENT ME!
483
         *
484
         * @throws DriverException
485
         * @throws XMLException
486
         */
487
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
488
                XMLEntity xml = new XMLEntity();
489
                xml.putProperty("className", this.getClass().getName());
490
                xml.putProperty("VERSION", VERSION);
491
                xml.putProperty("comments", comments);
492
                xml.putProperty("creationDate", creationDate);
493

    
494
                int numExtents = extents.size();
495

    
496
                /*double[] xs = new double[size];
497
                   double[] ys = new double[size];
498
                   double[] ws = new double[size];
499
                   double[] hs = new double[size];
500
                 */
501
                xml.putProperty("numExtents", numExtents);
502

    
503
                for (int i = 0; i < numExtents; i++) {
504
                        /*        Rectangle2D rect = (Rectangle2D) extents.get(i);
505
                           xs[i] = rect.getX();
506
                           ys[i] = rect.getY();
507
                           ws[i] = rect.getWidth();
508
                           hs[i] = rect.getHeight();
509
                         */
510
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
511
                }
512

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

    
521
                for (int i = 0; i < infos.length; i++) {
522
                        XMLEntity child = new XMLEntity();
523
                        SourceInfo di = infos[i];
524

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

    
549
                        xml.addChild(child);
550
                }
551

    
552
                /*
553
                   xml.putProperty("extentsX", xs);
554
                   xml.putProperty("extentsY", ys);
555
                   xml.putProperty("extentsW", ws);
556
                   xml.putProperty("extentsH", hs);
557
                 */
558
                
559
                int numViews=0;
560
                for (int i = 0; i < views.size(); i++) {
561
                        try {
562
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
563
                                xml.addChild(xmlchild);
564
                                numViews++;
565
                        } catch (SaveException e) {
566
                                e.showError();
567
                        }
568
                }
569
                xml.putProperty("numViews", numViews);
570
                
571
                int numMaps=0;
572
                for (int i = 0; i < maps.size(); i++) {
573
                        try {
574
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
575
                                xml.addChild(xmlchild);
576
                                numMaps++;
577
                        } catch (SaveException e) {
578
                                e.showError();
579
                        }
580
                }
581
                xml.putProperty("numMaps", numMaps);
582
                xml.putProperty("modificationDate", modificationDate);
583
                xml.putProperty("modified", modified);
584
                xml.putProperty("name", name);
585
                xml.putProperty("owner", owner);
586
                xml.putProperty("path", path);
587
                xml.putProperty("selectionColor",
588
                        StringUtilities.color2String(selectionColor));
589
                
590
                int numTables=0;
591
                for (int i = 0; i < tables.size(); i++) {
592
                        try {
593
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
594
                                xml.addChild(xmlchild);
595
                                numTables++;
596
                        } catch (SaveException e) {
597
                                
598
                                e.showError();
599
                        }
600
                }
601
                xml.putProperty("numTables", numTables);
602
                xml.putProperty("projection", defaultProjection.getAbrev());
603

    
604
                return xml;
605
        }
606
        /**
607
         * DOCUMENT ME!
608
         *
609
         * @param xml DOCUMENT ME!
610
         *
611
         * @return DOCUMENT ME!
612
         * @throws XMLException
613
         * @throws DriverException
614
         * @throws DriverIOException
615
         * @throws OpenException 
616
         */
617
        public static Project createFromXML03(XMLEntity xml)
618
                throws OpenException {
619
                Project p = new Project();
620
                try{
621
                p.comments = xml.getStringProperty("comments");
622
                p.creationDate = xml.getStringProperty("creationDate");
623
/*
624
                double[] xs = xml.getDoubleArrayProperty("extentsX");
625
                double[] ys = xml.getDoubleArrayProperty("extentsY");
626
                double[] ws = xml.getDoubleArrayProperty("extentsW");
627
                double[] hs = xml.getDoubleArrayProperty("extentsH");
628
*/
629
                int numExtents=xml.getIntProperty("numExtents");
630
                for (int i = 0; i < numExtents; i++) {
631
                        ProjectExtent pe=ProjectExtent.createFromXML03(xml.getChild(i));
632
                        p.extents.add(pe);
633
                }
634

    
635
                int numViews = xml.getIntProperty("numViews");
636

    
637
                for (int i = numExtents; i < numViews+numExtents; i++) {
638
                        p.views.add(ProjectView.createFromXML03(xml.getChild(i), p));
639
                }
640

    
641
                int numMaps = xml.getIntProperty("numMaps");
642

    
643
                for (int i = numViews+numExtents; i < (numMaps + numViews+numExtents); i++) {
644
                        p.maps.add(ProjectMap.createFromXML03(xml.getChild(i), p));
645
                }
646

    
647
                p.modificationDate = xml.getStringProperty("modificationDate");
648
                p.modified = xml.getBooleanProperty("modified");
649
                p.name = xml.getStringProperty("name");
650
                p.owner = xml.getStringProperty("owner");
651
                p.path = xml.getStringProperty("path");
652
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
653
                                        "selectionColor"));
654

    
655
                int numTables = xml.getIntProperty("numTables");
656

    
657
                for (int i = numExtents+numMaps + numViews; i < (numExtents+numTables + numMaps + numViews);
658
                                i++) {
659
                        p.tables.add(ProjectTable.createFromXML03(xml.getChild(i), p));
660
                }
661
                String strProj = xml.getStringProperty("projection");
662
                if (strProj != null)
663
                        Project.setProjection(ProjectionPool.get(strProj));
664
                }catch (Exception e) {
665
                        throw new OpenException(e,p.getClass().getName());
666
                }
667
                return p;
668
        }
669

    
670
        /**
671
         * DOCUMENT ME!
672
         *
673
         * @param xml DOCUMENT ME!
674
         *
675
         * @return DOCUMENT ME!
676
         *
677
         * @throws XMLException
678
         * @throws DriverException
679
         * @throws DriverIOException
680
         * @throws OpenException 
681
         * @throws VersionException 
682
         */
683
        public static Project createFromXML(XMLEntity xml)
684
                throws OpenException {
685
                
686
                int childNumber = 0;
687
                Project p = new Project();
688
                try{
689
                p.comments = xml.getStringProperty("comments");
690
                p.creationDate = xml.getStringProperty("creationDate");
691

    
692
                /*
693
                   double[] xs = xml.getDoubleArrayProperty("extentsX");
694
                   double[] ys = xml.getDoubleArrayProperty("extentsY");
695
                   double[] ws = xml.getDoubleArrayProperty("extentsW");
696
                   double[] hs = xml.getDoubleArrayProperty("extentsH");
697
                 */
698
                int numExtents = xml.getIntProperty("numExtents");
699

    
700
                for (int i = 0; i < numExtents; i++) {
701
                        ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
702
                        p.extents.add(pe);
703
                }
704

    
705
                childNumber = numExtents;
706

    
707
                int numDataSources = xml.getIntProperty("data-source-count");
708

    
709
                for (int i = childNumber; i < (childNumber + numDataSources); i++) {
710
                        XMLEntity child = xml.getChild(i);
711

    
712
                        if (child.getStringProperty("type").equals("otherDriverFile")) {
713
                                LayerFactory.getDataSourceFactory().addFileDataSource(child.getStringProperty(
714
                                                "driverName"), child.getStringProperty("gdbmsname"),
715
                                        child.getStringProperty("file"));
716
                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
717
                                /*                                String layerName = child.getStringProperty("layerName");
718
                                   ProjectView vista = project.getViewByName(child.getStringProperty(
719
                                   "viewName"));
720
                                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
721
                                
722
                                   modelo = ((AlphanumericData) layer).getRecordset();
723
                                   associatedTable = (AlphanumericData) layer;
724
                                 */
725
                        } else if (child.getStringProperty("type").equals("db")) {
726
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(child.getStringProperty(
727
                                                "gdbmsname"), child.getStringProperty("host"),
728
                                        child.getIntProperty("port"),
729
                                        child.getStringProperty("user"),
730
                                        child.getStringProperty("password"),
731
                                        child.getStringProperty("dbName"),
732
                                        child.getStringProperty("tableName"),
733
                                        child.getStringProperty("driverInfo"));
734
                        }
735
                }
736

    
737
                childNumber += numDataSources;
738

    
739
                int numViews = xml.getIntProperty("numViews");
740

    
741
                for (int i = childNumber; i < (numViews + childNumber); i++) {
742
                        try{
743
                                p.views.add(ProjectView.createFromXML(xml.getChild(i), p));
744
                        }catch(OpenException e){
745
                                e.showError();
746
                        }
747
                }
748

    
749
                childNumber += numViews;
750

    
751
                int numMaps = xml.getIntProperty("numMaps");
752

    
753
                for (int i = childNumber; i < (numMaps + childNumber); i++) {
754
                        try{
755
                                p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
756
                        }catch(OpenException e){
757
                                e.showError();
758
                        }
759
                }
760

    
761
                childNumber += numMaps;
762
                p.modificationDate = xml.getStringProperty("modificationDate");
763
                p.modified = xml.getBooleanProperty("modified");
764
                p.name = xml.getStringProperty("name");
765
                p.owner = xml.getStringProperty("owner");
766
                p.path = xml.getStringProperty("path");
767
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
768
                                        "selectionColor"));
769

    
770
                int numTables = xml.getIntProperty("numTables");
771

    
772
                for (int i = childNumber; i < (childNumber + numTables); i++) {
773
                        try{
774
                                p.tables.add(ProjectTable.createFromXML(xml.getChild(i), p));
775
                        }catch(OpenException e){
776
                                e.showError();
777
                        }
778
                }
779

    
780
                p.setLinkTable();
781

    
782
                String strProj = xml.getStringProperty("projection");
783

    
784
                if (strProj != null) {
785
                        Project.setProjection(ProjectionPool.get(strProj));
786
                }
787
        PostProcessSupport.executeCalls();
788
                }catch (Exception e) {
789
                        throw new OpenException(e,p.getClass().getName());
790
                }
791
                return p;
792
                
793
        }
794

    
795
        /**
796
         * Reestablece los link que ten?a cada tabla con las dem?s.
797
         */
798
        public void setLinkTable() {
799
                ArrayList tables = getTables();
800

    
801
                for (int i = 0; i < tables.size(); i++) {
802
                        for (int j = 0; j < tables.size(); j++) {
803
                                /*System.out.println("name = " +
804
                                        ((ProjectTable) tables.get(j)).getModelo().getName());
805
                                System.out.println("linktable = " +
806
                                        ((ProjectTable) tables.get(i)).getLinkTable());
807
*/
808
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
809
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
810
                                                                                                                                                                  .get(
811
                                                                j)).getModelo().getName())) {
812
                                        LinkSelectionListener lsl = new LinkSelectionListener(((ProjectTable) tables.get(
813
                                                                i)).getModelo(),
814
                                                        ((ProjectTable) tables.get(j)).getModelo(),
815
                                                        ((ProjectTable) tables.get(i)).getField1(),
816
                                                        ((ProjectTable) tables.get(i)).getField2());
817
                                        ((ProjectTable) tables.get(i)).getModelo()
818
                                         .addSelectionListener(lsl);
819
                                }
820
                        }
821
                }
822
        }
823

    
824
        /**
825
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
826
         *
827
         * @param layer Capa cuya vista se quiere obtener
828
         *
829
         * @return
830
         *
831
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
832
         *                    encuentra en ninguna vista
833
         */
834
        public String getView(FLayer layer) {
835
                for (int v = 0; v < views.size(); v++) {
836
                        ProjectView pView = (ProjectView) views.get(v);
837
                        FLayers layers = pView.getMapContext().getLayers();
838
                        if (isView(layers,layer))return pView.getName();
839
                }
840

    
841
                throw new RuntimeException("The layer is not in a view");
842
        }
843
        public boolean isView(FLayers layers,FLayer layer) {
844
                        for (int i = 0; i < layers.getLayersCount(); i++) {
845
                                if (layers.getLayer(i) instanceof FLayers){
846
                                        return isView((FLayers)layers.getLayer(i),layer);
847
                                }else{
848
                                if (layers.getLayer(i) == layer) {
849
                                        return true;
850
                                }
851
                                }
852
                        }
853
                        return false;
854
        }
855
        /**
856
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas) con el
857
         * que se pasa como par?metro. Devuelve null si no hay ninguna vista con
858
         * ese nombre
859
         *
860
         * @param viewName Nombre de la vista que se quiere obtener
861
         *
862
         * @return DOCUMENT ME!
863
         */
864
        public ProjectView getViewByName(String viewName) {
865
                Object o = getProjectElementByName(viewName, views);
866

    
867
                if (o == null) {
868
                        return null;
869
                }
870

    
871
                return (ProjectView) o;
872
        }
873

    
874
        /**
875
         * DOCUMENT ME!
876
         *
877
         * @return DOCUMENT ME!
878
         */
879
        public static IProjection getProjection() {
880
                return defaultProjection;
881
        }
882

    
883
        /**
884
         * DOCUMENT ME!
885
         *
886
         * @param defaultProjection DOCUMENT ME!
887
         */
888
        public static void setProjection(IProjection defaultProjection) {
889
                Project.defaultProjection = defaultProjection;
890
        }
891

    
892
        /**
893
         * DOCUMENT ME!
894
         *
895
         * @param name DOCUMENT ME!
896
         * @param l DOCUMENT ME!
897
         *
898
         * @return DOCUMENT ME!
899
         */
900
        private Object getProjectElementByName(String name, ArrayList l) {
901
                for (Iterator iter = l.iterator(); iter.hasNext();) {
902
                        ProjectElement elem = (ProjectElement) iter.next();
903

    
904
                        if (elem.getName().equals(name)) {
905
                                return elem;
906
                        }
907
                }
908

    
909
                return null;
910
        }
911

    
912
        /**
913
         * DOCUMENT ME!
914
         *
915
         * @param name
916
         *
917
         * @return
918
         */
919
        public ProjectTable getTableByName(String name) {
920
                Object o = getProjectElementByName(name, tables);
921

    
922
                if (o == null) {
923
                        return null;
924
                }
925

    
926
                return (ProjectTable) o;
927
        }
928

    
929
        /**
930
         * DOCUMENT ME!
931
         *
932
         * @param name
933
         *
934
         * @return
935
         */
936
        public ProjectMap getLayoutByName(String name) {
937
                Object o = getProjectElementByName(name, maps);
938

    
939
                if (o == null) {
940
                        return null;
941
                }
942

    
943
                return (ProjectMap) o;
944
        }
945
    
946
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
947
        ArrayList tables = getTables();
948
        SelectableDataSource dataSource = null;
949
        for (int i = 0; i < tables.size(); i++) {
950
            ProjectTable pt = (ProjectTable) tables.get(i);
951
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
952
                dataSource = pt.getModelo();
953
                break;
954
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
955
                dataSource = pt.getModelo();
956
                break;
957
            }
958
        }
959
        
960
        if (dataSource == null){
961
            //No se ha creado la tabla asociada al tema
962
            dataSource = ((AlphanumericData) layer).getRecordset();
963
        }
964
        
965
        return dataSource;
966
        
967
    }
968
    
969
    /**
970
     * Recorremos las capas y las tablas del proyecto, y creamos una lista
971
     * con todos los datasources de GDBMS que estamos usando.
972
     * Luego recorremos los que est?n registrados, y borramos
973
     * aquellos que no est?n siendo usados, es decir, aquellos
974
     * que no est?n en nuestra lista (un Hash con clave el nombre
975
     * del GDBMS)
976
     * 
977
     */
978
    private void cleanBadReferences()
979
    {
980
        Hashtable usedDataSources = new Hashtable();
981
        // Primero las tablas
982
        int i, j;
983
        for (i=0; i<tables.size(); i++)
984
        {
985
            ProjectTable t = (ProjectTable) tables.get(i);
986
            SelectableDataSource ds = t.getModelo();
987
            if (t.getOriginal() != null)
988
                usedDataSources.put(t.getOriginal().getName(), t.getOriginal());
989
            usedDataSources.put(ds.getName(), ds);
990
        }
991
        
992
        // Ahora las vistas
993
        try {
994
            for (i=0; i<views.size(); i++)
995
            {
996
                ProjectView pv = (ProjectView) views.get(i);
997
                
998
                FLayers lyrs = pv.getMapContext().getLayers();
999
                FLayers lyrsOverview = pv.getMapOverViewContext().getLayers();
1000
                for (j=0; j<lyrs.getLayersCount(); j++)
1001
                {
1002
                    FLayer lyr = lyrs.getLayer(j);
1003
                    if (lyr instanceof FLyrVect)
1004
                    {
1005
                        FLyrVect lyrVect = (FLyrVect) lyr;
1006
                        usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getRecordset());
1007
                    }
1008
                }
1009
                for (j=0; j<lyrsOverview.getLayersCount(); j++)
1010
                {
1011
                    FLayer lyr = lyrsOverview.getLayer(j);
1012
                    if (lyr instanceof FLyrVect)
1013
                    {
1014
                        FLyrVect lyrVect = (FLyrVect) lyr;
1015
                        usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getRecordset());
1016
                    }
1017
                }
1018
                
1019
            } // for i
1020
        } catch (DriverException e) {
1021
            // TODO Auto-generated catch block
1022
            e.printStackTrace();
1023
        }
1024
        // Recorremos los dataSources y los borramos si no
1025
        // los estamos usando.
1026
        SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
1027
        try {
1028
            for (i=0; i < infos.length; i++)
1029
            {
1030
                if (!usedDataSources.containsKey(infos[i].name))
1031
                {
1032
                    DataSource ds;
1033
                    ds = LayerFactory.getDataSourceFactory().createRandomDataSource(infos[i].name);
1034
                    ds.remove();
1035
                }
1036
            }
1037
        } catch (DriverLoadException e) {
1038
            // TODO Auto-generated catch block
1039
            e.printStackTrace();
1040
        } catch (NoSuchTableException e) {
1041
            // TODO Auto-generated catch block
1042
            e.printStackTrace();
1043
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1044
            // TODO Auto-generated catch block
1045
            e.printStackTrace();
1046
        }
1047
    }
1048
}