svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayers.java @ 885
History | View | Annotate | Download (11.7 KB)
1 |
package com.iver.cit.gvsig.fmap.layers; |
---|---|
2 |
|
3 |
import java.awt.Graphics2D; |
4 |
import java.awt.geom.Rectangle2D; |
5 |
import java.awt.image.BufferedImage; |
6 |
import java.io.File; |
7 |
import java.util.ArrayList; |
8 |
import java.util.BitSet; |
9 |
import java.util.Iterator; |
10 |
|
11 |
import org.cresques.cts.ICoordTrans; |
12 |
|
13 |
import com.hardcode.driverManager.DriverLoadException; |
14 |
import com.iver.cit.gvsig.fmap.DriverException; |
15 |
import com.iver.cit.gvsig.fmap.FMap; |
16 |
import com.iver.cit.gvsig.fmap.ViewPort; |
17 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
18 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection; |
19 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
20 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
21 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
22 |
import com.iver.utiles.XMLEntity; |
23 |
|
24 |
|
25 |
/**
|
26 |
* Representa un nodo interno en el ?rbol de capas
|
27 |
*/
|
28 |
public class FLayers extends FLyrDefault implements VectorialData, LayerCollection{ |
29 |
//ArrayList de Listener registrados.
|
30 |
private ArrayList layerListeners = new ArrayList(); |
31 |
private ArrayList layers = new ArrayList(4); |
32 |
private FMap fmap;
|
33 |
|
34 |
/**
|
35 |
* Crea un nuevo FLayers.
|
36 |
*/
|
37 |
public FLayers(FMap fmap, FLayers parent) {
|
38 |
setParentLayer(parent); |
39 |
this.fmap = fmap;
|
40 |
} |
41 |
|
42 |
/**
|
43 |
* A?ade un listener de los eventos de la colecci?n de capas
|
44 |
*
|
45 |
* @param listener DOCUMENT ME!
|
46 |
*/
|
47 |
public void addLayerCollectionListener(LayerCollectionListener listener) { |
48 |
layerListeners.add(listener); |
49 |
} |
50 |
|
51 |
public void setAllActives(boolean active){ |
52 |
FLayer lyr; |
53 |
for (int i = 0; i < layers.size(); i++) { |
54 |
lyr = ((FLayer) layers.get(i)); |
55 |
lyr.setActive(active); |
56 |
if (lyr instanceof LayerCollection){ |
57 |
((LayerCollection) lyr).setAllActives(active); |
58 |
} |
59 |
} |
60 |
} |
61 |
|
62 |
public void setAllVisibles(boolean visible){ |
63 |
FLayer lyr; |
64 |
for (int i = 0; i < layers.size(); i++) { |
65 |
lyr = ((FLayer) layers.get(i)); |
66 |
lyr.setVisible(visible); |
67 |
if (lyr instanceof LayerCollection){ |
68 |
((LayerCollection) lyr).setAllVisibles(visible); |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
/**
|
74 |
* ELimina un listener de los eventos de la colecci?n de capas
|
75 |
*
|
76 |
* @param listener DOCUMENT ME!
|
77 |
*/
|
78 |
public void removeLayerCollectionListener(LayerCollectionListener listener) { |
79 |
layerListeners.remove(listener); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* DOCUMENT ME!
|
84 |
*
|
85 |
* @param layer DOCUMENT ME!
|
86 |
*/
|
87 |
private void doAddLayer(FLayer layer) { |
88 |
layers.add(layer); |
89 |
layer.setParentLayer(this);
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* A?ade una capa al final de la lista de subcapas
|
94 |
*
|
95 |
* @param layer DOCUMENT ME!
|
96 |
*
|
97 |
* @throws CancelationException
|
98 |
*/
|
99 |
public void addLayer(FLayer layer) throws CancelationException { |
100 |
LayerCollectionEvent event = new LayerCollectionEvent(layer);
|
101 |
|
102 |
callLayerAdding(event); |
103 |
|
104 |
doAddLayer(layer); |
105 |
|
106 |
callLayerAdded(event); |
107 |
} |
108 |
|
109 |
/**
|
110 |
* Mueve una capa de la posici?n from a la posici?n to. (Se supone que
|
111 |
* est?n dentro del mismo nivel). Para mover entre niveles, usar otro
|
112 |
* m?todo (por hacer)
|
113 |
*
|
114 |
* @param from DOCUMENT ME!
|
115 |
* @param to DOCUMENT ME!
|
116 |
*
|
117 |
* @throws CancelationException
|
118 |
*/
|
119 |
public void moveTo(int from, int to) throws CancelationException { |
120 |
FLayer aux = (FLayer) layers.get(from); |
121 |
LayerPositionEvent event = new LayerPositionEvent(aux, from, to);
|
122 |
callLayerMoving(event); |
123 |
layers.remove(from); |
124 |
layers.add(to, aux); |
125 |
callLayerMoved(event); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* DOCUMENT ME!
|
130 |
*
|
131 |
* @param lyr DOCUMENT ME!
|
132 |
*/
|
133 |
private void doRemoveLayer(FLayer lyr) { |
134 |
layers.remove(lyr); |
135 |
} |
136 |
|
137 |
/**
|
138 |
* DOCUMENT ME!
|
139 |
*
|
140 |
* @param lyr DOCUMENT ME!
|
141 |
*
|
142 |
* @throws CancelationException DOCUMENT ME!
|
143 |
*/
|
144 |
public void removeLayer(FLayer lyr) throws CancelationException { |
145 |
LayerCollectionEvent event = new LayerCollectionEvent(lyr);
|
146 |
callLayerRemoving(event); |
147 |
doRemoveLayer(lyr); |
148 |
callLayerRemoved(event); |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Borra una FLayer del vector a partir del identificador.
|
153 |
*
|
154 |
* @param idLayer
|
155 |
*/
|
156 |
public void removeLayer(int idLayer) { |
157 |
layers.remove(idLayer); |
158 |
} |
159 |
|
160 |
/**
|
161 |
* M?todo de conveniencia para borrar una capa con ese nombre
|
162 |
*
|
163 |
* @param layerName
|
164 |
*/
|
165 |
public void removeLayer(String layerName) { |
166 |
FLayer lyr; |
167 |
|
168 |
for (int i = 0; i < layers.size(); i++) { |
169 |
lyr = ((FLayer) layers.get(i)); |
170 |
|
171 |
if (lyr.getName().compareToIgnoreCase(layerName) == 0) { |
172 |
layers.remove(i); |
173 |
|
174 |
break;
|
175 |
} |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Obtiene el array de capas visibles que penden del arbol cuya ra?z es
|
181 |
* este nodo
|
182 |
*
|
183 |
* @return DOCUMENT ME!
|
184 |
*/
|
185 |
public FLayer[] getVisibles() { |
186 |
ArrayList array = new ArrayList(); |
187 |
|
188 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
189 |
if (((FLayer) iter.next()).isVisible()) {
|
190 |
array.add((FLayer) iter.next()); |
191 |
} |
192 |
} |
193 |
|
194 |
return (FLayer[]) array.toArray(new FLayer[0]); |
195 |
} |
196 |
|
197 |
/**
|
198 |
* Obtiene el array de capas activas en el sub?rbol que pende de este nodo
|
199 |
*
|
200 |
* @return DOCUMENT ME!
|
201 |
*/
|
202 |
public FLayer[] getActives() { |
203 |
ArrayList ret = new ArrayList(); |
204 |
|
205 |
for (int i = 0; i < layers.size(); i++) { |
206 |
if (((FLayer) layers.get(i)).isActive()) {
|
207 |
ret.add(layers.get(i)); |
208 |
} |
209 |
} |
210 |
|
211 |
return (FLayer[]) ret.toArray(new FLayer[0]); |
212 |
} |
213 |
|
214 |
/**
|
215 |
* Obtiene el hijo i-?simo directo de esta colecci?n
|
216 |
*
|
217 |
* @param index DOCUMENT ME!
|
218 |
*
|
219 |
* @return DOCUMENT ME!
|
220 |
*/
|
221 |
public FLayer getLayer(int index) { |
222 |
return (FLayer) layers.get(index);
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* M?todo de conveniencia para buscar una capa por su nombre Es conveniente
|
227 |
* usar getLayer(int) siempre que se pueda, es m?s directo.
|
228 |
*
|
229 |
* @param layerName
|
230 |
*
|
231 |
* @return
|
232 |
*/
|
233 |
public FLayer getLayer(String layerName) { |
234 |
FLayer lyr; |
235 |
|
236 |
for (int i = 0; i < layers.size(); i++) { |
237 |
lyr = ((FLayer) layers.get(i)); |
238 |
|
239 |
if (lyr.getName().compareToIgnoreCase(layerName) == 0) { |
240 |
return lyr;
|
241 |
} |
242 |
} |
243 |
|
244 |
return null; |
245 |
} |
246 |
|
247 |
/**
|
248 |
* Obtiene el n?mero de capas que hay en su mismo nivel Es decir, no cuenta
|
249 |
* las subcapas de un FLayers
|
250 |
*
|
251 |
* @return DOCUMENT ME!
|
252 |
*/
|
253 |
public int getLayersCount() { |
254 |
return layers.size();
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* DOCUMENT ME!
|
259 |
*
|
260 |
* @param image DOCUMENT ME!
|
261 |
* @param g DOCUMENT ME!
|
262 |
* @param viewPort DOCUMENT ME!
|
263 |
* @param cancel
|
264 |
*
|
265 |
* @throws DriverLoadException
|
266 |
* @throws DriverException
|
267 |
*
|
268 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#draw(java.awt.image.BufferedImage,
|
269 |
* java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
|
270 |
*/
|
271 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
272 |
Cancellable cancel) |
273 |
throws DriverException {
|
274 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
275 |
FLayer lyr = (FLayer) iter.next(); |
276 |
|
277 |
if (lyr.isVisible()) {
|
278 |
lyr.draw(image, g, viewPort, cancel); |
279 |
} |
280 |
} |
281 |
if (getVirtualLayers() != null) |
282 |
getVirtualLayers().draw(image, g, viewPort, cancel); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable)
|
287 |
*/
|
288 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel) throws DriverException{ |
289 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
290 |
FLayer lyr = (FLayer) iter.next(); |
291 |
|
292 |
if (lyr.isVisible()) {
|
293 |
lyr.print(g, viewPort, cancel); |
294 |
} |
295 |
} |
296 |
} |
297 |
|
298 |
/**
|
299 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
|
300 |
*/
|
301 |
public Rectangle2D getFullExtent() throws DriverException { |
302 |
Rectangle2D rAux = null; |
303 |
int i = 0; |
304 |
|
305 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
306 |
FLayer capa = (FLayer) iter.next(); |
307 |
|
308 |
if (i == 0) { |
309 |
rAux = capa.getFullExtent(); |
310 |
} else {
|
311 |
rAux.add(capa.getFullExtent()); |
312 |
} |
313 |
|
314 |
i++; |
315 |
} |
316 |
|
317 |
return rAux;
|
318 |
} |
319 |
/**
|
320 |
* DOCUMENT ME!
|
321 |
*
|
322 |
* @param event DOCUMENT ME!
|
323 |
*
|
324 |
* @throws CancelationException
|
325 |
*/
|
326 |
private void callLayerAdding(LayerCollectionEvent event) |
327 |
throws CancelationException {
|
328 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
329 |
((LayerCollectionListener) iter.next()).layerAdding(event); |
330 |
} |
331 |
} |
332 |
|
333 |
/**
|
334 |
* DOCUMENT ME!
|
335 |
*
|
336 |
* @param event DOCUMENT ME!
|
337 |
*
|
338 |
* @throws CancelationException
|
339 |
*/
|
340 |
private void callLayerRemoving(LayerCollectionEvent event) |
341 |
throws CancelationException {
|
342 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
343 |
((LayerCollectionListener) iter.next()).layerRemoving(event); |
344 |
} |
345 |
} |
346 |
|
347 |
/**
|
348 |
* DOCUMENT ME!
|
349 |
*
|
350 |
* @param event DOCUMENT ME!
|
351 |
*
|
352 |
* @throws CancelationException
|
353 |
*/
|
354 |
private void callLayerMoving(LayerPositionEvent event) |
355 |
throws CancelationException {
|
356 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
357 |
((LayerCollectionListener) iter.next()).layerMoving(event); |
358 |
} |
359 |
} |
360 |
|
361 |
/**
|
362 |
* DOCUMENT ME!
|
363 |
*
|
364 |
* @param event DOCUMENT ME!
|
365 |
*/
|
366 |
private void callLayerAdded(LayerCollectionEvent event) { |
367 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
368 |
((LayerCollectionListener) iter.next()).layerAdded(event); |
369 |
} |
370 |
} |
371 |
|
372 |
/**
|
373 |
* DOCUMENT ME!
|
374 |
*
|
375 |
* @param event DOCUMENT ME!
|
376 |
*/
|
377 |
private void callLayerRemoved(LayerCollectionEvent event) { |
378 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
379 |
((LayerCollectionListener) iter.next()).layerRemoved(event); |
380 |
} |
381 |
} |
382 |
|
383 |
/**
|
384 |
* DOCUMENT ME!
|
385 |
*
|
386 |
* @param event DOCUMENT ME!
|
387 |
*/
|
388 |
private void callLayerMoved(LayerPositionEvent event) { |
389 |
for (Iterator iter = layerListeners.iterator(); iter.hasNext();) { |
390 |
((LayerCollectionListener) iter.next()).layerMoved(event); |
391 |
} |
392 |
} |
393 |
|
394 |
/**
|
395 |
* DOCUMENT ME!
|
396 |
*
|
397 |
* @return DOCUMENT ME!
|
398 |
*
|
399 |
* @throws DriverException
|
400 |
*
|
401 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
|
402 |
*/
|
403 |
public XMLEntity getXMLEntity() {
|
404 |
XMLEntity xml = super.getXMLEntity();
|
405 |
xml.putProperty("numLayers", layers.size());
|
406 |
String[] s=new String[layers.size()]; |
407 |
for (int i=0;i<layers.size();i++){ |
408 |
s[i]=((FLayer)layers.get(i)).getName(); |
409 |
} |
410 |
xml.putProperty("LayerNames",s);
|
411 |
for (int i = 0; i < layers.size(); i++) { |
412 |
xml.addChild(((FLayer) layers.get(i)).getXMLEntity()); |
413 |
} |
414 |
|
415 |
return xml;
|
416 |
} |
417 |
|
418 |
/**
|
419 |
* DOCUMENT ME!
|
420 |
*
|
421 |
* @param xml DOCUMENT ME!
|
422 |
*
|
423 |
* @throws XMLException
|
424 |
* @throws DriverException
|
425 |
*
|
426 |
* @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
|
427 |
*/
|
428 |
public void setXMLEntity(XMLEntity xml) throws XMLException, DriverException { |
429 |
super.setXMLEntity(xml);
|
430 |
|
431 |
int numLayers = xml.getIntProperty("numLayers"); |
432 |
String[] s=xml.getStringArrayProperty("LayerNames"); |
433 |
for (int i = 0; i < numLayers; i++) { |
434 |
if (xml.getStringProperty("file") != null) { |
435 |
layers.add(LayerFactory.createLayer(s[i], |
436 |
xml.getChild(i).getStringProperty("driverName"),
|
437 |
new File(xml.getChild(i).getStringProperty("name")), |
438 |
getProjection())); |
439 |
} else if (true) { |
440 |
} else if (true) { |
441 |
} |
442 |
} |
443 |
} |
444 |
|
445 |
/**
|
446 |
* @throws DriverIOException
|
447 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, java.util.BitSet)
|
448 |
*/
|
449 |
public void process(FeatureVisitor visitor, BitSet subset) throws DriverException { |
450 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
451 |
FLayer layer = (FLayer) iter.next(); |
452 |
|
453 |
if (layer instanceof VectorialData) { |
454 |
((VectorialData) layer).process(visitor, subset); |
455 |
} |
456 |
} |
457 |
} |
458 |
|
459 |
/**
|
460 |
* @throws DriverIOException
|
461 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
|
462 |
*/
|
463 |
public void process(FeatureVisitor visitor) throws DriverException { |
464 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
465 |
FLayer layer = (FLayer) iter.next(); |
466 |
if (layer.isActive())
|
467 |
if (layer instanceof VectorialData) { |
468 |
((VectorialData) layer).process(visitor); |
469 |
} |
470 |
} |
471 |
} |
472 |
|
473 |
/**
|
474 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFMap()
|
475 |
*/
|
476 |
public FMap getFMap() {
|
477 |
return fmap;
|
478 |
} |
479 |
|
480 |
/* (non-Javadoc)
|
481 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#setCoordTrans(org.cresques.cts.ICoordTrans)
|
482 |
*/
|
483 |
public void setCoordTrans(ICoordTrans ct) { |
484 |
super.setCoordTrans(ct);
|
485 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
486 |
FLayer layer = (FLayer) iter.next(); |
487 |
layer.setCoordTrans(ct); |
488 |
} |
489 |
} |
490 |
} |