Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / Project.java @ 6438

History | View | Annotate | Download (33.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.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
        /**
240
         * A?ade una tabla al proyecto
241
         *
242
         * @param t
243
         */
244
        public void addTable(ProjectTable t) {
245
                tables.add(t);
246
                t.addPropertyChangeListener(this);
247
                modified = true;
248
                change.firePropertyChange("", null, null);
249
                t.setProject(this, tables.size() - 1);
250
        }
251

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

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

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

    
287
        /**
288
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
289
         * fue modificado
290
         *
291
         * @return
292
         */
293
        public boolean isModified() {
294
                if (this.getMaps().size()==0 && this.getViews().size()==0 && this.getTables().size() == 0){
295
                        return false;
296
                }
297
                return true;
298
                ///return modified;    TODO El atributo modified solo detecta cuando se elimina o a?ade una vista,
299
                ///mapa o tabla pero no cuando se modifican.
300
        }
301

    
302
        /**
303
         * Obtiene los comentarios
304
         *
305
         * @return
306
         */
307
        public String getComments() {
308
                return comments;
309
        }
310

    
311
        /**
312
         * Obtiene la fecha de la ?ltima modificaci?n
313
         *
314
         * @return
315
         */
316
        public String getModificationDate() {
317
                return modificationDate;
318
        }
319

    
320
        /**
321
         * Obtiene el propietario del proyecto
322
         *
323
         * @return
324
         */
325
        public String getOwner() {
326
                return owner;
327
        }
328

    
329
        /**
330
         * Establece una cadena como comentarios al proyecto
331
         *
332
         * @param string
333
         */
334
        public void setComments(String string) {
335
                comments = string;
336
                modified = true;
337
                change.firePropertyChange("", null, null);
338
        }
339

    
340
        /**
341
         * Establece la fecha de la ?ltima modificaci?n
342
         *
343
         * @param string
344
         */
345
        public void setModificationDate(String string) {
346
                modificationDate = string;
347
                modified = true;
348
                change.firePropertyChange("", null, null);
349
        }
350

    
351
        /**
352
         * Establece el propietario del proyecto
353
         *
354
         * @param string
355
         */
356
        public void setOwner(String string) {
357
                owner = string;
358
                modified = true;
359
                change.firePropertyChange("", null, null);
360
        }
361

    
362
        /**
363
         * Establece el flag de modificado del proyecto
364
         *
365
         * @param b
366
         */
367
        public void setModified(boolean b) {
368
                modified = b;
369
        }
370

    
371
        /**
372
         * Obtiene el color de selecci?n que se usar? en el proyecto
373
         *
374
         * @return
375
         */
376
        public Color getSelectionColor() {
377
                return selectionColor;
378
        }
379

    
380
        /**
381
         * Establece el color de selecci?n
382
         *
383
         * @param color
384
         */
385
        public void setSelectionColor(Color color) {
386
                selectionColor = color;
387
                FSymbol.setSelectionColor(color);
388
                modified = true;
389
                change.firePropertyChange("selectionColor", null, color);
390
        }
391

    
392
        /**
393
         * Obtiene el color como un entero para su serializaci?n a XML
394
         *
395
         * @return
396
         */
397
        public String getColor() {
398
                return StringUtilities.color2String(selectionColor);
399
        }
400

    
401
        /**
402
         * M?todo invocado al recuperar de XML para establecer el color de
403
         * seleccion del proyecto
404
         *
405
         * @param color Entero que representa un color
406
         */
407
        public void setColor(String color) {
408
                modified = true;
409
                selectionColor = StringUtilities.string2Color(color);
410
        }
411

    
412
        /* (non-Javadoc)
413
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
414
         */
415
        public void propertyChange(PropertyChangeEvent evt) {
416
                this.modified = true;
417
                change.firePropertyChange(evt);
418
        }
419

    
420
        /**
421
         * DOCUMENT ME!
422
         *
423
         * @param arg1
424
         */
425
        public void addExtent(ProjectExtent arg1) {
426
                extents.add(arg1);
427
                modified = true;
428
                change.firePropertyChange("addExtent", null, null);
429
        }
430

    
431
        /**
432
         * DOCUMENT ME!
433
         *
434
         * @param arg0
435
         *
436
         * @return
437
         */
438
        public Object removeExtent(int arg0) {
439
                modified = true;
440
                change.firePropertyChange("delExtent", null, null);
441

    
442
                return extents.remove(arg0);
443
        }
444

    
445
        /**
446
         * DOCUMENT ME!
447
         *
448
         * @return DOCUMENT ME!
449
         */
450
        public ProjectExtent[] getExtents() {
451
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
452
        }
453

    
454
        /**
455
         * DOCUMENT ME!
456
         *
457
         * @param arg0
458
         */
459
        public synchronized void addPropertyChangeListener(
460
                PropertyChangeListener arg0) {
461
                change.addPropertyChangeListener(arg0);
462
        }
463

    
464
        /**
465
         * DOCUMENT ME!
466
         *
467
         * @return
468
         */
469
        public ArrayList getMaps() {
470
                return maps;
471
        }
472

    
473
        /**
474
         * DOCUMENT ME!
475
         *
476
         * @return
477
         */
478
        public ArrayList getTables() {
479
                return tables;
480
        }
481

    
482
        /**
483
         * DOCUMENT ME!
484
         *
485
         * @return
486
         */
487
        public ArrayList getViews() {
488
                return views;
489
        }
490

    
491
        /**
492
         * DOCUMENT ME!
493
         *
494
         * @return DOCUMENT ME!
495
         *
496
         * @throws DriverException
497
         * @throws XMLException
498
         */
499
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
500
                XMLEntity xml = new XMLEntity();
501
                xml.putProperty("className", this.getClass().getName());
502
                xml.putProperty("VERSION", VERSION);
503
                xml.putProperty("comments", comments);
504
                xml.putProperty("creationDate", creationDate);
505

    
506
                int numExtents = extents.size();
507

    
508
                /*double[] xs = new double[size];
509
                   double[] ys = new double[size];
510
                   double[] ws = new double[size];
511
                   double[] hs = new double[size];
512
                 */
513
                xml.putProperty("numExtents", numExtents);
514

    
515
                for (int i = 0; i < numExtents; i++) {
516
                        /*        Rectangle2D rect = (Rectangle2D) extents.get(i);
517
                           xs[i] = rect.getX();
518
                           ys[i] = rect.getY();
519
                           ws[i] = rect.getWidth();
520
                           hs[i] = rect.getHeight();
521
                         */
522
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
523
                }
524

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

    
533
                for (int i = 0; i < infos.length; i++) {
534
                        SourceInfo di = infos[i];
535
                        XMLEntity child = this.getSourceInfoXMLEntity(di);
536
                        xml.addChild(child);
537
                }
538

    
539
                /*
540
                   xml.putProperty("extentsX", xs);
541
                   xml.putProperty("extentsY", ys);
542
                   xml.putProperty("extentsW", ws);
543
                   xml.putProperty("extentsH", hs);
544
                 */
545

    
546
                int numViews=0;
547
                for (int i = 0; i < views.size(); i++) {
548
                        try {
549
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
550
                                xml.addChild(xmlchild);
551
                                numViews++;
552
                        } catch (SaveException e) {
553
                                e.showError();
554
                        }
555
                }
556
                xml.putProperty("numViews", numViews);
557

    
558
                int numMaps=0;
559
                for (int i = 0; i < maps.size(); i++) {
560
                        try {
561
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
562
                                xml.addChild(xmlchild);
563
                                numMaps++;
564
                        } catch (SaveException e) {
565
                                e.showError();
566
                        }
567
                }
568
                xml.putProperty("numMaps", numMaps);
569
                xml.putProperty("modificationDate", modificationDate);
570
                xml.putProperty("modified", modified);
571
                xml.putProperty("name", name);
572
                xml.putProperty("owner", owner);
573
                xml.putProperty("path", path);
574
                xml.putProperty("selectionColor",
575
                        StringUtilities.color2String(selectionColor));
576

    
577
                int numTables=0;
578
                for (int i = 0; i < tables.size(); i++) {
579
                        try {
580
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
581
                                xml.addChild(xmlchild);
582
                                numTables++;
583
                        } catch (SaveException e) {
584

    
585
                                e.showError();
586
                        }
587
                }
588
                xml.putProperty("numTables", numTables);
589
                xml.putProperty("projection", defaultProjection.getAbrev());
590

    
591
                // save the properties of the Project Manager window
592
                View []andamiViews = PluginServices.getMDIManager().getAllViews();
593
                for (int i=0; i<andamiViews.length; i++) {
594
                        if (andamiViews[i] instanceof ProjectWindow) {
595
                                ProjectWindow pw = (ProjectWindow) andamiViews[i];
596
                                try {
597
                                        XMLEntity xmlchild = null;
598
                                        xmlchild = getViewInfoXMLEntity(pw);
599
                                        if (xmlchild!=null) {
600
                                                xml.addChild(xmlchild);
601
                                        }
602
                                }
603
                                catch (SaveException e){
604
                                        e.showError();
605
                                }
606
                        }
607
                }
608

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

    
640
                int numViews = xml.getIntProperty("numViews");
641

    
642
                for (int i = numExtents; i < numViews+numExtents; i++) {
643
                        p.views.add(ProjectView.createFromXML03(xml.getChild(i), p));
644
                }
645

    
646
                int numMaps = xml.getIntProperty("numMaps");
647

    
648
                for (int i = numViews+numExtents; i < (numMaps + numViews+numExtents); i++) {
649
                        p.maps.add(ProjectMap.createFromXML03(xml.getChild(i), p));
650
                }
651

    
652
                p.modificationDate = xml.getStringProperty("modificationDate");
653
                p.modified = xml.getBooleanProperty("modified");
654
                p.name = xml.getStringProperty("name");
655
                p.owner = xml.getStringProperty("owner");
656
                p.path = xml.getStringProperty("path");
657
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
658
                                        "selectionColor"));
659

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

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

    
675
        /**
676
         * DOCUMENT ME!
677
         *
678
         * @param xml DOCUMENT ME!
679
         *
680
         * @return DOCUMENT ME!
681
         *
682
         * @throws XMLException
683
         * @throws DriverException
684
         * @throws DriverIOException
685
         * @throws OpenException
686
         * @throws VersionException
687
         */
688
        public static Project createFromXML(XMLEntity xml)
689
                throws OpenException {
690

    
691
                int childNumber = 0;
692
                Project p = new Project();
693
                try{
694
                p.comments = xml.getStringProperty("comments");
695
                p.creationDate = xml.getStringProperty("creationDate");
696

    
697
                /*
698
                   double[] xs = xml.getDoubleArrayProperty("extentsX");
699
                   double[] ys = xml.getDoubleArrayProperty("extentsY");
700
                   double[] ws = xml.getDoubleArrayProperty("extentsW");
701
                   double[] hs = xml.getDoubleArrayProperty("extentsH");
702
                 */
703
                int numExtents = xml.getIntProperty("numExtents");
704

    
705
                for (int i = 0; i < numExtents; i++) {
706
                        ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
707
                        p.extents.add(pe);
708
                }
709

    
710
                childNumber = numExtents;
711

    
712
                int numDataSources = xml.getIntProperty("data-source-count");
713

    
714
                for (int i = childNumber; i < (childNumber + numDataSources); i++) {
715
                        XMLEntity child = xml.getChild(i);
716

    
717
                        if (child.getStringProperty("type").equals("otherDriverFile")) {
718
                                LayerFactory.getDataSourceFactory().addFileDataSource(child.getStringProperty(
719
                                                "driverName"), child.getStringProperty("gdbmsname"),
720
                                        child.getStringProperty("file"));
721
                        } else if (child.getStringProperty("type").equals("sameDriverFile")) {
722
                                /*                                String layerName = child.getStringProperty("layerName");
723
                                   ProjectView vista = project.getViewByName(child.getStringProperty(
724
                                   "viewName"));
725
                                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
726

727
                                   modelo = ((AlphanumericData) layer).getRecordset();
728
                                   associatedTable = (AlphanumericData) layer;
729
                                 */
730
                        } else if (child.getStringProperty("type").equals("db")) {
731
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(child.getStringProperty(
732
                                                "gdbmsname"), child.getStringProperty("host"),
733
                                        child.getIntProperty("port"),
734
                                        child.getStringProperty("user"),
735
                                        child.getStringProperty("password"),
736
                                        child.getStringProperty("dbName"),
737
                                        child.getStringProperty("tableName"),
738
                                        child.getStringProperty("driverInfo"));
739
                        }
740
                }
741

    
742
                childNumber += numDataSources;
743

    
744
                int numViews = xml.getIntProperty("numViews");
745

    
746
                for (int i = childNumber; i < (numViews + childNumber); i++) {
747
                        try{
748
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(xml.getChild(i), p);
749
                                p.addView(pv);
750
//                                if (pv.getSeedViewInfo()!=null) { // open the view, if it was open, and restore its dimensions
751
//                                        com.iver.cit.gvsig.gui.View vista = new com.iver.cit.gvsig.gui.View();
752
//                                        vista.setModel(pv);
753
//                                        PluginServices.getMDIManager().addView(vista);
754
//                                        PluginServices.getMDIManager().changeViewInfo(vista, pv.getSeedViewInfo());
755
//                                }
756
                        }catch(OpenException e){
757
                                e.showError();
758
                        }
759
                }
760

    
761
                childNumber += numViews;
762

    
763
                int numMaps = xml.getIntProperty("numMaps");
764

    
765
                for (int i = childNumber; i < (numMaps + childNumber); i++) {
766
                        try{
767
                                // p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
768
                                ProjectMap map = (ProjectMap) ProjectMap.createFromXML(xml.getChild(i), p);
769
                                p.maps.add(map);
770
                                if (map.getSeedViewInfo()!=null) { // open the view, if it was open, and restore its dimensions
771
                                        map.getModel().setProjectMap(map);
772
                                        PluginServices.getMDIManager().addView(map.getModel());
773
                                        PluginServices.getMDIManager().changeViewInfo(map.getModel(), map.getSeedViewInfo());
774
                                }
775
                        }catch(OpenException e){
776
                                e.showError();
777
                        }
778
                }
779

    
780
                childNumber += numMaps;
781
                p.modificationDate = xml.getStringProperty("modificationDate");
782
                p.modified = xml.getBooleanProperty("modified");
783
                p.name = xml.getStringProperty("name");
784
                p.owner = xml.getStringProperty("owner");
785
                p.path = xml.getStringProperty("path");
786
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
787
                                        "selectionColor"));
788

    
789
                int numTables = xml.getIntProperty("numTables");
790

    
791
                for (int i = childNumber; i < (childNumber + numTables); i++) {
792
                        try{
793
                                ProjectTable ptable = (ProjectTable) ProjectTable.createFromXML(xml.getChild(i), p);
794
                                p.tables.add(ptable);
795
                                if (ptable.getSeedViewInfo()!=null && ptable.getAndamiView()!=null) { // open the view, if it was open, and restore its dimensions
796
                                        PluginServices.getMDIManager().addView(ptable.getAndamiView());
797
                                        PluginServices.getMDIManager().changeViewInfo(ptable.getAndamiView(), ptable.getSeedViewInfo());
798
                                }
799
                        }catch(OpenException e){
800
                                e.showError();
801
                        }
802
                }
803

    
804

    
805
                /* The project window defines its own size!!
806
                childNumber += numTables;
807
                if (childNumber < xml.getNumChild()) { // restore the position of the project manager window
808
                        XMLEntity child = xml.getChild(childNumber);
809
                        if (child.contains("className")
810
                                        && child.getStringProperty("className").equals("com.iver.cit.gvsig.project.Project")
811
                                        && child.contains("name")
812
                                        && child.getStringProperty("name").equals("ViewInfoProperties")) {
813
                                seedProjectWindow = Project.createViewInfoFromXMLEntity(xml.getChild(childNumber));
814
                        }
815
                }
816
                 */
817

    
818
                p.setLinkTable();
819

    
820
                String strProj = xml.getStringProperty("projection");
821

    
822
                if (strProj != null) {
823
                        Project.setProjection(ProjectionPool.get(strProj));
824
                }
825
        PostProcessSupport.executeCalls();
826
                }catch (Exception e) {
827
                        throw new OpenException(e,p.getClass().getName());
828
                }
829
                return p;
830

    
831
        }
832

    
833
        /**
834
         * Reestablece los link que ten?a cada tabla con las dem?s.
835
         */
836
        public void setLinkTable() {
837
                ArrayList tables = getTables();
838

    
839
                for (int i = 0; i < tables.size(); i++) {
840
                        for (int j = 0; j < tables.size(); j++) {
841
                                /*System.out.println("name = " +
842
                                        ((ProjectTable) tables.get(j)).getModelo().getName());
843
                                System.out.println("linktable = " +
844
                                        ((ProjectTable) tables.get(i)).getLinkTable());
845
*/
846
                                try {
847
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
848
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
849
                                                                                                                                                                  .get(
850
                                                                j)).getModelo().getRecordset().getName())) {
851
                                        LinkSelectionListener lsl;
852

    
853
                                                lsl = new LinkSelectionListener((SelectableDataSource)((ProjectTable) tables.get(
854
                                                                        i)).getModelo().getRecordset(),
855
                                                                        (SelectableDataSource)((ProjectTable) tables.get(j)).getModelo().getRecordset(),
856
                                                                ((ProjectTable) tables.get(i)).getField1(),
857
                                                                ((ProjectTable) tables.get(i)).getField2());
858

    
859
                                                ((SelectableDataSource)((ProjectTable) tables.get(i)).getModelo().getRecordset())
860
                                         .addSelectionListener(lsl);
861

    
862
                                }
863
                                } catch (DriverLoadException e) {
864
                                        // TODO Auto-generated catch block
865
                                        e.printStackTrace();
866
                                }
867
                        }
868
                }
869
        }
870

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

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

    
914
                if (o == null) {
915
                        return null;
916
                }
917

    
918
                return (ProjectView) o;
919
        }
920

    
921
        /**
922
         * DOCUMENT ME!
923
         *
924
         * @return DOCUMENT ME!
925
         */
926
        public static IProjection getProjection() {
927
                return defaultProjection;
928
        }
929

    
930
        /**
931
         * DOCUMENT ME!
932
         *
933
         * @param defaultProjection DOCUMENT ME!
934
         */
935
        public static void setProjection(IProjection defaultProjection) {
936
                Project.defaultProjection = defaultProjection;
937
        }
938

    
939
        /**
940
         * DOCUMENT ME!
941
         *
942
         * @param name DOCUMENT ME!
943
         * @param l DOCUMENT ME!
944
         *
945
         * @return DOCUMENT ME!
946
         */
947
        private Object getProjectElementByName(String name, ArrayList l) {
948
                for (Iterator iter = l.iterator(); iter.hasNext();) {
949
                        ProjectElement elem = (ProjectElement) iter.next();
950

    
951
                        if (elem.getName().equals(name)) {
952
                                return elem;
953
                        }
954
                }
955

    
956
                return null;
957
        }
958

    
959
        /**
960
         * DOCUMENT ME!
961
         *
962
         * @param name
963
         *
964
         * @return
965
         */
966
        public ProjectTable getTableByName(String name) {
967
                Object o = getProjectElementByName(name, tables);
968

    
969
                if (o == null) {
970
                        return null;
971
                }
972

    
973
                return (ProjectTable) o;
974
        }
975

    
976
        /**
977
         * DOCUMENT ME!
978
         *
979
         * @param name
980
         *
981
         * @return
982
         */
983
        public ProjectMap getLayoutByName(String name) {
984
                Object o = getProjectElementByName(name, maps);
985

    
986
                if (o == null) {
987
                        return null;
988
                }
989

    
990
                return (ProjectMap) o;
991
        }
992

    
993
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
994
        ArrayList tables = getTables();
995
        SelectableDataSource dataSource = null;
996
        try {
997
        for (int i = 0; i < tables.size(); i++) {
998
            ProjectTable pt = (ProjectTable) tables.get(i);
999
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
1000
                dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1001
                break;
1002
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
1003

    
1004
                                        dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1005

    
1006
                break;
1007
            }
1008
        }
1009
        } catch (DriverLoadException e) {
1010
                        // TODO Auto-generated catch block
1011
                        e.printStackTrace();
1012
                }
1013

    
1014
        if (dataSource == null){
1015
            //No se ha creado la tabla asociada al tema
1016
            dataSource = ((AlphanumericData) layer).getRecordset();
1017
        }
1018

    
1019
        return dataSource;
1020

    
1021
    }
1022

    
1023
    /**
1024
     * Recorremos las capas y las tablas del proyecto, y creamos una lista
1025
     * con todos los datasources de GDBMS que estamos usando.
1026
     * Luego recorremos los que est?n registrados, y borramos
1027
     * aquellos que no est?n siendo usados, es decir, aquellos
1028
     * que no est?n en nuestra lista (un Hash con clave el nombre
1029
     * del GDBMS)
1030
     *
1031
     */
1032
    private void cleanBadReferences()
1033
    {
1034
        Hashtable usedDataSources = new Hashtable();
1035
        // Primero las tablas
1036
        int i, j;
1037
        try {
1038
        for (i=0; i<tables.size(); i++)
1039
        {
1040
            ProjectTable t = (ProjectTable) tables.get(i);
1041
            SelectableDataSource ds;
1042

    
1043
                                ds = (SelectableDataSource)t.getModelo().getRecordset();
1044

    
1045
            if (t.getOriginal() != null)
1046
                usedDataSources.put(t.getOriginal().getRecordset().getName(), t.getOriginal());
1047
            usedDataSources.put(ds.getName(), ds);
1048
        }
1049
        } catch (DriverLoadException e) {
1050
                        // TODO Auto-generated catch block
1051
                        e.printStackTrace();
1052
                }
1053
        // Ahora las vistas
1054
        try {
1055
            for (i=0; i<views.size(); i++)
1056
            {
1057
                ProjectView pv = (ProjectView) views.get(i);
1058

    
1059
                FLayers lyrs = pv.getMapContext().getLayers();
1060

    
1061
                for (j=0; j<lyrs.getLayersCount(); j++)
1062
                {
1063
                    FLayer lyr = lyrs.getLayer(j);
1064
                    if (lyr instanceof FLyrVect)
1065
                    {
1066
                        FLyrVect lyrVect = (FLyrVect) lyr;
1067
                        if (!lyrVect.isUnavailable())
1068
                                usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getSource().getRecordset());
1069
                    }
1070
                }
1071
                FMap aux = pv.getMapOverViewContext();
1072
                if (aux != null)
1073
                {
1074
                    FLayers lyrsOverview = aux.getLayers();
1075
                    for (j=0; j<lyrsOverview.getLayersCount(); j++)
1076
                    {
1077
                        FLayer lyr = lyrsOverview.getLayer(j);
1078
                        if (lyr instanceof FLyrVect)
1079
                        {
1080
                            FLyrVect lyrVect = (FLyrVect) lyr;
1081
                            usedDataSources.put(lyrVect.getSource().getRecordset().getName(), lyrVect.getSource().getRecordset());
1082
                        }
1083
                    }
1084
                }
1085

    
1086
            } // for i
1087
        } catch (DriverLoadException e) {
1088
                        // TODO Auto-generated catch block
1089
                        e.printStackTrace();
1090
                } catch (DriverException e) {
1091
                        // TODO Auto-generated catch block
1092
                        e.printStackTrace();
1093
                }
1094
        // Recorremos los dataSources y los borramos si no
1095
        // los estamos usando.
1096
        SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
1097
        try {
1098
            for (i=0; i < infos.length; i++)
1099
            {
1100
                if (!usedDataSources.containsKey(infos[i].name))
1101
                {
1102
                    DataSource ds;
1103
                    ds = LayerFactory.getDataSourceFactory().createRandomDataSource(infos[i].name);
1104
                    ds.remove();
1105
                }
1106
            }
1107
        } catch (DriverLoadException e) {
1108
            // TODO Auto-generated catch block
1109
            e.printStackTrace();
1110
        } catch (NoSuchTableException e) {
1111
            // TODO Auto-generated catch block
1112
            e.printStackTrace();
1113
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1114
            // TODO Auto-generated catch block
1115
            e.printStackTrace();
1116
        }
1117
    }
1118

    
1119
    /**
1120
         * DOCUMENT ME!
1121
         *
1122
         * @return DOCUMENT ME!
1123
     * @throws SaveException
1124
         * @throws XMLException
1125
         * @throws SaveException
1126
         */
1127
        public XMLEntity getViewInfoXMLEntity(com.iver.andami.ui.mdiManager.View view) throws SaveException {
1128
                if (view!=null && PluginServices.getMDIManager().getViewInfo(view)!=null) {
1129
                        ViewInfo vi = PluginServices.getMDIManager().getViewInfo(view);
1130
                        XMLEntity xml = new XMLEntity();
1131
                        //xml.putProperty("nameClass", this.getClass().getName());
1132
                        try{
1133
                                xml.setName("ViewInfoProperties");
1134
                                xml.putProperty("className", this.getClass().getName());
1135
                                xml.putProperty("X", vi.getX());
1136
                                xml.putProperty("Y", vi.getY());
1137
                                xml.putProperty("Width", vi.getWidth());
1138
                                xml.putProperty("Height", vi.getHeight());
1139
                                xml.putProperty("isVisible", vi.isVisible());
1140
                                xml.putProperty("isResizable", vi.isResizable());
1141
                                xml.putProperty("isMaximizable", vi.isMaximizable());
1142
                                xml.putProperty("isModal", vi.isModal());
1143
                                xml.putProperty("isModeless", vi.isModeless());
1144
                                xml.putProperty("isClosed", vi.isClosed());
1145
                                if (vi.isMaximized()==true) {
1146
                                        xml.putProperty("isMaximized", vi.isMaximized());
1147
                                        xml.putProperty("normalX", vi.getNormalX());
1148
                                        xml.putProperty("normalY", vi.getNormalY());
1149
                                        xml.putProperty("normalWidth", vi.getNormalWidth());
1150
                                        xml.putProperty("normalHeight", vi.getNormalHeight());
1151
                                }
1152
                        }catch (Exception e) {
1153
                                throw new SaveException(e,this.getClass().getName());
1154
                        }
1155
                        return xml;
1156
                }
1157
                else
1158
                        return null;
1159
        }
1160

    
1161
        public static ViewInfo createViewInfoFromXMLEntity(XMLEntity xml)
1162
        {
1163
                ViewInfo result = new ViewInfo();
1164
                result.setX(xml.getIntProperty("X"));
1165
                result.setY(xml.getIntProperty("Y"));
1166
                result.setHeight(xml.getIntProperty("Height"));
1167
                result.setWidth(xml.getIntProperty("Width"));
1168
                result.setClosed(xml.getBooleanProperty("isClosed"));
1169
                if (xml.contains("isMaximized")) {
1170
                        boolean maximized = xml.getBooleanProperty("isMaximized");
1171
                        result.setMaximized(maximized);
1172
                        if (maximized==true) {
1173
                                result.setNormalBounds(xml.getIntProperty("normalX"), xml.getIntProperty("normalY"), xml.getIntProperty("normalWidth"), xml.getIntProperty("normalHeight"));
1174
                        }
1175
                        else {
1176
                                result.setNormalBounds(result.getBounds());
1177
                        }
1178
                }
1179

    
1180
                return result;
1181
        }
1182

    
1183
        public static ViewInfo getSeedProjectWindow() {
1184
                return seedProjectWindow;
1185
        }
1186
        
1187
        public XMLEntity getSourceInfoXMLEntity(SourceInfo di) {
1188
                XMLEntity child = new XMLEntity();
1189
                
1190

    
1191
                if (di instanceof ObjectSourceInfo) {
1192
                        ObjectSourceInfo driver = (ObjectSourceInfo) di;
1193
                        child.putProperty("type", "sameDriverFile");
1194
                        child.putProperty("gdbmsname", driver.name);
1195
                } else if (di instanceof FileSourceInfo) {
1196
                        FileSourceInfo vfdi = (FileSourceInfo) di;
1197
                        child.putProperty("type", "otherDriverFile");
1198
                        child.putProperty("gdbmsname", vfdi.name);
1199
                        child.putProperty("file", vfdi.file);
1200
                        child.putProperty("driverName", vfdi.driverName);
1201
                } else if (di instanceof DBSourceInfo) {
1202
                        DBTableSourceInfo dbdi = (DBTableSourceInfo) di;
1203
                        child.putProperty("type", "db");
1204
                        child.putProperty("gdbmsname", dbdi.name);
1205
                        child.putProperty("dbms", dbdi.dbms);
1206
                        child.putProperty("host", dbdi.host);
1207
                        child.putProperty("port", dbdi.port);
1208
                        child.putProperty("user", dbdi.user);
1209
                        child.putProperty("password", dbdi.password);
1210
                        child.putProperty("dbName", dbdi.dbName);
1211
                        child.putProperty("tableName", dbdi.tableName);
1212
                        child.putProperty("driverInfo", dbdi.driverName);
1213
                }
1214
                
1215
                return child;
1216
        }
1217
}