Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / Project.java @ 24759

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