Statistics
| Revision:

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

History | View | Annotate | Download (58.7 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 7343 caballero
import java.util.Comparator;
51 2217 fernando
import java.util.Date;
52 3060 fjp
import java.util.Hashtable;
53 2217 fernando
import java.util.Iterator;
54 8137 jcampos
import java.util.List;
55 7343 caballero
import java.util.TreeMap;
56 1222 fernando
57 2217 fernando
import org.cresques.cts.IProjection;
58 1222 fernando
59 3060 fjp
import com.hardcode.driverManager.DriverLoadException;
60 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
61
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
62 3060 fjp
import com.hardcode.gdbms.engine.data.DataSource;
63 7753 jmvivo
import com.hardcode.gdbms.engine.data.DataSourceFactory;
64 3060 fjp
import com.hardcode.gdbms.engine.data.NoSuchTableException;
65 2217 fernando
import com.hardcode.gdbms.engine.data.SourceInfo;
66
import com.hardcode.gdbms.engine.data.db.DBSourceInfo;
67
import com.hardcode.gdbms.engine.data.db.DBTableSourceInfo;
68
import com.hardcode.gdbms.engine.data.file.FileSourceInfo;
69
import com.hardcode.gdbms.engine.data.object.ObjectSourceInfo;
70 5029 cesar
import com.iver.andami.PluginServices;
71 7753 jmvivo
import com.iver.andami.messages.NotificationManager;
72 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
73 7689 cesar
import com.iver.andami.ui.mdiManager.SingletonWindow;
74 6880 cesar
import com.iver.andami.ui.mdiManager.WindowInfo;
75 7689 cesar
import com.iver.cit.gvsig.ProjectExtension;
76 7753 jmvivo
import com.iver.cit.gvsig.Version;
77 6878 cesar
import com.iver.cit.gvsig.fmap.MapContext;
78 8381 jmvivo
import com.iver.cit.gvsig.fmap.ViewPort;
79 1222 fernando
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
80 7436 caballero
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
81 1222 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
82
import com.iver.cit.gvsig.fmap.layers.FLayer;
83
import com.iver.cit.gvsig.fmap.layers.FLayers;
84 3060 fjp
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
85 1222 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
86 2610 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
87 1222 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
88
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
89 2183 fernando
import com.iver.cit.gvsig.fmap.operations.selection.LinkSelectionListener;
90 7304 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
91 7343 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocumentFactory;
92 7304 caballero
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
93
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
94 9199 maquerol
import com.iver.cit.gvsig.project.documents.gui.IDocumentWindow;
95 7343 caballero
import com.iver.cit.gvsig.project.documents.gui.ProjectWindow;
96 9044 cesar
import com.iver.cit.gvsig.project.documents.gui.WindowData;
97 7304 caballero
import com.iver.cit.gvsig.project.documents.layout.ProjectMap;
98 7379 caballero
import com.iver.cit.gvsig.project.documents.layout.ProjectMapFactory;
99 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
100 7304 caballero
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
101 7379 caballero
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
102 7304 caballero
import com.iver.cit.gvsig.project.documents.view.ProjectView;
103 7379 caballero
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
104 8137 jcampos
import com.iver.utiles.IPersistance;
105 2673 fjp
import com.iver.utiles.PostProcessSupport;
106 1222 fernando
import com.iver.utiles.StringUtilities;
107
import com.iver.utiles.XMLEntity;
108 7753 jmvivo
import com.iver.utiles.extensionPoints.ExtensionPoint;
109
import com.iver.utiles.extensionPoints.ExtensionPoints;
110
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
111 1222 fernando
112
/**
113
 * Clase que representa un proyecto de openSIG
114 9237 caballero
 *
115 1222 fernando
 * @author Fernando Gonz?lez Cort?s
116
 */
117
public class Project implements Serializable, PropertyChangeListener {
118 11334 jmvivo
        public static String VERSION = Version.format();
119 7654 jaume
120 11334 jmvivo
        static private IProjection defaultProjection = null;
121
122
        static private IProjection defaultFactoryProjection = CRSFactory
123 8137 jcampos
                        .getCRS("EPSG:23030");
124
125 7654 jaume
        /*
126 8137 jcampos
         * distiguishing between a static field "defaultSelectionColor" and a
127
         * selectionColor field will allow to define default color in a multiple
128 7654 jaume
         * project scenario
129
         */
130 7766 jaume
        static private Color defaultSelectionColor = Color.YELLOW;
131 8137 jcampos
132 7787 jaume
        private static int defaultMapUnits = -1;
133 8137 jcampos
134 7787 jaume
        private static int defaultDistanceUnits = -1;
135 7654 jaume
136 1222 fernando
        private PropertyChangeSupport change;
137 8137 jcampos
138 9532 caballero
        private boolean modified = false;
139 8137 jcampos
140 10679 jaume
        private String name = PluginServices.getText(this, "untitled");
141 8137 jcampos
142
        // private String path;
143 10679 jaume
        private String creationDate = new Date().toGMTString();
144 8137 jcampos
145 10679 jaume
        private String modificationDate = new Date().toGMTString();
146 8137 jcampos
147 10679 jaume
        private String owner = "";
148 8137 jcampos
149 10679 jaume
        private String comments = "";
150 8137 jcampos
151 7654 jaume
        private Color selectionColor = null;
152 8137 jcampos
153
        // private ArrayList views = new ArrayList();
154
        // private ArrayList tables = new ArrayList();
155
        // private ArrayList maps = new ArrayList();
156
        private ArrayList documents = new ArrayList();
157
158 1222 fernando
        private ArrayList extents = new ArrayList();
159 8137 jcampos
160
        // Lista de objetos del tipo camera. Necesarios para almacenar la posicion
161
        // del usuario haciendo uso de los marcadores
162
        private List cameras = new ArrayList();
163
164 10679 jaume
        /**
165
         * this is a runtime-calculated value, do NOT persist it!
166
         */
167
        private long signatureAtStartup;
168 7222 jaume
        private IProjection projection;
169 8137 jcampos
170 7689 cesar
        /**
171 8137 jcampos
         * Stores the initial properties of the windows, to be restored just after
172
         * the project is loaded. It's an ordered iterator of XMLEntity objects,
173
         * each containing a XML version of a WindowInfo object.
174 7689 cesar
         */
175
        private Iterator initialWindowProperties = null;
176 7766 jaume
177 7787 jaume
        private TreeMap sortedDocuments = new TreeMap(new Comparator() {
178
                public int compare(Object o1, Object o2) {
179
                        if ((o1 != null) && (o2 != null)) {
180 8137 jcampos
                                int priority1 = ((ProjectDocument) o1)
181
                                                .getProjectDocumentFactory().getPriority();
182
                                int priority2 = ((ProjectDocument) o2)
183
                                                .getProjectDocumentFactory().getPriority();
184
                                if (priority1 >= priority2)
185 7787 jaume
                                        return 1;
186
                                return -1;
187
                        }
188
                        return 0;
189
                }
190
        }); // Para poder ordenar
191 7766 jaume
192 1222 fernando
        /**
193
         * Creates a new Project object.
194
         */
195
        public Project() {
196
                change = new PropertyChangeSupport(this);
197
198 8137 jcampos
                // change.addPropertyChangeListener(this);
199 1222 fernando
                creationDate = DateFormat.getDateInstance().format(new Date());
200
                modificationDate = creationDate;
201 7766 jaume
                setSelectionColor(getDefaultSelectionColor());
202 11341 jmvivo
                getDefaultProjection(); //For initialize it
203 8137 jcampos
                // signatureAtStartup = computeSignature();
204 1222 fernando
205 8137 jcampos
                /*
206
                 * LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
207
                 * .getPluginDirectory() .getAbsolutePath() + File.separator +
208
                 * "drivers");
209 2183 fernando
                 */
210
        }
211
212 1222 fernando
        /**
213
         * Obtiene la fecha de creaci?n del proyecto
214 9237 caballero
         *
215 1222 fernando
         * @return
216
         */
217
        public String getCreationDate() {
218
                return creationDate;
219
        }
220
221
        /**
222
         * Obtiene el nombre del proyecto
223 9237 caballero
         *
224 1222 fernando
         * @return
225
         */
226
        public String getName() {
227
                return name;
228
        }
229
230 8137 jcampos
        // /**
231
        // * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
232
        // * proyecto
233
        // *
234
        // * @return
235
        // */
236
        // public String getPath() {
237
        // return path;
238
        // }
239 1222 fernando
240
        /**
241
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
242
         * por que al recuperar la fecha del XML hay que asignarla al objeto
243
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
244
         * deber?a de modificar nunca
245 9237 caballero
         *
246 1222 fernando
         * @param string
247
         */
248
        public void setCreationDate(String string) {
249
                creationDate = string;
250 9532 caballero
                //modified = true;
251 1222 fernando
                change.firePropertyChange("", null, null);
252
        }
253
254
        /**
255
         * Establece el nombre del proyecto
256 9237 caballero
         *
257 1222 fernando
         * @param string
258
         */
259
        public void setName(String string) {
260
                name = string;
261 9532 caballero
                //modified = true;
262 1222 fernando
                change.firePropertyChange("", null, null);
263
        }
264
265
        /**
266 7532 caballero
         * Devuelve a partir de la capa la tabla asociada.
267 9237 caballero
         *
268 8137 jcampos
         * @param co
269
         *            Capa.
270 9237 caballero
         *
271 7532 caballero
         * @return ProjectTable de la tabla asociada.
272 1222 fernando
         */
273
        public ProjectTable getTable(AlphanumericData co) {
274 8137 jcampos
                ArrayList tables = getDocumentsByType(ProjectTableFactory.registerName);
275 1222 fernando
                /**
276
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
277
                 * una capa, se puede intentar a?adir dos veces la misma tabla
278
                 */
279
                for (int i = 0; i < tables.size(); i++) {
280
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
281
                                return (ProjectTable) tables.get(i);
282
                        }
283
                }
284
285
                return null;
286
        }
287
288 7532 caballero
        /**
289
         * Devuelve a partir del nombre la tabla asociada.
290 9237 caballero
         *
291 8137 jcampos
         * @param name
292
         *            Nombre.
293 7532 caballero
         * @deprecated utilizar getProjectDocumentByName(...);
294
         * @return ProjectTable de la tabla asociada.
295
         */
296 6451 jmvivo
        public ProjectTable getTable(String name) {
297 8137 jcampos
                ArrayList tables = getDocumentsByType(ProjectTableFactory.registerName);
298 6451 jmvivo
                /**
299
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
300
                 * una capa, se puede intentar a?adir dos veces la misma tabla
301
                 */
302
                for (int i = 0; i < tables.size(); i++) {
303
                        if (((ProjectTable) tables.get(i)).getName().equals(name)) {
304
                                return (ProjectTable) tables.get(i);
305
                        }
306
                }
307
308
                return null;
309
        }
310
311 9532 caballero
        private boolean isModifiedDocuments() {
312
                ProjectDocument[] documents=(ProjectDocument[])getDocuments().toArray(new ProjectDocument[0]);
313
                for (int i=0;i<documents.length;i++) {
314
                        if (documents[i].isModified()) {
315
                                return true;
316
                        }
317
                }
318
                return false;
319
        }
320 10679 jaume
//        /**
321
//         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
322
//         * fue modificado
323
//         *
324
//         * @return
325
//         */
326
//        public boolean isModified() {
327
//                if ((this.getDocuments().size() == 0) && !modified && !isModifiedDocuments()) {
328
//                        return false;
329
//                }
330
//                return true;
331
//                // /return modified; TODO El atributo modified solo detecta cuando se
332
//                // elimina o a?ade una vista,
333
//                // /mapa o tabla pero no cuando se modifican.
334
//        }
335 1222 fernando
336
        /**
337
         * Obtiene los comentarios
338 9237 caballero
         *
339 1222 fernando
         * @return
340
         */
341
        public String getComments() {
342
                return comments;
343
        }
344
345
        /**
346
         * Obtiene la fecha de la ?ltima modificaci?n
347 9237 caballero
         *
348 1222 fernando
         * @return
349
         */
350
        public String getModificationDate() {
351
                return modificationDate;
352
        }
353
354
        /**
355
         * Obtiene el propietario del proyecto
356 9237 caballero
         *
357 1222 fernando
         * @return
358
         */
359
        public String getOwner() {
360
                return owner;
361
        }
362
363
        /**
364
         * Establece una cadena como comentarios al proyecto
365 9237 caballero
         *
366 1222 fernando
         * @param string
367
         */
368
        public void setComments(String string) {
369
                comments = string;
370 9532 caballero
                //modified = true;
371 1222 fernando
                change.firePropertyChange("", null, null);
372
        }
373
374
        /**
375
         * Establece la fecha de la ?ltima modificaci?n
376 9237 caballero
         *
377 1222 fernando
         * @param string
378
         */
379
        public void setModificationDate(String string) {
380
                modificationDate = string;
381 9532 caballero
                //modified = true;
382 1222 fernando
                change.firePropertyChange("", null, null);
383
        }
384
385
        /**
386
         * Establece el propietario del proyecto
387 9237 caballero
         *
388 1222 fernando
         * @param string
389
         */
390
        public void setOwner(String string) {
391
                owner = string;
392 9532 caballero
                //modified = true;
393 1222 fernando
                change.firePropertyChange("", null, null);
394
        }
395
396
        /**
397
         * Establece el flag de modificado del proyecto
398 9237 caballero
         *
399 1222 fernando
         * @param b
400
         */
401
        public void setModified(boolean b) {
402
                modified = b;
403 9532 caballero
                if (modified==false) {
404
                        ProjectDocument[] documents=(ProjectDocument[])getDocuments().toArray(new ProjectDocument[0]);
405
                        for (int i=0;i<documents.length;i++) {
406
                                documents[i].setModified(false);
407
                        }
408
                }
409 1222 fernando
        }
410
411
        /**
412
         * Obtiene el color de selecci?n que se usar? en el proyecto
413 9237 caballero
         *
414 1222 fernando
         * @return
415
         */
416
        public Color getSelectionColor() {
417 7654 jaume
                if (selectionColor == null) {
418
                        selectionColor = defaultSelectionColor;
419
                }
420 1222 fernando
                return selectionColor;
421
        }
422
423
        /**
424
         * Establece el color de selecci?n
425 9237 caballero
         *
426 1222 fernando
         * @param color
427
         */
428
        public void setSelectionColor(Color color) {
429
                selectionColor = color;
430
                FSymbol.setSelectionColor(color);
431 9532 caballero
                //modified = true;
432 1222 fernando
                change.firePropertyChange("selectionColor", null, color);
433
        }
434
435
        /**
436
         * Obtiene el color como un entero para su serializaci?n a XML
437 9237 caballero
         *
438 1222 fernando
         * @return
439
         */
440
        public String getColor() {
441
                return StringUtilities.color2String(selectionColor);
442
        }
443
444
        /**
445 8137 jcampos
         * M?todo invocado al recuperar de XML para establecer el color de seleccion
446
         * del proyecto
447 9237 caballero
         *
448 8137 jcampos
         * @param color
449
         *            Entero que representa un color
450 1222 fernando
         */
451
        public void setColor(String color) {
452 9532 caballero
                //modified = true;
453 1222 fernando
                selectionColor = StringUtilities.string2Color(color);
454
        }
455
456 8137 jcampos
        /*
457
         * (non-Javadoc)
458 9237 caballero
         *
459 1222 fernando
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
460
         */
461
        public void propertyChange(PropertyChangeEvent evt) {
462 9532 caballero
                //this.modified = true;
463 1222 fernando
                change.firePropertyChange(evt);
464
        }
465
466
        /**
467
         * DOCUMENT ME!
468 9237 caballero
         *
469 1222 fernando
         * @param arg1
470
         */
471
        public void addExtent(ProjectExtent arg1) {
472
                extents.add(arg1);
473 9532 caballero
                //modified = true;
474 1222 fernando
                change.firePropertyChange("addExtent", null, null);
475
        }
476
477
        /**
478
         * DOCUMENT ME!
479 9237 caballero
         *
480 1222 fernando
         * @param arg0
481 9237 caballero
         *
482 1222 fernando
         * @return
483
         */
484
        public Object removeExtent(int arg0) {
485 9532 caballero
                //modified = true;
486 1222 fernando
                change.firePropertyChange("delExtent", null, null);
487
488
                return extents.remove(arg0);
489
        }
490
491
        /**
492
         * DOCUMENT ME!
493 9237 caballero
         *
494 1222 fernando
         * @return DOCUMENT ME!
495
         */
496
        public ProjectExtent[] getExtents() {
497
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
498
        }
499
500
        /**
501 8137 jcampos
         * Metodo que a?ade una nueva camera a la lista de cameras
502 9237 caballero
         *
503 8137 jcampos
         * @param arg1
504
         *            camera introducida
505
         */
506
        public void addCamera(Object arg1) {
507
                this.cameras.add(arg1);
508 9532 caballero
                //modified = true;
509 8137 jcampos
                change.firePropertyChange("addCamera", null, null);
510
        }
511
512
        /**
513
         * Metodo que borra de la lisat un elemento seleccionado
514 9237 caballero
         *
515 8137 jcampos
         * @param arg0
516
         *            indice del elemento que se va a borrar
517 9237 caballero
         *
518 8137 jcampos
         * @return resultado de la operacion de borrado
519
         */
520
        public Object removeCamera(int arg0) {
521 9532 caballero
                //modified = true;
522 8137 jcampos
                change.firePropertyChange("delCamera", null, null);
523
524
                return this.cameras.remove(arg0);
525
        }
526
527
        /**
528
         * Metodo que devuelve la lista de cameras
529 9237 caballero
         *
530 8137 jcampos
         * @return lista de objetos de tipo camera
531
         */
532
        public Object[] getCameras() {
533
                return (Object[]) this.cameras.toArray(new Object[0]);
534
        }
535
536
        /**
537 1222 fernando
         * DOCUMENT ME!
538 9237 caballero
         *
539 1222 fernando
         * @param arg0
540
         */
541
        public synchronized void addPropertyChangeListener(
542 8137 jcampos
                        PropertyChangeListener arg0) {
543 1222 fernando
                change.addPropertyChangeListener(arg0);
544
        }
545
546
        /**
547
         * DOCUMENT ME!
548 9237 caballero
         *
549 7304 caballero
         * @deprecated utilizar getDocument(String s);
550 1222 fernando
         * @return
551
         */
552
        public ArrayList getMaps() {
553 7379 caballero
                return getDocumentsByType(ProjectMapFactory.registerName);
554 1222 fernando
        }
555
556
        /**
557
         * DOCUMENT ME!
558 9237 caballero
         *
559 7304 caballero
         * @deprecated utilizar getDocument(String s);
560 1222 fernando
         * @return
561
         */
562
        public ArrayList getTables() {
563 7379 caballero
                return getDocumentsByType(ProjectTableFactory.registerName);
564 1222 fernando
        }
565
566
        /**
567
         * DOCUMENT ME!
568 9237 caballero
         *
569 7304 caballero
         * @deprecated utilizar getDocument(String s);
570 1222 fernando
         * @return
571
         */
572
        public ArrayList getViews() {
573 7379 caballero
                return getDocumentsByType(ProjectViewFactory.registerName);
574 1222 fernando
        }
575 8137 jcampos
576 7304 caballero
        /**
577
         * A?ade un mapa al proyecto
578 9237 caballero
         *
579 7304 caballero
         * @deprecated utilizar addDocument(ProjectDocument pD);
580
         * @param m
581
         */
582
        public void addMap(ProjectMap m) {
583
                addDocument(m);
584
        }
585 1222 fernando
586
        /**
587 7304 caballero
         * Elimina un mapa del proyecto
588 9237 caballero
         *
589 7304 caballero
         * @deprecated utilizar delDocument(ProjectDocument pD);
590 8137 jcampos
         * @param i
591
         *            indice del mapa
592 7304 caballero
         */
593
        public void delMap(int i) {
594 8137 jcampos
                ArrayList list = getDocumentsByType(ProjectMapFactory.registerName);
595
                delDocument((ProjectDocument) list.get(i));
596 7304 caballero
        }
597 8137 jcampos
598 7304 caballero
        /**
599
         * A?ade una tabla al proyecto
600 9237 caballero
         *
601 7304 caballero
         * @deprecated utilizar addDocument(ProjectDocument pD);
602
         * @param t
603
         */
604
        public void addTable(ProjectTable t) {
605
                addDocument(t);
606
        }
607
608
        /**
609
         * Elimina una tabla del proyecto
610 9237 caballero
         *
611 7304 caballero
         * @deprecated utilizar delDocument(ProjectDocument pD);
612 8137 jcampos
         * @param i
613
         *            indice de la tabla
614 7304 caballero
         */
615
        public void delTable(int i) {
616 8137 jcampos
                ArrayList list = getDocumentsByType(ProjectTableFactory.registerName);
617
                delDocument((ProjectDocument) list.get(i));
618 7304 caballero
        }
619 8137 jcampos
620 7304 caballero
        /**
621
         * A?ade una vista al proyecto
622 9237 caballero
         *
623 7304 caballero
         * @deprecated utilizar addDocument(ProjectDocument pD);
624
         * @param v
625
         */
626
        public void addView(ProjectView v) {
627
                addDocument(v);
628
        }
629
630
        /**
631
         * Elimina una tabla del proyecto
632 9237 caballero
         *
633 7304 caballero
         * @deprecated utilizar delDocument(ProjectDocument pD);
634 8137 jcampos
         * @param i
635
         *            indice del proyecto
636 7304 caballero
         */
637
        public void delView(int i) {
638 8137 jcampos
                ArrayList list = getDocumentsByType(ProjectViewFactory.registerName);
639
                delDocument((ProjectDocument) list.get(i));
640 7304 caballero
        }
641
642
        /**
643 1222 fernando
         * DOCUMENT ME!
644 9237 caballero
         *
645 1222 fernando
         * @return DOCUMENT ME!
646 9237 caballero
         *
647 1222 fernando
         * @throws DriverException
648 1830 fernando
         * @throws XMLException
649 1222 fernando
         */
650 7304 caballero
        public XMLEntity getXMLEntity() {
651 1222 fernando
                XMLEntity xml = new XMLEntity();
652 1830 fernando
                xml.putProperty("className", this.getClass().getName());
653
                xml.putProperty("VERSION", VERSION);
654 10679 jaume
                xml.putProperty("comments", getComments());
655 1222 fernando
                xml.putProperty("creationDate", creationDate);
656
657 1302 vcaballero
                int numExtents = extents.size();
658 1830 fernando
                xml.putProperty("numExtents", numExtents);
659
660 1302 vcaballero
                for (int i = 0; i < numExtents; i++) {
661 1830 fernando
                        xml.addChild(((ProjectExtent) extents.get(i)).getXMLEntity());
662 1222 fernando
                }
663 1830 fernando
664 8137 jcampos
                // Guardando propiedades de las camaras
665
                int numCameras = this.cameras.size();
666
                xml.putProperty("numCameras", numCameras);
667
668
                for (int i = 0; i < numCameras; i++) {
669
                        xml.addChild(((IPersistance) this.cameras.get(i)).getXMLEntity());
670
                }
671
672
                // NUEVO: ESTO ESTA EN PRUEBAS. SIRVE PARA
673
                // BORRAR LAS REFERENCIAS A DATASOURCES QUE HEMOS
674
                // BORRADO. Hay que probar a borrarlos cuando se
675
                // borra una tabla y cuando se borra una capa.
676
                cleanBadReferences();
677
                SourceInfo[] infos = LayerFactory.getDataSourceFactory()
678
                                .getDriverInfos();
679 1830 fernando
                xml.putProperty("data-source-count", infos.length);
680
681
                for (int i = 0; i < infos.length; i++) {
682 2217 fernando
                        SourceInfo di = infos[i];
683 6438 jmvivo
                        XMLEntity child = this.getSourceInfoXMLEntity(di);
684 1830 fernando
                        xml.addChild(child);
685
                }
686 8137 jcampos
                int numDocuments = 0;
687 7304 caballero
                for (int i = 0; i < documents.size(); i++) {
688
                        try {
689 8137 jcampos
                                XMLEntity xmlchild = ((ProjectDocument) documents.get(i))
690
                                                .getXMLEntity();
691 7304 caballero
                                xml.addChild(xmlchild);
692
                                numDocuments++;
693
                        } catch (SaveException e) {
694
                                e.showError();
695
                        }
696
                }
697
                xml.putProperty("numDocuments", numDocuments);
698 8137 jcampos
                /*
699
                 * int numViews=0; for (int i = 0; i < views.size(); i++) { try {
700
                 * XMLEntity xmlchild=((ProjectView) views.get(i)).getXMLEntity();
701
                 * xml.addChild(xmlchild); numViews++; } catch (SaveException e) {
702
                 * e.showError(); } } xml.putProperty("numViews", numViews);
703 9237 caballero
                 *
704 8137 jcampos
                 * int numMaps=0; for (int i = 0; i < maps.size(); i++) { try {
705
                 * XMLEntity xmlchild=((ProjectMap) maps.get(i)).getXMLEntity();
706
                 * xml.addChild(xmlchild); numMaps++; } catch (SaveException e) {
707
                 * e.showError(); } } xml.putProperty("numMaps", numMaps);
708
                 */
709 1222 fernando
                xml.putProperty("modificationDate", modificationDate);
710 10737 jaume
                xml.putProperty("name", name, false);
711 1222 fernando
                xml.putProperty("owner", owner);
712 8137 jcampos
                xml.putProperty("selectionColor", StringUtilities
713
                                .color2String(selectionColor));
714
                /*
715
                 * int numTables=0; for (int i = 0; i < tables.size(); i++) { try {
716
                 * XMLEntity xmlchild=((ProjectTable) tables.get(i)).getXMLEntity();
717
                 * xml.addChild(xmlchild); numTables++; } catch (SaveException e) {
718 9237 caballero
                 *
719 8137 jcampos
                 * e.showError(); } } xml.putProperty("numTables", numTables);
720
                 */
721 1222 fernando
                xml.putProperty("projection", defaultProjection.getAbrev());
722 6147 jaume
723 7689 cesar
                saveWindowProperties(xml);
724 1222 fernando
                return xml;
725
        }
726 7766 jaume
727 7689 cesar
        private void saveWindowProperties(XMLEntity xml) {
728
                XMLEntity propertyList = new XMLEntity();
729 7766 jaume
730 7689 cesar
                propertyList.setName("AndamiPersistence");
731 10621 cesar
                propertyList.putProperty("className", Project.class.getName(), false);
732 7766 jaume
733 7689 cesar
                boolean projectWindowSaved = false;
734 7766 jaume
735 8137 jcampos
                IWindow[] windowList = PluginServices.getMDIManager()
736
                                .getOrderedWindows();
737 7689 cesar
                WindowInfo wi;
738
                XMLEntity windowProperties;
739 8137 jcampos
                for (int winIndex = windowList.length - 1; winIndex >= 0; winIndex--) {
740
                        wi = PluginServices.getMDIManager().getWindowInfo(
741
                                        windowList[winIndex]);
742
                        if (wi != null && wi.checkPersistence()) {
743
                                if (windowList[winIndex] instanceof Layout) { // for the
744
                                        // moment we
745
                                        // can't do this
746
                                        // for Maps
747
                                        // because they
748
                                        // don't have a
749
                                        // standard
750
                                        // model
751 7689 cesar
                                        Layout layoutWindow = (Layout) windowList[winIndex];
752
                                        windowProperties = wi.getXMLEntity();
753 8137 jcampos
                                        windowProperties.putProperty("documentType",
754 10621 cesar
                                                        ProjectMapFactory.registerName, false);
755 8137 jcampos
                                        windowProperties.putProperty("documentName", layoutWindow
756 10621 cesar
                                                        .getName(), false);
757
                                        windowProperties.putProperty("zPosition", winIndex, false);
758 7689 cesar
                                        propertyList.addChild(windowProperties);
759 8137 jcampos
                                } else if (windowList[winIndex] instanceof ProjectWindow) {
760 7689 cesar
                                        projectWindowSaved = true;
761 7766 jaume
                                        windowProperties = wi.getXMLEntity();
762 8137 jcampos
                                        windowProperties
763
                                                        .putProperty("className",
764 10621 cesar
                                                                        "com.iver.cit.gvsig.project.document.gui.ProjectWindow", false);
765
                                        windowProperties.putProperty("zPosition", winIndex, false);
766 7689 cesar
                                        propertyList.addChild(windowProperties);
767 8137 jcampos
                                } else if (windowList[winIndex] instanceof SingletonWindow) { // for
768
                                        // table,
769
                                        // view
770
                                        // and
771
                                        // maybe
772
                                        // other
773
                                        // documents
774 7766 jaume
                                        SingletonWindow viewWindow = (SingletonWindow) windowList[winIndex];
775 7689 cesar
                                        if (viewWindow.getWindowModel() instanceof ProjectDocument) {
776 8137 jcampos
                                                ProjectDocument doc = (ProjectDocument) viewWindow
777
                                                                .getWindowModel();
778 7689 cesar
                                                windowProperties = wi.getXMLEntity();
779 8137 jcampos
                                                windowProperties.putProperty("documentType", doc
780 10621 cesar
                                                                .getProjectDocumentFactory().getRegisterName(), false);
781 8137 jcampos
                                                windowProperties.putProperty("documentName",
782
                                                                ((ProjectDocument) viewWindow.getWindowModel())
783 10621 cesar
                                                                                .getName(), false);
784
                                                windowProperties.putProperty("zPosition", winIndex, false);
785 9237 caballero
786 9044 cesar
                                                // TODO this will be generalized to all ProjectDocuments as soon as possible
787 9199 maquerol
//                                                if (viewWindow instanceof BaseView) {
788
//                                                        BaseView win = (BaseView) viewWindow;
789
//                                                        windowProperties.addChild(win.getWindowData().getXMLEntity());
790
//                                                }
791
                                                if (viewWindow instanceof IDocumentWindow) {
792
                                                        IDocumentWindow win = (IDocumentWindow) viewWindow;
793 9044 cesar
                                                        windowProperties.addChild(win.getWindowData().getXMLEntity());
794
                                                }
795 9237 caballero
796 7689 cesar
                                                propertyList.addChild(windowProperties);
797 7766 jaume
                                        }
798 7689 cesar
                                }
799
                        }
800
                }
801 7766 jaume
802 7689 cesar
                if (projectWindowSaved == false) {
803
                        // If the Project Manager was closed, it was not in the
804
                        // previous window list. Save it now
805 8137 jcampos
                        ProjectExtension pe = (ProjectExtension) PluginServices
806
                                        .getExtension(com.iver.cit.gvsig.ProjectExtension.class);
807 7766 jaume
808 8137 jcampos
                        if (pe != null) {
809 7689 cesar
                                IWindow projectWindow = pe.getProjectWindow();
810 8137 jcampos
                                wi = PluginServices.getMDIManager()
811
                                                .getWindowInfo(projectWindow);
812
                                if (wi != null && wi.checkPersistence()) {
813 7689 cesar
                                        windowProperties = wi.getXMLEntity();
814 8137 jcampos
                                        if (windowProperties != null) {
815
                                                windowProperties
816
                                                                .putProperty("className",
817 10621 cesar
                                                                                "com.iver.cit.gvsig.project.document.gui.ProjectWindow", false);
818 7689 cesar
                                                propertyList.addChild(windowProperties);
819
                                        }
820
                                }
821
                        }
822
                }
823 7766 jaume
824 7689 cesar
                xml.addChild(propertyList);
825
        }
826 7766 jaume
827 8137 jcampos
        /**
828
         * Store the initial window properties, to later restore the window sizes
829
         * and positions
830
         */
831 7689 cesar
        private void storeInitialWindowProperties(XMLEntity xml) {
832
                XMLEntity child;
833
                int childNumb;
834 7766 jaume
835 7689 cesar
                // order the window properties before restoring them, so that we also
836
                // restore the zPosition
837
                TreeMap orderedProperties = new TreeMap();
838
                int maximum = 1;
839 8137 jcampos
                for (childNumb = xml.getChildrenCount() - 1; childNumb >= 0; childNumb--) {
840 7689 cesar
                        child = xml.getChild(childNumb);
841
                        if (child.contains("zPosition")) {
842 8137 jcampos
                                orderedProperties.put(new Integer(-child
843
                                                .getIntProperty("zPosition")), child); // reverse the
844
                                // order, so
845
                                // that we add
846
                                // the back
847
                                // windows first
848
                        } else {
849
                                orderedProperties.put(new Integer(maximum++), child); // the
850
                                // windows
851
                                // without
852
                                // zPosition
853
                                // will
854
                                // be on
855
                                // the
856
                                // fore
857 7689 cesar
                        }
858
                }
859 7766 jaume
860
                this.initialWindowProperties = orderedProperties.values().iterator();
861 7689 cesar
        }
862 7766 jaume
863 8137 jcampos
        /**
864
         * Store the initial window properties, to later restore the window sizes
865
         * and positions
866
         */
867 7689 cesar
        private void storeInitialWindowProperties061(XMLEntity xml) {
868
                XMLEntity child;
869 7697 cesar
                int childNumb = 0;
870 7689 cesar
                ArrayList windowList = new ArrayList();
871 7766 jaume
872 8137 jcampos
                child = xml.getChild(xml.getChildrenCount() - 1);
873 7689 cesar
                if (child.contains("className")
874 8137 jcampos
                                && child.getStringProperty("className").equals(
875
                                                "com.iver.cit.gvsig.project.Project")
876 7689 cesar
                                && child.contains("name")
877
                                && child.getStringProperty("name").equals("ViewInfoProperties")) {
878 8137 jcampos
                        child.putProperty("className",
879
                                        "com.iver.cit.gvsig.project.document.gui.ProjectWindow");
880 7689 cesar
                        windowList.add(child);
881
                }
882 7766 jaume
883 7689 cesar
                // try to open the views
884 7697 cesar
                if (xml.contains("numExtents"))
885
                        childNumb += xml.getIntProperty("numExtents");
886
                if (xml.contains("data-source-count"))
887
                        childNumb += xml.getIntProperty("data-source-count");
888 8137 jcampos
                int limit = 0;
889 7697 cesar
                if (xml.contains("numViews"))
890 8400 cesar
                        limit = xml.getIntProperty("numViews")+childNumb;
891 7766 jaume
892 7689 cesar
                XMLEntity view;
893
                for (int i = childNumb; i < limit; i++) {
894
                        view = xml.getChild(i);
895 8137 jcampos
                        child = view.getChild(view.getChildrenCount() - 1);
896
                        if (child.contains("className")
897
                                        && child.getStringProperty("className").equals(
898
                                                        "com.iver.cit.gvsig.project.ProjectView")
899
                                        && child.contains("name")
900
                                        && child.getStringProperty("name").equals(
901
                                                        "ViewInfoProperties")) {
902
                                child.putProperty("documentName", view
903
                                                .getStringProperty("name"));
904
                                child.putProperty("documentType",
905
                                                ProjectViewFactory.registerName);
906 7689 cesar
                                windowList.add(child);
907
                        }
908
                }
909 7766 jaume
910 7697 cesar
                if (xml.contains("numViews"))
911
                        childNumb += xml.getIntProperty("numViews");
912 7689 cesar
913 7697 cesar
                if (xml.contains("numMaps"))
914 7766 jaume
                        limit = childNumb + xml.getIntProperty("numMaps");
915 7697 cesar
916 7689 cesar
                // try to open the maps
917
                XMLEntity map;
918
                for (int i = childNumb; i < limit; i++) {
919
                        map = xml.getChild(i);
920 8137 jcampos
                        for (int j = 0; j < map.getChildrenCount(); j++) {
921 7689 cesar
                                child = map.getChild(j);
922 8137 jcampos
                                if (child.contains("className")
923
                                                && child.getStringProperty("className").equals(
924
                                                                "com.iver.cit.gvsig.project.ProjectMap")
925
                                                && child.contains("name")
926
                                                && child.getStringProperty("name").equals(
927
                                                                "ViewInfoProperties")) {
928
                                        child.putProperty("documentName", map
929
                                                        .getStringProperty("name"));
930
                                        child.putProperty("documentType",
931
                                                        ProjectMapFactory.registerName);
932 7689 cesar
                                        windowList.add(child);
933
                                }
934
                        }
935
                }
936 7766 jaume
937
                this.initialWindowProperties = windowList.iterator();
938 7689 cesar
        }
939
940 2183 fernando
        /**
941 8137 jcampos
         * Restores the size, position and order of the windows, according to
942
         * variable initialWindowProperties. If this variable is null, the method
943
         * just opens the project manager window.
944 9237 caballero
         *
945 7689 cesar
         */
946
        public void restoreWindowProperties() {
947
                boolean projectWindowRestored = false;
948
                XMLEntity child;
949 7766 jaume
950 7689 cesar
                Iterator propertiesIterator = this.initialWindowProperties;
951 8137 jcampos
                if (propertiesIterator != null) {
952 7689 cesar
                        this.initialWindowProperties = null;
953 7766 jaume
954 7689 cesar
                        while (propertiesIterator.hasNext()) {
955
                                child = (XMLEntity) propertiesIterator.next();
956 8137 jcampos
                                if (child.contains("name") // restore the position of the
957
                                                // document windows
958
                                                && child.getStringProperty("name").equals(
959
                                                                "ViewInfoProperties")
960
                                                && child.contains("documentType")) {
961
                                        boolean isClosed = true;
962
                                        if (child.contains("isClosed"))
963
                                                isClosed = child.getBooleanProperty("isClosed");
964
                                        if (isClosed == false) {
965
                                                WindowInfo windowProps = WindowInfo
966
                                                                .createFromXMLEntity(child);
967
                                                String documentName = child
968
                                                                .getStringProperty("documentName");
969
                                                String documentType = child
970
                                                                .getStringProperty("documentType");
971
                                                ProjectDocument pd = this.getProjectDocumentByName(
972
                                                                documentName, documentType);
973 10336 cesar
                                                if (pd==null) continue;
974 9044 cesar
                                                IWindow win = null;
975 9199 maquerol
                                                if (pd instanceof ProjectDocument
976 9044 cesar
                                                                && child.getChildrenCount()>0
977
                                                                && child.getChild(0).getName().equals("windowData")) {
978
                                                        // this will be generalized to all ProjectDocuments as soon as possible
979
                                                        WindowData windowData = new WindowData();
980
                                                        windowData.setXMLEntity(child.getChild(0));
981 9199 maquerol
                                                        pd.storeWindowData(windowData);
982
                                                        win = ((ProjectDocument)pd).createWindow();
983 9044 cesar
                                                } else {
984
                                                        win = pd.createWindow();
985
                                                }
986 8137 jcampos
                                                PluginServices.getMDIManager().addWindow(win);
987
                                                PluginServices.getMDIManager().changeWindowInfo(win,
988
                                                                windowProps);
989
                                        }
990
                                } else if (child.contains("className") // restore the position
991
                                                // of the project
992
                                                // manager window
993
                                                && child
994
                                                                .getStringProperty("className")
995
                                                                .equals(
996
                                                                                "com.iver.cit.gvsig.project.document.gui.ProjectWindow")
997
                                                && child.contains("name")
998
                                                && child.getStringProperty("name").equals(
999
                                                                "ViewInfoProperties")) {
1000
                                        WindowInfo wi = WindowInfo.createFromXMLEntity(child);
1001 8374 cesar
                                        // don't restore size for ProjectManager window, as it's not resizable
1002
                                        wi.setHeight(-1);
1003
                                        wi.setWidth(-1);
1004 8137 jcampos
                                        ProjectExtension pe = (ProjectExtension) PluginServices
1005
                                                        .getExtension(com.iver.cit.gvsig.ProjectExtension.class);
1006
                                        if (pe != null) {
1007
                                                pe.setProject(this);
1008
                                                pe.showProjectWindow(wi);
1009
                                        }
1010
                                        projectWindowRestored = true;
1011
                                }
1012 7689 cesar
                        }
1013
                }
1014 7766 jaume
1015 8137 jcampos
                if (!projectWindowRestored) { // if the project window was not stored
1016
                        // in the project, open it now
1017
                        ProjectExtension pe = (ProjectExtension) PluginServices
1018
                                        .getExtension(com.iver.cit.gvsig.ProjectExtension.class);
1019 7766 jaume
1020 8137 jcampos
                        if (pe != null) {
1021 7689 cesar
                                pe.setProject(this);
1022
                                pe.showProjectWindow();
1023
                        }
1024
                }
1025
        }
1026 7766 jaume
1027 7689 cesar
        /**
1028 2183 fernando
         * DOCUMENT ME!
1029 9237 caballero
         *
1030 8137 jcampos
         * @param xml
1031
         *            DOCUMENT ME!
1032 9237 caballero
         *
1033 2183 fernando
         * @return DOCUMENT ME!
1034
         * @throws XMLException
1035
         * @throws DriverException
1036
         * @throws DriverIOException
1037 3940 caballero
         * @throws OpenException
1038 2183 fernando
         */
1039 8137 jcampos
        public static Project createFromXML03(XMLEntity xml) throws OpenException {
1040 2183 fernando
                Project p = new Project();
1041 8137 jcampos
                try {
1042
                        p.comments = xml.getStringProperty("comments");
1043
                        p.creationDate = xml.getStringProperty("creationDate");
1044 7304 caballero
1045 8137 jcampos
                        int numExtents = xml.getIntProperty("numExtents");
1046
                        for (int i = 0; i < numExtents; i++) {
1047
                                ProjectExtent pe = ProjectExtent.createFromXML03(xml
1048
                                                .getChild(i));
1049
                                p.extents.add(pe);
1050
                        }
1051 1222 fernando
1052 8137 jcampos
                        int numDocuments = 0;
1053
                        if (xml.contains("numDocuments")) {
1054
                                numDocuments = xml.getIntProperty("numDocuments");
1055
                        } else {
1056
                                int numViews = xml.getIntProperty("numViews");
1057
                                int numMaps = xml.getIntProperty("numMaps");
1058
                                int numTables = xml.getIntProperty("numTables");
1059
                                numDocuments = numViews + numMaps + numTables;
1060
                        }
1061
                        for (int i = numExtents; i < numDocuments + numExtents; i++) {
1062
                                ProjectDocument pD = ProjectDocument.createFromXML03(xml
1063
                                                .getChild(i), p);
1064
                                p.addDocument(pD);
1065
                                p.sortedDocuments.put(pD, new Integer(i));
1066
                        }
1067
                        ProjectDocument[] sortDocKeys = (ProjectDocument[]) p.sortedDocuments
1068
                                        .keySet().toArray(new ProjectDocument[0]);
1069
                        Integer[] sortDocValues = (Integer[]) p.sortedDocuments.values()
1070
                                        .toArray(new Integer[0]);
1071 7349 caballero
1072 8137 jcampos
                        int i = 0;
1073
                        for (int k = numExtents; k < (numDocuments + numExtents); k++) {
1074
                                sortDocKeys[i].setXMLEntity(xml.getChild(sortDocValues[i]
1075
                                                .intValue()));
1076
                                i++;
1077
                        }
1078 2183 fernando
1079 8137 jcampos
                        p.modificationDate = xml.getStringProperty("modificationDate");
1080 9532 caballero
                        //p.modified = xml.getBooleanProperty("modified");
1081 8137 jcampos
                        p.name = xml.getStringProperty("name");
1082
                        p.owner = xml.getStringProperty("owner");
1083
                        p.selectionColor = StringUtilities.string2Color(xml
1084
                                        .getStringProperty("selectionColor"));
1085 2183 fernando
1086 8137 jcampos
                        String strProj = xml.getStringProperty("projection");
1087
                        if (strProj != null)
1088
                                p.setProjection(CRSFactory.getCRS(strProj));
1089
                } catch (Exception e) {
1090
                        throw new OpenException(e, p.getClass().getName());
1091 3183 caballero
                }
1092 2183 fernando
                return p;
1093
        }
1094
1095 1222 fernando
        /**
1096
         * DOCUMENT ME!
1097 9237 caballero
         *
1098 8137 jcampos
         * @param xml
1099
         *            DOCUMENT ME!
1100 9237 caballero
         *
1101 1222 fernando
         * @return DOCUMENT ME!
1102 9237 caballero
         *
1103 1222 fernando
         * @throws XMLException
1104
         * @throws DriverException
1105
         * @throws DriverIOException
1106 3940 caballero
         * @throws OpenException
1107
         * @throws VersionException
1108 1222 fernando
         */
1109 8137 jcampos
        public static Project createFromXML(XMLEntity xml) throws OpenException {
1110 3940 caballero
1111 1830 fernando
                int childNumber = 0;
1112 1222 fernando
                Project p = new Project();
1113 10679 jaume
1114 8137 jcampos
                try {
1115
                        p.comments = xml.getStringProperty("comments");
1116
                        p.creationDate = xml.getStringProperty("creationDate");
1117
                        int numExtents = xml.getIntProperty("numExtents");
1118 1830 fernando
1119 8137 jcampos
                        for (int i = 0; i < numExtents; i++) {
1120
                                ProjectExtent pe = ProjectExtent.createFromXML(xml.getChild(i));
1121
                                p.extents.add(pe);
1122
                        }
1123 1222 fernando
1124 8137 jcampos
                        // Leemos el ultiom hijo recogido
1125
                        childNumber = numExtents;
1126 1830 fernando
1127 8137 jcampos
                        // Recogiendo el numero de cameras
1128 8145 sbayarri
                        int numCameras = 0;
1129
                        if (xml.contains("numCameras"))
1130
                                numCameras = xml.getIntProperty("numCameras");
1131 1830 fernando
1132 8137 jcampos
                        // Recogiendo todo las camaras
1133
                        for (int i = childNumber; i < (childNumber + numCameras); i++) {
1134
                                XMLEntity xmlProp = xml.getChild(i);
1135
                                try {
1136
                                        String className = xmlProp.getStringProperty("className");
1137
                                        Class classProp = Class.forName(className);
1138
                                        Object obj = classProp.newInstance();
1139
                                        IPersistance objPersist = (IPersistance) obj;
1140
                                        objPersist.setXMLEntity(xmlProp);
1141
                                        p.cameras.add(obj);
1142
                                } catch (Exception e) {
1143
                                        continue;
1144
                                }
1145
                        }
1146 1830 fernando
1147 8137 jcampos
                        childNumber += numCameras;
1148 1830 fernando
1149 8137 jcampos
                        int numDataSources = xml.getIntProperty("data-source-count");
1150
1151
                        for (int i = childNumber; i < (childNumber + numDataSources); i++) {
1152
                                XMLEntity child = xml.getChild(i);
1153
                                registerDataSourceFromXML(child);
1154 7304 caballero
                        }
1155 1222 fernando
1156 8137 jcampos
                        childNumber += numDataSources;
1157
                        int numDocuments = 0;
1158
                        if (xml.contains("numDocuments")) {
1159
                                numDocuments = xml.getIntProperty("numDocuments");
1160
                        } else {
1161
                                int numViews = xml.getIntProperty("numViews");
1162
                                int numMaps = xml.getIntProperty("numMaps");
1163
                                int numTables = xml.getIntProperty("numTables");
1164
                                numDocuments = numViews + numMaps + numTables;
1165 7436 caballero
                        }
1166 7304 caballero
1167 8137 jcampos
                        for (int i = childNumber; i < (numDocuments + childNumber); i++) {
1168
                                try {
1169
                                        ProjectDocument pD = ProjectDocument.createFromXML(xml
1170
                                                        .getChild(i), p);
1171
                                        p.addDocument(pD);
1172
                                        p.sortedDocuments.put(pD, new Integer(i));
1173
                                } catch (OpenException e) {
1174
                                        e.showError();
1175
                                }
1176
                        }
1177
                        ProjectDocument[] sortDocKeys = (ProjectDocument[]) p.sortedDocuments
1178
                                        .keySet().toArray(new ProjectDocument[0]);
1179
                        Integer[] sortDocValues = (Integer[]) p.sortedDocuments.values()
1180
                                        .toArray(new Integer[0]);
1181 1222 fernando
1182 8137 jcampos
                        int i = 0;
1183
                        for (int k = childNumber; k < (numDocuments + childNumber); k++) {
1184
                                try {
1185
                                        sortDocKeys[i].setXMLEntity(xml.getChild(sortDocValues[i]
1186
                                                        .intValue()));
1187
                                } catch (OpenException e) {
1188
                                        e.showError();
1189
                                }
1190
                                i++;
1191
                        }
1192
                        childNumber += numDocuments;
1193 1830 fernando
1194 8137 jcampos
                        p.modificationDate = xml.getStringProperty("modificationDate");
1195 9532 caballero
                        //p.modified = xml.getBooleanProperty("modified");
1196 8137 jcampos
                        p.name = xml.getStringProperty("name");
1197
                        p.owner = xml.getStringProperty("owner");
1198
                        p.selectionColor = StringUtilities.string2Color(xml
1199
                                        .getStringProperty("selectionColor"));
1200 7766 jaume
1201 8137 jcampos
                        p.setLinkTable();
1202
                        String strProj = xml.getStringProperty("projection");
1203
1204
                        if (strProj != null) {
1205
                                p.setProjection(CRSFactory.getCRS(strProj));
1206 7689 cesar
                        }
1207 8137 jcampos
1208
                        if (childNumber < xml.getChildrenCount()) { // restore the position
1209
                                // of the windows
1210
                                XMLEntity child = xml.getChild(childNumber);
1211
                                if (child.contains("name")
1212
                                                && child.getStringProperty("name").equals(
1213
                                                                "AndamiPersistence")) {
1214
                                        p.storeInitialWindowProperties(child);
1215
                                } else if (child.contains("className")
1216
                                                && child.getStringProperty("className").equals(
1217
                                                                "com.iver.cit.gvsig.project.Project")
1218
                                                && child.contains("name")
1219
                                                && child.getStringProperty("name").equals(
1220
                                                                "ViewInfoProperties")) {
1221
                                        p.storeInitialWindowProperties061(xml);
1222
                                }
1223 7689 cesar
                        }
1224 7766 jaume
1225 8137 jcampos
                        PostProcessSupport.executeCalls();
1226
                } catch (Exception e) {
1227
                        throw new OpenException(e, p.getClass().getName());
1228 3171 caballero
                }
1229 10679 jaume
1230 3183 caballero
                return p;
1231 3940 caballero
1232 1222 fernando
        }
1233
1234
        /**
1235 2183 fernando
         * Reestablece los link que ten?a cada tabla con las dem?s.
1236
         */
1237
        public void setLinkTable() {
1238 7379 caballero
                ArrayList tables = getDocumentsByType(ProjectTableFactory.registerName);
1239 2183 fernando
1240
                for (int i = 0; i < tables.size(); i++) {
1241
                        for (int j = 0; j < tables.size(); j++) {
1242 8137 jcampos
                                /*
1243
                                 * System.out.println("name = " + ((ProjectTable)
1244
                                 * tables.get(j)).getModelo().getName());
1245
                                 * System.out.println("linktable = " + ((ProjectTable)
1246
                                 * tables.get(i)).getLinkTable());
1247
                                 */
1248 3940 caballero
                                try {
1249 8137 jcampos
                                        if ((((ProjectTable) tables.get(i)).getLinkTable() != null)
1250
                                                        && ((ProjectTable) tables.get(i)).getLinkTable()
1251 10679 jaume
                                                        .equals(
1252
                                                                        ((ProjectTable) tables.get(j))
1253
                                                                        .getModelo().getRecordset()
1254
                                                                        .getName())) {
1255 8137 jcampos
                                                LinkSelectionListener lsl;
1256 3940 caballero
1257 8137 jcampos
                                                lsl = new LinkSelectionListener(((ProjectTable) tables
1258
                                                                .get(i)).getModelo().getRecordset(),
1259
                                                                ((ProjectTable) tables.get(j)).getModelo()
1260 10679 jaume
                                                                .getRecordset(), ((ProjectTable) tables
1261 8137 jcampos
                                                                                .get(i)).getField1(),
1262 10679 jaume
                                                                                ((ProjectTable) tables.get(i)).getField2());
1263 3940 caballero
1264 8137 jcampos
                                                (((ProjectTable) tables.get(i)).getModelo()
1265
                                                                .getRecordset()).addSelectionListener(lsl);
1266 3940 caballero
1267 8137 jcampos
                                        }
1268 10626 caballero
                                } catch (ReadDriverException e) {
1269
                                        e.printStackTrace();
1270 3940 caballero
                                }
1271 2183 fernando
                        }
1272
                }
1273
        }
1274
1275
        /**
1276 1830 fernando
         * Obtiene la vista que contiene a la capa que se pasa como par?metro
1277 9237 caballero
         *
1278 8137 jcampos
         * @param layer
1279
         *            Capa cuya vista se quiere obtener
1280 9237 caballero
         *
1281 1222 fernando
         * @return
1282 9237 caballero
         *
1283 8137 jcampos
         * @throws RuntimeException
1284
         *             Si la capa que se pasa como par?metro no se encuentra en
1285
         *             ninguna vista
1286 1222 fernando
         */
1287
        public String getView(FLayer layer) {
1288 8137 jcampos
                ArrayList views = getDocumentsByType(ProjectViewFactory.registerName);
1289 1222 fernando
                for (int v = 0; v < views.size(); v++) {
1290
                        ProjectView pView = (ProjectView) views.get(v);
1291
                        FLayers layers = pView.getMapContext().getLayers();
1292 8137 jcampos
                        if (isView(layers, layer))
1293
                                return pView.getName();
1294 2611 caballero
                }
1295 1222 fernando
1296 9479 fdiaz
                throw new RuntimeException("The layer '"+layer.getName()+"' is not in a view");
1297 2611 caballero
        }
1298 6888 jaume
1299
        public boolean isView(FLayers layers, FLayer layer) {
1300
                for (int i = 0; i < layers.getLayersCount(); i++) {
1301 8137 jcampos
                        if (layers.getLayer(i) instanceof FLayers) {
1302
                                return isView((FLayers) layers.getLayer(i), layer);
1303 7304 caballero
                        }
1304
                        if (layers.getLayer(i) == layer) {
1305 8137 jcampos
                                return true;
1306 1222 fernando
                        }
1307 6888 jaume
                }
1308
                return false;
1309 1222 fernando
        }
1310 6888 jaume
1311 1222 fernando
        /**
1312 8137 jcampos
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas) con el que
1313
         * se pasa como par?metro. Devuelve null si no hay ninguna vista con ese
1314
         * nombre
1315 9237 caballero
         *
1316 8137 jcampos
         * @param viewName
1317
         *            Nombre de la vista que se quiere obtener
1318 9237 caballero
         *
1319 1222 fernando
         * @return DOCUMENT ME!
1320
         */
1321 8137 jcampos
        /*
1322
         * public ProjectView getViewByName(String viewName) { ArrayList
1323
         * views=getDocuments(PluginServices.getText(this,"Vista")); Object o =
1324
         * getProjectDocumentByName(viewName, PluginServices.getText(this,"Vista"));
1325 9237 caballero
         *
1326 8137 jcampos
         * if (o == null) { return null; }
1327 9237 caballero
         *
1328 8137 jcampos
         * return (ProjectView) o; }
1329
         */
1330 1830 fernando
        /**
1331
         * DOCUMENT ME!
1332 9237 caballero
         *
1333 1830 fernando
         * @return DOCUMENT ME!
1334
         */
1335 7222 jaume
        public IProjection getProjection() {
1336
                if (projection == null)
1337
                        projection = Project.defaultProjection;
1338
                return projection;
1339 1222 fernando
        }
1340 1830 fernando
1341
        /**
1342
         * DOCUMENT ME!
1343 9237 caballero
         *
1344 8137 jcampos
         * @param defaultProjection
1345
         *            DOCUMENT ME!
1346 1830 fernando
         */
1347 7222 jaume
        public void setProjection(IProjection projection) {
1348
                this.projection = projection;
1349
        }
1350
1351
        /**
1352
         * Sets the projection used when no projection is defined
1353 9237 caballero
         *
1354 8137 jcampos
         * @param defaultProjection
1355
         *            DOCUMENT ME!
1356 7222 jaume
         */
1357
        public static void setDefaultProjection(IProjection defaultProjection) {
1358 1222 fernando
                Project.defaultProjection = defaultProjection;
1359
        }
1360 1830 fernando
1361 7222 jaume
        public static IProjection getDefaultProjection() {
1362 11334 jmvivo
                if (defaultProjection == null){
1363
                        XMLEntity xml = PluginServices.getPluginServices("com.iver.cit.gvsig")
1364
                        .getPersistentXML();
1365 11503 jaume
1366 11334 jmvivo
                        // Default Projection
1367 11503 jaume
                        String projCode = null;
1368 11334 jmvivo
                        if (xml.contains("DefaultProjection")) {
1369
                                projCode = xml.getStringProperty("DefaultProjection");
1370
                                Project.setDefaultProjection(CRSFactory.getCRS(projCode));
1371
                        } else {
1372
                                Project.setDefaultProjection(defaultFactoryProjection);
1373
                        }
1374 11503 jaume
1375 11334 jmvivo
                }
1376 7222 jaume
                return Project.defaultProjection;
1377
        }
1378
1379 1830 fernando
        /**
1380 8137 jcampos
         * Obtiene un documento a partir de su nombre y el nombre de registro en el
1381
         * pointExtension, este ?ltimo se puede obtener del
1382
         * Project****Factory.registerName.
1383 9237 caballero
         *
1384 8137 jcampos
         * @param name
1385
         *            Nombre del documento
1386
         * @param type
1387
         *            nombre de registro en el extensionPoint
1388 9237 caballero
         *
1389 7529 caballero
         * @return Documento
1390 1830 fernando
         */
1391 7304 caballero
        public ProjectDocument getProjectDocumentByName(String name, String type) {
1392 8137 jcampos
                ArrayList docs = getDocumentsByType(type);
1393 7304 caballero
                for (Iterator iter = docs.iterator(); iter.hasNext();) {
1394
                        ProjectDocument elem = (ProjectDocument) iter.next();
1395 1830 fernando
1396
                        if (elem.getName().equals(name)) {
1397
                                return elem;
1398
                        }
1399
                }
1400
1401
                return null;
1402
        }
1403
1404
        /**
1405
         * DOCUMENT ME!
1406 9237 caballero
         *
1407 1830 fernando
         * @param name
1408 9237 caballero
         *
1409 1830 fernando
         * @return
1410
         */
1411 8137 jcampos
        /*
1412
         * public ProjectTable getTableByName(String name) { ArrayList
1413
         * tables=getDocuments(PluginServices.getText(this,"Tabla")); Object o =
1414
         * getProjectElementByName(name, tables);
1415 9237 caballero
         *
1416 8137 jcampos
         * if (o == null) { return null; }
1417 9237 caballero
         *
1418 8137 jcampos
         * return (ProjectTable) o; }
1419
         */
1420 1830 fernando
        /**
1421
         * DOCUMENT ME!
1422 9237 caballero
         *
1423 1830 fernando
         * @param name
1424 9237 caballero
         *
1425 1830 fernando
         * @return
1426
         */
1427 8137 jcampos
        /*
1428
         * public ProjectMap getLayoutByName(String name) { Object o =
1429
         * getProjectElementByName(name, maps);
1430 9237 caballero
         *
1431 8137 jcampos
         * if (o == null) { return null; }
1432 9237 caballero
         *
1433 8137 jcampos
         * return (ProjectMap) o; }
1434
         */
1435
        public SelectableDataSource getDataSourceByLayer(FLayer layer)
1436 10626 caballero
                        throws ReadDriverException {
1437 8137 jcampos
                ArrayList tables = getDocumentsByType(ProjectTableFactory.registerName);
1438
                SelectableDataSource dataSource = null;
1439 10661 caballero
                for (int i = 0; i < tables.size(); i++) {
1440
                        ProjectTable pt = (ProjectTable) tables.get(i);
1441
                        if (pt.getOriginal() == ((AlphanumericData) layer)
1442
                                        .getRecordset()) {
1443
                                dataSource = pt.getModelo().getRecordset();
1444
                                break;
1445
                        } else if (pt.getModelo() == ((AlphanumericData) layer)
1446
                                        .getRecordset()) {
1447 8137 jcampos
                                        dataSource = pt.getModelo().getRecordset();
1448
                                        break;
1449
                        }
1450 3940 caballero
                }
1451
1452 8137 jcampos
                if (dataSource == null) {
1453
                        // No se ha creado la tabla asociada al tema
1454
                        dataSource = ((AlphanumericData) layer).getRecordset();
1455
                }
1456 3940 caballero
1457 8137 jcampos
                return dataSource;
1458 3940 caballero
1459 8137 jcampos
        }
1460 3940 caballero
1461 8137 jcampos
        /**
1462
         * Recorremos las capas y las tablas del proyecto, y creamos una lista con
1463
         * todos los datasources de GDBMS que estamos usando. Luego recorremos los
1464
         * que est?n registrados, y borramos aquellos que no est?n siendo usados, es
1465
         * decir, aquellos que no est?n en nuestra lista (un Hash con clave el
1466
         * nombre del GDBMS)
1467 9237 caballero
         *
1468 8137 jcampos
         */
1469
        private void cleanBadReferences() {
1470
                ArrayList tables = getDocumentsByType(ProjectTableFactory.registerName);
1471
                Hashtable usedDataSources = new Hashtable();
1472
                // Primero las tablas
1473
                int i, j;
1474
                try {
1475
                        for (i = 0; i < tables.size(); i++) {
1476
                                ProjectTable t = (ProjectTable) tables.get(i);
1477
                                SelectableDataSource ds;
1478 3940 caballero
1479 8137 jcampos
                                ds = t.getModelo().getRecordset();
1480 3940 caballero
1481 8137 jcampos
                                if (t.getOriginal() != null)
1482
                                        usedDataSources.put(t.getOriginal().getRecordset()
1483
                                                        .getName(), t.getOriginal());
1484
                                usedDataSources.put(ds.getName(), ds);
1485
                        }
1486 10626 caballero
                } catch (ReadDriverException e) {
1487
                        e.printStackTrace();
1488 8137 jcampos
                }
1489
                // Ahora las vistas
1490
                ArrayList views = getDocumentsByType(ProjectViewFactory.registerName);
1491
                try {
1492
                        for (i = 0; i < views.size(); i++) {
1493
                                ProjectView pv = (ProjectView) views.get(i);
1494 3940 caballero
1495 8137 jcampos
                                FLayers lyrs = pv.getMapContext().getLayers();
1496 3940 caballero
1497 8137 jcampos
                                for (j = 0; j < lyrs.getLayersCount(); j++) {
1498
                                        FLayer lyr = lyrs.getLayer(j);
1499
                                        if (lyr instanceof FLyrVect) {
1500
                                                FLyrVect lyrVect = (FLyrVect) lyr;
1501
                                                if (lyrVect.isAvailable())
1502
                                                        usedDataSources.put(lyrVect.getRecordset()
1503
                                                                        .getName(), lyrVect.getSource()
1504
                                                                        .getRecordset());
1505
                                        }
1506
                                }
1507
                                MapContext aux = pv.getMapOverViewContext();
1508
                                if (aux != null) {
1509
                                        FLayers lyrsOverview = aux.getLayers();
1510
                                        for (j = 0; j < lyrsOverview.getLayersCount(); j++) {
1511
                                                FLayer lyr = lyrsOverview.getLayer(j);
1512
                                                if (lyr instanceof FLyrVect) {
1513
                                                        FLyrVect lyrVect = (FLyrVect) lyr;
1514
                                                        usedDataSources.put(lyrVect.getSource()
1515
                                                                        .getRecordset().getName(), lyrVect
1516
                                                                        .getSource().getRecordset());
1517
                                                }
1518
                                        }
1519
                                }
1520 3940 caballero
1521 8137 jcampos
                        } // for i
1522 10626 caballero
                } catch (ReadDriverException e) {
1523 4455 fjp
                        e.printStackTrace();
1524 3963 caballero
                }
1525 11704 jaume
1526 8137 jcampos
                // Recorremos los dataSources y los borramos si no
1527
                // los estamos usando.
1528
                SourceInfo[] infos = LayerFactory.getDataSourceFactory()
1529
                                .getDriverInfos();
1530
                try {
1531
                        for (i = 0; i < infos.length; i++) {
1532
                                if (!usedDataSources.containsKey(infos[i].name)) {
1533
                                        DataSource ds;
1534
                                        ds = LayerFactory.getDataSourceFactory()
1535
                                                        .createRandomDataSource(infos[i].name);
1536
                                        ds.remove();
1537
                                }
1538
                        }
1539
                } catch (DriverLoadException e) {
1540
                        e.printStackTrace();
1541
                } catch (NoSuchTableException e) {
1542
                        e.printStackTrace();
1543 10626 caballero
                } catch (ReadDriverException e) {
1544 8137 jcampos
                        e.printStackTrace();
1545 10626 caballero
                } catch (WriteDriverException e) {
1546
                        e.printStackTrace();
1547 8137 jcampos
                }
1548
        }
1549 6147 jaume
1550 8137 jcampos
        /**
1551 5029 cesar
         * DOCUMENT ME!
1552 9237 caballero
         *
1553 5029 cesar
         * @return DOCUMENT ME!
1554 8137 jcampos
         * @throws SaveException
1555 5029 cesar
         * @throws XMLException
1556 6147 jaume
         * @throws SaveException
1557 5029 cesar
         */
1558 8137 jcampos
        public XMLEntity getWindowInfoXMLEntity(IWindow window)
1559
                        throws SaveException {
1560
                if (window != null
1561
                                && PluginServices.getMDIManager().getWindowInfo(window) != null) {
1562
                        WindowInfo vi = PluginServices.getMDIManager()
1563
                                        .getWindowInfo(window);
1564 5029 cesar
                        XMLEntity xml = new XMLEntity();
1565 8137 jcampos
                        // xml.putProperty("nameClass", this.getClass().getName());
1566
                        try {
1567 5029 cesar
                                xml.setName("ViewInfoProperties");
1568
                                xml.putProperty("className", this.getClass().getName());
1569
                                xml.putProperty("X", vi.getX());
1570
                                xml.putProperty("Y", vi.getY());
1571
                                xml.putProperty("Width", vi.getWidth());
1572
                                xml.putProperty("Height", vi.getHeight());
1573
                                xml.putProperty("isVisible", vi.isVisible());
1574
                                xml.putProperty("isResizable", vi.isResizable());
1575
                                xml.putProperty("isMaximizable", vi.isMaximizable());
1576
                                xml.putProperty("isModal", vi.isModal());
1577
                                xml.putProperty("isModeless", vi.isModeless());
1578
                                xml.putProperty("isClosed", vi.isClosed());
1579 8137 jcampos
                                if (vi.isMaximized() == true) {
1580 5029 cesar
                                        xml.putProperty("isMaximized", vi.isMaximized());
1581
                                        xml.putProperty("normalX", vi.getNormalX());
1582
                                        xml.putProperty("normalY", vi.getNormalY());
1583
                                        xml.putProperty("normalWidth", vi.getNormalWidth());
1584
                                        xml.putProperty("normalHeight", vi.getNormalHeight());
1585 10679 jaume
}
1586 8137 jcampos
                        } catch (Exception e) {
1587
                                throw new SaveException(e, this.getClass().getName());
1588 5029 cesar
                        }
1589
                        return xml;
1590
                }
1591 7304 caballero
                return null;
1592 5029 cesar
        }
1593 6147 jaume
1594 8137 jcampos
        public static WindowInfo createWindowInfoFromXMLEntity(XMLEntity xml) {
1595 6880 cesar
                WindowInfo result = new WindowInfo();
1596 5029 cesar
                result.setX(xml.getIntProperty("X"));
1597
                result.setY(xml.getIntProperty("Y"));
1598
                result.setHeight(xml.getIntProperty("Height"));
1599
                result.setWidth(xml.getIntProperty("Width"));
1600
                result.setClosed(xml.getBooleanProperty("isClosed"));
1601
                if (xml.contains("isMaximized")) {
1602
                        boolean maximized = xml.getBooleanProperty("isMaximized");
1603
                        result.setMaximized(maximized);
1604 8137 jcampos
                        if (maximized == true) {
1605
                                result.setNormalBounds(xml.getIntProperty("normalX"), xml
1606
                                                .getIntProperty("normalY"), xml
1607
                                                .getIntProperty("normalWidth"), xml
1608
                                                .getIntProperty("normalHeight"));
1609
                        } else {
1610 5029 cesar
                                result.setNormalBounds(result.getBounds());
1611
                        }
1612
                }
1613
1614
                return result;
1615
        }
1616 6147 jaume
1617 6438 jmvivo
        public XMLEntity getSourceInfoXMLEntity(SourceInfo di) {
1618
                XMLEntity child = new XMLEntity();
1619
1620
                if (di instanceof ObjectSourceInfo) {
1621
                        ObjectSourceInfo driver = (ObjectSourceInfo) di;
1622
                        child.putProperty("type", "sameDriverFile");
1623
                        child.putProperty("gdbmsname", driver.name);
1624
                } else if (di instanceof FileSourceInfo) {
1625
                        FileSourceInfo vfdi = (FileSourceInfo) di;
1626
                        child.putProperty("type", "otherDriverFile");
1627
                        child.putProperty("gdbmsname", vfdi.name);
1628
                        child.putProperty("file", vfdi.file);
1629
                        child.putProperty("driverName", vfdi.driverName);
1630
                } else if (di instanceof DBSourceInfo) {
1631
                        DBTableSourceInfo dbdi = (DBTableSourceInfo) di;
1632
                        child.putProperty("type", "db");
1633
                        child.putProperty("gdbmsname", dbdi.name);
1634
                        child.putProperty("dbms", dbdi.dbms);
1635
                        child.putProperty("host", dbdi.host);
1636
                        child.putProperty("port", dbdi.port);
1637
                        child.putProperty("user", dbdi.user);
1638
                        child.putProperty("password", dbdi.password);
1639
                        child.putProperty("dbName", dbdi.dbName);
1640
                        child.putProperty("tableName", dbdi.tableName);
1641
                        child.putProperty("driverInfo", dbdi.driverName);
1642
                }
1643 6719 caballero
1644 6438 jmvivo
                return child;
1645
        }
1646 8137 jcampos
1647 7529 caballero
        /**
1648 8137 jcampos
         * Devuelve un arrayList con todos los documentos del tipo especificado como
1649
         * par?metro.
1650 9237 caballero
         *
1651 8137 jcampos
         * @param registerName
1652
         *            nombre de registro en el extensionPoint
1653 9237 caballero
         *
1654 7529 caballero
         * @return Documentos del tipo especificado
1655
         */
1656 7318 caballero
        public ArrayList getDocumentsByType(String registerName) {
1657 8137 jcampos
                ArrayList docuArray = new ArrayList();
1658
                for (int i = 0; i < documents.size(); i++) {
1659
                        ProjectDocument projectDocument = (ProjectDocument) documents
1660
                                        .get(i);
1661
                        ProjectDocumentFactory pdf = projectDocument
1662
                                        .getProjectDocumentFactory();
1663
                        if (pdf == null)
1664 7343 caballero
                                continue;
1665
                        if (pdf.getRegisterName().equals(registerName)) {
1666 7304 caballero
                                docuArray.add(projectDocument);
1667
                        }
1668
                }
1669
                return docuArray;
1670
        }
1671 8137 jcampos
1672 7543 caballero
        /**
1673
         * Devuelve un arrayList con todos los documentos.
1674 9237 caballero
         *
1675 7543 caballero
         * @return Documentos
1676
         */
1677 7318 caballero
        public ArrayList getDocuments() {
1678 8137 jcampos
                ArrayList docuArray = new ArrayList();
1679
                for (int i = 0; i < documents.size(); i++) {
1680
                        ProjectDocument projectDocument = (ProjectDocument) documents
1681
                                        .get(i);
1682 7318 caballero
                        docuArray.add(projectDocument);
1683
                }
1684
                return docuArray;
1685
        }
1686 8137 jcampos
1687 7543 caballero
        /**
1688
         * Inserta un documento.
1689 9237 caballero
         *
1690 8137 jcampos
         * @param doc
1691
         *            Documento
1692 7543 caballero
         */
1693 7304 caballero
        public void addDocument(ProjectDocument doc) {
1694
                documents.add(doc);
1695
                doc.addPropertyChangeListener(this);
1696 9532 caballero
                //modified = true;
1697 7753 jmvivo
                change.firePropertyChange("", "", null);
1698 7304 caballero
                doc.setProject(this, 0);
1699 7343 caballero
                doc.afterAdd();
1700 7222 jaume
1701 7304 caballero
        }
1702 8137 jcampos
1703 7543 caballero
        /**
1704
         * Borra un documento.
1705 9237 caballero
         *
1706 8137 jcampos
         * @param doc
1707
         *            Documento
1708 7543 caballero
         */
1709 7304 caballero
        public void delDocument(ProjectDocument doc) {
1710
                documents.remove(doc);
1711 9532 caballero
                //modified = true;
1712 7304 caballero
                change.firePropertyChange("", null, null);
1713 7343 caballero
                doc.afterRemove();
1714 7304 caballero
        }
1715
1716 7654 jaume
        /**
1717
         * Sets the default selection color that will be used in subsequent
1718
         * projects.
1719 9237 caballero
         *
1720 7654 jaume
         * @param color
1721
         */
1722
        public static void setDefaultSelectionColor(Color color) {
1723
                defaultSelectionColor = color;
1724
        }
1725
1726
        /**
1727 8137 jcampos
         * Returns the current default selection color defined which is the color
1728
         * defined when the user does not define any other one
1729 9237 caballero
         *
1730 7654 jaume
         * @return java.awt.Color
1731
         */
1732 7766 jaume
        public static Color getDefaultSelectionColor() {
1733 7658 jaume
                // TODO es millorable?
1734 8137 jcampos
                XMLEntity xml = PluginServices.getPluginServices("com.iver.cit.gvsig")
1735
                                .getPersistentXML();
1736 7658 jaume
                if (xml.contains("DefaultSelectionColor"))
1737 8137 jcampos
                        defaultSelectionColor = StringUtilities.string2Color(xml
1738
                                        .getStringProperty("DefaultSelectionColor"));
1739 7654 jaume
                return defaultSelectionColor;
1740
        }
1741
1742 7797 jaume
        /**
1743 8137 jcampos
         * Returns the user's default map units. This is the cartography data
1744
         * distance units.
1745 9237 caballero
         *
1746 7797 jaume
         * @return int (index of the <b>Attributes.NAMES array</b>)
1747
         */
1748 7787 jaume
        public static int getDefaultMapUnits() {
1749
                if (defaultMapUnits == -1) {
1750 8137 jcampos
                        XMLEntity xml = PluginServices.getPluginServices(
1751
                                        "com.iver.cit.gvsig").getPersistentXML();
1752 7794 jaume
                        if (xml.contains("DefaultMapUnits")) {
1753
                                defaultMapUnits = xml.getIntProperty("DefaultMapUnits");
1754
                        } else {
1755
                                // first app run case
1756 11503 jaume
                                String[] unitNames = MapContext.NAMES;
1757 7794 jaume
                                for (int i = 0; i < unitNames.length; i++) {
1758
                                        // meter is the factory default's map unit
1759 8137 jcampos
                                        if (unitNames[i].equals(PluginServices.getText(
1760
                                                        Project.class, "Metros"))) {
1761 7794 jaume
                                                defaultMapUnits = i;
1762
                                                break;
1763
                                        }
1764 7787 jaume
                                }
1765
                        }
1766 8381 jmvivo
                        if (defaultMapUnits == -1)
1767
                                defaultMapUnits = ViewPort.METROS;
1768 7787 jaume
                }
1769
                return defaultMapUnits;
1770
        }
1771
1772 7797 jaume
        /**
1773 8137 jcampos
         * Returns the user's default view units for measuring distances. This is
1774
         * the units that the user will see in the status bar of the view.
1775 9237 caballero
         *
1776 7797 jaume
         * @return int (index of the <b>Attributes.NAMES array</b>)
1777
         */
1778 7787 jaume
        public static int getDefaultDistanceUnits() {
1779
                if (defaultDistanceUnits == -1) {
1780 8137 jcampos
                        XMLEntity xml = PluginServices.getPluginServices(
1781
                                        "com.iver.cit.gvsig").getPersistentXML();
1782 7794 jaume
                        if (xml.contains("DefaultDistanceUnits")) {
1783 8137 jcampos
                                defaultDistanceUnits = xml
1784
                                                .getIntProperty("DefaultDistanceUnits");
1785 7794 jaume
                        } else {
1786
                                // first app run case
1787 11503 jaume
                                String[] unitNames = MapContext.NAMES;
1788 7794 jaume
                                for (int i = 0; i < unitNames.length; i++) {
1789
                                        // meter is the factory default's distance unit
1790 8379 cesar
                                        if (unitNames[i].equals("Metros")) {
1791 7794 jaume
                                                defaultDistanceUnits = i;
1792
                                                break;
1793
                                        }
1794 7787 jaume
                                }
1795
                        }
1796 8381 jmvivo
                        if (defaultDistanceUnits == -1)
1797
                                defaultDistanceUnits = ViewPort.METROS;
1798 7787 jaume
                }
1799
                return defaultDistanceUnits;
1800
        }
1801
1802 7798 jaume
        /**
1803
         * Sets the default map unit (the units used by the data).
1804 9237 caballero
         *
1805 7798 jaume
         * @param mapUnits
1806
         */
1807 7787 jaume
        public static void setDefaultMapUnits(int mapUnits) {
1808
                defaultMapUnits = mapUnits;
1809
        }
1810
1811 7798 jaume
        /**
1812
         * Sets the default distance units (the units shown in the status bar)
1813 9237 caballero
         *
1814 7798 jaume
         * @param distanceUnits
1815
         */
1816 7787 jaume
        public static void setDefaultDistanceUnits(int distanceUnits) {
1817
                defaultDistanceUnits = distanceUnits;
1818
        }
1819
1820 7753 jmvivo
        public String exportToXML() throws SaveException {
1821
                XMLEntity xml = this.newExportXMLRootNode();
1822 7766 jaume
1823 7753 jmvivo
                Iterator iter = this.documents.iterator();
1824
                ProjectDocument document;
1825
                // FIXME: Falta atrapar los errores
1826
                while (iter.hasNext()) {
1827 8137 jcampos
                        document = (ProjectDocument) iter.next();
1828
                        document.exportToXML(xml, this);
1829 7753 jmvivo
                }
1830 7766 jaume
1831 7753 jmvivo
                return xml.toString();
1832
        }
1833 7766 jaume
1834 7753 jmvivo
        public String exportToXML(ProjectDocument[] documents) throws SaveException {
1835
                XMLEntity xml = this.newExportXMLRootNode();
1836 7766 jaume
1837 8137 jcampos
                for (int i = 0; i < documents.length; i++) {
1838
                        documents[i].exportToXML(xml, this);
1839 7753 jmvivo
                }
1840 7766 jaume
1841 7753 jmvivo
                return xml.toString();
1842
        }
1843
1844
        public void importFromXML(String xml) throws Exception {
1845
                throw new Exception("Not Implemented");
1846
                /*
1847 8137 jcampos
                 * // FIXME: ?? Exceptions XMLEntity xmlEntity = new XMLEntity();
1848 9237 caballero
                 *
1849 8137 jcampos
                 * try { xmlEntity.parse(xml); } catch (Exception e) { throw new
1850
                 * Exception(e); }
1851 9237 caballero
                 *
1852 8137 jcampos
                 * if (!checkExportXMLRootNode(xmlEntity)) { throw new Exception("Check
1853
                 * Error"); //FIXME: traducir }
1854 9237 caballero
                 *
1855 8137 jcampos
                 * int i;
1856 9237 caballero
                 *
1857 8137 jcampos
                 * XMLEntity xmlDocumentRoot; ProjectDocument document = null;
1858
                 * ProjectDocumentFactory documentFactory = null; for (i=0;i<xmlEntity.getChildrenCount();i++) {
1859
                 * xmlDocumentRoot = xmlEntity.getChild(i); if
1860
                 * (!xmlDocumentRoot.contains("type")) { throw new Exception("Document
1861
                 * root "+i+ "error"); } documentFactory =
1862
                 * Project.getProjectDocumentFactory(xmlDocumentRoot.getStringProperty("type"));
1863
                 * int j; }
1864
                 */
1865 7766 jaume
1866 7753 jmvivo
        }
1867 7766 jaume
1868 8137 jcampos
        public void importFromXML(String xml, String type) throws Exception {
1869
                // FIXME: EXCEPTIONS!!!!
1870 7753 jmvivo
                XMLEntity xmlEntity = XMLEntity.parse(xml);
1871 7766 jaume
1872 7753 jmvivo
                if (!checkExportXMLRootNode(xmlEntity)) {
1873 7766 jaume
                        throw new Exception();
1874 7753 jmvivo
                }
1875
1876 8137 jcampos
                XMLEntity typeRoot = xmlEntity.firstChild("type", type);
1877 7753 jmvivo
                if (typeRoot.getChildrenCount() == 0) {
1878
                        return;
1879
                }
1880 7766 jaume
1881 7753 jmvivo
                ProjectDocumentFactory documentFactory = getProjectDocumentFactory(type);
1882 7766 jaume
1883 7753 jmvivo
                Hashtable conflicts = getConflicts(xmlEntity);
1884 7766 jaume
1885 7753 jmvivo
                if (conflicts.size() != 0) {
1886 8137 jcampos
                        if (!documentFactory.resolveImportXMLConflicts(xmlEntity, this,
1887
                                        conflicts)) {
1888 7753 jmvivo
                                return;
1889
                        }
1890
                }
1891 8137 jcampos
                this.registerDataSources(this.getExportXMLTypeRootNode(xmlEntity,
1892
                                "dataSources"));
1893 7766 jaume
1894 7753 jmvivo
                int i;
1895 7766 jaume
                ProjectDocument document;
1896 8137 jcampos
                for (i = 0; i < typeRoot.getChildrenCount(); i++) {
1897 7766 jaume
                        document = documentFactory.create(this);
1898 8137 jcampos
                        document.importFromXML(xmlEntity, typeRoot, i, this);
1899 7753 jmvivo
                }
1900
1901
        }
1902 7766 jaume
1903 7753 jmvivo
        private Hashtable getConflicts(XMLEntity xml) {
1904
                int iType;
1905
                Hashtable conflicts = new Hashtable();
1906
                ArrayList elements;
1907
                XMLEntity typeRoot;
1908 8137 jcampos
                for (iType = 0; iType < xml.getChildrenCount(); iType++) {
1909 7753 jmvivo
                        typeRoot = xml.getChild(iType);
1910
                        elements = getDocumentsByType(typeRoot.getStringProperty("type"));
1911 7766 jaume
                        Hashtable conflictsType = new Hashtable();
1912 8137 jcampos
                        for (int iXML = 0; iXML < typeRoot.getChildrenCount(); iXML++) {
1913 7753 jmvivo
                                XMLEntity child = typeRoot.getChild(iXML);
1914
                                Iterator iter = elements.iterator();
1915
                                while (iter.hasNext()) {
1916 8137 jcampos
                                        ProjectDocument element = (ProjectDocument) iter.next();
1917
                                        if (element.getName().equalsIgnoreCase(
1918
                                                        child.getStringProperty("name"))) {
1919
                                                conflictsType.put(new Integer(iXML), child);
1920 7753 jmvivo
                                                break;
1921
                                        }
1922 7766 jaume
1923 7753 jmvivo
                                }
1924
                        }
1925
                        if (conflictsType.size() > 0) {
1926 8137 jcampos
                                conflicts
1927
                                                .put(typeRoot.getStringProperty("type"), conflictsType);
1928 7753 jmvivo
                        }
1929
                }
1930
                return conflicts;
1931
        }
1932
1933
        public boolean isValidXMLForImport(String xml) {
1934
                XMLEntity xmlEntity;
1935
                try {
1936
                        xmlEntity = XMLEntity.parse(xml);
1937
                } catch (Exception e) {
1938
                        return false;
1939
                }
1940 7766 jaume
1941 7753 jmvivo
                return checkExportXMLRootNode(xmlEntity);
1942
        }
1943
1944 8137 jcampos
        public boolean isValidXMLForImport(String xml, String type) {
1945 7753 jmvivo
                XMLEntity xmlEntity;
1946
                try {
1947 8137 jcampos
                        xmlEntity = XMLEntity.parse(xml);
1948 7753 jmvivo
                } catch (Exception e) {
1949
                        return false;
1950
                }
1951 7766 jaume
1952 7753 jmvivo
                if (!checkExportXMLRootNode(xmlEntity)) {
1953 7766 jaume
                        return false;
1954 7753 jmvivo
                }
1955
1956 8137 jcampos
                XMLEntity typeRoot = xmlEntity.firstChild("type", type);
1957 7766 jaume
1958 7753 jmvivo
                if (typeRoot == null) {
1959
                        return false;
1960
                }
1961 7766 jaume
1962 7753 jmvivo
                return (typeRoot.getChildrenCount() > 0);
1963
        }
1964 7766 jaume
1965 7753 jmvivo
        private boolean checkExportXMLRootNode(XMLEntity xml) {
1966 8137 jcampos
                if (!xml.contains("applicationName"))
1967
                        return false;
1968
                if (!xml.getStringProperty("applicationName").equalsIgnoreCase("gvSIG"))
1969
                        return false;
1970 7753 jmvivo
1971 8137 jcampos
                if (!xml.contains("version"))
1972
                        return false;
1973
                if (!xml.getStringProperty("version")
1974
                                .equalsIgnoreCase(Version.format()))
1975
                        return false;
1976 7753 jmvivo
1977
                return true;
1978
        }
1979
1980
        public void exportToXMLDataSource(XMLEntity root, String dataSourceName) {
1981 8137 jcampos
                XMLEntity dsRoot = this.getExportXMLTypeRootNode(root, "dataSources");
1982
                SourceInfo sourceInfo = LayerFactory.getDataSourceFactory()
1983
                                .getDriverInfo(dataSourceName);
1984 7753 jmvivo
                dsRoot.addChild(this.getSourceInfoXMLEntity(sourceInfo));
1985
        }
1986 7766 jaume
1987 7753 jmvivo
        private XMLEntity newExportXMLRootNode() {
1988
                XMLEntity xml = new XMLEntity();
1989 8137 jcampos
                xml.putProperty("applicationName", "gvSIG");
1990
                xml.putProperty("version", Version.format());
1991 7753 jmvivo
                return xml;
1992
        }
1993 7766 jaume
1994 8137 jcampos
        public XMLEntity getExportXMLTypeRootNode(XMLEntity root, String type) {
1995
                XMLEntity typeRoot = root.firstChild("type", type);
1996 7753 jmvivo
                if (typeRoot == null) {
1997
                        typeRoot = this.newExportXMLTypeNode(type);
1998
                        root.addChild(typeRoot);
1999
                }
2000
                return typeRoot;
2001
        }
2002 7766 jaume
2003 7753 jmvivo
        private XMLEntity newExportXMLTypeNode(String type) {
2004
                XMLEntity xmlDataSources = new XMLEntity();
2005 8137 jcampos
                xmlDataSources.putProperty("type", type);
2006 7753 jmvivo
                return xmlDataSources;
2007
        }
2008
2009
        private static boolean registerDataSourceFromXML(XMLEntity xmlDataSource) {
2010
                String name = xmlDataSource.getStringProperty("gdbmsname");
2011
2012
                if (LayerFactory.getDataSourceFactory().getDriverInfo(name) == null) {
2013 8137 jcampos
                        if (xmlDataSource.getStringProperty("type").equals(
2014
                                        "otherDriverFile")) {
2015 7753 jmvivo
                                LayerFactory.getDataSourceFactory().addFileDataSource(
2016 8137 jcampos
                                                xmlDataSource.getStringProperty("driverName"), name,
2017
                                                xmlDataSource.getStringProperty("file"));
2018 7753 jmvivo
2019 8137 jcampos
                        } else if (xmlDataSource.getStringProperty("type").equals(
2020
                                        "sameDriverFile")) {
2021 7753 jmvivo
2022
                        } else if (xmlDataSource.getStringProperty("type").equals("db")) {
2023
                                LayerFactory.getDataSourceFactory().addDBDataSourceByTable(
2024 8137 jcampos
                                                name, xmlDataSource.getStringProperty("host"),
2025 7753 jmvivo
                                                xmlDataSource.getIntProperty("port"),
2026
                                                xmlDataSource.getStringProperty("user"),
2027
                                                xmlDataSource.getStringProperty("password"),
2028
                                                xmlDataSource.getStringProperty("dbName"),
2029
                                                xmlDataSource.getStringProperty("tableName"),
2030 8137 jcampos
                                                xmlDataSource.getStringProperty("driverInfo"));
2031
                        } else {
2032 7753 jmvivo
                                return false;
2033
                        }
2034 7766 jaume
2035 7753 jmvivo
                }
2036
                return true;
2037
        }
2038 7766 jaume
2039 7753 jmvivo
        private boolean registerDataSources(XMLEntity xmlDataSources) {
2040
                try {
2041
                        int numDataSources = xmlDataSources.getChildrenCount();
2042
2043 8137 jcampos
                        if (numDataSources == 0)
2044
                                return true;
2045 7753 jmvivo
                        DataSourceFactory dsFactory = LayerFactory.getDataSourceFactory();
2046
2047
                        for (int i = 0; i < numDataSources; i++) {
2048
                                XMLEntity child = xmlDataSources.getChild(i);
2049
                                if (!this.registerDataSourceFromXML(child)) {
2050
                                        return false;
2051
                                }
2052
                        }
2053
2054
                        return true;
2055
                } catch (Exception e) {
2056
                        e.printStackTrace();
2057
                        return false;
2058
                }
2059
        }
2060
2061
        public static ProjectDocumentFactory getProjectDocumentFactory(String type) {
2062
                ProjectDocumentFactory pde = null;
2063 8137 jcampos
                try {
2064
                        ExtensionPoints extensionPoints = ExtensionPointsSingleton
2065
                                        .getInstance();
2066
                        ExtensionPoint extPoint = ((ExtensionPoint) extensionPoints
2067
                                        .get("Documents"));
2068 7753 jmvivo
                        try {
2069
                                pde = (ProjectDocumentFactory) extPoint.create(type);
2070
                        } catch (InstantiationException e) {
2071 8137 jcampos
                                NotificationManager.addError(
2072
                                                "Clase de ProjectDocument no reconocida", e);
2073 7753 jmvivo
                        } catch (IllegalAccessException e) {
2074 8137 jcampos
                                NotificationManager.addError(
2075
                                                "Clase de ProjectDocument no reconocida", e);
2076 7753 jmvivo
                        }
2077 7766 jaume
2078 7753 jmvivo
                } catch (Exception e1) {
2079
                        return null;
2080
                }
2081 7766 jaume
                return pde;
2082 7753 jmvivo
        }
2083
2084 10679 jaume
         public boolean hasChanged() {
2085 12698 cesar
                 // we return true if the project is not empty (until we have a better method...)
2086
                 if ((this.getDocuments().size() != 0) || modified) {
2087
                         return true;
2088
                 }
2089
                 return false;
2090 11704 jaume
//                 return signatureAtStartup != getXMLEntity().hash();
2091 10679 jaume
         }
2092
2093
        public void setSignature(long hash) {
2094
                signatureAtStartup = hash;
2095
        }
2096 1222 fernando
}