Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / Project.java @ 6541

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

    
87

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

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

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

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

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

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

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

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

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

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

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

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

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

    
236
                return null;
237
        }
238

    
239
        public ProjectTable getTable(String name) {
240
                /**
241
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
242
                 * una capa, se puede intentar a?adir dos veces la misma tabla
243
                 */
244
                for (int i = 0; i < tables.size(); i++) {
245
                        if (((ProjectTable) tables.get(i)).getName().equals(name)) {
246
                                return (ProjectTable) tables.get(i);
247
                        }
248
                }
249

    
250
                return null;
251
        }
252

    
253
        /**
254
         * A?ade una tabla al proyecto
255
         *
256
         * @param t
257
         */
258
        public void addTable(ProjectTable t) {
259
                tables.add(t);
260
                t.addPropertyChangeListener(this);
261
                modified = true;
262
                change.firePropertyChange("", null, null);
263
                t.setProject(this, tables.size() - 1);
264
        }
265

    
266
        /**
267
         * Elimina una tabla del proyecto
268
         *
269
         * @param i indice de la tabla
270
         */
271
        public void delTable(int i) {
272
                tables.remove(i);
273
                modified = true;
274
                change.firePropertyChange("", null, null);
275
        }
276

    
277
        /**
278
         * A?ade una vista al proyecto
279
         *
280
         * @param v
281
         */
282
        public void addView(ProjectView v) {
283
                views.add(v);
284
                v.addPropertyChangeListener(this);
285
                modified = true;
286
                change.firePropertyChange("", null, null);
287
                v.setProject(this, views.size() - 1);
288
        }
289

    
290
        /**
291
         * Elimina una tabla del proyecto
292
         *
293
         * @param i indice del proyecto
294
         */
295
        public void delView(int i) {
296
                views.remove(i);
297
                modified = true;
298
                change.firePropertyChange("", null, null);
299
        }
300

    
301
        /**
302
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
303
         * fue modificado
304
         *
305
         * @return
306
         */
307
        public boolean isModified() {
308
                if (this.getMaps().size()==0 && this.getViews().size()==0 && this.getTables().size() == 0){
309
                        return false;
310
                }
311
                return true;
312
                ///return modified;    TODO El atributo modified solo detecta cuando se elimina o a?ade una vista,
313
                ///mapa o tabla pero no cuando se modifican.
314
        }
315

    
316
        /**
317
         * Obtiene los comentarios
318
         *
319
         * @return
320
         */
321
        public String getComments() {
322
                return comments;
323
        }
324

    
325
        /**
326
         * Obtiene la fecha de la ?ltima modificaci?n
327
         *
328
         * @return
329
         */
330
        public String getModificationDate() {
331
                return modificationDate;
332
        }
333

    
334
        /**
335
         * Obtiene el propietario del proyecto
336
         *
337
         * @return
338
         */
339
        public String getOwner() {
340
                return owner;
341
        }
342

    
343
        /**
344
         * Establece una cadena como comentarios al proyecto
345
         *
346
         * @param string
347
         */
348
        public void setComments(String string) {
349
                comments = string;
350
                modified = true;
351
                change.firePropertyChange("", null, null);
352
        }
353

    
354
        /**
355
         * Establece la fecha de la ?ltima modificaci?n
356
         *
357
         * @param string
358
         */
359
        public void setModificationDate(String string) {
360
                modificationDate = string;
361
                modified = true;
362
                change.firePropertyChange("", null, null);
363
        }
364

    
365
        /**
366
         * Establece el propietario del proyecto
367
         *
368
         * @param string
369
         */
370
        public void setOwner(String string) {
371
                owner = string;
372
                modified = true;
373
                change.firePropertyChange("", null, null);
374
        }
375

    
376
        /**
377
         * Establece el flag de modificado del proyecto
378
         *
379
         * @param b
380
         */
381
        public void setModified(boolean b) {
382
                modified = b;
383
        }
384

    
385
        /**
386
         * Obtiene el color de selecci?n que se usar? en el proyecto
387
         *
388
         * @return
389
         */
390
        public Color getSelectionColor() {
391
                return selectionColor;
392
        }
393

    
394
        /**
395
         * Establece el color de selecci?n
396
         *
397
         * @param color
398
         */
399
        public void setSelectionColor(Color color) {
400
                selectionColor = color;
401
                FSymbol.setSelectionColor(color);
402
                modified = true;
403
                change.firePropertyChange("selectionColor", null, color);
404
        }
405

    
406
        /**
407
         * Obtiene el color como un entero para su serializaci?n a XML
408
         *
409
         * @return
410
         */
411
        public String getColor() {
412
                return StringUtilities.color2String(selectionColor);
413
        }
414

    
415
        /**
416
         * M?todo invocado al recuperar de XML para establecer el color de
417
         * seleccion del proyecto
418
         *
419
         * @param color Entero que representa un color
420
         */
421
        public void setColor(String color) {
422
                modified = true;
423
                selectionColor = StringUtilities.string2Color(color);
424
        }
425

    
426
        /* (non-Javadoc)
427
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
428
         */
429
        public void propertyChange(PropertyChangeEvent evt) {
430
                this.modified = true;
431
                change.firePropertyChange(evt);
432
        }
433

    
434
        /**
435
         * DOCUMENT ME!
436
         *
437
         * @param arg1
438
         */
439
        public void addExtent(ProjectExtent arg1) {
440
                extents.add(arg1);
441
                modified = true;
442
                change.firePropertyChange("addExtent", null, null);
443
        }
444

    
445
        /**
446
         * DOCUMENT ME!
447
         *
448
         * @param arg0
449
         *
450
         * @return
451
         */
452
        public Object removeExtent(int arg0) {
453
                modified = true;
454
                change.firePropertyChange("delExtent", null, null);
455

    
456
                return extents.remove(arg0);
457
        }
458

    
459
        /**
460
         * DOCUMENT ME!
461
         *
462
         * @return DOCUMENT ME!
463
         */
464
        public ProjectExtent[] getExtents() {
465
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
466
        }
467

    
468
        /**
469
         * DOCUMENT ME!
470
         *
471
         * @param arg0
472
         */
473
        public synchronized void addPropertyChangeListener(
474
                PropertyChangeListener arg0) {
475
                change.addPropertyChangeListener(arg0);
476
        }
477

    
478
        /**
479
         * DOCUMENT ME!
480
         *
481
         * @return
482
         */
483
        public ArrayList getMaps() {
484
                return maps;
485
        }
486

    
487
        /**
488
         * DOCUMENT ME!
489
         *
490
         * @return
491
         */
492
        public ArrayList getTables() {
493
                return tables;
494
        }
495

    
496
        /**
497
         * DOCUMENT ME!
498
         *
499
         * @return
500
         */
501
        public ArrayList getViews() {
502
                return views;
503
        }
504

    
505
        /**
506
         * DOCUMENT ME!
507
         *
508
         * @return DOCUMENT ME!
509
         *
510
         * @throws DriverException
511
         * @throws XMLException
512
         */
513
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
514
                XMLEntity xml = new XMLEntity();
515
                xml.putProperty("className", this.getClass().getName());
516
                xml.putProperty("VERSION", VERSION);
517
                xml.putProperty("comments", comments);
518
                xml.putProperty("creationDate", creationDate);
519

    
520
                int numExtents = extents.size();
521

    
522
                /*double[] xs = new double[size];
523
                   double[] ys = new double[size];
524
                   double[] ws = new double[size];
525
                   double[] hs = new double[size];
526
                 */
527
                xml.putProperty("numExtents", numExtents);
528

    
529
                for (int i = 0; i < numExtents; i++) {
530
                        /*        Rectangle2D rect = (Rectangle2D) extents.get(i);
531
                           xs[i] = rect.getX();
532
                           ys[i] = rect.getY();
533
                           ws[i] = rect.getWidth();
534
                           hs[i] = rect.getHeight();
535
                         */
536
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
537
                }
538

    
539
        // NUEVO: ESTO ESTA EN PRUEBAS. SIRVE PARA
540
        // BORRAR LAS REFERENCIAS A DATASOURCES QUE HEMOS
541
        // BORRADO. Hay que probar a borrarlos cuando se
542
        // borra una tabla y cuando se borra una capa.
543
        cleanBadReferences();
544
                SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
545
                xml.putProperty("data-source-count", infos.length);
546

    
547
                for (int i = 0; i < infos.length; i++) {
548
                        SourceInfo di = infos[i];
549
                        XMLEntity child = this.getSourceInfoXMLEntity(di);
550
                        xml.addChild(child);
551
                }
552

    
553
                /*
554
                   xml.putProperty("extentsX", xs);
555
                   xml.putProperty("extentsY", ys);
556
                   xml.putProperty("extentsW", ws);
557
                   xml.putProperty("extentsH", hs);
558
                 */
559

    
560
                int numViews=0;
561
                for (int i = 0; i < views.size(); i++) {
562
                        try {
563
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
564
                                xml.addChild(xmlchild);
565
                                numViews++;
566
                        } catch (SaveException e) {
567
                                e.showError();
568
                        }
569
                }
570
                xml.putProperty("numViews", numViews);
571

    
572
                int numMaps=0;
573
                for (int i = 0; i < maps.size(); i++) {
574
                        try {
575
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
576
                                xml.addChild(xmlchild);
577
                                numMaps++;
578
                        } catch (SaveException e) {
579
                                e.showError();
580
                        }
581
                }
582
                xml.putProperty("numMaps", numMaps);
583
                xml.putProperty("modificationDate", modificationDate);
584
                xml.putProperty("modified", modified);
585
                xml.putProperty("name", name);
586
                xml.putProperty("owner", owner);
587
                xml.putProperty("path", path);
588
                xml.putProperty("selectionColor",
589
                        StringUtilities.color2String(selectionColor));
590

    
591
                int numTables=0;
592
                for (int i = 0; i < tables.size(); i++) {
593
                        try {
594
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
595
                                xml.addChild(xmlchild);
596
                                numTables++;
597
                        } catch (SaveException e) {
598

    
599
                                e.showError();
600
                        }
601
                }
602
                xml.putProperty("numTables", numTables);
603
                xml.putProperty("projection", defaultProjection.getAbrev());
604

    
605
                // save the properties of the Project Manager window
606
                View []andamiViews = PluginServices.getMDIManager().getAllViews();
607
                for (int i=0; i<andamiViews.length; i++) {
608
                        if (andamiViews[i] instanceof ProjectWindow) {
609
                                ProjectWindow pw = (ProjectWindow) andamiViews[i];
610
                                try {
611
                                        XMLEntity xmlchild = null;
612
                                        xmlchild = getViewInfoXMLEntity(pw);
613
                                        if (xmlchild!=null) {
614
                                                xml.addChild(xmlchild);
615
                                        }
616
                                }
617
                                catch (SaveException e){
618
                                        e.showError();
619
                                }
620
                        }
621
                }
622

    
623
                return xml;
624
        }
625
        /**
626
         * DOCUMENT ME!
627
         *
628
         * @param xml DOCUMENT ME!
629
         *
630
         * @return DOCUMENT ME!
631
         * @throws XMLException
632
         * @throws DriverException
633
         * @throws DriverIOException
634
         * @throws OpenException
635
         */
636
        public static Project createFromXML03(XMLEntity xml)
637
                throws OpenException {
638
                Project p = new Project();
639
                try{
640
                p.comments = xml.getStringProperty("comments");
641
                p.creationDate = xml.getStringProperty("creationDate");
642
/*
643
                double[] xs = xml.getDoubleArrayProperty("extentsX");
644
                double[] ys = xml.getDoubleArrayProperty("extentsY");
645
                double[] ws = xml.getDoubleArrayProperty("extentsW");
646
                double[] hs = xml.getDoubleArrayProperty("extentsH");
647
*/
648
                int numExtents=xml.getIntProperty("numExtents");
649
                for (int i = 0; i < numExtents; i++) {
650
                        ProjectExtent pe=ProjectExtent.createFromXML03(xml.getChild(i));
651
                        p.extents.add(pe);
652
                }
653

    
654
                int numViews = xml.getIntProperty("numViews");
655

    
656
                for (int i = numExtents; i < numViews+numExtents; i++) {
657
                        p.views.add(ProjectView.createFromXML03(xml.getChild(i), p));
658
                }
659

    
660
                int numMaps = xml.getIntProperty("numMaps");
661

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

    
666
                p.modificationDate = xml.getStringProperty("modificationDate");
667
                p.modified = xml.getBooleanProperty("modified");
668
                p.name = xml.getStringProperty("name");
669
                p.owner = xml.getStringProperty("owner");
670
                p.path = xml.getStringProperty("path");
671
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
672
                                        "selectionColor"));
673

    
674
                int numTables = xml.getIntProperty("numTables");
675

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

    
689
        /**
690
         * DOCUMENT ME!
691
         *
692
         * @param xml DOCUMENT ME!
693
         *
694
         * @return DOCUMENT ME!
695
         *
696
         * @throws XMLException
697
         * @throws DriverException
698
         * @throws DriverIOException
699
         * @throws OpenException
700
         * @throws VersionException
701
         */
702
        public static Project createFromXML(XMLEntity xml)
703
                throws OpenException {
704

    
705
                int childNumber = 0;
706
                Project p = new Project();
707
                try{
708
                p.comments = xml.getStringProperty("comments");
709
                p.creationDate = xml.getStringProperty("creationDate");
710

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

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

    
724
                childNumber = numExtents;
725

    
726
                int numDataSources = xml.getIntProperty("data-source-count");
727

    
728
                for (int i = childNumber; i < (childNumber + numDataSources); i++) {
729
                        XMLEntity child = xml.getChild(i);
730

    
731
                        if (child.getStringProperty("type").equals("otherDriverFile")) {
732
                                LayerFactory.getDataSourceFactory().addFileDataSource(child.getStringProperty(
733
                                                "driverName"), child.getStringProperty("gdbmsname"),
734
                                        child.getStringProperty("file"));
735
                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
736
                                /*                                String layerName = child.getStringProperty("layerName");
737
                                   ProjectView vista = project.getViewByName(child.getStringProperty(
738
                                   "viewName"));
739
                                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
740

741
                                   modelo = ((AlphanumericData) layer).getRecordset();
742
                                   associatedTable = (AlphanumericData) layer;
743
                                 */
744
                        } else if (child.getStringProperty("type").equals("db")) {
745
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(child.getStringProperty(
746
                                                "gdbmsname"), child.getStringProperty("host"),
747
                                        child.getIntProperty("port"),
748
                                        child.getStringProperty("user"),
749
                                        child.getStringProperty("password"),
750
                                        child.getStringProperty("dbName"),
751
                                        child.getStringProperty("tableName"),
752
                                        child.getStringProperty("driverInfo"));
753
                        }
754
                }
755

    
756
                childNumber += numDataSources;
757

    
758
                int numViews = xml.getIntProperty("numViews");
759

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

    
775
                childNumber += numViews;
776

    
777
                int numMaps = xml.getIntProperty("numMaps");
778

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

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

    
803
                int numTables = xml.getIntProperty("numTables");
804

    
805
                for (int i = childNumber; i < (childNumber + numTables); i++) {
806
                        try{
807
                                ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xml.getChild(i), p);
808
                                p.tables.add(ptable);
809
                                if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
810
                                        PluginServices.getMDIManager().addView(ptable.getAndamiView());
811
                                        PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
812
                                }
813
                        }catch(OpenException e){
814
                                e.showError();
815
                        }
816
                }
817

    
818

    
819
                /* The project window defines its own size!!
820
                childNumber += numTables;
821
                if (childNumber < xml.getNumChild()) { // restore the position of the project manager window
822
                        XMLEntity child = xml.getChild(childNumber);
823
                        if (child.contains("className")
824
                                        && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.Project")
825
                                        && child.contains("name")
826
                                        && child.getStringProperty("name").equals("ViewInfoProperties")) {
827
                                seedProjectWindow = Project.createViewInfoFromXMLEntity(xml.getChild(childNumber));
828
                        }
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
                                try {
861
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
862
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
863
                                                                                                                                                                  .get(
864
                                                                j)).getModelo().getRecordset().getName())) {
865
                                        LinkSelectionListener lsl;
866

    
867
                                                lsl = new LinkSelectionListener((SelectableDataSource)((ProjectTable) tables.get(
868
                                                                        i)).getModelo().getRecordset(),
869
                                                                        (SelectableDataSource)((ProjectTable) tables.get(j)).getModelo().getRecordset(),
870
                                                                ((ProjectTable) tables.get(i)).getField1(),
871
                                                                ((ProjectTable) tables.get(i)).getField2());
872

    
873
                                                ((SelectableDataSource)((ProjectTable) tables.get(i)).getModelo().getRecordset())
874
                                         .addSelectionListener(lsl);
875

    
876
                                }
877
                                } catch (DriverLoadException e) {
878
                                        // TODO Auto-generated catch block
879
                                        e.printStackTrace();
880
                                }
881
                        }
882
                }
883
        }
884

    
885
        /**
886
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
887
         *
888
         * @param layer Capa cuya vista se quiere obtener
889
         *
890
         * @return
891
         *
892
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
893
         *                    encuentra en ninguna vista
894
         */
895
        public String getView(FLayer layer) {
896
                for (int v = 0; v < views.size(); v++) {
897
                        ProjectView pView = (ProjectView) views.get(v);
898
                        FLayers layers = pView.getMapContext().getLayers();
899
                        if (isView(layers,layer))return pView.getName();
900
                }
901

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

    
928
                if (o == null) {
929
                        return null;
930
                }
931

    
932
                return (ProjectView) o;
933
        }
934

    
935
        /**
936
         * DOCUMENT ME!
937
         *
938
         * @return DOCUMENT ME!
939
         */
940
        public static IProjection getProjection() {
941
                return defaultProjection;
942
        }
943

    
944
        /**
945
         * DOCUMENT ME!
946
         *
947
         * @param defaultProjection DOCUMENT ME!
948
         */
949
        public static void setProjection(IProjection defaultProjection) {
950
                Project.defaultProjection = defaultProjection;
951
        }
952

    
953
        /**
954
         * DOCUMENT ME!
955
         *
956
         * @param name DOCUMENT ME!
957
         * @param l DOCUMENT ME!
958
         *
959
         * @return DOCUMENT ME!
960
         */
961
        private Object getProjectElementByName(String name, ArrayList l) {
962
                for (Iterator iter = l.iterator(); iter.hasNext();) {
963
                        ProjectElement elem = (ProjectElement) iter.next();
964

    
965
                        if (elem.getName().equals(name)) {
966
                                return elem;
967
                        }
968
                }
969

    
970
                return null;
971
        }
972

    
973
        /**
974
         * DOCUMENT ME!
975
         *
976
         * @param name
977
         *
978
         * @return
979
         */
980
        public ProjectTable getTableByName(String name) {
981
                Object o = getProjectElementByName(name, tables);
982

    
983
                if (o == null) {
984
                        return null;
985
                }
986

    
987
                return (ProjectTable) o;
988
        }
989

    
990
        /**
991
         * DOCUMENT ME!
992
         *
993
         * @param name
994
         *
995
         * @return
996
         */
997
        public ProjectMap getLayoutByName(String name) {
998
                Object o = getProjectElementByName(name, maps);
999

    
1000
                if (o == null) {
1001
                        return null;
1002
                }
1003

    
1004
                return (ProjectMap) o;
1005
        }
1006

    
1007
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
1008
        ArrayList tables = getTables();
1009
        SelectableDataSource dataSource = null;
1010
        try {
1011
        for (int i = 0; i < tables.size(); i++) {
1012
            ProjectTable pt = (ProjectTable) tables.get(i);
1013
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
1014
                dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1015
                break;
1016
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
1017

    
1018
                                        dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1019

    
1020
                break;
1021
            }
1022
        }
1023
        } catch (DriverLoadException e) {
1024
                        // TODO Auto-generated catch block
1025
                        e.printStackTrace();
1026
                }
1027

    
1028
        if (dataSource == null){
1029
            //No se ha creado la tabla asociada al tema
1030
            dataSource = ((AlphanumericData) layer).getRecordset();
1031
        }
1032

    
1033
        return dataSource;
1034

    
1035
    }
1036

    
1037
    /**
1038
     * Recorremos las capas y las tablas del proyecto, y creamos una lista
1039
     * con todos los datasources de GDBMS que estamos usando.
1040
     * Luego recorremos los que est?n registrados, y borramos
1041
     * aquellos que no est?n siendo usados, es decir, aquellos
1042
     * que no est?n en nuestra lista (un Hash con clave el nombre
1043
     * del GDBMS)
1044
     *
1045
     */
1046
    private void cleanBadReferences()
1047
    {
1048
        Hashtable usedDataSources = new Hashtable();
1049
        // Primero las tablas
1050
        int i, j;
1051
        try {
1052
        for (i=0; i<tables.size(); i++)
1053
        {
1054
            ProjectTable t = (ProjectTable) tables.get(i);
1055
            SelectableDataSource ds;
1056

    
1057
                                ds = (SelectableDataSource)t.getModelo().getRecordset();
1058

    
1059
            if (t.getOriginal() != null)
1060
                usedDataSources.put(t.getOriginal().getRecordset().getName(), t.getOriginal());
1061
            usedDataSources.put(ds.getName(), ds);
1062
        }
1063
        } catch (DriverLoadException e) {
1064
                        // TODO Auto-generated catch block
1065
                        e.printStackTrace();
1066
                }
1067
        // Ahora las vistas
1068
        try {
1069
            for (i=0; i<views.size(); i++)
1070
            {
1071
                ProjectView pv = (ProjectView) views.get(i);
1072

    
1073
                FLayers lyrs = pv.getMapContext().getLayers();
1074

    
1075
                for (j=0; j<lyrs.getLayersCount(); j++)
1076
                {
1077
                    FLayer lyr = lyrs.getLayer(j);
1078
                    if (lyr instanceof FLyrVect)
1079
                    {
1080
                        FLyrVect lyrVect = (FLyrVect) lyr;
1081
                        if (lyrVect.isAvialable())
1082
                                usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getSource().getRecordset());
1083
                    }
1084
                }
1085
                FMap aux = pv.getMapOverViewContext();
1086
                if (aux != null)
1087
                {
1088
                    FLayers lyrsOverview = aux.getLayers();
1089
                    for (j=0; j<lyrsOverview.getLayersCount(); j++)
1090
                    {
1091
                        FLayer lyr = lyrsOverview.getLayer(j);
1092
                        if (lyr instanceof FLyrVect)
1093
                        {
1094
                            FLyrVect lyrVect = (FLyrVect) lyr;
1095
                            usedDataSources.put(lyrVect.getSource().getRecordset().getName(), lyrVect.getSource().getRecordset());
1096
                        }
1097
                    }
1098
                }
1099

    
1100
            } // for i
1101
        } catch (DriverLoadException e) {
1102
                        // TODO Auto-generated catch block
1103
                        e.printStackTrace();
1104
                } catch (DriverException e) {
1105
                        // TODO Auto-generated catch block
1106
                        e.printStackTrace();
1107
                }
1108
        // Recorremos los dataSources y los borramos si no
1109
        // los estamos usando.
1110
        SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
1111
        try {
1112
            for (i=0; i < infos.length; i++)
1113
            {
1114
                if (!usedDataSources.containsKey(infos[i].name))
1115
                {
1116
                    DataSource ds;
1117
                    ds = LayerFactory.getDataSourceFactory().createRandomDataSource(infos[i].name);
1118
                    ds.remove();
1119
                }
1120
            }
1121
        } catch (DriverLoadException e) {
1122
            // TODO Auto-generated catch block
1123
            e.printStackTrace();
1124
        } catch (NoSuchTableException e) {
1125
            // TODO Auto-generated catch block
1126
            e.printStackTrace();
1127
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1128
            // TODO Auto-generated catch block
1129
            e.printStackTrace();
1130
        }
1131
    }
1132

    
1133
    /**
1134
         * DOCUMENT ME!
1135
         *
1136
         * @return DOCUMENT ME!
1137
     * @throws SaveException
1138
         * @throws XMLException
1139
         * @throws SaveException
1140
         */
1141
        public XMLEntity getViewInfoXMLEntity(com.iver.andami.ui.mdiManager.View view) throws SaveException {
1142
                if (view!=null && PluginServices.getMDIManager().getViewInfo(view)!=null) {
1143
                        ViewInfo vi = PluginServices.getMDIManager().getViewInfo(view);
1144
                        XMLEntity xml = new XMLEntity();
1145
                        //xml.putProperty("nameClass", this.getClass().getName());
1146
                        try{
1147
                                xml.setName("ViewInfoProperties");
1148
                                xml.putProperty("className", this.getClass().getName());
1149
                                xml.putProperty("X", vi.getX());
1150
                                xml.putProperty("Y", vi.getY());
1151
                                xml.putProperty("Width", vi.getWidth());
1152
                                xml.putProperty("Height", vi.getHeight());
1153
                                xml.putProperty("isVisible", vi.isVisible());
1154
                                xml.putProperty("isResizable", vi.isResizable());
1155
                                xml.putProperty("isMaximizable", vi.isMaximizable());
1156
                                xml.putProperty("isModal", vi.isModal());
1157
                                xml.putProperty("isModeless", vi.isModeless());
1158
                                xml.putProperty("isClosed", vi.isClosed());
1159
                                if (vi.isMaximized()==true) {
1160
                                        xml.putProperty("isMaximized", vi.isMaximized());
1161
                                        xml.putProperty("normalX", vi.getNormalX());
1162
                                        xml.putProperty("normalY", vi.getNormalY());
1163
                                        xml.putProperty("normalWidth", vi.getNormalWidth());
1164
                                        xml.putProperty("normalHeight", vi.getNormalHeight());
1165
                                }
1166
                        }catch (Exception e) {
1167
                                throw new SaveException(e,this.getClass().getName());
1168
                        }
1169
                        return xml;
1170
                }
1171
                else
1172
                        return null;
1173
        }
1174

    
1175
        public static ViewInfo createViewInfoFromXMLEntity(XMLEntity xml)
1176
        {
1177
                ViewInfo result = new ViewInfo();
1178
                result.setX(xml.getIntProperty("X"));
1179
                result.setY(xml.getIntProperty("Y"));
1180
                result.setHeight(xml.getIntProperty("Height"));
1181
                result.setWidth(xml.getIntProperty("Width"));
1182
                result.setClosed(xml.getBooleanProperty("isClosed"));
1183
                if (xml.contains("isMaximized")) {
1184
                        boolean maximized = xml.getBooleanProperty("isMaximized");
1185
                        result.setMaximized(maximized);
1186
                        if (maximized==true) {
1187
                                result.setNormalBounds(xml.getIntProperty("normalX"), xml.getIntProperty("normalY"), xml.getIntProperty("normalWidth"), xml.getIntProperty("normalHeight"));
1188
                        }
1189
                        else {
1190
                                result.setNormalBounds(result.getBounds());
1191
                        }
1192
                }
1193

    
1194
                return result;
1195
        }
1196

    
1197
        public static ViewInfo getSeedProjectWindow() {
1198
                return seedProjectWindow;
1199
        }
1200
        
1201
        public XMLEntity getSourceInfoXMLEntity(SourceInfo di) {
1202
                XMLEntity child = new XMLEntity();
1203
                
1204

    
1205
                if (di instanceof ObjectSourceInfo) {
1206
                        ObjectSourceInfo driver = (ObjectSourceInfo) di;
1207
                        child.putProperty("type", "sameDriverFile");
1208
                        child.putProperty("gdbmsname", driver.name);
1209
                } else if (di instanceof FileSourceInfo) {
1210
                        FileSourceInfo vfdi = (FileSourceInfo) di;
1211
                        child.putProperty("type", "otherDriverFile");
1212
                        child.putProperty("gdbmsname", vfdi.name);
1213
                        child.putProperty("file", vfdi.file);
1214
                        child.putProperty("driverName", vfdi.driverName);
1215
                } else if (di instanceof DBSourceInfo) {
1216
                        DBTableSourceInfo dbdi = (DBTableSourceInfo) di;
1217
                        child.putProperty("type", "db");
1218
                        child.putProperty("gdbmsname", dbdi.name);
1219
                        child.putProperty("dbms", dbdi.dbms);
1220
                        child.putProperty("host", dbdi.host);
1221
                        child.putProperty("port", dbdi.port);
1222
                        child.putProperty("user", dbdi.user);
1223
                        child.putProperty("password", dbdi.password);
1224
                        child.putProperty("dbName", dbdi.dbName);
1225
                        child.putProperty("tableName", dbdi.tableName);
1226
                        child.putProperty("driverInfo", dbdi.driverName);
1227
                }
1228
                
1229
                return child;
1230
        }
1231
}