Statistics
| Revision:

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

History | View | Annotate | Download (33.1 KB)

1 1222 fernando
/* 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 2217 fernando
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 3060 fjp
import java.util.Hashtable;
52 2217 fernando
import java.util.Iterator;
53 1222 fernando
54 2217 fernando
import org.cresques.cts.IProjection;
55
import org.cresques.cts.ProjectionPool;
56 1222 fernando
57 3060 fjp
import com.hardcode.driverManager.DriverLoadException;
58
import com.hardcode.gdbms.engine.data.DataSource;
59
import com.hardcode.gdbms.engine.data.NoSuchTableException;
60 2217 fernando
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 5029 cesar
import com.iver.andami.PluginServices;
66
import com.iver.andami.ui.mdiManager.View;
67
import com.iver.andami.ui.mdiManager.ViewInfo;
68 1222 fernando
import com.iver.cit.gvsig.fmap.DriverException;
69 3565 fjp
import com.iver.cit.gvsig.fmap.FMap;
70 1222 fernando
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 3060 fjp
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
75 1222 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
76 2610 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
77 1222 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
79 2183 fernando
import com.iver.cit.gvsig.fmap.operations.selection.LinkSelectionListener;
80 3183 caballero
import com.iver.cit.gvsig.gui.project.OpenException;
81 5029 cesar
import com.iver.cit.gvsig.gui.project.ProjectWindow;
82 3183 caballero
import com.iver.cit.gvsig.gui.project.SaveException;
83 2673 fjp
import com.iver.utiles.PostProcessSupport;
84 1222 fernando
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 4883 fjp
        public static String VERSION = "1.0";
95 1830 fernando
        static private IProjection defaultProjection = ProjectionPool.get(
96
                        "EPSG:23030");
97 1222 fernando
        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 5029 cesar
        // used to restore the position of the Project Manager window when loading a project from file
111
        private static ViewInfo seedProjectWindow = null;
112 1222 fernando
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 2183 fernando
                /*                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
124
                   .getPluginDirectory()
125
                   .getAbsolutePath() +
126
                   File.separator + "drivers");
127
                 */
128
        }
129
130 1222 fernando
        /**
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 2183 fernando
                m.setProject(this, maps.size() - 1);
183 1222 fernando
        }
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 2183 fernando
                t.setProject(this, tables.size() - 1);
250 1222 fernando
        }
251 2183 fernando
252 1222 fernando
        /**
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 1830 fernando
                v.setProject(this, views.size() - 1);
274 1222 fernando
        }
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 3586 caballero
                if (this.getMaps().size()==0 && this.getViews().size()==0 && this.getTables().size() == 0){
295
                        return false;
296
                }
297
                return true;
298 3940 caballero
                ///return modified;    TODO El atributo modified solo detecta cuando se elimina o a?ade una vista,
299 3586 caballero
                ///mapa o tabla pero no cuando se modifican.
300 1222 fernando
        }
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 1830 fernando
         * @throws XMLException
498 1222 fernando
         */
499 1830 fernando
        public XMLEntity getXMLEntity() throws DriverException, XMLException {
500 1222 fernando
                XMLEntity xml = new XMLEntity();
501 1830 fernando
                xml.putProperty("className", this.getClass().getName());
502
                xml.putProperty("VERSION", VERSION);
503 1222 fernando
                xml.putProperty("comments", comments);
504
                xml.putProperty("creationDate", creationDate);
505
506 1302 vcaballero
                int numExtents = extents.size();
507 1830 fernando
508 1302 vcaballero
                /*double[] xs = new double[size];
509 1830 fernando
                   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 1302 vcaballero
                for (int i = 0; i < numExtents; i++) {
516 1830 fernando
                        /*        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 1222 fernando
                }
524 1830 fernando
525 3060 fjp
        // 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 2217 fernando
                SourceInfo[] infos = LayerFactory.getDataSourceFactory().getDriverInfos();
531 1830 fernando
                xml.putProperty("data-source-count", infos.length);
532
533
                for (int i = 0; i < infos.length; i++) {
534 2217 fernando
                        SourceInfo di = infos[i];
535 6438 jmvivo
                        XMLEntity child = this.getSourceInfoXMLEntity(di);
536 1830 fernando
                        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 3940 caballero
546 3424 caballero
                int numViews=0;
547 1222 fernando
                for (int i = 0; i < views.size(); i++) {
548 3183 caballero
                        try {
549 3424 caballero
                                XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
550
                                xml.addChild(xmlchild);
551
                                numViews++;
552 3183 caballero
                        } catch (SaveException e) {
553
                                e.showError();
554
                        }
555 1222 fernando
                }
556 3424 caballero
                xml.putProperty("numViews", numViews);
557 3940 caballero
558 3424 caballero
                int numMaps=0;
559 1222 fernando
                for (int i = 0; i < maps.size(); i++) {
560 3183 caballero
                        try {
561 3424 caballero
                                XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
562
                                xml.addChild(xmlchild);
563
                                numMaps++;
564 3183 caballero
                        } catch (SaveException e) {
565
                                e.showError();
566
                        }
567 1222 fernando
                }
568 3424 caballero
                xml.putProperty("numMaps", numMaps);
569 1222 fernando
                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 3940 caballero
577 3424 caballero
                int numTables=0;
578 1222 fernando
                for (int i = 0; i < tables.size(); i++) {
579 3183 caballero
                        try {
580 3424 caballero
                                XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
581
                                xml.addChild(xmlchild);
582
                                numTables++;
583 3183 caballero
                        } catch (SaveException e) {
584 3940 caballero
585 3183 caballero
                                e.showError();
586
                        }
587 1222 fernando
                }
588 3424 caballero
                xml.putProperty("numTables", numTables);
589 1222 fernando
                xml.putProperty("projection", defaultProjection.getAbrev());
590 6147 jaume
591 5029 cesar
                // 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 1222 fernando
609
                return xml;
610
        }
611 2183 fernando
        /**
612
         * DOCUMENT ME!
613
         *
614
         * @param xml DOCUMENT ME!
615
         *
616
         * @return DOCUMENT ME!
617
         * @throws XMLException
618
         * @throws DriverException
619
         * @throws DriverIOException
620 3940 caballero
         * @throws OpenException
621 2183 fernando
         */
622
        public static Project createFromXML03(XMLEntity xml)
623 3183 caballero
                throws OpenException {
624 2183 fernando
                Project p = new Project();
625 3183 caballero
                try{
626 2183 fernando
                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 1222 fernando
640 2183 fernando
                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 3183 caballero
                }catch (Exception e) {
670
                        throw new OpenException(e,p.getClass().getName());
671
                }
672 2183 fernando
                return p;
673
        }
674
675 1222 fernando
        /**
676
         * DOCUMENT ME!
677
         *
678
         * @param xml DOCUMENT ME!
679
         *
680
         * @return DOCUMENT ME!
681 1830 fernando
         *
682 1222 fernando
         * @throws XMLException
683
         * @throws DriverException
684
         * @throws DriverIOException
685 3940 caballero
         * @throws OpenException
686
         * @throws VersionException
687 1222 fernando
         */
688
        public static Project createFromXML(XMLEntity xml)
689 3183 caballero
                throws OpenException {
690 3940 caballero
691 1830 fernando
                int childNumber = 0;
692 1222 fernando
                Project p = new Project();
693 3183 caballero
                try{
694 1222 fernando
                p.comments = xml.getStringProperty("comments");
695
                p.creationDate = xml.getStringProperty("creationDate");
696 1830 fernando
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 1302 vcaballero
                for (int i = 0; i < numExtents; i++) {
706 1830 fernando
                        ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
707 1302 vcaballero
                        p.extents.add(pe);
708 1222 fernando
                }
709
710 1830 fernando
                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 2217 fernando
                                        child.getStringProperty("file"));
721 1830 fernando
                        } 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 3940 caballero

727 1830 fernando
                                   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 2217 fernando
                                        child.getStringProperty("driverInfo"));
739 1830 fernando
                        }
740
                }
741
742
                childNumber += numDataSources;
743
744 1222 fernando
                int numViews = xml.getIntProperty("numViews");
745
746 1830 fernando
                for (int i = childNumber; i < (numViews + childNumber); i++) {
747 3183 caballero
                        try{
748 5033 cesar
                                ProjectView pv = (ProjectView) ProjectView.createFromXML(xml.getChild(i), p);
749
                                p.addView(pv);
750 5870 jaume
//                                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 6147 jaume
//                                        PluginServices.getMDIManager().addView(vista);
754 5870 jaume
//                                        PluginServices.getMDIManager().changeViewInfo(vista, pv.getSeedViewInfo());
755
//                                }
756 3183 caballero
                        }catch(OpenException e){
757
                                e.showError();
758
                        }
759 1222 fernando
                }
760
761 1830 fernando
                childNumber += numViews;
762
763 1222 fernando
                int numMaps = xml.getIntProperty("numMaps");
764
765 1830 fernando
                for (int i = childNumber; i < (numMaps + childNumber); i++) {
766 3183 caballero
                        try{
767 5029 cesar
                                // 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 3183 caballero
                        }catch(OpenException e){
776
                                e.showError();
777
                        }
778 1222 fernando
                }
779
780 1830 fernando
                childNumber += numMaps;
781 1222 fernando
                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 1830 fernando
                for (int i = childNumber; i < (childNumber + numTables); i++) {
792 3183 caballero
                        try{
793 5029 cesar
                                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 3183 caballero
                        }catch(OpenException e){
800
                                e.showError();
801
                        }
802 1222 fernando
                }
803 6147 jaume
804
805
                /* The project window defines its own size!!
806 5029 cesar
                childNumber += numTables;
807
                if (childNumber < xml.getNumChild()) { // restore the position of the project manager window
808
                        XMLEntity child = xml.getChild(childNumber);
809 6147 jaume
                        if (child.contains("className")
810 5029 cesar
                                        && 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 6147 jaume
                 */
817 1830 fernando
818 2183 fernando
                p.setLinkTable();
819
820 1222 fernando
                String strProj = xml.getStringProperty("projection");
821 1830 fernando
822
                if (strProj != null) {
823 1222 fernando
                        Project.setProjection(ProjectionPool.get(strProj));
824 1830 fernando
                }
825 2673 fjp
        PostProcessSupport.executeCalls();
826 3171 caballero
                }catch (Exception e) {
827 3183 caballero
                        throw new OpenException(e,p.getClass().getName());
828 3171 caballero
                }
829 3183 caballero
                return p;
830 3940 caballero
831 1222 fernando
        }
832
833
        /**
834 2183 fernando
         * 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 3940 caballero
                                try {
847 2183 fernando
                                if ((((ProjectTable) tables.get(i)).getLinkTable() != null) &&
848
                                                ((ProjectTable) tables.get(i)).getLinkTable().equals(((ProjectTable) tables
849
                                                                                                                                                                  .get(
850 3940 caballero
                                                                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 2183 fernando
                                         .addSelectionListener(lsl);
861 3940 caballero
862 2183 fernando
                                }
863 3940 caballero
                                } catch (DriverLoadException e) {
864
                                        // TODO Auto-generated catch block
865
                                        e.printStackTrace();
866
                                }
867 2183 fernando
                        }
868
                }
869
        }
870
871
        /**
872 1830 fernando
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
873 1222 fernando
         *
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 1830 fernando
         *                    encuentra en ninguna vista
880 1222 fernando
         */
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 2611 caballero
                        if (isView(layers,layer))return pView.getName();
886
                }
887 1222 fernando
888 2611 caballero
                throw new RuntimeException("The layer is not in a view");
889
        }
890
        public boolean isView(FLayers layers,FLayer layer) {
891 1222 fernando
                        for (int i = 0; i < layers.getLayersCount(); i++) {
892 2611 caballero
                                if (layers.getLayer(i) instanceof FLayers){
893
                                        return isView((FLayers)layers.getLayer(i),layer);
894
                                }else{
895 1222 fernando
                                if (layers.getLayer(i) == layer) {
896 2611 caballero
                                        return true;
897 1222 fernando
                                }
898 2611 caballero
                                }
899 1222 fernando
                        }
900 2611 caballero
                        return false;
901 1222 fernando
        }
902
        /**
903 1830 fernando
         * 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 1222 fernando
         *
907
         * @param viewName Nombre de la vista que se quiere obtener
908
         *
909
         * @return DOCUMENT ME!
910
         */
911
        public ProjectView getViewByName(String viewName) {
912 1830 fernando
                Object o = getProjectElementByName(viewName, views);
913 1222 fernando
914 1830 fernando
                if (o == null) {
915
                        return null;
916 1222 fernando
                }
917
918 1830 fernando
                return (ProjectView) o;
919 1222 fernando
        }
920 1830 fernando
921
        /**
922
         * DOCUMENT ME!
923
         *
924
         * @return DOCUMENT ME!
925
         */
926 1222 fernando
        public static IProjection getProjection() {
927
                return defaultProjection;
928
        }
929 1830 fernando
930
        /**
931
         * DOCUMENT ME!
932
         *
933
         * @param defaultProjection DOCUMENT ME!
934
         */
935 1222 fernando
        public static void setProjection(IProjection defaultProjection) {
936
                Project.defaultProjection = defaultProjection;
937
        }
938 1830 fernando
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 3940 caballero
993 2610 fernando
    public SelectableDataSource getDataSourceByLayer(FLayer layer) throws DriverException {
994
        ArrayList tables = getTables();
995
        SelectableDataSource dataSource = null;
996 3940 caballero
        try {
997 2610 fernando
        for (int i = 0; i < tables.size(); i++) {
998
            ProjectTable pt = (ProjectTable) tables.get(i);
999
            if (pt.getOriginal() == ((AlphanumericData) layer).getRecordset()){
1000 3940 caballero
                dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1001 2610 fernando
                break;
1002
            }else if (pt.getModelo() == ((AlphanumericData) layer).getRecordset()){
1003 3940 caballero
1004
                                        dataSource = (SelectableDataSource)pt.getModelo().getRecordset();
1005
1006 2610 fernando
                break;
1007
            }
1008
        }
1009 3940 caballero
        } catch (DriverLoadException e) {
1010
                        // TODO Auto-generated catch block
1011
                        e.printStackTrace();
1012
                }
1013
1014 2610 fernando
        if (dataSource == null){
1015
            //No se ha creado la tabla asociada al tema
1016
            dataSource = ((AlphanumericData) layer).getRecordset();
1017
        }
1018 3940 caballero
1019 2610 fernando
        return dataSource;
1020 3940 caballero
1021 2610 fernando
    }
1022 3940 caballero
1023 3060 fjp
    /**
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 3940 caballero
     *
1031 3060 fjp
     */
1032
    private void cleanBadReferences()
1033
    {
1034
        Hashtable usedDataSources = new Hashtable();
1035
        // Primero las tablas
1036
        int i, j;
1037 3940 caballero
        try {
1038 3060 fjp
        for (i=0; i<tables.size(); i++)
1039
        {
1040
            ProjectTable t = (ProjectTable) tables.get(i);
1041 3940 caballero
            SelectableDataSource ds;
1042
1043
                                ds = (SelectableDataSource)t.getModelo().getRecordset();
1044
1045 3060 fjp
            if (t.getOriginal() != null)
1046 3940 caballero
                usedDataSources.put(t.getOriginal().getRecordset().getName(), t.getOriginal());
1047 3060 fjp
            usedDataSources.put(ds.getName(), ds);
1048
        }
1049 3940 caballero
        } catch (DriverLoadException e) {
1050
                        // TODO Auto-generated catch block
1051
                        e.printStackTrace();
1052
                }
1053 3060 fjp
        // Ahora las vistas
1054
        try {
1055
            for (i=0; i<views.size(); i++)
1056
            {
1057
                ProjectView pv = (ProjectView) views.get(i);
1058 3940 caballero
1059 3060 fjp
                FLayers lyrs = pv.getMapContext().getLayers();
1060 3940 caballero
1061 3060 fjp
                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 6400 jmvivo
                        if (!lyrVect.isUnavailable())
1068
                                usedDataSources.put(lyrVect.getRecordset().getName(), lyrVect.getSource().getRecordset());
1069 3060 fjp
                    }
1070
                }
1071 3565 fjp
                FMap aux = pv.getMapOverViewContext();
1072
                if (aux != null)
1073 3060 fjp
                {
1074 3565 fjp
                    FLayers lyrsOverview = aux.getLayers();
1075
                    for (j=0; j<lyrsOverview.getLayersCount(); j++)
1076 3060 fjp
                    {
1077 3565 fjp
                        FLayer lyr = lyrsOverview.getLayer(j);
1078
                        if (lyr instanceof FLyrVect)
1079
                        {
1080
                            FLyrVect lyrVect = (FLyrVect) lyr;
1081 3963 caballero
                            usedDataSources.put(lyrVect.getSource().getRecordset().getName(), lyrVect.getSource().getRecordset());
1082 3565 fjp
                        }
1083 3060 fjp
                    }
1084
                }
1085 3940 caballero
1086 3060 fjp
            } // for i
1087 3963 caballero
        } catch (DriverLoadException e) {
1088
                        // TODO Auto-generated catch block
1089
                        e.printStackTrace();
1090 4455 fjp
                } catch (DriverException e) {
1091
                        // TODO Auto-generated catch block
1092
                        e.printStackTrace();
1093 3963 caballero
                }
1094 3060 fjp
        // 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 6147 jaume
1119 5029 cesar
    /**
1120
         * DOCUMENT ME!
1121
         *
1122
         * @return DOCUMENT ME!
1123 6147 jaume
     * @throws SaveException
1124 5029 cesar
         * @throws XMLException
1125 6147 jaume
         * @throws SaveException
1126 5029 cesar
         */
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 6147 jaume
1161 5029 cesar
        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 6147 jaume
1183 5029 cesar
        public static ViewInfo getSeedProjectWindow() {
1184
                return seedProjectWindow;
1185
        }
1186 6438 jmvivo
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 1222 fernando
}