svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / castor / Project.java @ 904
History | View | Annotate | Download (13.2 KB)
1 |
package com.iver.cit.gvsig.project.castor; |
---|---|
2 |
|
3 |
import com.hardcode.driverManager.DriverLoadException; |
4 |
|
5 |
import com.iver.andami.PluginServices; |
6 |
|
7 |
import com.iver.cit.gvsig.fmap.DriverException; |
8 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
9 |
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 |
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 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
16 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
17 |
|
18 |
import com.iver.utiles.StringUtilities; |
19 |
import com.iver.utiles.XMLEntity; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.geom.Rectangle2D; |
23 |
|
24 |
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 |
import org.cresques.cts.IProjection; |
38 |
import org.cresques.cts.ProjectionPool; |
39 |
|
40 |
|
41 |
/**
|
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 |
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 |
static private IProjection defaultProjection = ProjectionPool.get("23030"); |
61 |
|
62 |
/**
|
63 |
* Creates a new Project object.
|
64 |
*/
|
65 |
public Project() {
|
66 |
change = new PropertyChangeSupport(this); |
67 |
|
68 |
// 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 |
|
77 |
/**
|
78 |
* Obtiene la fecha de creaci?n del proyecto
|
79 |
*
|
80 |
* @return
|
81 |
*/
|
82 |
public String getCreationDate() { |
83 |
return creationDate;
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* Obtiene el nombre del proyecto
|
88 |
*
|
89 |
* @return
|
90 |
*/
|
91 |
public String getName() { |
92 |
return name;
|
93 |
} |
94 |
|
95 |
/**
|
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 |
|
105 |
/**
|
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 |
|
119 |
/**
|
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 |
|
132 |
/**
|
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 |
|
143 |
/**
|
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 |
|
154 |
/**
|
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 |
|
165 |
/**
|
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 |
|
183 |
return null; |
184 |
} |
185 |
|
186 |
/**
|
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 |
|
199 |
/**
|
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 |
|
210 |
/**
|
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 |
|
223 |
/**
|
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 |
|
234 |
/**
|
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 |
|
244 |
/**
|
245 |
* Obtiene los comentarios
|
246 |
*
|
247 |
* @return
|
248 |
*/
|
249 |
public String getComments() { |
250 |
return comments;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* Obtiene la fecha de la ?ltima modificaci?n
|
255 |
*
|
256 |
* @return
|
257 |
*/
|
258 |
public String getModificationDate() { |
259 |
return modificationDate;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Obtiene el propietario del proyecto
|
264 |
*
|
265 |
* @return
|
266 |
*/
|
267 |
public String getOwner() { |
268 |
return owner;
|
269 |
} |
270 |
|
271 |
/**
|
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 |
|
282 |
/**
|
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 |
|
293 |
/**
|
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 |
|
304 |
/**
|
305 |
* Establece el flag de modificado del proyecto
|
306 |
*
|
307 |
* @param b
|
308 |
*/
|
309 |
public void setModified(boolean b) { |
310 |
modified = b; |
311 |
} |
312 |
|
313 |
/**
|
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 |
|
322 |
/**
|
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 |
|
334 |
/**
|
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 |
|
343 |
/**
|
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 |
|
354 |
/* (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 |
|
362 |
/**
|
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 |
|
373 |
/**
|
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 |
|
384 |
return extents.remove(arg0);
|
385 |
} |
386 |
|
387 |
/**
|
388 |
* DOCUMENT ME!
|
389 |
*
|
390 |
* @return DOCUMENT ME!
|
391 |
*/
|
392 |
public ProjectExtent[] getExtents() { |
393 |
return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]); |
394 |
} |
395 |
|
396 |
/**
|
397 |
* DOCUMENT ME!
|
398 |
*
|
399 |
* @param arg0
|
400 |
*/
|
401 |
public synchronized void addPropertyChangeListener( |
402 |
PropertyChangeListener arg0) {
|
403 |
change.addPropertyChangeListener(arg0); |
404 |
} |
405 |
|
406 |
/**
|
407 |
* DOCUMENT ME!
|
408 |
*
|
409 |
* @return
|
410 |
*/
|
411 |
public ArrayList getMaps() { |
412 |
return maps;
|
413 |
} |
414 |
|
415 |
/**
|
416 |
* DOCUMENT ME!
|
417 |
*
|
418 |
* @return
|
419 |
*/
|
420 |
public ArrayList getTables() { |
421 |
return tables;
|
422 |
} |
423 |
|
424 |
/**
|
425 |
* DOCUMENT ME!
|
426 |
*
|
427 |
* @return
|
428 |
*/
|
429 |
public ArrayList getViews() { |
430 |
return views;
|
431 |
} |
432 |
|
433 |
/**
|
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 |
|
445 |
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 |
|
451 |
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 |
|
459 |
xml.putProperty("extentsX", xs);
|
460 |
xml.putProperty("extentsY", ys);
|
461 |
xml.putProperty("extentsW", ws);
|
462 |
xml.putProperty("extentsH", hs);
|
463 |
|
464 |
xml.putProperty("numViews", views.size());
|
465 |
|
466 |
for (int i = 0; i < views.size(); i++) { |
467 |
xml.addChild(((ProjectView) views.get(i)).getXMLEntity()); |
468 |
} |
469 |
|
470 |
xml.putProperty("numMaps", maps.size());
|
471 |
|
472 |
for (int i = 0; i < maps.size(); i++) { |
473 |
xml.addChild(((ProjectMap) maps.get(i)).getXMLEntity()); |
474 |
} |
475 |
|
476 |
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 |
|
485 |
for (int i = 0; i < tables.size(); i++) { |
486 |
xml.addChild(((ProjectTable) tables.get(i)).getXMLEntity()); |
487 |
} |
488 |
xml.putProperty("projection", defaultProjection.getAbrev());
|
489 |
|
490 |
return xml;
|
491 |
} |
492 |
|
493 |
/**
|
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 |
|
509 |
double[] xs = xml.getDoubleArrayProperty("extentsX"); |
510 |
double[] ys = xml.getDoubleArrayProperty("extentsY"); |
511 |
double[] ws = xml.getDoubleArrayProperty("extentsW"); |
512 |
double[] hs = xml.getDoubleArrayProperty("extentsH"); |
513 |
|
514 |
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 |
|
519 |
int numViews = xml.getIntProperty("numViews"); |
520 |
|
521 |
for (int i = 0; i < numViews; i++) { |
522 |
p.views.add(ProjectView.createFromXML(xml.getChild(i), p)); |
523 |
} |
524 |
|
525 |
int numMaps = xml.getIntProperty("numMaps"); |
526 |
|
527 |
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 |
String strProj = xml.getStringProperty("projection"); |
546 |
if (strProj != null) |
547 |
Project.setProjection(ProjectionPool.get(strProj)); |
548 |
|
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 |
public static IProjection getProjection() { |
599 |
return defaultProjection;
|
600 |
} |
601 |
public static void setProjection(IProjection defaultProjection) { |
602 |
Project.defaultProjection = defaultProjection; |
603 |
} |
604 |
} |