Statistics
| Revision:

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

History | View | Annotate | Download (12.9 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.Serializable;
9
import java.text.DateFormat;
10
import java.util.ArrayList;
11
import java.util.Date;
12

    
13
import com.hardcode.driverManager.DriverLoadException;
14
import com.iver.cit.gvsig.fmap.DriverException;
15
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
16
import com.iver.cit.gvsig.fmap.layers.CancelationException;
17
import com.iver.cit.gvsig.fmap.layers.DifferentVersionException;
18
import com.iver.cit.gvsig.fmap.layers.XMLException;
19
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
20
import com.iver.utiles.StringUtilities;
21
import com.iver.utiles.XMLEntity;
22

    
23

    
24
/**
25
 * Clase que representa un proyecto de openSIG
26
 *
27
 * @author Fernando Gonz?lez Cort?s
28
 */
29
public class Project implements Serializable, PropertyChangeListener {
30
    private PropertyChangeSupport change;
31
    boolean modified = false;
32
    private String name;
33
    private String path;
34
    private String creationDate;
35
    private String modificationDate;
36
    private String owner;
37
    private String comments;
38
    private Color selectionColor = new Color(255, 255, 0);
39
    private ArrayList views = new ArrayList();
40
    private ArrayList tables = new ArrayList();
41
    private ArrayList maps = new ArrayList();
42
    private ArrayList extents = new ArrayList();
43

    
44
    /**
45
     * Creates a new Project object.
46
     */
47
    public Project() {
48
        change = new PropertyChangeSupport(this);
49

    
50
        //        change.addPropertyChangeListener(this);
51
        creationDate = DateFormat.getDateInstance().format(new Date());
52
        modificationDate = creationDate;
53
    }
54

    
55
    /**
56
     * Obtiene la fecha de creaci?n del proyecto
57
     *
58
     * @return
59
     */
60
    public String getCreationDate() {
61
        return creationDate;
62
    }
63

    
64
    /**
65
     * Obtiene el nombre del proyecto
66
     *
67
     * @return
68
     */
69
    public String getName() {
70
        return name;
71
    }
72

    
73
    /**
74
     * Obtiene la ruta completa del fichero donde se guardo por ?ltima vez el
75
     * proyecto
76
     *
77
     * @return
78
     */
79
    public String getPath() {
80
        return path;
81
    }
82

    
83
    /**
84
     * Asigna la fecha de creaci?n del proyecto. Este m?todo tiene sentido s?lo
85
     * por que al recuperar la fecha del XML hay que asignarla al objeto
86
     * proyecto de alguna manera. La fecha se asigna en el constructor y no se
87
     * deber?a de modificar nunca
88
     *
89
     * @param string
90
     */
91
    public void setCreationDate(String string) {
92
        creationDate = string;
93
        modified = true;
94
        change.firePropertyChange("", null, null);
95
    }
96

    
97
    /**
98
     * A?ade un mapa al proyecto
99
     *
100
     * @param m
101
     */
102
    public void addMap(ProjectMap m) {
103
        maps.add(m);
104
        m.addPropertyChangeListener(this);
105
        modified = true;
106
        change.firePropertyChange("", null, null);
107
    }
108

    
109
    /**
110
     * Elimina un mapa del proyecto
111
     *
112
     * @param i indice del mapa
113
     */
114
    public void delMap(int i) {
115
        maps.remove(i);
116
        modified = true;
117
        change.firePropertyChange("", null, null);
118
    }
119

    
120
    /**
121
     * Establece el nombre del proyecto
122
     *
123
     * @param string
124
     */
125
    public void setName(String string) {
126
        name = string;
127
        modified = true;
128
        change.firePropertyChange("", null, null);
129
    }
130

    
131
    /**
132
     * establece la ruta completa de donde se encuentra guardado el proyecto
133
     *
134
     * @param string
135
     */
136
    public void setPath(String string) {
137
        path = string;
138
        modified = true;
139
        change.firePropertyChange("", null, null);
140
    }
141

    
142
    public ProjectTable getTable(AlphanumericData co){
143
        /**
144
         * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
145
         * una capa, se puede intentar a?adir dos veces la misma tabla
146
         */
147
        for (int i = 0; i < tables.size(); i++) {
148
            if (((ProjectTable) tables.get(i)).getAssociatedTable()==co) {
149
                return (ProjectTable) tables.get(i);
150
            }
151
        }
152
        
153
        return null;
154
    }
155
    
156
    /**
157
     * A?ade una tabla al proyecto
158
     *
159
     * @param t
160
     */
161
    public void addTable(ProjectTable t) {
162
        tables.add(t);
163
        t.addPropertyChangeListener(this);
164
        modified = true;
165
        change.firePropertyChange("", null, null);
166
    }
167

    
168
    /**
169
     * Elimina una tabla del proyecto
170
     *
171
     * @param i indice de la tabla
172
     */
173
    public void delTable(int i) {
174
        tables.remove(i);
175
        modified = true;
176
        change.firePropertyChange("", null, null);
177
    }
178

    
179
    /**
180
     * A?ade una vista al proyecto
181
     *
182
     * @param v
183
     */
184
    public void addView(ProjectView v) {
185
        views.add(v);
186
        v.addPropertyChangeListener(this);
187
        modified = true;
188
        change.firePropertyChange("", null, null);
189
    }
190

    
191
    /**
192
     * Elimina una tabla del proyecto
193
     *
194
     * @param i indice del proyecto
195
     */
196
    public void delView(int i) {
197
        views.remove(i);
198
        modified = true;
199
        change.firePropertyChange("", null, null);
200
    }
201

    
202
    /**
203
     * Devuelve true si el proyecto (o alguna tabla, vista o mapa que contiene)
204
     * fue modificado
205
     *
206
     * @return
207
     */
208
    public boolean isModified() {
209
        return modified;
210
    }
211

    
212
    /**
213
     * Obtiene los comentarios
214
     *
215
     * @return
216
     */
217
    public String getComments() {
218
        return comments;
219
    }
220

    
221
    /**
222
     * Obtiene la fecha de la ?ltima modificaci?n
223
     *
224
     * @return
225
     */
226
    public String getModificationDate() {
227
        return modificationDate;
228
    }
229

    
230
    /**
231
     * Obtiene el propietario del proyecto
232
     *
233
     * @return
234
     */
235
    public String getOwner() {
236
        return owner;
237
    }
238

    
239
    /**
240
     * Establece una cadena como comentarios al proyecto
241
     *
242
     * @param string
243
     */
244
    public void setComments(String string) {
245
        comments = string;
246
        modified = true;
247
        change.firePropertyChange("", null, null);
248
    }
249

    
250
    /**
251
     * Establece la fecha de la ?ltima modificaci?n
252
     *
253
     * @param string
254
     */
255
    public void setModificationDate(String string) {
256
        modificationDate = string;
257
        modified = true;
258
        change.firePropertyChange("", null, null);
259
    }
260

    
261
    /**
262
     * Establece el propietario del proyecto
263
     *
264
     * @param string
265
     */
266
    public void setOwner(String string) {
267
        owner = string;
268
        modified = true;
269
        change.firePropertyChange("", null, null);
270
    }
271

    
272
    /**
273
     * Establece el flag de modificado del proyecto
274
     *
275
     * @param b
276
     */
277
    public void setModified(boolean b) {
278
        modified = b;
279
    }
280

    
281
    /**
282
     * Obtiene el color de selecci?n que se usar? en el proyecto
283
     *
284
     * @return
285
     */
286
    public Color getSelectionColor() {
287
        return selectionColor;
288
    }
289

    
290
    /**
291
     * Establece el color de selecci?n
292
     *
293
     * @param color
294
     */
295
    public void setSelectionColor(Color color) {
296
        selectionColor = color;
297
        modified = true;
298
        change.firePropertyChange("", null, null);
299
    }
300

    
301
    /**
302
     * Obtiene el color como un entero para su serializaci?n a XML
303
     *
304
     * @return
305
     */
306
    public String getColor() {
307
        return StringUtilities.color2String(selectionColor);
308
    }
309

    
310
    /**
311
     * M?todo invocado al recuperar de XML para establecer el color de
312
     * seleccion del proyecto
313
     *
314
     * @param color Entero que representa un color
315
     */
316
    public void setColor(String color) {
317
        modified = true;
318
        selectionColor = StringUtilities.string2Color(color);
319
    }
320

    
321
    /* (non-Javadoc)
322
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
323
     */
324
    public void propertyChange(PropertyChangeEvent evt) {
325
        this.modified = true;
326
        change.firePropertyChange(evt);
327
    }
328

    
329
    /**
330
     * DOCUMENT ME!
331
     *
332
     * @param arg1
333
     */
334
    public void addExtent(ProjectExtent arg1) {
335
        extents.add(arg1);
336
        modified = true;
337
        change.firePropertyChange("addExtent", null, null);
338
    }
339

    
340
    /**
341
     * DOCUMENT ME!
342
     *
343
     * @param arg0
344
     *
345
     * @return
346
     */
347
    public Object removeExtent(int arg0) {
348
        modified = true;
349
        change.firePropertyChange("delExtent", null, null);
350

    
351
        return extents.remove(arg0);
352
    }
353

    
354
    /**
355
     * DOCUMENT ME!
356
     *
357
     * @return DOCUMENT ME!
358
     */
359
    public ProjectExtent[] getExtents() {
360
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
361
    }
362

    
363
    /**
364
     * DOCUMENT ME!
365
     *
366
     * @param arg0
367
     */
368
    public synchronized void addPropertyChangeListener(
369
        PropertyChangeListener arg0) {
370
        change.addPropertyChangeListener(arg0);
371
    }
372

    
373
    /**
374
     * DOCUMENT ME!
375
     *
376
     * @return
377
     */
378
    public ArrayList getMaps() {
379
        return maps;
380
    }
381

    
382
    /**
383
     * DOCUMENT ME!
384
     *
385
     * @return
386
     */
387
    public ArrayList getTables() {
388
        return tables;
389
    }
390

    
391
    /**
392
     * DOCUMENT ME!
393
     *
394
     * @return
395
     */
396
    public ArrayList getViews() {
397
        return views;
398
    }
399

    
400
    /**
401
     * DOCUMENT ME!
402
     *
403
     * @return DOCUMENT ME!
404
     * @throws DriverException
405
     */
406
    public XMLEntity getXMLEntity() throws DriverException {
407
        XMLEntity xml = new XMLEntity();
408
        xml.putProperty("comments", comments);
409
        xml.putProperty("creationDate", creationDate);
410

    
411
        int size = extents.size();
412
        double[] xs = new double[size];
413
        double[] ys = new double[size];
414
        double[] ws = new double[size];
415
        double[] hs = new double[size];
416

    
417
        for (int i = 0; i < size; i++) {
418
            Rectangle2D rect = (Rectangle2D) extents.get(i);
419
            xs[i] = rect.getX();
420
            ys[i] = rect.getY();
421
            ws[i] = rect.getWidth();
422
            hs[i] = rect.getHeight();
423
        }
424

    
425
        xml.putProperty("extentsX", xs);
426
        xml.putProperty("extentsY", ys);
427
        xml.putProperty("extentsW", ws);
428
        xml.putProperty("extentsH", hs);
429

    
430
        xml.putProperty("numViews", views.size());
431

    
432
        for (int i = 0; i < views.size(); i++) {
433
            xml.addChild(((ProjectView) views.get(i)).getXMLEntity());
434
        }
435

    
436
        xml.putProperty("numMaps", maps.size());
437

    
438
        for (int i = 0; i < maps.size(); i++) {
439
            xml.addChild(((ProjectMap) maps.get(i)).getXMLEntity());
440
        }
441

    
442
        xml.putProperty("modificationDate", modificationDate);
443
        xml.putProperty("modified", modified);
444
        xml.putProperty("name", name);
445
        xml.putProperty("owner", owner);
446
        xml.putProperty("path", path);
447
        xml.putProperty("selectionColor",
448
            StringUtilities.color2String(selectionColor));
449
        xml.putProperty("numTables", tables.size());
450

    
451
        for (int i = 0; i < tables.size(); i++) {
452
            xml.addChild(((ProjectTable) tables.get(i)).getXMLEntity());
453
        }
454
       
455
        return xml;
456
    }
457

    
458
    /**
459
     * DOCUMENT ME!
460
     *
461
     * @param xml DOCUMENT ME!
462
     *
463
     * @return DOCUMENT ME!
464
     * @throws XMLException
465
     * @throws com.iver.cit.gvsig.fmap.DriverException
466
     *
467
     * @throws CancelationException
468
     * @throws DifferentVersionException
469
     * @throws ClassNotFoundException
470
     * @throws InstantiationException
471
     * @throws IllegalAccessException
472
     * @throws DriverLoadException
473
     * @throws DriverIOException
474
     */
475
    public static Project createFromXML(XMLEntity xml)
476
        throws XMLException, DriverException {
477
        Project p = new Project();
478
        p.comments = xml.getStringProperty("comments");
479
        p.creationDate = xml.getStringProperty("creationDate");
480

    
481
        double[] xs = xml.getDoubleArrayProperty("extentsX");
482
        double[] ys = xml.getDoubleArrayProperty("extentsY");
483
        double[] ws = xml.getDoubleArrayProperty("extentsW");
484
        double[] hs = xml.getDoubleArrayProperty("extentsH");
485

    
486
        for (int i = 0; i < xs.length; i++) {
487
            Rectangle2D rect = new Rectangle2D.Double(xs[i], ys[i], ws[i], hs[i]);
488
            p.extents.add(rect);
489
        }
490
        int numViews = xml.getIntProperty("numViews");
491

    
492
        for (int i = 0; i < numViews;
493
                i++) {
494
            p.views.add(ProjectView.createFromXML(xml.getChild(i), p));
495
        }
496
        int numMaps = xml.getIntProperty("numMaps");
497

    
498
        for (int i = numViews; i < (numMaps+numViews); i++) {
499
            p.maps.add(ProjectMap.createFromXML(xml.getChild(i), p));
500
        }
501

    
502
        p.modificationDate = xml.getStringProperty("modificationDate");
503
        p.modified = xml.getBooleanProperty("modified");
504
        p.name = xml.getStringProperty("name");
505
        p.owner = xml.getStringProperty("owner");
506
        p.path = xml.getStringProperty("path");
507
        p.selectionColor = StringUtilities.string2Color(xml.getStringProperty(
508
                    "selectionColor"));
509

    
510
        int numTables = xml.getIntProperty("numTables");
511

    
512
        for (int i = numMaps+numViews; i < (numTables + numMaps+numViews); i++) {
513
            p.tables.add(ProjectTable.createFromXML(xml.getChild(i), p));
514
        }
515

    
516
        
517

    
518
        return p;
519
    }
520
}