Statistics
| Revision:

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

History | View | Annotate | Download (12.7 KB)

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

    
3
import com.hardcode.driverManager.DriverLoadException;
4

    
5
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
6
import com.iver.cit.gvsig.fmap.layers.CancelationException;
7
import com.iver.cit.gvsig.fmap.layers.DifferentVersionException;
8

    
9
import com.iver.utiles.StringUtilities;
10
import com.iver.utiles.XMLEntity;
11

    
12
import java.awt.Color;
13
import java.awt.geom.Rectangle2D;
14

    
15
import java.beans.PropertyChangeEvent;
16
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyChangeSupport;
18

    
19
import java.io.Serializable;
20

    
21
import java.text.DateFormat;
22

    
23
import java.util.ArrayList;
24
import java.util.Date;
25

    
26

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

    
47
    /**
48
     * Creates a new Project object.
49
     */
50
    public Project() {
51
        change = new PropertyChangeSupport(this);
52

    
53
        //        change.addPropertyChangeListener(this);
54
        creationDate = DateFormat.getDateInstance().format(new Date());
55
        modificationDate = creationDate;
56
    }
57

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

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

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

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

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

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

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

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

    
145
    /**
146
     * A?ade una tabla al proyecto
147
     *
148
     * @param t
149
     */
150
    public void addTable(ProjectTable t) {
151
        //TODO implementar bien
152

    
153
        /**
154
         * Como las tablas se pueden a?adir cuando se pincha en "ver tabla" de
155
         * una capa, se puede intentar a?adir dos veces la misma tabla
156
         */
157
        for (int i = 0; i < tables.size(); i++) {
158
            if (((ProjectTable) tables.get(i)).getName().equals(t.getName())) {
159
                return;
160
            }
161
        }
162

    
163
        tables.add(t);
164
        t.addPropertyChangeListener(this);
165
        modified = true;
166
        change.firePropertyChange("", null, null);
167
    }
168

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
401
    /**
402
     * DOCUMENT ME!
403
     *
404
     * @return DOCUMENT ME!
405
     */
406
    public XMLEntity getXMLEntity() {
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
     *
465
     * @throws CancelationException
466
     * @throws DifferentVersionException
467
     * @throws ClassNotFoundException
468
     * @throws InstantiationException
469
     * @throws IllegalAccessException
470
     * @throws DriverLoadException
471
     * @throws DriverIOException
472
     */
473
    public static Project createFromXML(XMLEntity xml)
474
        throws CancelationException, DifferentVersionException, 
475
            ClassNotFoundException, InstantiationException, 
476
            IllegalAccessException, DriverLoadException, DriverIOException {
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
}