Statistics
| Revision:

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

History | View | Annotate | Download (13.2 KB)

1 412 vcaballero
package com.iver.cit.gvsig.project.castor;
2
3 732 fernando
import com.hardcode.driverManager.DriverLoadException;
4 412 vcaballero
5 732 fernando
import com.iver.andami.PluginServices;
6
7 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
8 661 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
9 552 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
10
import com.iver.cit.gvsig.fmap.layers.CancelationException;
11
import com.iver.cit.gvsig.fmap.layers.DifferentVersionException;
12 732 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
13
import com.iver.cit.gvsig.fmap.layers.FLayers;
14
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
15 552 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
16 564 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
17 732 fernando
18 552 fernando
import com.iver.utiles.StringUtilities;
19
import com.iver.utiles.XMLEntity;
20 412 vcaballero
21 732 fernando
import java.awt.Color;
22
import java.awt.geom.Rectangle2D;
23 552 fernando
24 732 fernando
import java.beans.PropertyChangeEvent;
25
import java.beans.PropertyChangeListener;
26
import java.beans.PropertyChangeSupport;
27
28
import java.io.File;
29
import java.io.Serializable;
30
31
import java.text.DateFormat;
32
33
import java.util.ArrayList;
34
import java.util.Date;
35
import java.util.Iterator;
36
37 904 fjp
import org.cresques.cts.IProjection;
38
import org.cresques.cts.ProjectionPool;
39 732 fernando
40 904 fjp
41 412 vcaballero
/**
42
 * Clase que representa un proyecto de openSIG
43
 *
44
 * @author Fernando Gonz?lez Cort?s
45
 */
46
public class Project implements Serializable, PropertyChangeListener {
47 732 fernando
        private PropertyChangeSupport change;
48
        boolean modified = false;
49
        private String name;
50
        private String path;
51
        private String creationDate;
52
        private String modificationDate;
53
        private String owner;
54
        private String comments;
55
        private Color selectionColor = new Color(255, 255, 0);
56
        private ArrayList views = new ArrayList();
57
        private ArrayList tables = new ArrayList();
58
        private ArrayList maps = new ArrayList();
59
        private ArrayList extents = new ArrayList();
60 904 fjp
        static private IProjection defaultProjection = ProjectionPool.get("23030");
61 412 vcaballero
62 732 fernando
        /**
63
         * Creates a new Project object.
64
         */
65
        public Project() {
66
                change = new PropertyChangeSupport(this);
67 412 vcaballero
68 732 fernando
                //        change.addPropertyChangeListener(this);
69
                creationDate = DateFormat.getDateInstance().format(new Date());
70
                modificationDate = creationDate;
71
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
72
                                                                                                  .getPluginDirectory()
73
                                                                                                  .getAbsolutePath() +
74
                        File.separator + "drivers");
75
        }
76 412 vcaballero
77 732 fernando
        /**
78
         * Obtiene la fecha de creaci?n del proyecto
79
         *
80
         * @return
81
         */
82
        public String getCreationDate() {
83
                return creationDate;
84
        }
85 412 vcaballero
86 732 fernando
        /**
87
         * Obtiene el nombre del proyecto
88
         *
89
         * @return
90
         */
91
        public String getName() {
92
                return name;
93
        }
94 412 vcaballero
95 732 fernando
        /**
96
         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
97
         * proyecto
98
         *
99
         * @return
100
         */
101
        public String getPath() {
102
                return path;
103
        }
104 412 vcaballero
105 732 fernando
        /**
106
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
107
         * por que al recuperar la fecha del XML hay que asignarla al objeto
108
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
109
         * deber?a de modificar nunca
110
         *
111
         * @param string
112
         */
113
        public void setCreationDate(String string) {
114
                creationDate = string;
115
                modified = true;
116
                change.firePropertyChange("", null, null);
117
        }
118 412 vcaballero
119 732 fernando
        /**
120
         * A?ade un mapa al proyecto
121
         *
122
         * @param m
123
         */
124
        public void addMap(ProjectMap m) {
125
                maps.add(m);
126
                m.addPropertyChangeListener(this);
127
                modified = true;
128
                change.firePropertyChange("", null, null);
129
                m.setProject(this);
130
        }
131 412 vcaballero
132 732 fernando
        /**
133
         * Elimina un mapa del proyecto
134
         *
135
         * @param i indice del mapa
136
         */
137
        public void delMap(int i) {
138
                maps.remove(i);
139
                modified = true;
140
                change.firePropertyChange("", null, null);
141
        }
142 412 vcaballero
143 732 fernando
        /**
144
         * Establece el nombre del proyecto
145
         *
146
         * @param string
147
         */
148
        public void setName(String string) {
149
                name = string;
150
                modified = true;
151
                change.firePropertyChange("", null, null);
152
        }
153 412 vcaballero
154 732 fernando
        /**
155
         * establece la ruta completa de donde se encuentra guardado el proyecto
156
         *
157
         * @param string
158
         */
159
        public void setPath(String string) {
160
                path = string;
161
                modified = true;
162
                change.firePropertyChange("", null, null);
163
        }
164 412 vcaballero
165 732 fernando
        /**
166
         * DOCUMENT ME!
167
         *
168
         * @param co DOCUMENT ME!
169
         *
170
         * @return DOCUMENT ME!
171
         */
172
        public ProjectTable getTable(AlphanumericData co) {
173
                /**
174
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
175
                 * una capa, se puede intentar a?adir dos veces la misma tabla
176
                 */
177
                for (int i = 0; i < tables.size(); i++) {
178
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
179
                                return (ProjectTable) tables.get(i);
180
                        }
181
                }
182 412 vcaballero
183 732 fernando
                return null;
184
        }
185 412 vcaballero
186 732 fernando
        /**
187
         * A?ade una tabla al proyecto
188
         *
189
         * @param t
190
         */
191
        public void addTable(ProjectTable t) {
192
                tables.add(t);
193
                t.addPropertyChangeListener(this);
194
                modified = true;
195
                change.firePropertyChange("", null, null);
196
                t.setProject(this);
197
        }
198 412 vcaballero
199 732 fernando
        /**
200
         * Elimina una tabla del proyecto
201
         *
202
         * @param i indice de la tabla
203
         */
204
        public void delTable(int i) {
205
                tables.remove(i);
206
                modified = true;
207
                change.firePropertyChange("", null, null);
208
        }
209 412 vcaballero
210 732 fernando
        /**
211
         * A?ade una vista al proyecto
212
         *
213
         * @param v
214
         */
215
        public void addView(ProjectView v) {
216
                views.add(v);
217
                v.addPropertyChangeListener(this);
218
                modified = true;
219
                change.firePropertyChange("", null, null);
220
                v.setProject(this);
221
        }
222 412 vcaballero
223 732 fernando
        /**
224
         * Elimina una tabla del proyecto
225
         *
226
         * @param i indice del proyecto
227
         */
228
        public void delView(int i) {
229
                views.remove(i);
230
                modified = true;
231
                change.firePropertyChange("", null, null);
232
        }
233 412 vcaballero
234 732 fernando
        /**
235
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
236
         * fue modificado
237
         *
238
         * @return
239
         */
240
        public boolean isModified() {
241
                return modified;
242
        }
243 412 vcaballero
244 732 fernando
        /**
245
         * Obtiene los comentarios
246
         *
247
         * @return
248
         */
249
        public String getComments() {
250
                return comments;
251
        }
252 412 vcaballero
253 732 fernando
        /**
254
         * Obtiene la fecha de la ?ltima modificaci?n
255
         *
256
         * @return
257
         */
258
        public String getModificationDate() {
259
                return modificationDate;
260
        }
261 412 vcaballero
262 732 fernando
        /**
263
         * Obtiene el propietario del proyecto
264
         *
265
         * @return
266
         */
267
        public String getOwner() {
268
                return owner;
269
        }
270 412 vcaballero
271 732 fernando
        /**
272
         * Establece una cadena como comentarios al proyecto
273
         *
274
         * @param string
275
         */
276
        public void setComments(String string) {
277
                comments = string;
278
                modified = true;
279
                change.firePropertyChange("", null, null);
280
        }
281 412 vcaballero
282 732 fernando
        /**
283
         * Establece la fecha de la ?ltima modificaci?n
284
         *
285
         * @param string
286
         */
287
        public void setModificationDate(String string) {
288
                modificationDate = string;
289
                modified = true;
290
                change.firePropertyChange("", null, null);
291
        }
292 412 vcaballero
293 732 fernando
        /**
294
         * Establece el propietario del proyecto
295
         *
296
         * @param string
297
         */
298
        public void setOwner(String string) {
299
                owner = string;
300
                modified = true;
301
                change.firePropertyChange("", null, null);
302
        }
303 412 vcaballero
304 732 fernando
        /**
305
         * Establece el flag de modificado del proyecto
306
         *
307
         * @param b
308
         */
309
        public void setModified(boolean b) {
310
                modified = b;
311
        }
312 412 vcaballero
313 732 fernando
        /**
314
         * Obtiene el color de selecci?n que se usar? en el proyecto
315
         *
316
         * @return
317
         */
318
        public Color getSelectionColor() {
319
                return selectionColor;
320
        }
321 412 vcaballero
322 732 fernando
        /**
323
         * Establece el color de selecci?n
324
         *
325
         * @param color
326
         */
327
        public void setSelectionColor(Color color) {
328
                selectionColor = color;
329
                FSymbol.setSelectionColor(color);
330
                modified = true;
331
                change.firePropertyChange("selectionColor", null, color);
332
        }
333 412 vcaballero
334 732 fernando
        /**
335
         * Obtiene el color como un entero para su serializaci?n a XML
336
         *
337
         * @return
338
         */
339
        public String getColor() {
340
                return StringUtilities.color2String(selectionColor);
341
        }
342 412 vcaballero
343 732 fernando
        /**
344
         * M?todo invocado al recuperar de XML para establecer el color de
345
         * seleccion del proyecto
346
         *
347
         * @param color Entero que representa un color
348
         */
349
        public void setColor(String color) {
350
                modified = true;
351
                selectionColor = StringUtilities.string2Color(color);
352
        }
353 412 vcaballero
354 732 fernando
        /* (non-Javadoc)
355
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
356
         */
357
        public void propertyChange(PropertyChangeEvent evt) {
358
                this.modified = true;
359
                change.firePropertyChange(evt);
360
        }
361 412 vcaballero
362 732 fernando
        /**
363
         * DOCUMENT ME!
364
         *
365
         * @param arg1
366
         */
367
        public void addExtent(ProjectExtent arg1) {
368
                extents.add(arg1);
369
                modified = true;
370
                change.firePropertyChange("addExtent", null, null);
371
        }
372 412 vcaballero
373 732 fernando
        /**
374
         * DOCUMENT ME!
375
         *
376
         * @param arg0
377
         *
378
         * @return
379
         */
380
        public Object removeExtent(int arg0) {
381
                modified = true;
382
                change.firePropertyChange("delExtent", null, null);
383 412 vcaballero
384 732 fernando
                return extents.remove(arg0);
385
        }
386 412 vcaballero
387 732 fernando
        /**
388
         * DOCUMENT ME!
389
         *
390
         * @return DOCUMENT ME!
391
         */
392
        public ProjectExtent[] getExtents() {
393
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
394
        }
395 412 vcaballero
396 732 fernando
        /**
397
         * DOCUMENT ME!
398
         *
399
         * @param arg0
400
         */
401
        public synchronized void addPropertyChangeListener(
402
                PropertyChangeListener arg0) {
403
                change.addPropertyChangeListener(arg0);
404
        }
405 412 vcaballero
406 732 fernando
        /**
407
         * DOCUMENT ME!
408
         *
409
         * @return
410
         */
411
        public ArrayList getMaps() {
412
                return maps;
413
        }
414 412 vcaballero
415 732 fernando
        /**
416
         * DOCUMENT ME!
417
         *
418
         * @return
419
         */
420
        public ArrayList getTables() {
421
                return tables;
422
        }
423 412 vcaballero
424 732 fernando
        /**
425
         * DOCUMENT ME!
426
         *
427
         * @return
428
         */
429
        public ArrayList getViews() {
430
                return views;
431
        }
432 412 vcaballero
433 732 fernando
        /**
434
         * DOCUMENT ME!
435
         *
436
         * @return DOCUMENT ME!
437
         *
438
         * @throws DriverException
439
         */
440
        public XMLEntity getXMLEntity() throws DriverException {
441
                XMLEntity xml = new XMLEntity();
442
                xml.putProperty("comments", comments);
443
                xml.putProperty("creationDate", creationDate);
444 412 vcaballero
445 732 fernando
                int size = extents.size();
446
                double[] xs = new double[size];
447
                double[] ys = new double[size];
448
                double[] ws = new double[size];
449
                double[] hs = new double[size];
450 412 vcaballero
451 732 fernando
                for (int i = 0; i < size; i++) {
452
                        Rectangle2D rect = (Rectangle2D) extents.get(i);
453
                        xs[i] = rect.getX();
454
                        ys[i] = rect.getY();
455
                        ws[i] = rect.getWidth();
456
                        hs[i] = rect.getHeight();
457
                }
458 442 vcaballero
459 732 fernando
                xml.putProperty("extentsX", xs);
460
                xml.putProperty("extentsY", ys);
461
                xml.putProperty("extentsW", ws);
462
                xml.putProperty("extentsH", hs);
463 442 vcaballero
464 732 fernando
                xml.putProperty("numViews", views.size());
465 442 vcaballero
466 732 fernando
                for (int i = 0; i < views.size(); i++) {
467
                        xml.addChild(((ProjectView) views.get(i)).getXMLEntity());
468
                }
469 442 vcaballero
470 732 fernando
                xml.putProperty("numMaps", maps.size());
471 442 vcaballero
472 732 fernando
                for (int i = 0; i < maps.size(); i++) {
473
                        xml.addChild(((ProjectMap) maps.get(i)).getXMLEntity());
474
                }
475 412 vcaballero
476 732 fernando
                xml.putProperty("modificationDate", modificationDate);
477
                xml.putProperty("modified", modified);
478
                xml.putProperty("name", name);
479
                xml.putProperty("owner", owner);
480
                xml.putProperty("path", path);
481
                xml.putProperty("selectionColor",
482
                        StringUtilities.color2String(selectionColor));
483
                xml.putProperty("numTables", tables.size());
484 412 vcaballero
485 732 fernando
                for (int i = 0; i < tables.size(); i++) {
486
                        xml.addChild(((ProjectTable) tables.get(i)).getXMLEntity());
487
                }
488 904 fjp
                xml.putProperty("projection", defaultProjection.getAbrev());
489 412 vcaballero
490 732 fernando
                return xml;
491
        }
492 412 vcaballero
493 732 fernando
        /**
494
         * DOCUMENT ME!
495
         *
496
         * @param xml DOCUMENT ME!
497
         *
498
         * @return DOCUMENT ME!
499
         *
500
         * @throws XMLException
501
         * @throws DriverException
502
         */
503
        public static Project createFromXML(XMLEntity xml)
504
                throws XMLException, DriverException {
505
                Project p = new Project();
506
                p.comments = xml.getStringProperty("comments");
507
                p.creationDate = xml.getStringProperty("creationDate");
508 442 vcaballero
509 732 fernando
                double[] xs = xml.getDoubleArrayProperty("extentsX");
510
                double[] ys = xml.getDoubleArrayProperty("extentsY");
511
                double[] ws = xml.getDoubleArrayProperty("extentsW");
512
                double[] hs = xml.getDoubleArrayProperty("extentsH");
513 442 vcaballero
514 732 fernando
                for (int i = 0; i < xs.length; i++) {
515
                        Rectangle2D rect = new Rectangle2D.Double(xs[i], ys[i], ws[i], hs[i]);
516
                        p.extents.add(rect);
517
                }
518 412 vcaballero
519 732 fernando
                int numViews = xml.getIntProperty("numViews");
520 442 vcaballero
521 732 fernando
                for (int i = 0; i < numViews; i++) {
522
                        p.views.add(ProjectView.createFromXML(xml.getChild(i), p));
523
                }
524 442 vcaballero
525 732 fernando
                int numMaps = xml.getIntProperty("numMaps");
526 442 vcaballero
527 732 fernando
                for (int i = numViews; i < (numMaps + numViews); i++) {
528
                        p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
529
                }
530
531
                p.modificationDate = xml.getStringProperty("modificationDate");
532
                p.modified = xml.getBooleanProperty("modified");
533
                p.name = xml.getStringProperty("name");
534
                p.owner = xml.getStringProperty("owner");
535
                p.path = xml.getStringProperty("path");
536
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
537
                                        "selectionColor"));
538
539
                int numTables = xml.getIntProperty("numTables");
540
541
                for (int i = numMaps + numViews; i < (numTables + numMaps + numViews);
542
                                i++) {
543
                        p.tables.add(ProjectTable.createFromXML(xml.getChild(i), p));
544
                }
545 904 fjp
                String strProj = xml.getStringProperty("projection");
546
                if (strProj != null)
547
                        Project.setProjection(ProjectionPool.get(strProj));
548 732 fernando
549
                return p;
550
        }
551
552
        /**
553
         * Obtiene la vista que contiene a la capa que se pasa como
554
         * par?metro
555
         *
556
         * @param layer Capa cuya vista se quiere obtener
557
         *
558
         * @return
559
         *
560
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
561
         * encuentra en ninguna vista
562
         */
563
        public String getView(FLayer layer) {
564
                for (int v = 0; v < views.size(); v++) {
565
                        ProjectView pView = (ProjectView) views.get(v);
566
                        FLayers layers = pView.getMapContext().getLayers();
567
568
                        for (int i = 0; i < layers.getLayersCount(); i++) {
569
                                if (layers.getLayer(i) == layer) {
570
                                        return pView.getName();
571
                                }
572
                        }
573
                }
574
575
                throw new RuntimeException("The layer is not in a view");
576
        }
577
578
        /**
579
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas)
580
         * con el que se pasa como par?metro. Devuelve null si no hay ninguna
581
         * vista con ese nombre
582
         *
583
         * @param viewName Nombre de la vista que se quiere obtener
584
         *
585
         * @return DOCUMENT ME!
586
         */
587
        public ProjectView getViewByName(String viewName) {
588
                for (int v = 0; v < views.size(); v++) {
589
                        ProjectView pView = (ProjectView) views.get(v);
590
591
                        if (pView.getName().equals(viewName)) {
592
                                return pView;
593
                        }
594
                }
595
596
                return null;
597
        }
598 904 fjp
        public static IProjection getProjection() {
599
                return defaultProjection;
600
        }
601
        public static void setProjection(IProjection defaultProjection) {
602
                Project.defaultProjection = defaultProjection;
603
        }
604 412 vcaballero
}