Statistics
| Revision:

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

History | View | Annotate | Download (13.2 KB)

1
package com.iver.cit.gvsig.project.castor;
2

    
3
import java.awt.Color;
4
import java.awt.geom.Rectangle2D;
5
import java.beans.PropertyChangeEvent;
6
import java.beans.PropertyChangeListener;
7
import java.beans.PropertyChangeSupport;
8
import java.io.File;
9
import java.io.Serializable;
10
import java.text.DateFormat;
11
import java.util.ArrayList;
12
import java.util.Date;
13

    
14
import org.cresques.cts.IProjection;
15
import org.cresques.cts.ProjectionPool;
16

    
17
import com.iver.andami.PluginServices;
18
import com.iver.cit.gvsig.fmap.DriverException;
19
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
20
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
21
import com.iver.cit.gvsig.fmap.layers.FLayer;
22
import com.iver.cit.gvsig.fmap.layers.FLayers;
23
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
24
import com.iver.cit.gvsig.fmap.layers.XMLException;
25
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
26
import com.iver.utiles.StringUtilities;
27
import com.iver.utiles.XMLEntity;
28

    
29

    
30
/**
31
 * Clase que representa un proyecto de openSIG
32
 *
33
 * @author Fernando Gonz?lez Cort?s
34
 */
35
public class Project implements Serializable, PropertyChangeListener {
36
        public static String VERSION="0.3";
37
        private PropertyChangeSupport change;
38
        boolean modified = false;
39
        private String name;
40
        private String path;
41
        private String creationDate;
42
        private String modificationDate;
43
        private String owner;
44
        private String comments;
45
        private Color selectionColor = new Color(255, 255, 0);
46
        private ArrayList views = new ArrayList();
47
        private ArrayList tables = new ArrayList();
48
        private ArrayList maps = new ArrayList();
49
        private ArrayList extents = new ArrayList();
50
        static private IProjection defaultProjection = ProjectionPool.get("23030"); 
51

    
52
        /**
53
         * Creates a new Project object.
54
         */
55
        public Project() {
56
                change = new PropertyChangeSupport(this);
57

    
58
                //        change.addPropertyChangeListener(this);
59
                creationDate = DateFormat.getDateInstance().format(new Date());
60
                modificationDate = creationDate;
61
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
62
                                                                                                  .getPluginDirectory()
63
                                                                                                  .getAbsolutePath() +
64
                        File.separator + "drivers");
65
        }
66

    
67
        /**
68
         * Obtiene la fecha de creaci?n del proyecto
69
         *
70
         * @return
71
         */
72
        public String getCreationDate() {
73
                return creationDate;
74
        }
75

    
76
        /**
77
         * Obtiene el nombre del proyecto
78
         *
79
         * @return
80
         */
81
        public String getName() {
82
                return name;
83
        }
84

    
85
        /**
86
         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
87
         * proyecto
88
         *
89
         * @return
90
         */
91
        public String getPath() {
92
                return path;
93
        }
94

    
95
        /**
96
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
97
         * por que al recuperar la fecha del XML hay que asignarla al objeto
98
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
99
         * deber?a de modificar nunca
100
         *
101
         * @param string
102
         */
103
        public void setCreationDate(String string) {
104
                creationDate = string;
105
                modified = true;
106
                change.firePropertyChange("", null, null);
107
        }
108

    
109
        /**
110
         * A?ade un mapa al proyecto
111
         *
112
         * @param m
113
         */
114
        public void addMap(ProjectMap m) {
115
                maps.add(m);
116
                m.addPropertyChangeListener(this);
117
                modified = true;
118
                change.firePropertyChange("", null, null);
119
                m.setProject(this);
120
        }
121

    
122
        /**
123
         * Elimina un mapa del proyecto
124
         *
125
         * @param i indice del mapa
126
         */
127
        public void delMap(int i) {
128
                maps.remove(i);
129
                modified = true;
130
                change.firePropertyChange("", null, null);
131
        }
132

    
133
        /**
134
         * Establece el nombre del proyecto
135
         *
136
         * @param string
137
         */
138
        public void setName(String string) {
139
                name = string;
140
                modified = true;
141
                change.firePropertyChange("", null, null);
142
        }
143

    
144
        /**
145
         * establece la ruta completa de donde se encuentra guardado el proyecto
146
         *
147
         * @param string
148
         */
149
        public void setPath(String string) {
150
                path = string;
151
                modified = true;
152
                change.firePropertyChange("", null, null);
153
        }
154

    
155
        /**
156
         * DOCUMENT ME!
157
         *
158
         * @param co DOCUMENT ME!
159
         *
160
         * @return DOCUMENT ME!
161
         */
162
        public ProjectTable getTable(AlphanumericData co) {
163
                /**
164
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
165
                 * una capa, se puede intentar a?adir dos veces la misma tabla
166
                 */
167
                for (int i = 0; i < tables.size(); i++) {
168
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
169
                                return (ProjectTable) tables.get(i);
170
                        }
171
                }
172

    
173
                return null;
174
        }
175

    
176
        /**
177
         * A?ade una tabla al proyecto
178
         *
179
         * @param t
180
         */
181
        public void addTable(ProjectTable t) {
182
                tables.add(t);
183
                t.addPropertyChangeListener(this);
184
                modified = true;
185
                change.firePropertyChange("", null, null);
186
                t.setProject(this);
187
        }
188

    
189
        /**
190
         * Elimina una tabla del proyecto
191
         *
192
         * @param i indice de la tabla
193
         */
194
        public void delTable(int i) {
195
                tables.remove(i);
196
                modified = true;
197
                change.firePropertyChange("", null, null);
198
        }
199

    
200
        /**
201
         * A?ade una vista al proyecto
202
         *
203
         * @param v
204
         */
205
        public void addView(ProjectView v) {
206
                views.add(v);
207
                v.addPropertyChangeListener(this);
208
                modified = true;
209
                change.firePropertyChange("", null, null);
210
                v.setProject(this);
211
        }
212

    
213
        /**
214
         * Elimina una tabla del proyecto
215
         *
216
         * @param i indice del proyecto
217
         */
218
        public void delView(int i) {
219
                views.remove(i);
220
                modified = true;
221
                change.firePropertyChange("", null, null);
222
        }
223

    
224
        /**
225
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
226
         * fue modificado
227
         *
228
         * @return
229
         */
230
        public boolean isModified() {
231
                return modified;
232
        }
233

    
234
        /**
235
         * Obtiene los comentarios
236
         *
237
         * @return
238
         */
239
        public String getComments() {
240
                return comments;
241
        }
242

    
243
        /**
244
         * Obtiene la fecha de la ?ltima modificaci?n
245
         *
246
         * @return
247
         */
248
        public String getModificationDate() {
249
                return modificationDate;
250
        }
251

    
252
        /**
253
         * Obtiene el propietario del proyecto
254
         *
255
         * @return
256
         */
257
        public String getOwner() {
258
                return owner;
259
        }
260

    
261
        /**
262
         * Establece una cadena como comentarios al proyecto
263
         *
264
         * @param string
265
         */
266
        public void setComments(String string) {
267
                comments = string;
268
                modified = true;
269
                change.firePropertyChange("", null, null);
270
        }
271

    
272
        /**
273
         * Establece la fecha de la ?ltima modificaci?n
274
         *
275
         * @param string
276
         */
277
        public void setModificationDate(String string) {
278
                modificationDate = string;
279
                modified = true;
280
                change.firePropertyChange("", null, null);
281
        }
282

    
283
        /**
284
         * Establece el propietario del proyecto
285
         *
286
         * @param string
287
         */
288
        public void setOwner(String string) {
289
                owner = string;
290
                modified = true;
291
                change.firePropertyChange("", null, null);
292
        }
293

    
294
        /**
295
         * Establece el flag de modificado del proyecto
296
         *
297
         * @param b
298
         */
299
        public void setModified(boolean b) {
300
                modified = b;
301
        }
302

    
303
        /**
304
         * Obtiene el color de selecci?n que se usar? en el proyecto
305
         *
306
         * @return
307
         */
308
        public Color getSelectionColor() {
309
                return selectionColor;
310
        }
311

    
312
        /**
313
         * Establece el color de selecci?n
314
         *
315
         * @param color
316
         */
317
        public void setSelectionColor(Color color) {
318
                selectionColor = color;
319
                FSymbol.setSelectionColor(color);
320
                modified = true;
321
                change.firePropertyChange("selectionColor", null, color);
322
        }
323

    
324
        /**
325
         * Obtiene el color como un entero para su serializaci?n a XML
326
         *
327
         * @return
328
         */
329
        public String getColor() {
330
                return StringUtilities.color2String(selectionColor);
331
        }
332

    
333
        /**
334
         * M?todo invocado al recuperar de XML para establecer el color de
335
         * seleccion del proyecto
336
         *
337
         * @param color Entero que representa un color
338
         */
339
        public void setColor(String color) {
340
                modified = true;
341
                selectionColor = StringUtilities.string2Color(color);
342
        }
343

    
344
        /* (non-Javadoc)
345
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
346
         */
347
        public void propertyChange(PropertyChangeEvent evt) {
348
                this.modified = true;
349
                change.firePropertyChange(evt);
350
        }
351

    
352
        /**
353
         * DOCUMENT ME!
354
         *
355
         * @param arg1
356
         */
357
        public void addExtent(ProjectExtent arg1) {
358
                extents.add(arg1);
359
                modified = true;
360
                change.firePropertyChange("addExtent", null, null);
361
        }
362

    
363
        /**
364
         * DOCUMENT ME!
365
         *
366
         * @param arg0
367
         *
368
         * @return
369
         */
370
        public Object removeExtent(int arg0) {
371
                modified = true;
372
                change.firePropertyChange("delExtent", null, null);
373

    
374
                return extents.remove(arg0);
375
        }
376

    
377
        /**
378
         * DOCUMENT ME!
379
         *
380
         * @return DOCUMENT ME!
381
         */
382
        public ProjectExtent[] getExtents() {
383
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
384
        }
385

    
386
        /**
387
         * DOCUMENT ME!
388
         *
389
         * @param arg0
390
         */
391
        public synchronized void addPropertyChangeListener(
392
                PropertyChangeListener arg0) {
393
                change.addPropertyChangeListener(arg0);
394
        }
395

    
396
        /**
397
         * DOCUMENT ME!
398
         *
399
         * @return
400
         */
401
        public ArrayList getMaps() {
402
                return maps;
403
        }
404

    
405
        /**
406
         * DOCUMENT ME!
407
         *
408
         * @return
409
         */
410
        public ArrayList getTables() {
411
                return tables;
412
        }
413

    
414
        /**
415
         * DOCUMENT ME!
416
         *
417
         * @return
418
         */
419
        public ArrayList getViews() {
420
                return views;
421
        }
422

    
423
        /**
424
         * DOCUMENT ME!
425
         *
426
         * @return DOCUMENT ME!
427
         *
428
         * @throws DriverException
429
         */
430
        public XMLEntity getXMLEntity() throws DriverException {
431
                XMLEntity xml = new XMLEntity();
432
                xml.putProperty("className",this.getClass().getName());
433
                xml.putProperty("VERSION",VERSION);
434
                xml.putProperty("comments", comments);
435
                xml.putProperty("creationDate", creationDate);
436

    
437
                int size = extents.size();
438
                double[] xs = new double[size];
439
                double[] ys = new double[size];
440
                double[] ws = new double[size];
441
                double[] hs = new double[size];
442

    
443
                for (int i = 0; i < size; i++) {
444
                        Rectangle2D rect = (Rectangle2D) extents.get(i);
445
                        xs[i] = rect.getX();
446
                        ys[i] = rect.getY();
447
                        ws[i] = rect.getWidth();
448
                        hs[i] = rect.getHeight();
449
                }
450

    
451
                xml.putProperty("extentsX", xs);
452
                xml.putProperty("extentsY", ys);
453
                xml.putProperty("extentsW", ws);
454
                xml.putProperty("extentsH", hs);
455

    
456
                xml.putProperty("numViews", views.size());
457

    
458
                for (int i = 0; i < views.size(); i++) {
459
                        xml.addChild(((ProjectView) views.get(i)).getXMLEntity());
460
                }
461

    
462
                xml.putProperty("numMaps", maps.size());
463

    
464
                for (int i = 0; i < maps.size(); i++) {
465
                        xml.addChild(((ProjectMap) maps.get(i)).getXMLEntity());
466
                }
467

    
468
                xml.putProperty("modificationDate", modificationDate);
469
                xml.putProperty("modified", modified);
470
                xml.putProperty("name", name);
471
                xml.putProperty("owner", owner);
472
                xml.putProperty("path", path);
473
                xml.putProperty("selectionColor",
474
                        StringUtilities.color2String(selectionColor));
475
                xml.putProperty("numTables", tables.size());
476

    
477
                for (int i = 0; i < tables.size(); i++) {
478
                        xml.addChild(((ProjectTable) tables.get(i)).getXMLEntity());
479
                }
480
                xml.putProperty("projection", defaultProjection.getAbrev());
481

    
482
                return xml;
483
        }
484

    
485
        /**
486
         * DOCUMENT ME!
487
         *
488
         * @param xml DOCUMENT ME!
489
         *
490
         * @return DOCUMENT ME!
491
         * @throws XMLException
492
         * @throws DriverException
493
         * @throws DriverIOException
494
         */
495
        public static Project createFromXML(XMLEntity xml)
496
                throws XMLException, DriverException, DriverIOException {
497
                Project p = new Project();
498
                p.comments = xml.getStringProperty("comments");
499
                p.creationDate = xml.getStringProperty("creationDate");
500

    
501
                double[] xs = xml.getDoubleArrayProperty("extentsX");
502
                double[] ys = xml.getDoubleArrayProperty("extentsY");
503
                double[] ws = xml.getDoubleArrayProperty("extentsW");
504
                double[] hs = xml.getDoubleArrayProperty("extentsH");
505

    
506
                for (int i = 0; i < xs.length; i++) {
507
                        Rectangle2D rect = new Rectangle2D.Double(xs[i], ys[i], ws[i], hs[i]);
508
                        p.extents.add(rect);
509
                }
510

    
511
                int numViews = xml.getIntProperty("numViews");
512

    
513
                for (int i = 0; i < numViews; i++) {
514
                        p.views.add(ProjectView.createFromXML(xml.getChild(i), p));
515
                }
516

    
517
                int numMaps = xml.getIntProperty("numMaps");
518

    
519
                for (int i = numViews; i < (numMaps + numViews); i++) {
520
                        p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
521
                }
522

    
523
                p.modificationDate = xml.getStringProperty("modificationDate");
524
                p.modified = xml.getBooleanProperty("modified");
525
                p.name = xml.getStringProperty("name");
526
                p.owner = xml.getStringProperty("owner");
527
                p.path = xml.getStringProperty("path");
528
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
529
                                        "selectionColor"));
530

    
531
                int numTables = xml.getIntProperty("numTables");
532

    
533
                for (int i = numMaps + numViews; i < (numTables + numMaps + numViews);
534
                                i++) {
535
                        p.tables.add(ProjectTable.createFromXML(xml.getChild(i), p));
536
                }
537
                String strProj = xml.getStringProperty("projection");
538
                if (strProj != null)
539
                        Project.setProjection(ProjectionPool.get(strProj));
540

    
541
                return p;
542
        }
543

    
544
        /**
545
         * Obtiene la vista que contiene a la capa que se pasa como
546
         * par?metro
547
         *
548
         * @param layer Capa cuya vista se quiere obtener
549
         *
550
         * @return
551
         *
552
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
553
         * encuentra en ninguna vista
554
         */
555
        public String getView(FLayer layer) {
556
                for (int v = 0; v < views.size(); v++) {
557
                        ProjectView pView = (ProjectView) views.get(v);
558
                        FLayers layers = pView.getMapContext().getLayers();
559

    
560
                        for (int i = 0; i < layers.getLayersCount(); i++) {
561
                                if (layers.getLayer(i) == layer) {
562
                                        return pView.getName();
563
                                }
564
                        }
565
                }
566

    
567
                throw new RuntimeException("The layer is not in a view");
568
        }
569

    
570
        /**
571
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas)
572
         * con el que se pasa como par?metro. Devuelve null si no hay ninguna
573
         * vista con ese nombre
574
         *
575
         * @param viewName Nombre de la vista que se quiere obtener
576
         *
577
         * @return DOCUMENT ME!
578
         */
579
        public ProjectView getViewByName(String viewName) {
580
                for (int v = 0; v < views.size(); v++) {
581
                        ProjectView pView = (ProjectView) views.get(v);
582

    
583
                        if (pView.getName().equals(viewName)) {
584
                                return pView;
585
                        }
586
                }
587

    
588
                return null;
589
        }
590
        public static IProjection getProjection() {
591
                return defaultProjection;
592
        }
593
        public static void setProjection(IProjection defaultProjection) {
594
                Project.defaultProjection = defaultProjection;
595
        }
596
}