Statistics
| Revision:

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

History | View | Annotate | Download (14.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.castor;
42

    
43
import java.awt.Color;
44
import java.awt.geom.Rectangle2D;
45
import java.beans.PropertyChangeEvent;
46
import java.beans.PropertyChangeListener;
47
import java.beans.PropertyChangeSupport;
48
import java.io.File;
49
import java.io.Serializable;
50
import java.text.DateFormat;
51
import java.util.ArrayList;
52
import java.util.Date;
53

    
54
import org.cresques.cts.IProjection;
55
import org.cresques.cts.ProjectionPool;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.cit.gvsig.fmap.DriverException;
59
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62
import com.iver.cit.gvsig.fmap.layers.FLayers;
63
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
64
import com.iver.cit.gvsig.fmap.layers.XMLException;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
66
import com.iver.utiles.StringUtilities;
67
import com.iver.utiles.XMLEntity;
68

    
69

    
70
/**
71
 * Clase que representa un proyecto de openSIG
72
 *
73
 * @author Fernando Gonz?lez Cort?s
74
 */
75
public class Project implements Serializable, PropertyChangeListener {
76
        public static String VERSION="0.3";
77
        private PropertyChangeSupport change;
78
        boolean modified = false;
79
        private String name;
80
        private String path;
81
        private String creationDate;
82
        private String modificationDate;
83
        private String owner;
84
        private String comments;
85
        private Color selectionColor = new Color(255, 255, 0);
86
        private ArrayList views = new ArrayList();
87
        private ArrayList tables = new ArrayList();
88
        private ArrayList maps = new ArrayList();
89
        private ArrayList extents = new ArrayList();
90
        static private IProjection defaultProjection = ProjectionPool.get("23030"); 
91

    
92
        /**
93
         * Creates a new Project object.
94
         */
95
        public Project() {
96
                change = new PropertyChangeSupport(this);
97

    
98
                //        change.addPropertyChangeListener(this);
99
                creationDate = DateFormat.getDateInstance().format(new Date());
100
                modificationDate = creationDate;
101
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
102
                                                                                                  .getPluginDirectory()
103
                                                                                                  .getAbsolutePath() +
104
                        File.separator + "drivers");
105
        }
106

    
107
        /**
108
         * Obtiene la fecha de creaci?n del proyecto
109
         *
110
         * @return
111
         */
112
        public String getCreationDate() {
113
                return creationDate;
114
        }
115

    
116
        /**
117
         * Obtiene el nombre del proyecto
118
         *
119
         * @return
120
         */
121
        public String getName() {
122
                return name;
123
        }
124

    
125
        /**
126
         * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
127
         * proyecto
128
         *
129
         * @return
130
         */
131
        public String getPath() {
132
                return path;
133
        }
134

    
135
        /**
136
         * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
137
         * por que al recuperar la fecha del XML hay que asignarla al objeto
138
         * proyecto de alguna manera. La fecha se asigna en el constructor y no se
139
         * deber?a de modificar nunca
140
         *
141
         * @param string
142
         */
143
        public void setCreationDate(String string) {
144
                creationDate = string;
145
                modified = true;
146
                change.firePropertyChange("", null, null);
147
        }
148

    
149
        /**
150
         * A?ade un mapa al proyecto
151
         *
152
         * @param m
153
         */
154
        public void addMap(ProjectMap m) {
155
                maps.add(m);
156
                m.addPropertyChangeListener(this);
157
                modified = true;
158
                change.firePropertyChange("", null, null);
159
                m.setProject(this);
160
        }
161

    
162
        /**
163
         * Elimina un mapa del proyecto
164
         *
165
         * @param i indice del mapa
166
         */
167
        public void delMap(int i) {
168
                maps.remove(i);
169
                modified = true;
170
                change.firePropertyChange("", null, null);
171
        }
172

    
173
        /**
174
         * Establece el nombre del proyecto
175
         *
176
         * @param string
177
         */
178
        public void setName(String string) {
179
                name = string;
180
                modified = true;
181
                change.firePropertyChange("", null, null);
182
        }
183

    
184
        /**
185
         * establece la ruta completa de donde se encuentra guardado el proyecto
186
         *
187
         * @param string
188
         */
189
        public void setPath(String string) {
190
                path = string;
191
                modified = true;
192
                change.firePropertyChange("", null, null);
193
        }
194

    
195
        /**
196
         * DOCUMENT ME!
197
         *
198
         * @param co DOCUMENT ME!
199
         *
200
         * @return DOCUMENT ME!
201
         */
202
        public ProjectTable getTable(AlphanumericData co) {
203
                /**
204
                 * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
205
                 * una capa, se puede intentar a?adir dos veces la misma tabla
206
                 */
207
                for (int i = 0; i < tables.size(); i++) {
208
                        if (((ProjectTable) tables.get(i)).getAssociatedTable() == co) {
209
                                return (ProjectTable) tables.get(i);
210
                        }
211
                }
212

    
213
                return null;
214
        }
215

    
216
        /**
217
         * A?ade una tabla al proyecto
218
         *
219
         * @param t
220
         */
221
        public void addTable(ProjectTable t) {
222
                tables.add(t);
223
                t.addPropertyChangeListener(this);
224
                modified = true;
225
                change.firePropertyChange("", null, null);
226
                t.setProject(this);
227
        }
228

    
229
        /**
230
         * Elimina una tabla del proyecto
231
         *
232
         * @param i indice de la tabla
233
         */
234
        public void delTable(int i) {
235
                tables.remove(i);
236
                modified = true;
237
                change.firePropertyChange("", null, null);
238
        }
239

    
240
        /**
241
         * A?ade una vista al proyecto
242
         *
243
         * @param v
244
         */
245
        public void addView(ProjectView v) {
246
                views.add(v);
247
                v.addPropertyChangeListener(this);
248
                modified = true;
249
                change.firePropertyChange("", null, null);
250
                v.setProject(this);
251
        }
252

    
253
        /**
254
         * Elimina una tabla del proyecto
255
         *
256
         * @param i indice del proyecto
257
         */
258
        public void delView(int i) {
259
                views.remove(i);
260
                modified = true;
261
                change.firePropertyChange("", null, null);
262
        }
263

    
264
        /**
265
         * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
266
         * fue modificado
267
         *
268
         * @return
269
         */
270
        public boolean isModified() {
271
                return modified;
272
        }
273

    
274
        /**
275
         * Obtiene los comentarios
276
         *
277
         * @return
278
         */
279
        public String getComments() {
280
                return comments;
281
        }
282

    
283
        /**
284
         * Obtiene la fecha de la ?ltima modificaci?n
285
         *
286
         * @return
287
         */
288
        public String getModificationDate() {
289
                return modificationDate;
290
        }
291

    
292
        /**
293
         * Obtiene el propietario del proyecto
294
         *
295
         * @return
296
         */
297
        public String getOwner() {
298
                return owner;
299
        }
300

    
301
        /**
302
         * Establece una cadena como comentarios al proyecto
303
         *
304
         * @param string
305
         */
306
        public void setComments(String string) {
307
                comments = string;
308
                modified = true;
309
                change.firePropertyChange("", null, null);
310
        }
311

    
312
        /**
313
         * Establece la fecha de la ?ltima modificaci?n
314
         *
315
         * @param string
316
         */
317
        public void setModificationDate(String string) {
318
                modificationDate = string;
319
                modified = true;
320
                change.firePropertyChange("", null, null);
321
        }
322

    
323
        /**
324
         * Establece el propietario del proyecto
325
         *
326
         * @param string
327
         */
328
        public void setOwner(String string) {
329
                owner = string;
330
                modified = true;
331
                change.firePropertyChange("", null, null);
332
        }
333

    
334
        /**
335
         * Establece el flag de modificado del proyecto
336
         *
337
         * @param b
338
         */
339
        public void setModified(boolean b) {
340
                modified = b;
341
        }
342

    
343
        /**
344
         * Obtiene el color de selecci?n que se usar? en el proyecto
345
         *
346
         * @return
347
         */
348
        public Color getSelectionColor() {
349
                return selectionColor;
350
        }
351

    
352
        /**
353
         * Establece el color de selecci?n
354
         *
355
         * @param color
356
         */
357
        public void setSelectionColor(Color color) {
358
                selectionColor = color;
359
                FSymbol.setSelectionColor(color);
360
                modified = true;
361
                change.firePropertyChange("selectionColor", null, color);
362
        }
363

    
364
        /**
365
         * Obtiene el color como un entero para su serializaci?n a XML
366
         *
367
         * @return
368
         */
369
        public String getColor() {
370
                return StringUtilities.color2String(selectionColor);
371
        }
372

    
373
        /**
374
         * M?todo invocado al recuperar de XML para establecer el color de
375
         * seleccion del proyecto
376
         *
377
         * @param color Entero que representa un color
378
         */
379
        public void setColor(String color) {
380
                modified = true;
381
                selectionColor = StringUtilities.string2Color(color);
382
        }
383

    
384
        /* (non-Javadoc)
385
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
386
         */
387
        public void propertyChange(PropertyChangeEvent evt) {
388
                this.modified = true;
389
                change.firePropertyChange(evt);
390
        }
391

    
392
        /**
393
         * DOCUMENT ME!
394
         *
395
         * @param arg1
396
         */
397
        public void addExtent(ProjectExtent arg1) {
398
                extents.add(arg1);
399
                modified = true;
400
                change.firePropertyChange("addExtent", null, null);
401
        }
402

    
403
        /**
404
         * DOCUMENT ME!
405
         *
406
         * @param arg0
407
         *
408
         * @return
409
         */
410
        public Object removeExtent(int arg0) {
411
                modified = true;
412
                change.firePropertyChange("delExtent", null, null);
413

    
414
                return extents.remove(arg0);
415
        }
416

    
417
        /**
418
         * DOCUMENT ME!
419
         *
420
         * @return DOCUMENT ME!
421
         */
422
        public ProjectExtent[] getExtents() {
423
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
424
        }
425

    
426
        /**
427
         * DOCUMENT ME!
428
         *
429
         * @param arg0
430
         */
431
        public synchronized void addPropertyChangeListener(
432
                PropertyChangeListener arg0) {
433
                change.addPropertyChangeListener(arg0);
434
        }
435

    
436
        /**
437
         * DOCUMENT ME!
438
         *
439
         * @return
440
         */
441
        public ArrayList getMaps() {
442
                return maps;
443
        }
444

    
445
        /**
446
         * DOCUMENT ME!
447
         *
448
         * @return
449
         */
450
        public ArrayList getTables() {
451
                return tables;
452
        }
453

    
454
        /**
455
         * DOCUMENT ME!
456
         *
457
         * @return
458
         */
459
        public ArrayList getViews() {
460
                return views;
461
        }
462

    
463
        /**
464
         * DOCUMENT ME!
465
         *
466
         * @return DOCUMENT ME!
467
         *
468
         * @throws DriverException
469
         */
470
        public XMLEntity getXMLEntity() throws DriverException {
471
                XMLEntity xml = new XMLEntity();
472
                xml.putProperty("className",this.getClass().getName());
473
                xml.putProperty("VERSION",VERSION);
474
                xml.putProperty("comments", comments);
475
                xml.putProperty("creationDate", creationDate);
476

    
477
                int size = extents.size();
478
                double[] xs = new double[size];
479
                double[] ys = new double[size];
480
                double[] ws = new double[size];
481
                double[] hs = new double[size];
482

    
483
                for (int i = 0; i < size; i++) {
484
                        Rectangle2D rect = (Rectangle2D) extents.get(i);
485
                        xs[i] = rect.getX();
486
                        ys[i] = rect.getY();
487
                        ws[i] = rect.getWidth();
488
                        hs[i] = rect.getHeight();
489
                }
490

    
491
                xml.putProperty("extentsX", xs);
492
                xml.putProperty("extentsY", ys);
493
                xml.putProperty("extentsW", ws);
494
                xml.putProperty("extentsH", hs);
495

    
496
                xml.putProperty("numViews", views.size());
497

    
498
                for (int i = 0; i < views.size(); i++) {
499
                        xml.addChild(((ProjectView) views.get(i)).getXMLEntity());
500
                }
501

    
502
                xml.putProperty("numMaps", maps.size());
503

    
504
                for (int i = 0; i < maps.size(); i++) {
505
                        xml.addChild(((ProjectMap) maps.get(i)).getXMLEntity());
506
                }
507

    
508
                xml.putProperty("modificationDate", modificationDate);
509
                xml.putProperty("modified", modified);
510
                xml.putProperty("name", name);
511
                xml.putProperty("owner", owner);
512
                xml.putProperty("path", path);
513
                xml.putProperty("selectionColor",
514
                        StringUtilities.color2String(selectionColor));
515
                xml.putProperty("numTables", tables.size());
516

    
517
                for (int i = 0; i < tables.size(); i++) {
518
                        xml.addChild(((ProjectTable) tables.get(i)).getXMLEntity());
519
                }
520
                xml.putProperty("projection", defaultProjection.getAbrev());
521

    
522
                return xml;
523
        }
524

    
525
        /**
526
         * DOCUMENT ME!
527
         *
528
         * @param xml DOCUMENT ME!
529
         *
530
         * @return DOCUMENT ME!
531
         * @throws XMLException
532
         * @throws DriverException
533
         * @throws DriverIOException
534
         */
535
        public static Project createFromXML(XMLEntity xml)
536
                throws XMLException, DriverException, DriverIOException {
537
                Project p = new Project();
538
                p.comments = xml.getStringProperty("comments");
539
                p.creationDate = xml.getStringProperty("creationDate");
540

    
541
                double[] xs = xml.getDoubleArrayProperty("extentsX");
542
                double[] ys = xml.getDoubleArrayProperty("extentsY");
543
                double[] ws = xml.getDoubleArrayProperty("extentsW");
544
                double[] hs = xml.getDoubleArrayProperty("extentsH");
545

    
546
                for (int i = 0; i < xs.length; i++) {
547
                        Rectangle2D rect = new Rectangle2D.Double(xs[i], ys[i], ws[i], hs[i]);
548
                        p.extents.add(rect);
549
                }
550

    
551
                int numViews = xml.getIntProperty("numViews");
552

    
553
                for (int i = 0; i < numViews; i++) {
554
                        p.views.add(ProjectView.createFromXML(xml.getChild(i), p));
555
                }
556

    
557
                int numMaps = xml.getIntProperty("numMaps");
558

    
559
                for (int i = numViews; i < (numMaps + numViews); i++) {
560
                        p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
561
                }
562

    
563
                p.modificationDate = xml.getStringProperty("modificationDate");
564
                p.modified = xml.getBooleanProperty("modified");
565
                p.name = xml.getStringProperty("name");
566
                p.owner = xml.getStringProperty("owner");
567
                p.path = xml.getStringProperty("path");
568
                p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
569
                                        "selectionColor"));
570

    
571
                int numTables = xml.getIntProperty("numTables");
572

    
573
                for (int i = numMaps + numViews; i < (numTables + numMaps + numViews);
574
                                i++) {
575
                        p.tables.add(ProjectTable.createFromXML(xml.getChild(i), p));
576
                }
577
                String strProj = xml.getStringProperty("projection");
578
                if (strProj != null)
579
                        Project.setProjection(ProjectionPool.get(strProj));
580

    
581
                return p;
582
        }
583

    
584
        /**
585
         * Obtiene la vista que contiene a la capa que se pasa como
586
         * par?metro
587
         *
588
         * @param layer Capa cuya vista se quiere obtener
589
         *
590
         * @return
591
         *
592
         * @throws RuntimeException Si la capa que se pasa como par?metro no se
593
         * encuentra en ninguna vista
594
         */
595
        public String getView(FLayer layer) {
596
                for (int v = 0; v < views.size(); v++) {
597
                        ProjectView pView = (ProjectView) views.get(v);
598
                        FLayers layers = pView.getMapContext().getLayers();
599

    
600
                        for (int i = 0; i < layers.getLayersCount(); i++) {
601
                                if (layers.getLayer(i) == layer) {
602
                                        return pView.getName();
603
                                }
604
                        }
605
                }
606

    
607
                throw new RuntimeException("The layer is not in a view");
608
        }
609

    
610
        /**
611
         * Devuelve la vista cuyo nombre coincide (sensible a mayusculas)
612
         * con el que se pasa como par?metro. Devuelve null si no hay ninguna
613
         * vista con ese nombre
614
         *
615
         * @param viewName Nombre de la vista que se quiere obtener
616
         *
617
         * @return DOCUMENT ME!
618
         */
619
        public ProjectView getViewByName(String viewName) {
620
                for (int v = 0; v < views.size(); v++) {
621
                        ProjectView pView = (ProjectView) views.get(v);
622

    
623
                        if (pView.getName().equals(viewName)) {
624
                                return pView;
625
                        }
626
                }
627

    
628
                return null;
629
        }
630
        public static IProjection getProjection() {
631
                return defaultProjection;
632
        }
633
        public static void setProjection(IProjection defaultProjection) {
634
                Project.defaultProjection = defaultProjection;
635
        }
636
}