Statistics
| Revision:

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

History | View | Annotate | Download (60.5 KB)

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