Statistics
| Revision:

root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayers.java @ 20857

History | View | Annotate | Download (61.4 KB)

1 1100 fjp
/* 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 214 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 2608 caballero
import java.awt.Graphics2D;
44 4928 ldiaz
import java.awt.Point;
45 2608 caballero
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47 5923 fjp
import java.awt.image.WritableRaster;
48 2608 caballero
import java.io.File;
49
import java.util.ArrayList;
50 2857 jaume
import java.util.Collections;
51 2608 caballero
import java.util.Iterator;
52 2981 fjp
import java.util.List;
53 5057 ldiaz
import java.util.Vector;
54 2608 caballero
55 9013 caballero
import javax.print.attribute.PrintRequestAttributeSet;
56 5761 jmvivo
import javax.swing.ImageIcon;
57
58 3481 caballero
import org.apache.log4j.Logger;
59 2608 caballero
import org.cresques.cts.ICoordTrans;
60
import org.cresques.cts.IProjection;
61
62 1828 fernando
import com.hardcode.driverManager.Driver;
63
import com.hardcode.driverManager.DriverLoadException;
64 651 fernando
import com.iver.cit.gvsig.fmap.DriverException;
65 6878 cesar
import com.iver.cit.gvsig.fmap.MapContext;
66 5761 jmvivo
import com.iver.cit.gvsig.fmap.MapControl;
67 562 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
68 6164 luisw2
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
69 1453 luisw
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70 11928 caballero
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
71 3301 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
72 1828 fernando
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
73 11030 jmvivo
import com.iver.cit.gvsig.fmap.layers.layerOperations.ComposedLayer;
74 4928 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
75 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
77 5200 ldiaz
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
78 562 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
79 926 fernando
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
80 2659 fjp
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
81 3301 fjp
import com.iver.utiles.IPersistance;
82 562 fernando
import com.iver.utiles.XMLEntity;
83 6762 jmvivo
import com.iver.utiles.extensionPoints.ExtensionPoint;
84
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
85 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
86 541 vcaballero
87 20100 jmvivo
88 214 fernando
/**
89 20100 jmvivo
 * <p>Represents a generic collection of layers, that can be represented as a node in a tree of nodes of layers.</p>
90
 *
91 13116 ppiqueras
 * <p>Adapts the basic functionality implemented for a layer in the abstract class <code>FLyrDefault</code>, to
92
 *  a collection of layers, implementing, as well, specific methods for this kind of object, defined in the
93
 *  interfaces <code>VectorialData</code>, <code>LayerCollection</code>, and <code>InfoByPoint</code>.</p>
94 20100 jmvivo
 *
95 13370 ppiqueras
 * @see FLyrDefault
96 214 fernando
 */
97 4928 ldiaz
public class FLayers extends FLyrDefault implements VectorialData, LayerCollection, InfoByPoint
98
{
99 13059 ppiqueras
        /**
100
         * List with all listeners registered for this kind of node.
101 20100 jmvivo
         *
102 13370 ppiqueras
         * @see #addLayerCollectionListener(LayerCollectionListener)
103
         * @see #removeLayerCollectionListener(LayerCollectionListener)
104
         * @see #callLayerAdded(LayerCollectionEvent)
105
         * @see #callLayerAdding(LayerCollectionEvent)
106
         * @see #callLayerMoved(LayerPositionEvent)
107
         * @see #callLayerMoving(LayerPositionEvent)
108
         * @see #callLayerRemoved(LayerCollectionEvent)
109
         * @see #callLayerRemoving(LayerCollectionEvent)
110 13059 ppiqueras
         */
111 8765 jjdelcerro
        protected ArrayList layerCollectionListeners = new ArrayList();
112 3963 caballero
113 13059 ppiqueras
        /**
114
         * A synchronized list with the layers.
115 20100 jmvivo
         *
116 13370 ppiqueras
         * @see #setAllVisibles(boolean)
117
         * @see #addLayer(FLayer)
118
         * @see #addLayer(int, FLayer)
119
         * @see #moveTo(int, int)
120
         * @see #removeLayer(FLayer)
121
         * @see #removeLayer(int)
122
         * @see #removeLayer(String)
123
         * @see #replaceLayer(String, FLayer)
124
         * @see #getVisibles()
125
         * @see #getLayer(int)
126
         * @see #getLayer(String)
127
         * @see #getLayersCount()
128
         * @see #getFullExtent()
129 13059 ppiqueras
         */
130 8765 jjdelcerro
        protected List layers = Collections.synchronizedList(new ArrayList());
131 14593 vcaballero
132 13059 ppiqueras
        /**
133
         * The model of the layer.
134 14593 vcaballero
         *
135 13370 ppiqueras
         * @see #getMapContext()
136 13059 ppiqueras
         */
137 8765 jjdelcerro
        protected MapContext fmap;
138 14593 vcaballero
139 13059 ppiqueras
        /**
140
         * Useful for debug the problems during the implementation.
141
         */
142 8765 jjdelcerro
        protected static Logger logger = Logger.getLogger(FLayers.class.getName());
143 13059 ppiqueras
144 524 vcaballero
        /**
145 13059 ppiqueras
         * Default <code>FLayers</code> constructor.
146 1005 vcaballero
         *
147 20100 jmvivo
         * @param fmap reference to the <code>MapContext</code> instance that contains this node of layers
148 13059 ppiqueras
         * @param parent parent node of this one
149 524 vcaballero
         */
150 6878 cesar
        public FLayers(MapContext fmap, FLayers parent) {
151 694 fernando
                setParentLayer(parent);
152
                this.fmap = fmap;
153 524 vcaballero
        }
154 257 vcaballero
155 13059 ppiqueras
        /*
156
         * (non-Javadoc)
157
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
158 524 vcaballero
         */
159
        public void addLayerCollectionListener(LayerCollectionListener listener) {
160 5228 caballero
                if (!layerCollectionListeners.contains(listener))
161
                        layerCollectionListeners.add(listener);
162 524 vcaballero
        }
163 1005 vcaballero
164 13059 ppiqueras
        /*
165
         * (non-Javadoc)
166 1005 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
167
         */
168
        public void setAllVisibles(boolean visible) {
169 708 fernando
                FLayer lyr;
170 214 fernando
171 524 vcaballero
                for (int i = 0; i < layers.size(); i++) {
172
                        lyr = ((FLayer) layers.get(i));
173 708 fernando
                        lyr.setVisible(visible);
174 1005 vcaballero
175
                        if (lyr instanceof LayerCollection) {
176 708 fernando
                                ((LayerCollection) lyr).setAllVisibles(visible);
177
                        }
178 524 vcaballero
                }
179
        }
180 346 fernando
181 13059 ppiqueras
        /*
182
         * (non-Javadoc)
183
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
184 524 vcaballero
         */
185
        public void removeLayerCollectionListener(LayerCollectionListener listener) {
186 3772 fjp
                layerCollectionListeners.remove(listener);
187 524 vcaballero
        }
188 214 fernando
189 6556 caballero
//        private void doAddLayer(FLayer layer) {
190
//                layers.add(layer);
191
//                layer.setParentLayer(this);
192
//        }
193 13059 ppiqueras
194
        /**
195
         * Adds a layer on an specified position in this node.
196
         *
197
         * @param pos position in the inner list where the layer will be added
198
         * @param layer a layer
199
         */
200
        private void doAddLayer(int pos, FLayer layer) {
201 6556 caballero
                layers.add(pos,layer);
202 694 fernando
                layer.setParentLayer(this);
203 524 vcaballero
        }
204 14593 vcaballero
205 13059 ppiqueras
        /*
206
         * (non-Javadoc)
207
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
208
         */
209 6556 caballero
        public void addLayer(FLayer layer) {
210 13059 ppiqueras
                addLayer(layers.size(), layer);
211 6556 caballero
        }
212 13059 ppiqueras
213 524 vcaballero
        /**
214 13059 ppiqueras
         * Adds a layer in an specified position in this node.
215 524 vcaballero
         *
216 13059 ppiqueras
         * @param layer a layer
217 524 vcaballero
         *
218 13059 ppiqueras
         * @throws CancelationException any exception produced during the cancellation of the driver
219 524 vcaballero
         */
220 13059 ppiqueras
        public void addLayer(int pos, FLayer layer) throws CancelationException {
221 4376 nacho
                //Notificamos a la capa que va a ser a?adida
222 6762 jmvivo
                //FLyrDefault layerDef = (FLyrDefault)layer;
223 6537 jmvivo
                //if (!layerDef.isUnavailable()) {
224 6400 jmvivo
                        if (layer instanceof FLyrDefault)
225
                                ((FLyrDefault)layer).wakeUp();
226 6480 jaume
227 6400 jmvivo
                        if (layer instanceof FLayers){
228
                                FLayers layers=(FLayers)layer;
229
                                fmap.addAsCollectionListener(layers);
230
                        }
231
                        callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
232 6537 jmvivo
                //}
233 214 fernando
234 6556 caballero
                doAddLayer(pos,layer);
235 404 fjp
236 6537 jmvivo
                //if (!layerDef.isUnavailable()) {
237 6400 jmvivo
                        callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
238 6537 jmvivo
                //}
239 524 vcaballero
        }
240 404 fjp
241 13059 ppiqueras
        /*
242
         * (non-Javadoc)
243
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
244 524 vcaballero
         */
245
        public void moveTo(int from, int to) throws CancelationException {
246 2356 vcaballero
                int newfrom=layers.size()-from-1;
247
                int newto=layers.size()-to-1;
248 6466 jmvivo
                if ( newfrom < 0 || newfrom >=layers.size() || newto < 0 || newto >= layers.size()) return;
249 2356 vcaballero
                FLayer aux = (FLayer) layers.get(newfrom);
250
                callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
251
                layers.remove(newfrom);
252
                layers.add(newto, aux);
253
                callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
254 524 vcaballero
        }
255 257 vcaballero
256 524 vcaballero
        /**
257 13059 ppiqueras
         * Removes an inner layer.
258 524 vcaballero
         *
259 13059 ppiqueras
         * @param lyr a layer
260 524 vcaballero
         */
261
        private void doRemoveLayer(FLayer lyr) {
262 2198 vcaballero
                        layers.remove(lyr);
263 524 vcaballero
        }
264 214 fernando
265 13059 ppiqueras
        /*
266
         * (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
268 524 vcaballero
         */
269
        public void removeLayer(FLayer lyr) throws CancelationException {
270 1081 fernando
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
271 524 vcaballero
                doRemoveLayer(lyr);
272 1081 fernando
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
273 524 vcaballero
        }
274 214 fernando
275 13059 ppiqueras
        /*
276
         * (non-Javadoc)
277
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
278 524 vcaballero
         */
279
        public void removeLayer(int idLayer) {
280 1081 fernando
                FLayer lyr = (FLayer) layers.get(idLayer);
281
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
282 524 vcaballero
                layers.remove(idLayer);
283 1081 fernando
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
284 524 vcaballero
        }
285 257 vcaballero
286 13059 ppiqueras
        /*
287
         * (non-Javadoc)
288
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
289 524 vcaballero
         */
290
        public void removeLayer(String layerName) {
291
                FLayer lyr;
292 214 fernando
293 524 vcaballero
                for (int i = 0; i < layers.size(); i++) {
294
                        lyr = ((FLayer) layers.get(i));
295 402 fjp
296 524 vcaballero
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
297 1081 fernando
                                removeLayer(i);
298 214 fernando
299 524 vcaballero
                                break;
300
                        }
301
                }
302
        }
303 13059 ppiqueras
304 4945 ldiaz
        /**
305 13059 ppiqueras
         * Replace a layer identified by its name, by another.
306 20100 jmvivo
         *
307 13059 ppiqueras
         * @param layerName the name of the layer to be replaced
308
         * @param layer the new layer
309 4945 ldiaz
         */
310
        public void replaceLayer(String layerName, FLayer layer)
311
        {
312
                FLayer lyr;
313 5021 ldiaz
                FLayer parent;
314 4945 ldiaz
                for (int i = 0; i < layers.size(); i++) {
315
                        lyr = ((FLayer) layers.get(i));
316 214 fernando
317 4945 ldiaz
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
318 5228 caballero
                                parent = lyr.getParentLayer();
319 4945 ldiaz
                                removeLayer(i);
320 5021 ldiaz
                                if (parent != null)
321 4968 ldiaz
                                //Notificamos a la capa que va a ser a?adida
322
                                if (layer instanceof FLyrDefault)
323
                                        ((FLyrDefault)layer).wakeUp();
324 5228 caballero
325 4968 ldiaz
                                if (layer instanceof FLayers){
326
                                        FLayers layers=(FLayers)layer;
327
                                        fmap.addAsCollectionListener(layers);
328
                                }
329
                                callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
330
331 4945 ldiaz
                                layers.add(i,layer);
332 4968 ldiaz
                                layer.setParentLayer(this);
333
334
                                callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
335 4945 ldiaz
                                break;
336
                        }
337
                }
338
        }
339
340 13059 ppiqueras
        /*
341
         * (non-Javadoc)
342
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
343 524 vcaballero
         */
344
        public FLayer[] getVisibles() {
345
                ArrayList array = new ArrayList();
346 8765 jjdelcerro
                LayersIterator iter = new LayersIterator(this) {
347
                        public boolean evaluate(FLayer layer) {
348
                                return layer.isVisible();
349
                        }
350 9013 caballero
351 8765 jjdelcerro
                };
352 404 fjp
353 8765 jjdelcerro
                while (iter.hasNext()) {
354 9013 caballero
                        array.add(iter.nextLayer());
355 524 vcaballero
                }
356 404 fjp
357 524 vcaballero
                return (FLayer[]) array.toArray(new FLayer[0]);
358
        }
359 404 fjp
360 13059 ppiqueras
        /*
361
         * (non-Javadoc)
362
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
363 524 vcaballero
         */
364
        public FLayer getLayer(int index) {
365
                return (FLayer) layers.get(index);
366
        }
367 242 vcaballero
368 13059 ppiqueras
        /*
369
         * (non-Javadoc)
370
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
371 524 vcaballero
         */
372
        public FLayer getLayer(String layerName) {
373
                FLayer lyr;
374 3038 ldiaz
                FLayer lyr2;
375 3963 caballero
                ArrayList layerList;
376
377 524 vcaballero
                for (int i = 0; i < layers.size(); i++) {
378
                        lyr = ((FLayer) layers.get(i));
379 242 vcaballero
380 4638 ldiaz
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
381
                                        return lyr;
382
                        }
383 10656 caballero
384 3038 ldiaz
                        layerList = new ArrayList();
385 10583 ldiaz
                        getLayerFromGroup(lyr,layerList);
386 10656 caballero
387 3038 ldiaz
                        for(int j = 0; j<layerList.size(); j++ )
388
                        {
389
                                lyr2 = ((FLayer)layerList.get(j));
390
                                if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
391
                                        return lyr2;
392
                                }
393 524 vcaballero
                        }
394 10656 caballero
395 524 vcaballero
                }
396 346 fernando
397 524 vcaballero
                return null;
398
        }
399 13059 ppiqueras
400
        /**
401
         * <p>Looks in the layer tree including <code>FLayers</code></p>
402 20100 jmvivo
         *
403 13059 ppiqueras
         * @param layer the layer we are looking for
404
         * @param result an array list that will have the results of the search
405
         */
406
        private void getLayerFromGroup(FLayer layer, ArrayList result) {
407 10583 ldiaz
                int i;
408
                FLayers layerGroup;
409
                if (layer instanceof FLayers)
410
                {
411
                        result.add(layer);
412
                        layerGroup = (FLayers)layer;
413
                        for (i=0; i < layerGroup.getLayersCount(); i++ )
414
                        {
415
                                getLayerFromGroup(layerGroup.getLayer(i),result);
416
                        }
417
                }
418
                else
419
                {
420
                        result.add(layer);
421
                }
422
        }
423 10656 caballero
424 13059 ppiqueras
        /**
425
         * <p> Splits up a layer group in order to get a layer by name when there are layer groups</p>
426 20100 jmvivo
         *
427 13059 ppiqueras
         * <p>In <code>result</code> always will be at least one layer.</p>
428 20100 jmvivo
         *
429 13059 ppiqueras
         * @param layer the layer we are looking for
430
         * @param result an array list that will have the results of the search
431
         */
432 3038 ldiaz
        private void splitLayerGroup(FLayer layer, ArrayList result)
433
        {
434
                int i;
435
                FLayers layerGroup;
436
                if (layer instanceof FLayers)
437
                {
438
                        layerGroup = (FLayers)layer;
439
                        for (i=0; i < layerGroup.getLayersCount(); i++ )
440
                        {
441
                                splitLayerGroup(layerGroup.getLayer(i),result);
442
                        }
443
                }
444
                else
445
                {
446
                        result.add(layer);
447
                }
448
        }
449 346 fernando
450 13059 ppiqueras
        /*
451
         * (non-Javadoc)
452
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
453 524 vcaballero
         */
454
        public int getLayersCount() {
455
                return layers.size();
456
        }
457 346 fernando
458 13059 ppiqueras
        /*
459
         * (non-Javadoc)
460
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double)
461 524 vcaballero
         */
462
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
463 13059 ppiqueras
                        Cancellable cancel, double scale) throws DriverException {
464
                draw(image, g, viewPort, cancel, scale, null);
465 11125 jmvivo
        }
466 11194 caballero
467 13059 ppiqueras
        /**
468 13116 ppiqueras
         * <p>Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this collection and draws which are visible, and dirty (in cache) or aren't in cache. If a node is
469 20100 jmvivo
         *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
470
         *
471 13059 ppiqueras
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
472
         * group</code> is drawn, will be set to <code>null</code>.</p>
473 20100 jmvivo
         *
474 13059 ppiqueras
         * <p>The particular behavior depends on the kind of each layer and composed layer. And this process can be cancelled at any
475 20100 jmvivo
         *  time by the shared object <code>cancel</code>.</p>
476
         *
477 13481 ppiqueras
         * @param image buffer used sometimes instead <code>g</code> to accelerate the draw. For example, if two points are as closed that can't be distinguished, draws only one.
478
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
479
         * @param viewPort the information for drawing the layers
480 20100 jmvivo
         * @param cancel shared object that determines if this layer can continue being drawn
481 13481 ppiqueras
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
482 13059 ppiqueras
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
483
         *  will also be drawn
484 20100 jmvivo
         *
485
         * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
486
         *
487 13059 ppiqueras
         * @throws com.iver.cit.gvsig.fmap.DriverException if fails the driver used in this method.
488 20100 jmvivo
         *
489 13059 ppiqueras
         * @see ComposedLayer#draw(BufferedImage, Graphics2D, ViewPort, Cancellable, double)
490
         */
491 11125 jmvivo
        public ComposedLayer draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
492 13059 ppiqueras
                        Cancellable cancel, double scale, ComposedLayer group) throws DriverException {
493 2411 caballero
            //double scale = getFMap().getScaleView();
494 6611 caballero
//                Iterator iter = layers.iterator();
495
//        try
496
//        {
497 5923 fjp
                boolean bNeedRecalculateCache = false;
498 11030 jmvivo
499
                           ///// CHEMA ComposedLayer
500
                    long tg1 = System.currentTimeMillis();
501
                           ///// CHEMA ComposedLayer
502
503 11194 caballero
504
505 6611 caballero
//                    while (iter.hasNext())
506
//                    {
507
                     for (int i=0; i < layers.size(); i++) {
508 3372 fjp
                if (cancel.isCanceled())
509
                    break; // M?s que nada porque las capas raster no son interrumpibles por ahora.
510 6611 caballero
//                            FLayer lyr = (FLayer) iter.next();
511
                            FLayer lyr = (FLayer) layers.get(i);
512 11030 jmvivo
513 11194 caballero
514
515 3479 caballero
                    /*        if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken()){
516 3478 caballero
                                    continue;
517
                            }
518 3479 caballero
                    */
519 3772 fjp
                            LayerDrawEvent beforeEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
520
                            fmap.fireLayerDrawingEvent(beforeEvent);
521 10401 fjp
                        if ((lyr.isDirty()) && (lyr.isCachingDrawnLayers() == false))
522 6002 fjp
                                bNeedRecalculateCache = true;
523
524 2981 fjp
                            if (lyr.isVisible()) {
525 3963 caballero
526 2981 fjp
                                    long t1 = System.currentTimeMillis();
527 10656 caballero
                                    System.err.println("Layer " + lyr.getName() + " dirty=" + lyr.isDirty()+ " isCachingDrawnLayers= "+lyr.isCachingDrawnLayers());
528 3076 fjp
                                    // synchronized (this) {
529 3963 caballero
                                            //Sincronizaci?n del m?todo dibujar de cada Layer, esto es posible hacerlo de otra forma,
530 2981 fjp
                                            //pero de momento se queda as?, para solucionar el refresco de una misma vista que est? a?adida en el Layout.
531 3492 jaume
                                    try{
532 3685 fjp
                                            /* if (lyr instanceof IComposedDrawing)
533
                                            {
534
                                                    // Acumulamos las peticiones hasta que sea
535
                                                    // otro servidor o otro tipo de capa o la ?ltima
536
                                                    // Si hay que dibujar:
537
                                                    lyr.draw(image, g, viewPort, cancel,scale);
538
                                                    // Si hay que acumlar:
539
                                                    lyr.acumulateDrawing(antLayer);
540 3963 caballero

541 3685 fjp
                                            }
542
                                            else */
543 3963 caballero
544 6259 fjp
                                             if (lyr.isCachingDrawnLayers())
545 5923 fjp
                                            {
546 7368 jmvivo
                                                    if ((bNeedRecalculateCache) || (lyr.getCacheImageDrawnLayers()==null))
547 5923 fjp
                                                    {
548 7368 jmvivo
                                                    //if (bNeedRecalculateCache)
549 8765 jjdelcerro
                                                    // {
550 11030 jmvivo
551
                                                               ///// CHEMA ComposedLayer
552
                                                            if (group != null) {
553
                                                                    //si tenemos un grupo pendiente de pintar, pintamos
554
                                                                    // para que la cache sea fiable
555
                                                                    group.draw(image, g, viewPort, cancel,scale);
556
                                                                    group = null;
557
                                                            }
558
                                                               ///// CHEMA ComposedLayer
559
560 11194 caballero
561 5923 fjp
                                                            // Copiamos la imagen actual
562
                                                            BufferedImage buff = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
563 6480 jaume
                                                            WritableRaster w = buff.getRaster();
564 5923 fjp
                                                            image.copyData(w);
565
                                                            lyr.setCacheImageDrawnLayers(buff);
566
                                                            System.err.println("RECALCULO LA CACHE CON LO QUE HABIA ANTES DE " + lyr.getName());
567
                                                    }
568
//                                                    }
569
//                                                    else
570
//                                                    {
571
                                                            if (lyr.getCacheImageDrawnLayers() != null)
572
                                                            {
573
                                                                    // Previo a esto hemos tenido que fijar
574 6480 jaume
                                                                    // la imagen que queremos usar, en el
575 5923 fjp
                                                                    // prepareDrawing de FMap.
576
                                                                    if (lyr.isDirty())
577
                                                                    {
578 11194 caballero
579
580 11030 jmvivo
                                                                               ///// CHEMA ComposedLayer
581
                                                                            // Checks for draw group (ComposedLayer)
582
                                                                        if (group != null) {
583
                                                                                // it's going to load a cache image,
584
                                                                                // the current draw isn't needed
585
                                                                                group = null;
586
                                                                        }
587
                                                                           ///// CHEMA ComposedLayer
588
589 11194 caballero
590 5923 fjp
                                                                            g.drawImage(lyr.getCacheImageDrawnLayers(), 0, 0, null);
591
                                                                            System.err.println("Pinto con acelerado lo que hab?a antes de " + lyr.getName());
592
                                                                    }
593
                                                            }
594
//                                                    }
595 6480 jaume
                                            }
596 5923 fjp
                                            // Si la capa est? "sucia" o alguna de las de abajo est? sucia
597
                                            // hay que volver a dibujar.
598
                                                   if (lyr.isDirty() || bNeedRecalculateCache)
599 11194 caballero
                                                   {
600 11030 jmvivo
                                                           if (!lyr.getFLayerStatus().isDriverLoaded())
601 10638 fjp
                                                           {
602 11030 jmvivo
                                                                   continue;
603
                                                           }
604 11194 caballero
605 11030 jmvivo
                                                           ///// CHEMA ComposedLayer
606
                                                           //lyr.draw(image, g, viewPort, cancel,scale);
607
                                                           //bNeedRecalculateCache = true;
608
                                                           //System.err.println("Pinto sin acelerado " + lyr.getName());
609
                                                           ///// CHEMA ComposedLayer
610 11194 caballero
611 11030 jmvivo
                                                           ///// CHEMA ComposedLayer
612
                                                           // Checks for draw group (ComposedLayer)
613 11194 caballero
                                                           if (group != null) {
614 11125 jmvivo
                                                                   if (lyr instanceof FLayers){
615
                                                                           group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
616
                                                                           LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
617
                                                                           fmap.fireLayerDrawingEvent(afterEvent);
618 11030 jmvivo
                                                                   } else {
619 11125 jmvivo
                                                                           // If layer can be added to the group, does it
620
                                                                           if (group.canAdd(lyr)) {
621
                                                                                   group.add(lyr);
622
                                                                           } else {
623
                                                                                   // draw the 'pending to draw' layer group
624
                                                                                   group.draw(image, g, viewPort, cancel,scale);
625 11194 caballero
626 11125 jmvivo
                                                                                   // gets a new group instance
627 11194 caballero
                                                                                   group = lyr.newComposedLayer();
628 13201 ppiqueras
                                                                                   // if layer hasn't group, draws it immediately
629 11125 jmvivo
                                                                                   if (group == null) {
630
                                                                                           if (lyr instanceof FLayers){
631
                                                                                                   group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
632
                                                                                           } else {
633
                                                                                                   lyr.draw(image, g, viewPort, cancel,scale);
634
                                                                                           }
635
                                                                                           LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
636 11194 caballero
                                                                                           fmap.fireLayerDrawingEvent(afterEvent);
637 11125 jmvivo
                                                                                   } else {
638
                                                                                           // add the layer to the group
639
                                                                                           group.setMapContext(fmap);
640
                                                                                           group.add(lyr);
641 11194 caballero
642 11125 jmvivo
                                                                                   }
643
                                                                           }
644 11030 jmvivo
                                                                   }
645
                                                           } else {
646
                                                                   // gets a new group instance
647
                                                                   group = lyr.newComposedLayer();
648 11194 caballero
                                                                   // if layer hasn't group, draws it inmediately
649 11030 jmvivo
                                                                   if (group == null) {
650 11125 jmvivo
                                                                           if (lyr instanceof FLayers){
651
                                                                                   group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
652
                                                                           } else {
653
                                                                                   lyr.draw(image, g, viewPort, cancel,scale);
654
                                                                           }
655
                                                                           LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
656 11194 caballero
                                                                           fmap.fireLayerDrawingEvent(afterEvent);
657 11030 jmvivo
                                                                   } else {
658
                                                                           // adds the layer to the group
659 11125 jmvivo
                                                                           group.setMapContext(fmap);
660 11030 jmvivo
                                                                           group.add(lyr);
661
                                                                   }
662 10638 fjp
                                                           }
663 11030 jmvivo
                                                           ///// CHEMA ComposedLayer
664
                                                                bNeedRecalculateCache = true;
665 5923 fjp
                                                   }
666 3492 jaume
                                    } catch (DriverException e){
667 3941 fjp
                                            // fmap.callNewErrorEvent(new ErrorEvent(lyr.getName(), e));
668 5258 jmvivo
                                            fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
669 4181 fjp
                                            e.printStackTrace();
670 6537 jmvivo
                                            lyr.setAvailable(false);
671 4181 fjp
                                            // this.removeLayer(lyr);
672 6537 jmvivo
                                    } catch (Exception e) {
673
                                            fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
674
                                            e.printStackTrace();
675
                                            lyr.setAvailable(false);
676 3492 jaume
                                    }
677 3076 fjp
                                            // }
678 2981 fjp
                                            long t2 = System.currentTimeMillis();
679
                                            System.out.println("Layer " + lyr.getName() + " "
680 3963 caballero
                                                    + (t2-t1) + " milisecs.");
681 11034 caballero
                                            if (!cancel.isCanceled())
682
                                                    lyr.setDirty(false);
683 2981 fjp
                            }
684 3772 fjp
685 2981 fjp
                    }
686 11194 caballero
                    ///// CHEMA ComposedLayer
687 11125 jmvivo
                    if (group != null && this.getParentLayer() == null) {
688 11194 caballero
                                 //si tenemos un grupo pendiente de pintar, pintamos
689 11030 jmvivo
                                 group.draw(image, g, viewPort, cancel,scale);
690 11125 jmvivo
                                 group = null;
691 11030 jmvivo
692
                         }
693 11194 caballero
                        ///// CHEMA ComposedLayer
694 2981 fjp
                    if (getVirtualLayers() != null) {
695
                            getVirtualLayers().draw(image, g, viewPort, cancel,scale);
696
                    }
697 11194 caballero
698 11125 jmvivo
                    ///// CHEMA ComposedLayer
699
                    return group;
700
                    ///// CHEMA ComposedLayer
701 11194 caballero
702 6611 caballero
//        }
703
//        catch (ConcurrentModificationException e)
704
//        {
705
//            System.err.println(e.getMessage());
706
//        }
707 524 vcaballero
        }
708 11194 caballero
709 524 vcaballero
        /**
710 13116 ppiqueras
         * <p>Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this collection and draws their requested properties. If a node is
711 20100 jmvivo
         *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
712
         *
713 13059 ppiqueras
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
714
         * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.</p>
715 20100 jmvivo
         *
716
         * <p>The particular implementation depends on the kind of each layer and composed layer. And this process can be cancelled at any
717
         *  time by the shared object <code>cancel</code>.</p>
718
         *
719 13481 ppiqueras
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
720
         * @param viewPort the information for drawing the layers
721 20100 jmvivo
         * @param cancel shared object that determines if this layer can continue being drawn
722 13481 ppiqueras
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
723 13116 ppiqueras
         * @param properties properties that will be print
724 13059 ppiqueras
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
725 20100 jmvivo
         *
726
         * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
727
         *
728 13059 ppiqueras
         * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintRequestAttributeSet)
729 20100 jmvivo
         *
730 13059 ppiqueras
         * @throws com.iver.cit.gvsig.fmap.DriverException if fails the driver used in this method.
731 524 vcaballero
         */
732 12348 jmvivo
        public ComposedLayer print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties, ComposedLayer group)
733 1005 vcaballero
                throws DriverException {
734 3963 caballero
735 2510 fjp
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
736
        // calculando la escala en pantalla, no para el layout.
737
        // Revisar esto.
738 9361 fdiaz
                for (int i=0; i < layers.size(); i++) {
739
                        FLayer lyr = (FLayer) layers.get(i);
740 3963 caballero
741 12348 jmvivo
                        try{
742
743
                                ///// CHEMA ComposedLayer
744
                                // Checks for draw group (ComposedLayer)
745
                                if (group != null) {
746
                                        if (lyr instanceof FLayers){
747
                                                group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
748
                                        } else {
749
                                                // If layer can be added to the group, does it
750
                                                if (group.canAdd(lyr)){
751
                                                        group.add(lyr);
752
                                                } else {
753
                                                        // draw the 'pending to draw' layer group
754
                                                        group.print(g,viewPort,cancel,scale,properties);
755
756
                                                        // gets a new group instance
757
                                                        group = lyr.newComposedLayer();
758 13059 ppiqueras
                                                        // if layer hasn't group, draws it immediately
759 12348 jmvivo
                                                        if (group == null) {
760
                                                                if (lyr instanceof FLayers){
761
                                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
762
                                                                } else {
763
                                                                        lyr.print(g, viewPort, cancel,scale,properties);
764
                                                                }
765
                                                        } else {
766
                                                                // add the layer to the group
767
                                                                group.setMapContext(fmap);
768
                                                                group.add(lyr);
769
770
                                                        }
771
772
                                                }
773
                                        }
774
                                } else {
775
                                        // gets a new group instance
776
                                        group = lyr.newComposedLayer();
777
                                        // if layer hasn't group, draws it inmediately
778
                                        if (group == null) {
779
                                                if (lyr instanceof FLayers){
780
                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
781
                                                } else {
782
                                                        lyr.print(g, viewPort, cancel,scale,properties);
783
                                                }
784
                                        } else {
785
                                                // add the layer to the group
786
                                                group.setMapContext(fmap);
787
                                                group.add(lyr);
788
789
                                        }
790
                                }
791
                                ///// CHEMA ComposedLayer
792
793
                        } catch (Exception e){
794
                                fmap.addLayerError("Error en la impresion de la capa " + lyr.getName() + ": " + e.getMessage());
795
                                e.printStackTrace();
796
                        }
797
798 2531 caballero
                }
799 12348 jmvivo
800
                ///// CHEMA ComposedLayer
801
                if (group != null && this.getParentLayer() == null) {
802
                                //si tenemos un grupo pendiente de pintar, pintamos
803
                                group.print(g, viewPort, cancel,scale,properties);
804
                                group = null;
805
806
                }
807
                ///// CHEMA ComposedLayer
808
809
                if (getVirtualLayers() != null) {
810
                        getVirtualLayers().print( g, viewPort, cancel, scale, properties);
811
                }
812
813
                ///// CHEMA ComposedLayer
814
                return group;
815
                ///// CHEMA ComposedLayer
816
817 524 vcaballero
        }
818
819 13059 ppiqueras
        /*
820
         * (non-Javadoc)
821
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double, javax.print.attribute.PrintRequestAttributeSet)
822
         */
823 12348 jmvivo
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties)
824
        throws DriverException {
825
                this.print(g, viewPort, cancel, scale, properties, null);
826
        }
827 13059 ppiqueras
828
        /*
829
         * (non-Javadoc)
830 524 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
831
         */
832 651 fernando
        public Rectangle2D getFullExtent() throws DriverException {
833 524 vcaballero
                Rectangle2D rAux = null;
834 3478 caballero
                boolean first = true;
835 524 vcaballero
836 3963 caballero
837 524 vcaballero
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
838 533 vcaballero
                        FLayer capa = (FLayer) iter.next();
839 3478 caballero
                        try{
840
                        if (first) {
841 524 vcaballero
                                rAux = capa.getFullExtent();
842 3478 caballero
                                first=false;
843 524 vcaballero
                        } else {
844
                                rAux.add(capa.getFullExtent());
845
                        }
846 3478 caballero
                        }catch (Exception e) {
847
                                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
848
                        }
849 524 vcaballero
                }
850
851
                return rAux;
852
        }
853 1005 vcaballero
854 524 vcaballero
        /**
855 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
856
         *  that another layer is going to be added or replaced in the internal
857
         *  list of layers.
858 20100 jmvivo
         *
859 13059 ppiqueras
         * @param e a layer collection event with the new layer
860 524 vcaballero
         */
861 8765 jjdelcerro
        protected void callLayerAdding(LayerCollectionEvent event)
862 524 vcaballero
                throws CancelationException {
863 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
864 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
865 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerAdding(event);
866
                }
867
        }
868 257 vcaballero
869 524 vcaballero
        /**
870 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
871
         *  that a layer is going to be removed from the internal list of layers.
872 524 vcaballero
         *
873 13059 ppiqueras
         * @param event a layer collection event with the layer being removed
874 524 vcaballero
         *
875 13059 ppiqueras
         * @throws CancelationException any exception produced during the cancellation of the driver.
876 524 vcaballero
         */
877 8765 jjdelcerro
        protected void callLayerRemoving(LayerCollectionEvent event)
878 524 vcaballero
                throws CancelationException {
879 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
880 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
881 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerRemoving(event);
882
                }
883
        }
884 257 vcaballero
885 524 vcaballero
        /**
886 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
887
         *  that a layer is going to be moved in the internal list of layers.
888 524 vcaballero
         *
889 13059 ppiqueras
         * @param event a layer collection event with the layer being moved, and the initial and final positions
890 524 vcaballero
         *
891 13059 ppiqueras
         * @throws CancelationException any exception produced during the cancellation of the driver.
892 524 vcaballero
         */
893 8765 jjdelcerro
        protected void callLayerMoving(LayerPositionEvent event)
894 524 vcaballero
                throws CancelationException {
895 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
896 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
897 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerMoving(event);
898
                }
899
        }
900 353 fernando
901 524 vcaballero
        /**
902 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
903
         *  that another layer has been added or replaced in the internal
904
         *  list of layers.
905 20100 jmvivo
         *
906 13059 ppiqueras
         * @param e a layer collection event with the new layer
907 524 vcaballero
         */
908 8765 jjdelcerro
        protected void callLayerAdded(LayerCollectionEvent event) {
909 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
910 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
911 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerAdded(event);
912
                }
913
        }
914 353 fernando
915 524 vcaballero
        /**
916 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
917
         *  that another layer has been removed from the internal list of layers.
918 20100 jmvivo
         *
919 13059 ppiqueras
         * @param e a layer collection event with the layer removed
920 20100 jmvivo
         */
921 8765 jjdelcerro
        protected void callLayerRemoved(LayerCollectionEvent event) {
922 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
923 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
924 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerRemoved(event);
925
                }
926
        }
927 353 fernando
928 435 vcaballero
        /**
929 13303 ppiqueras
         * Notifies all listeners associated to this collection of layers,
930
         *  that another layer has been moved in the internal list of layers.
931 20100 jmvivo
         *
932 13059 ppiqueras
         * @param e a layer collection event with the layer moved, and the initial and final positions
933 524 vcaballero
         */
934 8765 jjdelcerro
        protected void callLayerMoved(LayerPositionEvent event) {
935 3772 fjp
            ArrayList aux = (ArrayList) layerCollectionListeners.clone();
936 2284 jmorell
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
937 524 vcaballero
                        ((LayerCollectionListener) iter.next()).layerMoved(event);
938
                }
939
        }
940
941
        /**
942 20100 jmvivo
         * <p>Returns an entity that represents this collection of layers stored as a tree-node with children that are also layers.</p>
943
         *
944
         * <p>The root node has the same properties that <code>FlyrDefault#getXMLEntity()</code> returns, and adds:
945 13059 ppiqueras
         *  <ul>
946
         *          <li> <i>numLayers</i> : number of layers of this collection (direct children of this node)
947
         *   <li> <i>LayerNames</i> : an array list with the name of the layers of this collection (direct children of this node)
948
         *    <code>FLayer.getXMLEntity()</code>
949
         *  </ul>
950
         * </p>
951 20100 jmvivo
         *
952
         * <p>All XML elements returned represent the information about this layer.</p>
953 14593 vcaballero
         *
954 13370 ppiqueras
         * @return an XML entity with information to this collection of layers
955 13481 ppiqueras
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is any error creating the XML from the layers.
956 435 vcaballero
         */
957 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
958 524 vcaballero
                XMLEntity xml = super.getXMLEntity();
959 12431 jmvivo
960 11272 jmvivo
                ArrayList names = new ArrayList();
961 1005 vcaballero
962 12431 jmvivo
963
                FLayer layer;
964 1005 vcaballero
                for (int i = 0; i < layers.size(); i++) {
965 11272 jmvivo
                        layer = (FLayer)layers.get(i);
966
                        // XXX: Parche para no guardar capas raster de memoria en el proyecto
967
                        if (layer instanceof FLyrRaster){
968
                                if (((FLyrRaster)layer).getSource() instanceof RasterFileAdapter){
969
                                        if (((RasterFileAdapter)((FLyrRaster)layer).getSource()).getFile() == null){
970
                                                continue;
971
                                        }
972
                                }
973
                        }
974
            // XXX: Parche para no guardar capas raster de memoria en el proyecto
975 11101 caballero
                        try{
976 11272 jmvivo
                                names.add(layer.getName());
977
                                xml.addChild(layer.getXMLEntity());
978 11101 caballero
                        }catch (XMLException e) {
979
                                e.printStackTrace();
980
                        }
981 435 vcaballero
                }
982 524 vcaballero
983 12431 jmvivo
                String[] s = new String[names.size()];
984
                System.arraycopy(names.toArray(),0,s,0,names.size());
985 11272 jmvivo
                xml.putProperty("numLayers", names.size());
986
                xml.putProperty("LayerNames", s);
987 12431 jmvivo
988 435 vcaballero
                return xml;
989
        }
990 341 fernando
991 435 vcaballero
        /**
992 13370 ppiqueras
         * <p>Inserts layers and their properties to this collection, from an XML entity. Also adds properties to the collection of layers (root node).
993 20100 jmvivo
         *
994
         * <p>The parameter <code>xml</code> must have a tree-node structure, the root must have at least two properties, and each (first-level)
995 13059 ppiqueras
         *  child can be a raster layer <i>(<code>FLyrRaster</code>)</i>, a vectorial layer <i>(<code>FLyrVect</code>)</i>,
996
         *  another collection of layers <i>(<code>FLayers</code>)</i>, or another kind of layer <i>(<code>FLayer</code>)</i> .</p>
997 20100 jmvivo
         *
998 13059 ppiqueras
         * <p> <b>Root node properties:</b>
999
         *  <ul>
1000
         *   <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1001
         *   <li> numLayers : number of layers
1002
         *   <li> LayerNames : name of the layers
1003
         *  </ul>
1004
         * </p>
1005 20100 jmvivo
         *
1006 13059 ppiqueras
         * <p> <b>Layers: each first-level child: </b>
1007 20100 jmvivo
         *  <ul>
1008 13059 ppiqueras
         *   <li> className : name of the class
1009 20100 jmvivo
         *   <li> <b> Capa Raster: </b>
1010 13059 ppiqueras
         *   <ul>
1011
         *    <li> name : name of the layer
1012
         *    <li> proj : the projection of this layer (only if it's defined)
1013
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1014
         *   </ul>
1015
         *   <li> <b> Capa Vectorial: </b>
1016
         *   <ul>
1017
         *    <li> file : the projection of this layer (only if it's defined)
1018
         *    <li> driverName : name of the driver used to access to the file
1019
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1020
         *   </ul>
1021
         *   <li> <b> Collection of layers: </b>
1022
         *   <ul>
1023
         *    <li> that node
1024
         *   </ul>
1025
         *   <li> <b> Another kind of layer: </b>
1026
         *   <ul>
1027
         *    <li> name : the name of the layer
1028
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1029
         *   </ul>
1030
         *  </ul>
1031
         * </p>
1032 1056 vcaballero
         *
1033 13059 ppiqueras
         * @see FLyrDefault#getXMLEntity03()
1034 1056 vcaballero
         *
1035 13059 ppiqueras
         * @param xml an <code>XMLEntity</code> with the information
1036 1056 vcaballero
         *
1037 13059 ppiqueras
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error obtaining the object.
1038 435 vcaballero
         */
1039 3481 caballero
        public void setXMLEntity03(XMLEntity xml) throws XMLException{
1040 2183 fernando
                super.setXMLEntity03(xml);
1041
                int numLayers = xml.getIntProperty("numLayers");
1042
1043
                String[] s = xml.getStringArrayProperty("LayerNames");
1044
                try {
1045
                        for (int i = 0; i < numLayers; i++) {
1046
                                FLayer layer = null;
1047 3963 caballero
1048 2183 fernando
                                String className = xml.getChild(i).getStringProperty("className");
1049 3963 caballero
1050 2183 fernando
                                if (className.equals(FLyrVect.class.getName())) {
1051
                                        if (xml.getChild(i).contains("file")) {
1052 3963 caballero
1053 2183 fernando
                                                        layer = LayerFactory.createLayer(s[i],
1054
                                                                        (VectorialFileDriver)LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName")),
1055
                                                                        new File(xml.getChild(i).getStringProperty("file")),
1056 6881 cesar
                                                                        this.getMapContext().getViewPort().getProjection());
1057 3963 caballero
1058 2183 fernando
                                        } else if (true) {
1059
                                                //TODO falta por implementar
1060
                                        } else if (true) {
1061
                                                //TODO falta por implementar
1062
                                        }
1063 3963 caballero
1064 2183 fernando
                                        layer.setXMLEntity03(xml.getChild(i));
1065
                                        // Comprobar que la proyecci?n es la misma que la de FMap
1066 3963 caballero
                                        // Si no lo es, es una capa que est? reproyectada al vuelo
1067 2183 fernando
                                        IProjection proj = layer.getProjection();
1068
                                        if (proj != null)
1069
                                            if (proj != fmap.getProjection())
1070
                                            {
1071 6164 luisw2
                                                        ICoordTrans ct = proj.getCT(fmap.getProjection());
1072 2183 fernando
                                                        layer.setCoordTrans(ct);
1073
                                                        System.err.println("coordTrans = " +
1074
                                                                proj.getAbrev() + " " +
1075
                                                                fmap.getProjection().getAbrev());
1076
                                            }
1077 3963 caballero
1078 3174 ldiaz
//                                } else if (className.equals(FLyrWMS.class.getName())) {
1079
//                                        try {
1080
//                                                layer = LayerFactory.createLayer(s[i],
1081
//                                                                StringUtilities.string2Rect(
1082
//                                                                        xml.getChild(i).getStringProperty("fullExtent")),
1083
//                                                                new URL(xml.getChild(i).getStringProperty("host")),
1084
//                                                                xml.getChild(i).getStringProperty("format"),
1085
//                                                                xml.getChild(i).getStringProperty("layerQuery"),
1086
//                                                                xml.getChild(i).getStringProperty("infoLayerQuery"),
1087
//                                                                xml.getChild(i).getStringProperty("srs"));
1088
//                                        } catch (MalformedURLException e) {
1089
//                                                throw new XMLException(e);
1090
//                                        }
1091 2183 fernando
                                } else if (className.equals((FLyrRaster.class.getName()))) {
1092
                                        //if (xml.getChild(i).contains("file")) {
1093
                                        /*layer = LayerFactory.createLayer(s[i],
1094
                                                        xml.getChild(i).getStringProperty("driverName"),
1095
                                                        new File(xml.getChild(i).getStringProperty("file")),
1096
                                                        this.getFMap().getViewPort().getProjection());*/
1097
                                        layer = new FLyrRaster();
1098
                                        layer.setName(s[i]);
1099
                                        if (!xml.getChild(i).contains("proj"))
1100 6881 cesar
                                                ((FLyrRaster)layer).setProjection(this.getMapContext().getViewPort().getProjection());
1101 2183 fernando
                                        layer.setXMLEntity03(xml.getChild(i));
1102
                                        try {
1103 3431 caballero
                                                        layer.load();
1104 3963 caballero
1105 2183 fernando
                                        } catch (DriverIOException e) {
1106
                                                throw new XMLException(e);
1107
                                        }
1108 3963 caballero
1109 2234 vcaballero
                                }else if (className.equals((FLayers.class.getName()))) {
1110 6881 cesar
                                        layer = new FLayers(getMapContext(),this);
1111 2234 vcaballero
                                        layer.setXMLEntity(xml.getChild(i));
1112 2183 fernando
                                } else {
1113
                                        // Capas Nuevas (externas)
1114
                                        try {
1115
                                                Class clase = Class.forName(className);
1116
                                                layer = (FLayer) clase.newInstance();
1117
                                                layer.setName(s[i]);
1118
                                                layer.setXMLEntity03(xml.getChild(i));
1119
                                                try {
1120 3431 caballero
                                                                layer.load();
1121 2183 fernando
                                                } catch (DriverIOException e) {
1122
                                                        throw new XMLException(e);
1123
                                                }
1124
                                        } catch (Exception e) {
1125
                                                //e.printStackTrace();
1126
                                                throw new XMLException(e);
1127
                                        }
1128
                                }
1129 3963 caballero
1130 2234 vcaballero
                                this.addLayer(layer);
1131 2183 fernando
                        }
1132 8765 jjdelcerro
                } /*azabala-modificaciones en layerfactory-
1133
                catch (DriverException e) {
1134 2183 fernando
                        throw new XMLException(e);
1135 8765 jjdelcerro
                }*/
1136
                catch (DriverLoadException e) {
1137 2183 fernando
            throw new XMLException(e);
1138
        }
1139 3963 caballero
1140 2183 fernando
        }
1141
1142
        /**
1143 13059 ppiqueras
         * <p>Inserts layers and properties to this collection of layers.</p>
1144 20100 jmvivo
         *
1145
         * <p>This root node has the same properties that return <code>FlyrDefault#getXMLEntity()</code> adding:
1146 13059 ppiqueras
         *  <ul>
1147 20100 jmvivo
         *          <li> <i>numLayers</i> : number of first-level layers of this collection
1148
         *   <li> <i>LayerNames</i> : an array list with the name of the first-level layers of this collection
1149 13059 ppiqueras
         *    <code>FLayer.getXMLEntity()</code>
1150
         *  </ul>
1151
         * </p>
1152 2183 fernando
         *
1153 13059 ppiqueras
         * @see FLyrDefault#setXMLEntity()
1154
         * @see FLyrDefault#getXMLEntity()
1155
         * @see FLayers#addLayerFromXML(XMLEntity, String)
1156 2183 fernando
         *
1157 13059 ppiqueras
         * @param xml an <code>XMLEntity</code> with the information
1158 2183 fernando
         *
1159 13059 ppiqueras
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error setting the object.
1160 2183 fernando
         */
1161 13059 ppiqueras
        public void setXMLEntity(XMLEntity xml) throws XMLException {
1162 442 vcaballero
                super.setXMLEntity(xml);
1163 3481 caballero
                //LoadLayerException loadLayerException=new LoadLayerException();
1164 894 vcaballero
                int numLayers = xml.getIntProperty("numLayers");
1165 524 vcaballero
1166 1005 vcaballero
                String[] s = xml.getStringArrayProperty("LayerNames");
1167 3304 jmorell
                // try {
1168 3492 jaume
                fmap.clearErrors();
1169 1060 fjp
                        for (int i = 0; i < numLayers; i++) {
1170 6437 jmvivo
                                this.addLayerFromXML(xml.getChild(i),s[i]);
1171 3963 caballero
1172
1173 894 vcaballero
                        }
1174 3304 jmorell
                /* } catch (DriverException e) {
1175 1060 fjp
                        throw new XMLException(e);
1176 3304 jmorell
                } */
1177 3481 caballero
                /*if (loadLayerException.getExceptions().size()>0){
1178
                        throw loadLayerException;
1179
                }*/
1180 435 vcaballero
        }
1181 562 fernando
1182 13059 ppiqueras
        /*
1183
         * (non-Javadoc)
1184
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, com.iver.cit.gvsig.fmap.layers.FBitSet)
1185 562 fernando
         */
1186 2969 fjp
        public void process(FeatureVisitor visitor, FBitSet subset)
1187 1005 vcaballero
                throws DriverException, VisitException {
1188 562 fernando
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1189
                        FLayer layer = (FLayer) iter.next();
1190 1005 vcaballero
1191 562 fernando
                        if (layer instanceof VectorialData) {
1192
                                ((VectorialData) layer).process(visitor, subset);
1193
                        }
1194
                }
1195
        }
1196
1197 13059 ppiqueras
        /*
1198
         * (non-Javadoc)
1199 562 fernando
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
1200
         */
1201 1005 vcaballero
        public void process(FeatureVisitor visitor)
1202
                throws DriverException, VisitException {
1203 562 fernando
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1204
                        FLayer layer = (FLayer) iter.next();
1205 1005 vcaballero
1206 9474 fdiaz
                        if (layer instanceof FLayers){
1207
                                FLayers lyrs=(FLayers)layer;
1208
                                for (int i=0;i<lyrs.getLayersCount();i++){
1209
                                        FLayer lyr=lyrs.getLayer(i);
1210
                                        if (lyr.isActive()) {
1211
                                                if (lyr instanceof VectorialData) {
1212
                                                        ((VectorialData) lyr).process(visitor);
1213
                                                }
1214
                                        }
1215
                                }
1216
                        }
1217 1005 vcaballero
                        if (layer.isActive()) {
1218 661 fjp
                                if (layer instanceof VectorialData) {
1219
                                        ((VectorialData) layer).process(visitor);
1220
                                }
1221 1005 vcaballero
                        }
1222 562 fernando
                }
1223
        }
1224 13059 ppiqueras
1225
        /*
1226
         * (non-Javadoc)
1227
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, java.awt.geom.Rectangle2D)
1228 4200 azabala
         */
1229
        public void process(FeatureVisitor visitor, Rectangle2D rect) throws DriverException, VisitException {
1230
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1231
                        FLayer layer = (FLayer) iter.next();
1232 694 fernando
1233 4200 azabala
                        if (layer.isActive()) {
1234
                                if (layer instanceof VectorialData) {
1235
                                        ((VectorialData) layer).process(visitor, rect);
1236
                                }
1237
                        }
1238
                }
1239 5228 caballero
1240 4200 azabala
        }
1241
1242 13059 ppiqueras
        /*
1243
         * (non-Javadoc)
1244
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
1245 694 fernando
         */
1246 6881 cesar
        public MapContext getMapContext() {
1247 694 fernando
                return fmap;
1248
        }
1249 885 fjp
1250 13059 ppiqueras
        /*
1251
         * (non-Javadoc)
1252
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setCoordTrans(org.cresques.cts.ICoordTrans)
1253 885 fjp
         */
1254
        public void setCoordTrans(ICoordTrans ct) {
1255
                super.setCoordTrans(ct);
1256 1005 vcaballero
1257 885 fjp
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1258
                        FLayer layer = (FLayer) iter.next();
1259
                        layer.setCoordTrans(ct);
1260 1005 vcaballero
                }
1261 885 fjp
        }
1262 1005 vcaballero
1263 13059 ppiqueras
        /*
1264
         * (non-Javadoc)
1265 1005 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
1266
         */
1267
        public void setAllActives(boolean active) {
1268
                FLayer lyr;
1269
1270
                for (int i = 0; i < layers.size(); i++) {
1271
                        lyr = ((FLayer) layers.get(i));
1272
                        lyr.setActive(active);
1273
1274
                        if (lyr instanceof LayerCollection) {
1275
                                ((LayerCollection) lyr).setAllActives(active);
1276
                        }
1277
                }
1278
        }
1279
1280 13059 ppiqueras
        /*
1281
         * (non-Javadoc)
1282 1005 vcaballero
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
1283
         */
1284
        public FLayer[] getActives() {
1285
                ArrayList ret = new ArrayList();
1286 8765 jjdelcerro
                LayersIterator it = new LayersIterator(this) {
1287 1005 vcaballero
1288 9013 caballero
                        public boolean evaluate(FLayer layer) {
1289 8765 jjdelcerro
                                return layer.isActive();
1290 1005 vcaballero
                        }
1291 9013 caballero
1292 8765 jjdelcerro
                };
1293 9013 caballero
1294 8765 jjdelcerro
                while (it.hasNext())
1295
                {
1296
                        ret.add(it.next());
1297 1005 vcaballero
                }
1298
                return (FLayer[]) ret.toArray(new FLayer[0]);
1299
        }
1300 1176 fjp
1301 13059 ppiqueras
        /*
1302
         * (non-Javadoc)
1303
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMinScale()
1304
         */
1305 1176 fjp
    public double getMinScale() {
1306
        return -1; // La visibilidad o no la controla cada capa
1307
                // dentro de una colecci?n
1308
    }
1309
1310 13059 ppiqueras
    /*
1311
     * (non-Javadoc)
1312
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMaxScale()
1313 1176 fjp
     */
1314
    public double getMaxScale() {
1315
        return -1;
1316
    }
1317 13059 ppiqueras
1318
    /*
1319
     * (non-Javadoc)
1320
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMinScale(double)
1321
     */
1322 1176 fjp
    public void setMinScale(double minScale)
1323
    {
1324
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1325
                        FLayer lyr = (FLayer) iter.next();
1326
                        lyr.setMinScale(minScale);
1327
                }
1328
    }
1329 13059 ppiqueras
1330
    /*
1331
     * (non-Javadoc)
1332
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMaxScale(double)
1333
     */
1334 1176 fjp
    public void setMaxScale(double maxScale)
1335
    {
1336
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1337
                        FLayer lyr = (FLayer) iter.next();
1338
                        lyr.setMinScale(maxScale);
1339
                }
1340
    }
1341 13059 ppiqueras
1342
    /*
1343
     * (non-Javadoc)
1344
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setActive(boolean)
1345
     */
1346 2411 caballero
    public void setActive(boolean b){
1347
            super.setActive(b);
1348
            for (int i=0;i<layers.size();i++){
1349
                    ((FLayer)layers.get(i)).setActive(b);
1350
            }
1351
    }
1352 3963 caballero
1353 4147 fjp
        /* (non-Javadoc)
1354
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
1355
         */
1356
        public boolean addLayerListener(LayerListener o) {
1357 5228 caballero
                for (int i = 0; i < layers.size(); i++)
1358 4147 fjp
                        ((FLayer) layers.get(i)).addLayerListener(o);
1359
                return true;
1360
        }
1361
1362 13059 ppiqueras
        /*
1363
         * (non-Javadoc)
1364
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo(java.awt.Point, double, com.iver.utiles.swing.threads.Cancellable)
1365
         */
1366 8765 jjdelcerro
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException {
1367 4928 ldiaz
                int i;
1368 5057 ldiaz
                Vector items = new Vector();
1369
                FLayer layer;
1370
                XMLItem[] aux;
1371 4928 ldiaz
                for (i = 0; i < this.layers.size(); i++){
1372 5057 ldiaz
                        layer = (FLayer)layers.get(i);
1373
                        if (layer instanceof InfoByPoint){
1374
                                InfoByPoint queryable_layer = (InfoByPoint) layer;
1375 8765 jjdelcerro
                                aux = queryable_layer.getInfo(p, tolerance, null);
1376
                                if (!(queryable_layer instanceof FLayers)){
1377 9013 caballero
                                        for(int j = 0; j < aux.length; j++){
1378 8765 jjdelcerro
                                                items.add(aux[j]);
1379
                                        }
1380 4928 ldiaz
                                }
1381 5228 caballero
                        }
1382 5057 ldiaz
                }
1383 13059 ppiqueras
1384 5057 ldiaz
                return (XMLItem[])items.toArray(new XMLItem[0]);
1385 5228 caballero
1386 5057 ldiaz
//                for (i = 0; i < this.layers.size(); i++){
1387
//                        FLayer laCapa = (FLayer) layers.get(i);
1388
//                        if (laCapa instanceof FLyrVect){
1389
//                        }
1390
//                        else if (laCapa instanceof RasterOperations) {
1391
//                                try {
1392
//                                        RasterOperations layer = (RasterOperations) laCapa;
1393
//                                        sb.append(layer.getInfo(p, tolerance));
1394
//                                } catch (DriverException e) {
1395 5228 caballero
//                                        e.printStackTrace();
1396
//                                }
1397 5057 ldiaz
//                        }
1398
//                        else if (laCapa instanceof InfoByPoint) {
1399
//                                try {
1400
//                                        InfoByPoint layer = (InfoByPoint) laCapa;
1401
//                                        sb.append(layer.getInfo(p, tolerance));
1402
//                                } catch (DriverException e) {
1403 5228 caballero
//                                        e.printStackTrace();
1404 5057 ldiaz
//                                }
1405
//                        }
1406 5228 caballero
//                }
1407 5057 ldiaz
1408 4928 ldiaz
        }
1409
1410 13059 ppiqueras
        /*
1411
         * (non-Javadoc)
1412
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
1413
         */
1414 6480 jaume
        public ImageIcon getTocImageIcon() {
1415 5761 jmvivo
                return new ImageIcon(MapControl.class.getResource("images/layerGroup.png"));
1416
        }
1417 6480 jaume
1418 13059 ppiqueras
        /*
1419
         * (non-Javadoc)
1420
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#isDirty()
1421
         */
1422 5923 fjp
        public boolean isDirty() {
1423
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1424
                        FLayer lyr = (FLayer) iter.next();
1425
                        if (lyr.isDirty())
1426
                                return true;
1427
                }
1428
                return false;
1429 6480 jaume
        }
1430 4200 azabala
1431 13059 ppiqueras
        /*
1432
         * (non-Javadoc)
1433
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setDirty(boolean)
1434
         */
1435 5923 fjp
        public void setDirty(boolean dirty) {
1436
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1437
                        FLayer lyr = (FLayer) iter.next();
1438
                        lyr.setDirty(dirty);
1439
                }
1440
1441
        }
1442 6480 jaume
1443 13059 ppiqueras
        /**
1444 20100 jmvivo
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
1445
         *
1446
         * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
1447
         *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
1448
         *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
1449
         *
1450 13059 ppiqueras
         * @param xml tree-node structure with information about layers
1451
         * @param name name of the layer to add
1452
         */
1453 6437 jmvivo
        public boolean addLayerFromXMLEntity(XMLEntity xml, String name) {
1454
                fmap.clearErrors();
1455 13059 ppiqueras
                this.addLayerFromXML(xml, name);
1456 5923 fjp
1457 6480 jaume
                return (fmap.getLayersError().size() == 0);
1458
1459 6437 jmvivo
        }
1460 5923 fjp
1461 13059 ppiqueras
        /**
1462 13370 ppiqueras
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
1463 20100 jmvivo
         *
1464 13059 ppiqueras
         * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
1465
         *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
1466
         *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
1467 20100 jmvivo
         *
1468 13059 ppiqueras
         * @param xml tree-node structure with information about layers
1469
         * @param name name of the layer to add
1470
         */
1471 6437 jmvivo
        private void addLayerFromXML(XMLEntity xml, String name) {
1472 6480 jaume
                FLayer layer = null;
1473 9287 fjp
                long t1 = System.currentTimeMillis();
1474 6437 jmvivo
                try {
1475
                if (name == null) name = xml.getName();
1476
1477
1478
                String className = xml.getStringProperty("className");
1479 10935 caballero
                //TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el c?digo de aq? y de los diferentes m?todos de LayerFactory,
1480
                //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1481
                if (className.equals(FLyrVect.class.getName()) || className.equals(FLyrAnnotation.class.getName())) {
1482 6437 jmvivo
                        String type = xml.getStringProperty("type");
1483
                        if ("vectorial".equals(type)){
1484
                                //String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
1485
                IProjection proj = null;
1486
                if (xml.contains("proj")) {
1487
                    proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
1488
                }
1489
                else
1490
                {
1491 6881 cesar
                    proj = this.getMapContext().getViewPort().getProjection();
1492 6437 jmvivo
                }
1493
                                if (xml.contains("file")) {
1494
                                        Driver d;
1495
                                        try {
1496
                                                d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
1497
                                        } catch (DriverLoadException e1) {
1498
                                                throw new XMLException(e1);
1499
                                        }
1500
                                        layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
1501
                                                                                                        new File(xml.getStringProperty("file")),
1502
                                                                                                        proj);
1503
1504
1505
                                }
1506
                if (xml.contains("db")) {
1507
1508
                    String driverName = xml.getStringProperty("db");
1509 11928 caballero
                    IVectorialDatabaseDriver driver;
1510 6437 jmvivo
                    try {
1511 11928 caballero
                        driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
1512 6437 jmvivo
                        //Hay que separar la carga de los datos del XMLEntity del load.
1513
                        driver.setXMLEntity(xml.getChild(2));
1514 6556 caballero
1515 6537 jmvivo
                        boolean loadOk = false;
1516 11928 caballero
//                        if (driver instanceof DefaultJDBCDriver) {
1517 11194 caballero
                                                        try {
1518 11928 caballero
                                                                ((IVectorialDatabaseDriver) driver).load();
1519
                                                                if (((IVectorialDatabaseDriver) driver)
1520 11194 caballero
                                                                                .getConnection() != null) {
1521
                                                                        loadOk = true;
1522
                                                                }
1523
                                                        } catch (DriverException e) {
1524
                                                                e.printStackTrace();
1525
                                                                loadOk = false;
1526
                                                        }
1527 11928 caballero
//                                                } else {
1528
//                                                        try{
1529
//                                                                ((IVectorialDatabaseDriver)driver).load();
1530
//                                                                loadOk=true;
1531
//                                                        }catch (Exception e) {
1532
//                                                                loadOk=false;
1533
//                                                        }
1534
//                                                }
1535 6556 caballero
1536 6437 jmvivo
                        layer = LayerFactory.createDBLayer(driver, name, proj);
1537 6537 jmvivo
                        if (!loadOk) {
1538
                                layer.setAvailable(false);
1539
                        }
1540 6480 jaume
1541 6437 jmvivo
                    } catch (DriverLoadException e) {
1542
                        e.printStackTrace();
1543
                        throw new XMLException(e);
1544
                    }
1545
1546
                }
1547
                // Clases con algun driver gen?rico creado por otro
1548
                // programador
1549
                if (xml.contains("other")) {
1550
1551
                    String driverName = xml.getStringProperty("other");
1552
                    VectorialDriver driver = null;
1553
                    try {
1554
                        driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
1555
                    } catch (DriverLoadException e) {
1556
                        // Si no existe ese driver, no pasa nada.
1557
                        // Puede que el desarrollador no quiera que
1558
                        // aparezca en el cuadro de di?logo y ha metido
1559
                        // el jar con sus clases en nuestro directorio lib.
1560
                        // Intentamos cargar esa clase "a pelo".
1561
                        if (xml.getChild(2).contains("className"))
1562
                        {
1563
                            String className2 = xml.getChild(2).getStringProperty("className");
1564
                            try {
1565
                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1566
                            } catch (Exception e1) {
1567
                                throw new XMLException(e1);
1568
                            }
1569
                        }
1570
                    } catch (NullPointerException npe) {
1571
                        // Si no existe ese driver, no pasa nada.
1572
                        // Puede que el desarrollador no quiera que
1573
                        // aparezca en el cuadro de di?logo y ha metido
1574
                        // el jar con sus clases en nuestro directorio lib.
1575
                        // Intentamos cargar esa clase "a pelo".
1576
                        if (xml.getChild(2).contains("className"))
1577
                        {
1578
                            String className2 = xml.getChild(2).getStringProperty("className");
1579
                            try {
1580
                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1581
                            } catch (Exception e1) {
1582
                                throw new XMLException(e1);
1583
                            }
1584
                        }
1585
                    }
1586
                    if (driver instanceof IPersistance)
1587
                    {
1588
                        IPersistance persist = (IPersistance) driver;
1589
                        persist.setXMLEntity(xml.getChild(2));
1590
                    }
1591
                    layer = LayerFactory.createLayer(name, driver, proj);
1592
                }
1593
1594 10935 caballero
                        }
1595 6437 jmvivo
1596 10935 caballero
                        //TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el c?digo de aq? y de los diferentes m?todos de LayerFactory,
1597
                        //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1598
                        if (className.equals(FLyrAnnotation.class.getName())){
1599
                                layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
1600
                        }
1601 6437 jmvivo
1602
1603
                        layer.setXMLEntity(xml);
1604
1605
//                } else if (className.equals(FLyrWMS.class.getName())) {
1606
//                        try {
1607
//                                layer = LayerFactory.createLayer(s[i],
1608
//                                                StringUtilities.string2Rect(
1609
//                                                        xml.getChild(i).getStringProperty("fullExtent")),
1610
//                                                new URL(xml.getChild(i).getStringProperty("host")),
1611
//                                                xml.getChild(i).getStringProperty("format"),
1612
//                                                xml.getChild(i).getStringProperty("layerQuery"),
1613
//                                                xml.getChild(i).getStringProperty("infoLayerQuery"),
1614
//                                                xml.getChild(i).getStringProperty("srs"));
1615
//                                layer.setXMLEntity(xml.getChild(i));
1616
//                        } catch (MalformedURLException e) {
1617
//                                throw new XMLException(e);
1618
//                        }
1619
                } else if (className.equals((FLyrRaster.class.getName()))) {
1620
                        //if (xml.getChild(i).contains("file")) {
1621
                        /*layer = LayerFactory.createLayer(s[i],
1622
                                        xml.getChild(i).getStringProperty("driverName"),
1623
                                        new File(xml.getChild(i).getStringProperty("file")),
1624
                                        this.getFMap().getViewPort().getProjection());*/
1625 12431 jmvivo
                        Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1626
                        layer = (FLayer) clase.newInstance();
1627 6437 jmvivo
                        layer.setName(name);
1628
                        if (!xml.contains("proj"))
1629 6881 cesar
                                ((FLyrRaster)layer).setProjection(this.getMapContext().getViewPort().getProjection());
1630 6437 jmvivo
                        layer.setXMLEntity(xml);
1631
                        try {
1632
                                layer.load();
1633
                        } catch (DriverIOException e) {
1634
                                throw new XMLException(e);
1635
                        }
1636
1637
                }else if (className.equals((FLayers.class.getName()))) {
1638 6881 cesar
                        layer = new FLayers(getMapContext(),this);
1639 6437 jmvivo
                        layer.setXMLEntity(xml);
1640
                } else {
1641
                        // Capas Nuevas (externas)
1642 12431 jmvivo
                                Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1643 6437 jmvivo
                                layer = (FLayer) clase.newInstance();
1644
                                layer.setName(name);
1645
                                layer.setXMLEntity(xml);
1646
                                try {
1647
                                        layer.load();
1648
                                }catch (DriverIOException e) {
1649
                                        throw new XMLException(e);
1650
                                }
1651
1652
                }
1653
1654
                this.addLayer(layer);
1655 9287 fjp
                long t2 = System.currentTimeMillis();
1656
                logger.debug("layer: "+ layer.getName() +" loaded. " + (t2-t1) + " msecs.");
1657 6437 jmvivo
                // Comprobar que la proyecci?n es la misma que la de FMap
1658
        // Si no lo es, es una capa que est? reproyectada al vuelo
1659
        IProjection proj = layer.getProjection();
1660
        if ((proj != null))
1661 14593 vcaballero
            if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode()))
1662 6437 jmvivo
            {
1663 6881 cesar
                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1664 6437 jmvivo
                // TODO: REVISAR CON LUIS
1665
                // Se lo fijamos a todas, luego cada una que se reproyecte
1666
                // si puede, o que no haga nada
1667
                layer.setCoordTrans(ct);
1668
                // NOTA: DESPU?S DE ESTO DEBER?AMOS CREAR LAS ETIQUETAS
1669
                // SI ES NECESARIO.
1670
                // TODO: REVISAR A FONDO ESTO, PORQUE LAS ETIQUETAS SE CREAN
1671
                // 2 VECES SI HAY REPROYECC?N
1672
                if (layer instanceof FLyrVect)
1673
                {
1674
                    FLyrVect lyrVect = (FLyrVect)layer;
1675
                    VectorialLegend legend = (VectorialLegend) lyrVect.getLegend();
1676
                    if (legend.getLabelField() != null) {
1677
                        lyrVect.createLabelLayer(lyrVect.getRecordset());
1678
                    }
1679
1680
                }
1681
            }
1682
        }catch (Exception e) {
1683
                        fmap.addLayerError(xml.getStringProperty("name"));
1684
                                //loadLayerException.addException(e);
1685
                        /*if (layer!=null){
1686
                layer.setActive(false);
1687
                        layer.setVisible(false);
1688
                        this.addLayer(layer);
1689
                }*/
1690
                        //throw new XMLException(e);
1691
            System.err.println("No se ha podido cargar la capa : "+xml.getStringProperty("name"));
1692
            logger.debug("No se ha podido cargar la capa : "+xml.getStringProperty("name") + ". Raz?n:");
1693
            // logger.debug(e.fillInStackTrace());
1694
            e.printStackTrace();
1695 6480 jaume
        }
1696 6437 jmvivo
        }
1697 8765 jjdelcerro
1698 13059 ppiqueras
        /**
1699 13370 ppiqueras
         * <p>Creates a new layer of the same class as the property <i>className</i> of the XML, after, adds the XML entity to that layer
1700 20100 jmvivo
         *  and loads the layer. Then, adds the layer to this collection of layers, and if there is a projection defined,
1701 13059 ppiqueras
         *  inserts the transformation coordinates to the layer.</p>
1702 20100 jmvivo
         *
1703 13059 ppiqueras
         * <p>If the new layer is an instance of <code>FLyrVect</code>, and has a label field, creates a label layer on the layer.</p>
1704 20100 jmvivo
         *
1705 13059 ppiqueras
         * @param xml tree-node structure with information about layers
1706
         * @param name name of the layer to add
1707
         */
1708 6762 jmvivo
        private void addLayerFromXMLNew(XMLEntity xml, String name) {
1709
                FLayer layer = null;
1710 6437 jmvivo
1711 8765 jjdelcerro
1712 6762 jmvivo
                try {
1713
                String className = xml.getStringProperty("className");
1714
1715
                if (className.equals((FLayers.class.getName()))){
1716 8765 jjdelcerro
                        layer = new FLayers(getMapContext(),this);
1717 6762 jmvivo
                } else {
1718
//                         Por compatibilidad
1719
                        if (className.equals(FLyrVect.class.getName())) {
1720
                                if (xml.contains("file")) {
1721 8765 jjdelcerro
                                        layer = new FLayerFileVectorial();
1722 6762 jmvivo
                                } else if (xml.contains("db")) {
1723
                                        try {
1724 8765 jjdelcerro
                                                layer = (FLayer)((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create("com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial");
1725 6762 jmvivo
                                        } catch (Exception e) {
1726
                                                throw new XMLException(new Exception("No se tiene registrada la capa de tipo JDBC"));
1727 8765 jjdelcerro
                                        }
1728
                                        //className = FLayerJDBCVectorial.class.getName();
1729 6762 jmvivo
                                } else if (xml.contains("other")){
1730 8765 jjdelcerro
                                        layer = new FLayerGenericVectorial();
1731 6762 jmvivo
                                } else {
1732
                                        throw new XMLException(new Exception("Capa vectorial de tipo no reconocido"));
1733
                                }
1734 8765 jjdelcerro
//                                Fin por compatibilidad
1735 6762 jmvivo
                        } else {
1736
                                try {
1737
                                        layer = (FLayer)(((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create(className));
1738
                                } catch (Exception e) {
1739 8765 jjdelcerro
                                        //puende que no este registrada como punto de extension
1740 6762 jmvivo
                                        Class clase = Class.forName(className);
1741
                                        layer = (FLayer) clase.newInstance();
1742
                                        // FIXME: Hacemos algo aqui o dejamos que suba el error?
1743
                                }
1744
                        }
1745 8765 jjdelcerro
1746
                }
1747 6762 jmvivo
                layer.setXMLEntity(xml);
1748
                if (name != null) layer.setName(name);
1749
                try {
1750
                        layer.load();
1751
                }catch (DriverIOException e) {
1752
                        throw new XMLException(e);
1753
                }
1754
1755
                this.addLayer(layer);
1756
                logger.debug("layer: "+ layer.getName() +" loaded");
1757
                // Comprobar que la proyecci?n es la misma que la de FMap
1758
        // Si no lo es, es una capa que est? reproyectada al vuelo
1759
        IProjection proj = layer.getProjection();
1760
        if ((proj != null))
1761 6881 cesar
            if (proj != getMapContext().getProjection())
1762 6762 jmvivo
            {
1763 6881 cesar
                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1764 6762 jmvivo
                // TODO: REVISAR CON LUIS
1765
                // Se lo fijamos a todas, luego cada una que se reproyecte
1766
                // si puede, o que no haga nada
1767
                layer.setCoordTrans(ct);
1768
                // NOTA: DESPU?S DE ESTO DEBER?AMOS CREAR LAS ETIQUETAS
1769
                // SI ES NECESARIO.
1770
                // TODO: REVISAR A FONDO ESTO, PORQUE LAS ETIQUETAS SE CREAN
1771
                // 2 VECES SI HAY REPROYECC?N
1772
                if (layer instanceof FLyrVect)
1773
                {
1774
                    FLyrVect lyrVect = (FLyrVect)layer;
1775
                    VectorialLegend legend = (VectorialLegend) lyrVect.getLegend();
1776
                    if (legend.getLabelField() != null) {
1777
                        lyrVect.createLabelLayer(lyrVect.getRecordset());
1778
                    }
1779
1780
                }
1781
            }
1782
        }catch (Exception e) {
1783
                        fmap.addLayerError(xml.getStringProperty("name"));
1784
                                //loadLayerException.addException(e);
1785
                        /*if (layer!=null){
1786
                layer.setActive(false);
1787
                        layer.setVisible(false);
1788
                        this.addLayer(layer);
1789
                }*/
1790
                        //throw new XMLException(e);
1791
            System.err.println("No se ha podido cargar la capa : "+xml.getStringProperty("name"));
1792
            logger.debug("No se ha podido cargar la capa : "+xml.getStringProperty("name") + ". Raz?n:");
1793
            // logger.debug(e.fillInStackTrace());
1794
            e.printStackTrace();
1795
        }
1796
        }
1797
1798 10048 fjp
        /**
1799 13059 ppiqueras
         * <p>Calls recursively to its layer children to tell them that the layer is going
1800
         * to be removed from the view. Overwrites <code>FLyrDefault.removingThisLayer()</code>.</p>
1801 20100 jmvivo
         *
1802 13059 ppiqueras
         * @see FLyrDefault#removingThisLayer()
1803 10048 fjp
         */
1804
        public void removingThisLayer() {
1805
1806
                for (int i=0; i < layers.size(); i++) {
1807
                        FLayer lyr = (FLayer) layers.get(i);
1808
                        if (lyr instanceof FLyrDefault) {
1809
                                ((FLyrDefault) lyr).removingThisLayer();
1810
                        }
1811
                }
1812
        }
1813 214 fernando
}