Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayers.java @ 19509

History | View | Annotate | Download (54.8 KB)

1
/* 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
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.Point;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47
import java.awt.image.WritableRaster;
48
import java.io.File;
49
import java.util.ArrayList;
50
import java.util.Collections;
51
import java.util.Iterator;
52
import java.util.List;
53
import java.util.Vector;
54

    
55
import javax.print.attribute.PrintRequestAttributeSet;
56
import javax.swing.ImageIcon;
57

    
58
import org.apache.log4j.Logger;
59
import org.cresques.cts.ICoordTrans;
60
import org.cresques.cts.IProjection;
61

    
62
import com.hardcode.driverManager.Driver;
63
import com.hardcode.driverManager.DriverLoadException;
64
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
65
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
66
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
67
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
68
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
69
import com.iver.cit.gvsig.fmap.MapContext;
70
import com.iver.cit.gvsig.fmap.MapControl;
71
import com.iver.cit.gvsig.fmap.Messages;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.core.ILabelable;
74
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
75
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver;
76
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
77
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
78
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.ComposedLayer;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
81
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
82
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
83
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
84
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
85
import com.iver.utiles.IPersistence;
86
import com.iver.utiles.XMLEntity;
87
import com.iver.utiles.extensionPoints.ExtensionPoint;
88
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
89
import com.iver.utiles.swing.threads.Cancellable;
90

    
91
/**
92
 * <p>Represents a generic collection of <a href="http://www.gvsig.gva.es/">gvSIG</a> 's layers, that can be represented
93
 *  as a node in a tree of nodes of layers.</p>
94
 *
95
 * <p>Adapts the basic functionality implemented for a layer in the abstract class <code>FLyrDefault</code>, to
96
 *  a collection of layers, implementing, as well, specific methods for this kind of object, defined in the
97
 *  interfaces <code>VectorialData</code>, <code>LayerCollection</code>, and <code>InfoByPoint</code>.</p>
98
 *
99
 * @see FLyrDefault
100
 */
101
public class FLayers extends FLyrDefault implements VectorialData, LayerCollection, InfoByPoint
102
{
103
        /**
104
         * List with all listeners registered for this kind of node.
105
         *
106
         * @see #addLayerCollectionListener(LayerCollectionListener)
107
         * @see #removeLayerCollectionListener(LayerCollectionListener)
108
         * @see #callLayerAdded(LayerCollectionEvent)
109
         * @see #callLayerAdding(LayerCollectionEvent)
110
         * @see #callLayerMoved(LayerPositionEvent)
111
         * @see #callLayerMoving(LayerPositionEvent)
112
         * @see #callLayerRemoved(LayerCollectionEvent)
113
         * @see #callLayerRemoving(LayerCollectionEvent)
114
         */
115
        protected ArrayList layerCollectionListeners = new ArrayList();
116
        /**
117
         * A synchronized list with the layers.
118
         *
119
         * @see #setAllVisibles(boolean)
120
         * @see #addLayer(FLayer)
121
         * @see #addLayer(int, FLayer)
122
         * @see #moveTo(int, int)
123
         * @see #removeLayer(FLayer)
124
         * @see #removeLayer(int)
125
         * @see #removeLayer(String)
126
         * @see #replaceLayer(String, FLayer)
127
         * @see #getVisibles()
128
         * @see #getLayer(int)
129
         * @see #getLayer(String)
130
         * @see #getLayersCount()
131
         * @see #getFullExtent()
132
         */
133
        protected List layers = Collections.synchronizedList(new ArrayList());
134

    
135
        /**
136
         * The model of the layer.
137
         *
138
         * @see #getMapContext()
139
         */
140
        protected MapContext fmap;
141

    
142
        /**
143
         * Useful for debug the problems during the implementation.
144
         */
145
        protected static Logger logger = Logger.getLogger(FLayers.class.getName());
146

    
147
        /**
148
         * Default <code>FLayers</code> constructor.
149
         *
150
         * @param fmap a model for the layer
151
         * @param parent parent node of this one
152
         */
153
        public FLayers(MapContext fmap, FLayers parent) {
154
                setParentLayer(parent);
155
                this.fmap = fmap;
156
        }
157

    
158
        /*
159
         * (non-Javadoc)
160
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
161
         */
162
        public void addLayerCollectionListener(LayerCollectionListener listener) {
163
                if (!layerCollectionListeners.contains(listener))
164
                        layerCollectionListeners.add(listener);
165
        }
166

    
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
171
         */
172
        public void setAllVisibles(boolean visible) {
173
                FLayer lyr;
174

    
175
                for (int i = 0; i < layers.size(); i++) {
176
                        lyr = ((FLayer) layers.get(i));
177
                        lyr.setVisible(visible);
178

    
179
                        if (lyr instanceof LayerCollection) {
180
                                ((LayerCollection) lyr).setAllVisibles(visible);
181
                        }
182
                }
183
        }
184

    
185
        /*
186
         * (non-Javadoc)
187
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
188
         */
189
        public void removeLayerCollectionListener(LayerCollectionListener listener) {
190
                layerCollectionListeners.remove(listener);
191
        }
192

    
193
//        private void doAddLayer(FLayer layer) {
194
//        layers.add(layer);
195
//        layer.setParentLayer(this);
196
//        }
197

    
198
        /**
199
         * Adds a layer on an specified position in this node.
200
         *
201
         * @param pos position in the inner list where the layer will be added
202
         * @param layer a layer
203
         */
204
        private void doAddLayer(int pos,FLayer layer) {
205
                layers.add(pos,layer);
206
                layer.setParentLayer(this);
207
        }
208
        /*
209
         * (non-Javadoc)
210
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
211
         */
212
        public void addLayer(FLayer layer) {
213
                addLayer(layers.size(),layer);
214
        }
215
        /**
216
         * Adds a layer in an specified position in this node.
217
         *
218
         * @param layer a layer
219
         *
220
         * @throws CancelationException any exception produced during the cancellation of the driver
221
         */
222
        public void addLayer(int pos,FLayer layer) {
223
                try {
224
                        //Notificamos a la capa que va a ser a?adida
225
                        //FLyrDefault layerDef = (FLyrDefault)layer;
226
                        //if (!layerDef.isUnavailable()) {
227
                        if (layer instanceof FLyrDefault)
228
                                ((FLyrDefault)layer).wakeUp();
229

    
230
                        if (layer instanceof FLayers){
231
                                FLayers layers=(FLayers)layer;
232
                                fmap.addAsCollectionListener(layers);
233
                        }
234
                        callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
235
                        //}
236

    
237
                        doAddLayer(pos,layer);
238

    
239
                        //if (!layerDef.isUnavailable()) {
240
                        callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
241
                        //}
242
                } catch (CancelationException e) {
243
                        logger.warn(e);
244
                } catch (LoadLayerException e) {
245
                        layer.setAvailable(false);
246
                        layer.addError(e);
247
                }
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
253
         */
254
        public void moveTo(int from, int to) throws CancelationException {
255
                int newfrom=layers.size()-from-1;
256
                int newto=layers.size()-to-1;
257
                if ( newfrom < 0 || newfrom >=layers.size() || newto < 0 || newto >= layers.size()) return;
258
                FLayer aux = (FLayer) layers.get(newfrom);
259
                callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
260
                layers.remove(newfrom);
261
                layers.add(newto, aux);
262
                callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
263
        }
264

    
265
        /**
266
         * Removes an inner layer.
267
         *
268
         * @param lyr a layer
269
         */
270
        private void doRemoveLayer(FLayer lyr) {
271
                layers.remove(lyr);
272
        }
273

    
274
        /*
275
         * (non-Javadoc)
276
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
277
         */
278
        public void removeLayer(FLayer lyr) throws CancelationException {
279
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
280
                doRemoveLayer(lyr);
281
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
282
        }
283

    
284
        /*
285
         * (non-Javadoc)
286
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
287
         */
288
        public void removeLayer(int idLayer) {
289
                FLayer lyr = (FLayer) layers.get(idLayer);
290
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
291
                layers.remove(idLayer);
292
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
293
        }
294

    
295
        /*
296
         * (non-Javadoc)
297
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
298
         */
299
        public void removeLayer(String layerName) {
300
                FLayer lyr;
301

    
302
                for (int i = 0; i < layers.size(); i++) {
303
                        lyr = ((FLayer) layers.get(i));
304

    
305
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
306
                                removeLayer(i);
307

    
308
                                break;
309
                        }
310
                }
311
        }
312
        /**
313
         * Replace a layer identified by its name, by another.
314
         *
315
         * @param layerName the name of the layer to be replaced
316
         * @param layer the new layer
317
         */
318
        public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException
319
        {
320
                FLayer lyr;
321
                FLayer parent;
322
                for (int i = 0; i < layers.size(); i++) {
323
                        lyr = ((FLayer) layers.get(i));
324

    
325
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
326
                                parent = lyr.getParentLayer();
327
                                removeLayer(i);
328
                                if (parent != null)
329
                                        //Notificamos a la capa que va a ser a?adida
330
                                        if (layer instanceof FLyrDefault)
331
                                                ((FLyrDefault)layer).wakeUp();
332

    
333
                                if (layer instanceof FLayers){
334
                                        FLayers layers=(FLayers)layer;
335
                                        fmap.addAsCollectionListener(layers);
336
                                }
337
                                callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
338

    
339
                                layers.add(i,layer);
340
                                layer.setParentLayer(this);
341

    
342
                                callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
343
                                break;
344
                        }
345
                }
346
        }
347

    
348

    
349
        /*
350
         * (non-Javadoc)
351
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
352
         */
353
        public FLayer[] getVisibles() {
354
                ArrayList array = new ArrayList();
355
                LayersIterator iter = new LayersIterator(this) {
356
                        public boolean evaluate(FLayer layer) {
357
                                return layer.isVisible();
358
                        }
359

    
360
                };
361

    
362
                while (iter.hasNext()) {
363
                        array.add(iter.nextLayer());
364
                }
365

    
366
                return (FLayer[]) array.toArray(new FLayer[0]);
367
        }
368

    
369
        /*
370
         * (non-Javadoc)
371
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
372
         */
373
        public FLayer getLayer(int index) {
374
                return (FLayer) layers.get(index);
375
        }
376

    
377
        /*
378
         * (non-Javadoc)
379
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
380
         */
381
        public FLayer getLayer(String layerName) {
382
                FLayer lyr;
383
                FLayer lyr2;
384
                ArrayList layerList;
385

    
386
                for (int i = 0; i < layers.size(); i++) {
387
                        lyr = ((FLayer) layers.get(i));
388

    
389
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
390
                                return lyr;
391
                        }
392

    
393
                        layerList = new ArrayList();
394
                        splitLayerGroup(lyr,layerList);
395
                        for(int j = 0; j<layerList.size(); j++ )
396
                        {
397
                                lyr2 = ((FLayer)layerList.get(j));
398
                                if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
399
                                        return lyr2;
400
                                }
401
                        }
402
                }
403

    
404
                return null;
405
        }
406
        /**
407
         * <p> Splits up a layer group in order to get a layer by name when there are layer groups</p>
408
         *
409
         * <p>In <code>result</code> always will be at least one layer.</p>
410
         *
411
         * @param layer the layer we are looking for
412
         * @param result an array list that will have the results of the search
413
         */
414
        private void splitLayerGroup(FLayer layer, ArrayList result)
415
        {
416
                int i;
417
                FLayers layerGroup;
418
                if (layer instanceof FLayers)
419
                {
420
                        layerGroup = (FLayers)layer;
421
                        for (i=0; i < layerGroup.getLayersCount(); i++ )
422
                        {
423
                                splitLayerGroup(layerGroup.getLayer(i),result);
424
                        }
425
                }
426
                else
427
                {
428
                        result.add(layer);
429
                }
430
        }
431

    
432
        /*
433
         * (non-Javadoc)
434
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
435
         */
436
        public int getLayersCount() {
437
                return layers.size();
438
        }
439

    
440
        /*
441
         * (non-Javadoc)
442
         * @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)
443
         */
444
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
445
                        Cancellable cancel,double scale) throws ReadDriverException {
446
                draw(image,g,viewPort,cancel, scale, null);
447
        }
448
        /**
449
         * <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
450
         *  a group of layers <i>(<code>ComposedLayer</code>)</i>, executes it's draw.</p>
451
         *
452
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
453
         * group</code> is drawn, will be set to <code>null</code>.</p>
454
         *
455
         * <p>The particular behavior depends on the kind of each layer and composed layer. And this process can be cancelled at any
456
         *  time by the thread <code>cancel</code>.</p>
457
         *
458
         * @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.
459
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
460
         * @param viewPort the information for drawing the layers
461
         * @param cancel an object thread that implements the {@link Cancellable Cancellable} interface, and will allow to cancel the draw
462
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
463
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
464
         *  will also be drawn
465
         *
466
         * @return <code>null</code> if the group of layers as parameter has been drawn or was <code>null</code>; the value of the parameter <code>group</code> otherwise
467
         *
468
         * @throws com.iver.cit.gvsig.fmap.DriverException if fails the driver used in this method.
469
         *
470
         * @see ComposedLayer#draw(BufferedImage, Graphics2D, ViewPort, Cancellable, double)
471
         */
472

    
473
        public ComposedLayer draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
474
                        Cancellable cancel,double scale, ComposedLayer group) throws ReadDriverException {
475
                boolean bNeedRecalculateCache = false;
476

    
477
                ///// CHEMA ComposedLayer
478
                long tg1 = System.currentTimeMillis();
479
                ///// CHEMA ComposedLayer
480

    
481

    
482
                for (int i=0; i < layers.size(); i++) {
483
                        if (cancel.isCanceled())
484
                                break; // M?s que nada porque las capas raster no son interrumpibles por ahora.
485
                        FLayer lyr = (FLayer) layers.get(i);
486

    
487
                        if (!lyr.isVisible() || !lyr.isWithinScale(scale)) continue;
488

    
489
                        LayerDrawEvent beforeEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
490
                        fmap.fireLayerDrawingEvent(beforeEvent);
491
                        if ((lyr.isDirty()) || (lyr.isCachingDrawnLayers() == false))
492
                                bNeedRecalculateCache = true;
493

    
494
                        if (lyr.isVisible()) {
495

    
496
                                long t1 = System.currentTimeMillis();
497
                                // synchronized (this) {
498
                                //Sincronizaci?n del m?todo dibujar de cada Layer, esto es posible hacerlo de otra forma,
499
                                //pero de momento se queda as?, para solucionar el refresco de una misma vista que est? a?adida en el Layout.
500
                                try{
501
                                        /* if (lyr instanceof IComposedDrawing)
502
                                             {
503
                                             // Acumulamos las peticiones hasta que sea
504
                                              // otro servidor o otro tipo de capa o la ?ltima
505
                                               // Si hay que dibujar:
506
                                                lyr.draw(image, g, viewPort, cancel,scale);
507
                                                // Si hay que acumlar:
508
                                                 lyr.acumulateDrawing(antLayer);
509

510
                                                 }
511
                                                 else */
512

    
513
                                        if (lyr.isCachingDrawnLayers())
514
                                        {
515
                                                if ((bNeedRecalculateCache) || (lyr.getCacheImageDrawnLayers()==null))
516
                                                {
517
                                                        //if (bNeedRecalculateCache)
518
                                                        // {
519

    
520
                                                        ///// CHEMA ComposedLayer
521
                                                        if (group != null) {
522
                                                                //si tenemos un grupo pendiente de pintar, pintamos
523
                                                                // para que la cache sea fiable
524
                                                                group.draw(image, g, viewPort, cancel,scale);
525
                                                                group = null;
526
                                                        }
527
                                                        ///// CHEMA ComposedLayer
528

    
529

    
530

    
531

    
532

    
533
                                                        // Copiamos la imagen actual
534
                                                        BufferedImage buff = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
535
                                                        WritableRaster w = buff.getRaster();
536
                                                        image.copyData(w);
537
                                                        lyr.setCacheImageDrawnLayers(buff);
538
                                                }
539
//                                                }
540
//                                                else
541
//                                                {
542
                                                if (lyr.getCacheImageDrawnLayers() != null)
543
                                                {
544
                                                        // Previo a esto hemos tenido que fijar
545
                                                        // la imagen que queremos usar, en el
546
                                                        // prepareDrawing de FMap.
547
                                                        if (lyr.isDirty())
548
                                                        {
549

    
550

    
551
                                                                ///// CHEMA ComposedLayer
552
                                                                // Checks for draw group (ComposedLayer)
553
                                                                if (group != null) {
554
                                                                        // it's going to load a cache image,
555
                                                                        // the current draw isn't needed
556
                                                                        group = null;
557
                                                                }
558
                                                                ///// CHEMA ComposedLayer
559

    
560
                                                                g.drawImage(lyr.getCacheImageDrawnLayers(), 0, 0, null);
561
                                                        }
562
                                                }
563
//                                                }
564
                                        }
565
                                        // Si la capa est? "sucia" o alguna de las de abajo est? sucia
566
                                        // hay que volver a dibujar.
567
                                        if (lyr.isDirty() || bNeedRecalculateCache)
568
                                        {
569
                                                if (!lyr.getFLayerStatus().isDriverLoaded()){
570
                                                        continue;
571
                                        }
572
                                                ///// CHEMA ComposedLayer
573
                                                //lyr.draw(image, g, viewPort, cancel,scale);
574
                                                ///// CHEMA ComposedLayer
575

    
576
                                                // Checks for draw group (ComposedLayer)
577
                                                if (group != null) {
578
                                                        if (lyr instanceof FLayers){
579
                                                                group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
580
                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
581
                                                                fmap.fireLayerDrawingEvent(afterEvent);
582
                                                        } else {
583
                                                                //If layer can be added to the group, does it
584
                                                                if (group.canAdd(lyr)) {
585
                                                                        group.add(lyr);
586
                                                                } else {
587
                                                                        // draw the 'pending to draw' layer group
588
                                                                        group.draw(image, g, viewPort, cancel,scale);
589

    
590
                                                                        // gets a new group instance
591
                                                                        group = lyr.newComposedLayer();
592
                                                                        // if layer hasn't group, draws it inmediately
593
                                                                        if (group == null) {
594
                                                                                if (lyr instanceof FLayers){
595
                                                                                        group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
596
                                                                                } else {
597
                                                                                        lyr.draw(image, g, viewPort, cancel,scale);
598
                                                                                }
599
                                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
600
                                                                                fmap.fireLayerDrawingEvent(afterEvent);
601
                                                                        } else {
602
                                                                                // add the layer to the group
603
                                                                                group.setMapContext(fmap);
604
                                                                                group.add(lyr);
605

    
606
                                                                        }
607
                                                                }
608
                                                        }
609
                                                } else {
610
                                                        // gets a new group instance
611
                                                        group = lyr.newComposedLayer();
612
                                                        // if layer hasn't group, draws it inmediately
613
                                                        if (group == null) {
614
                                                                if (lyr instanceof FLayers){
615
                                                                        group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
616
                                                                } else {
617
                                                                        lyr.draw(image, g, viewPort, cancel,scale);
618
                                                                }
619
                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
620
                                                                fmap.fireLayerDrawingEvent(afterEvent);
621
                                                        } else {
622
                                                                // adds the layer to the group
623
                                                                group.setMapContext(fmap);
624
                                                                group.add(lyr);
625
                                                        }
626
                                                }
627
                                                ///// CHEMA ComposedLayer
628

    
629
                                                /*
630
                                                 * (Jaume)
631
                                                 * If the layer is instance of ILabelable then it may have labels.
632
                                                 */
633
                                                if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
634

    
635
                                                        ///// CHEMA ComposedLayer
636
                                                        if (group != null) {
637
                                                                //si tenemos un grupo pendiente de pintar, pintamos
638
                                                                // para que se pinten correctamente las etiquetas
639
                                                                group.draw(image, g, viewPort, cancel,scale);
640
                                                                group = null;
641
                                                        }
642
                                                        ///// CHEMA ComposedLayer
643

    
644

    
645
                                                        ((ILabelable) lyr).drawLabels(image, g, viewPort, cancel, scale);
646
                                                }
647
                                                bNeedRecalculateCache = true;
648
                                        }
649
                                } catch (ReadDriverException e){
650
                                        // fmap.callNewErrorEvent(new ErrorEvent(lyr.getName(), e));
651
                                        fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
652
                                        lyr.addError(e);
653
                                        e.printStackTrace();
654
                                        lyr.setAvailable(false);
655
                                        // this.removeLayer(lyr);
656
                                } catch (Exception e) {
657
                                        fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
658
                                        e.printStackTrace();
659
                                        lyr.setAvailable(false);
660
                                }
661
                                // }
662
                                long t2 = System.currentTimeMillis();
663
                                logger.info("Layer " + lyr.getName() + " "
664
                                                + (t2-t1) + " milisecs.");
665
                                lyr.setDirty(false);
666
                        }
667

    
668
                }
669
                ///// CHEMA ComposedLayer
670
                if (group != null && this.getParentLayer() == null) {
671
                        //si tenemos un grupo pendiente de pintar, pintamos
672
                        group.draw(image, g, viewPort, cancel,scale);
673
                        group = null;
674

    
675
                }
676
                ///// CHEMA ComposedLayer
677
                if (getVirtualLayers() != null) {
678
                        getVirtualLayers().draw(image, g, viewPort, cancel,scale);
679
                }
680

    
681
                long tg2 = System.currentTimeMillis();
682

    
683
                logger.debug("Draw all layer " +this.getName() + " " +
684
                                + (tg2-tg1) + " milisecs.");
685
                ///// CHEMA ComposedLayer
686
                return group;
687
                ///// CHEMA ComposedLayer
688
        }
689
        /*
690
         * (non-Javadoc)
691
         * @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)
692
         */
693
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties)
694
        throws ReadDriverException {
695
                this.print(g, viewPort, cancel, scale, properties, null);
696
        }
697

    
698
        /**
699
         * <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
700
         *  a group of layers <i>(<code>ComposedLayer</code>)</i>, executes it's draw.</p>
701
         *
702
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
703
         * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.</p>
704
         *
705
         * <p>The particular behavior depends on the kind of each layer and composed layer. And this process can be cancelled at any
706
         *  time by the thread <code>cancel</code>.</p>
707
         *
708
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
709
         * @param viewPort the information for drawing the layers
710
         * @param cancel an object thread that implements the {@link Cancellable Cancellable} interface, and will allow to cancel the draw
711
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
712
         * @param properties properties that will be print
713
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
714
         *
715
         * @return <code>null</code> if the group of layers as parameter has been drawn or was <code>null</code>; the value of the parameter <code>group</code> otherwise
716
         *
717
         * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintRequestAttributeSet)
718
         *
719
         * @throws com.iver.cit.gvsig.fmap.DriverException if fails the driver used in this method.
720
         */
721
        public ComposedLayer print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties, ComposedLayer group)
722
        throws ReadDriverException {
723

    
724
                // TODO: A la hora de imprimir, isWithinScale falla, porque est?
725
                // calculando la escala en pantalla, no para el layout.
726
                // Revisar esto.
727

    
728
                // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
729
                for (int i=0; i < layers.size(); i++) {
730
                        FLayer lyr = (FLayer) layers.get(i);
731
                        if (!lyr.isVisible() || !lyr.isWithinScale(scale)) continue;
732

    
733
                        try{
734

    
735
                                ///// CHEMA ComposedLayer
736
                                // Checks for draw group (ComposedLayer)
737
                                if (group != null) {
738
                                        if (lyr instanceof FLayers){
739
                                                group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
740
                                        } else {
741
                                                // If layer can be added to the group, does it
742
                                                if (group.canAdd(lyr) && !(lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled())){
743
                                                        group.add(lyr);
744
                                                } else {
745
                                                        // draw the 'pending to draw' layer group
746
                                                        group.print(g,viewPort,cancel,scale,properties);
747

    
748
                                                        // gets a new group instance
749
                                                        if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
750
                                                                group = lyr.newComposedLayer();
751
                                                        } else {
752
                                                                group = null;
753
                                                        }
754
                                                        // if layer hasn't group, draws it inmediately
755
                                                        if (group == null) {
756
                                                                if (lyr instanceof FLayers){
757
                                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
758
                                                                } else {
759
                                                                        lyr.print(g, viewPort, cancel,scale,properties);
760
                                                                        if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
761
                                                                                ILabelable lLayer = (ILabelable) lyr;
762

    
763
                                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale);
764
                                                                        }
765
                                                                }
766
                                                        } else {
767
                                                                // add the layer to the group
768
                                                                group.setMapContext(fmap);
769
                                                                group.add(lyr);
770

    
771
                                                        }
772

    
773
                                                }
774
                                        }
775
                                } else {
776
                                        // gets a new group instance
777
                                        group = lyr.newComposedLayer();
778
                                        // if layer hasn't group, draws it inmediately
779
                                        if (group == null) {
780
                                                if (lyr instanceof FLayers){
781
                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
782
                                                } else {
783
                                                        lyr.print(g, viewPort, cancel,scale,properties);
784
                                                        if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
785
                                                                ILabelable lLayer = (ILabelable) lyr;
786

    
787
                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale);
788
                                                        }
789
                                                }
790
                                        } else {
791
                                                // add the layer to the group
792
                                                group.setMapContext(fmap);
793
                                                group.add(lyr);
794

    
795
                                        }
796
                                }
797
                                ///// CHEMA ComposedLayer
798

    
799
                        } catch (Exception e){
800
                                String mesg = Messages.getString("error_printing_layer")+" "+ lyr.getName() + ": " + e.getMessage();
801
                                fmap.addLayerError(mesg);
802
                                logger.error(mesg, e);
803
                        }
804

    
805
                }
806

    
807
                ///// CHEMA ComposedLayer
808
                if (group != null && this.getParentLayer() == null) {
809
                        //si tenemos un grupo pendiente de pintar, pintamos
810
                        group.print(g, viewPort, cancel,scale,properties);
811
                        group = null;
812

    
813
                }
814
                ///// CHEMA ComposedLayer
815

    
816
                if (getVirtualLayers() != null) {
817
                        getVirtualLayers().print( g, viewPort, cancel, scale, properties);
818
                }
819

    
820
                ///// CHEMA ComposedLayer
821
                return group;
822
                ///// CHEMA ComposedLayer
823
        }
824

    
825
        /*
826
         * (non-Javadoc)
827
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
828
         */
829
        public Rectangle2D getFullExtent() {
830
                Rectangle2D rAux = null;
831
                boolean first = true;
832

    
833

    
834
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
835
                        FLayer capa = (FLayer) iter.next();
836
                        try{
837
                                if (first) {
838
                                        rAux = capa.getFullExtent();
839
                                        first=false;
840
                                } else {
841
                                        rAux.add(capa.getFullExtent());
842
                                }
843
                        }catch (Exception e) {
844
                                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
845
                        }
846
                }
847

    
848
                return rAux;
849
        }
850

    
851
        /**
852
         * Notifies all listeners associated to this collection of layers,
853
         *  that another layer is going to be added or replaced in the internal
854
         *  list of layers.
855
         *
856
         * @param e a layer collection event with the new layer
857
         */
858
        protected void callLayerAdding(LayerCollectionEvent event)
859
        throws CancelationException {
860
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
861
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
862
                        ((LayerCollectionListener) iter.next()).layerAdding(event);
863
                }
864
        }
865

    
866
        /**
867
         * Notifies all listeners associated to this collection of layers,
868
         *  that a layer is going to be removed from the internal list of layers.
869
         *
870
         * @param event a layer collection event with the layer being removed
871
         *
872
         * @throws CancelationException any exception produced during the cancellation of the driver.
873
         */
874
        protected void callLayerRemoving(LayerCollectionEvent event)
875
        throws CancelationException {
876
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
877
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
878
                        ((LayerCollectionListener) iter.next()).layerRemoving(event);
879
                }
880
        }
881

    
882

    
883
        /**
884
         * Notifies all listeners associated to this collection of layers,
885
         *  that a layer is going to be moved in the internal list of layers.
886
         *
887
         * @param event a layer collection event with the layer being moved, and the initial and final positions
888
         *
889
         * @throws CancelationException any exception produced during the cancellation of the driver.
890
         */
891
        protected void callLayerMoving(LayerPositionEvent event)
892
        throws CancelationException {
893
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
894
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
895
                        ((LayerCollectionListener) iter.next()).layerMoving(event);
896
                }
897
        }
898

    
899
        /**
900
         * Notifies all listeners associated to this collection of layers,
901
         *  that another layer has been added or replaced in the internal
902
         *  list of layers.
903
         *
904
         * @param e a layer collection event with the new layer
905
         */
906
        protected void callLayerAdded(LayerCollectionEvent event) {
907
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
908
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
909
                        ((LayerCollectionListener) iter.next()).layerAdded(event);
910
                }
911
        }
912

    
913
        /**
914
         * Notifies all listeners associated to this collection of layers,
915
         *  that another layer has been removed from the internal list of layers.
916
         *
917
         * @param e a layer collection event with the layer removed
918
         */
919
        protected void callLayerRemoved(LayerCollectionEvent event) {
920
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
921
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
922
                        ((LayerCollectionListener) iter.next()).layerRemoved(event);
923
                }
924
        }
925

    
926
        /**
927
         * Notifies all listeners associated to this collection of layers,
928
         *  that another layer has been moved in the internal list of layers.
929
         *
930
         * @param e a layer collection event with the layer moved, and the initial and final positions
931
         */
932
        protected void callLayerMoved(LayerPositionEvent event) {
933
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
934
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
935
                        ((LayerCollectionListener) iter.next()).layerMoved(event);
936
                }
937
        }
938
        /**
939
         * <p>Returns an entity that represents this collection of layers stored as a tree-node with children that are each layer.</p>
940
         *
941
         * <p>The root node has the same properties that return <code>FlyrDefault#getXMLEntity()</code> and adds:
942
         *  <ul>
943
         *          <li> <i>numLayers</i> : number of layers of this collection (direct children of this node)
944
         *   <li> <i>LayerNames</i> : an array list with the name of the layers of this collection (direct children of this node)
945
         *    <code>FLayer.getXMLEntity()</code>
946
         *  </ul>
947
         * </p>
948
         *
949
         * <p>This XML entity has elements that represent and store information about this layer.</p>
950
         *
951
         * @return an XML entity with information to this collection of layers
952
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is any error creating the XML from the layers.
953
         */
954
        public XMLEntity getXMLEntity() throws XMLException {
955
                XMLEntity xml = super.getXMLEntity();
956
                xml.putProperty("numLayers", layers.size());
957

    
958
                String[] s = new String[layers.size()];
959

    
960
                for (int i = 0; i < layers.size(); i++) {
961
                        s[i] = ((FLayer) layers.get(i)).getName();
962
                }
963

    
964
                xml.putProperty("LayerNames", s);
965

    
966
                for (int i = 0; i < layers.size(); i++) {
967
                        try {
968
                                xml.addChild(((FLayer) layers.get(i)).getXMLEntity());
969
                        }catch (XMLException e) {
970
                                e.printStackTrace();
971
                        }
972
                }
973

    
974
                return xml;
975
        }
976
        /**
977
         * <p>Inserts layers and their properties to this collection, from an XML entity. Also adds properties to the collection of layers (root node).
978
         *
979
         * <p>The XML entity as parameter must have a tree-node structure, the root must have at least two properties, and each (first-level)
980
         *  child can be a raster layer <i>(<code>FLyrRaster</code>)</i>, a vectorial layer <i>(<code>FLyrVect</code>)</i>,
981
         *  another collection of layers <i>(<code>FLayers</code>)</i>, or another kind of layer <i>(<code>FLayer</code>)</i> .</p>
982
         *
983
         * <p> <b>Root node properties:</b>
984
         *  <ul>
985
         *   <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
986
         *   <li> numLayers : number of layers
987
         *   <li> LayerNames : name of the layers
988
         *  </ul>
989
         * </p>
990
         *
991
         * <p> <b>Layers: each first-level child: </b>
992
         *  <ul>
993
         *   <li> className : name of the class
994
         *   <li> <b> Capa Raster: </b>
995
         *   <ul>
996
         *    <li> name : name of the layer
997
         *    <li> proj : the projection of this layer (only if it's defined)
998
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
999
         *   </ul>
1000
         *   <li> <b> Capa Vectorial: </b>
1001
         *   <ul>
1002
         *    <li> file : the projection of this layer (only if it's defined)
1003
         *    <li> driverName : name of the driver used to access to the file
1004
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1005
         *   </ul>
1006
         *   <li> <b> Collection of layers: </b>
1007
         *   <ul>
1008
         *    <li> that node
1009
         *   </ul>
1010
         *   <li> <b> Another kind of layer: </b>
1011
         *   <ul>
1012
         *    <li> name : the name of the layer
1013
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
1014
         *   </ul>
1015
         *  </ul>
1016
         * </p>
1017
         *
1018
         * @see FLyrDefault#getXMLEntity03()
1019
         *
1020
         * @param xml an <code>XMLEntity</code> with the information
1021
         *
1022
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error obtaining the object.
1023
         */
1024
        public void setXMLEntity03(XMLEntity xml) throws XMLException{
1025
                super.setXMLEntity03(xml);
1026
                int numLayers = xml.getIntProperty("numLayers");
1027

    
1028
                String[] s = xml.getStringArrayProperty("LayerNames");
1029
                try {
1030
                        for (int i = 0; i < numLayers; i++) {
1031
                                FLayer layer = null;
1032

    
1033
                                String className = xml.getChild(i).getStringProperty("className");
1034

    
1035
                                if (className.equals(FLyrVect.class.getName())) {
1036
                                        if (xml.getChild(i).contains("file")) {
1037

    
1038
                                                layer = LayerFactory.createLayer(s[i],
1039
                                                                (VectorialFileDriver)LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName")),
1040
                                                                new File(xml.getChild(i).getStringProperty("file")),
1041
                                                                this.getMapContext().getViewPort().getProjection());
1042

    
1043
                                        } else if (true) {
1044
                                                //TODO falta por implementar
1045
                                        } else if (true) {
1046
                                                //TODO falta por implementar
1047
                                        }
1048

    
1049
                                        layer.setXMLEntity03(xml.getChild(i));
1050
                                        // Comprobar que la proyecci?n es la misma que la de FMap
1051
                                        // Si no lo es, es una capa que est? reproyectada al vuelo
1052
                                        IProjection proj = layer.getProjection();
1053
                                        if (proj != null)
1054
                                                if (proj != fmap.getProjection())
1055
                                                {
1056
                                                        ICoordTrans ct = proj.getCT(fmap.getProjection());
1057
                                                        layer.setCoordTrans(ct);
1058
                                                        logger.info("coordTrans = " +
1059
                                                                        proj.getAbrev() + " " +
1060
                                                                        fmap.getProjection().getAbrev());
1061
                                                }
1062

    
1063
                                } else if (className.equals((FLayers.class.getName()))) {
1064
                                        layer = new FLayers(getMapContext(),this);
1065
                                        layer.setXMLEntity(xml.getChild(i));
1066
                                } else {
1067
                                        // Capas Nuevas (externas)
1068
                                        try {
1069
                                                Class clase = Class.forName(className);
1070
                                                layer = (FLayer) clase.newInstance();
1071
                                                layer.setName(s[i]);
1072
                                                layer.setXMLEntity03(xml.getChild(i));
1073
                                                layer.load();
1074
                                        } catch (Exception e) {
1075
                                                //e.printStackTrace();
1076
                                                throw new XMLException(e);
1077
                                        }
1078
                                }
1079

    
1080
                                this.addLayer(layer);
1081
                        }
1082
                }
1083
                catch (DriverLoadException e) {
1084
                        throw new XMLException(e);
1085
                }
1086

    
1087
        }
1088
        /**
1089
         * <p>Inserts layers and properties to this collection of layers.</p>
1090
         *
1091
         * <p>The root node (this) has the same properties that return <code>FlyrDefault#getXMLEntity()</code> and adds:
1092
         *  <ul>
1093
         *          <li> <i>numLayers</i> : number of layers of this collection (direct children of this node)
1094
         *   <li> <i>LayerNames</i> : an array list with the name of the layers of this collection (direct children of this node)
1095
         *    <code>FLayer.getXMLEntity()</code>
1096
         *  </ul>
1097
         * </p>
1098
         *
1099
         * @see FLyrDefault#setXMLEntity()
1100
         * @see FLyrDefault#getXMLEntity()
1101
         * @see FLayers#addLayerFromXML(XMLEntity, String)
1102
         *
1103
         * @param xml an <code>XMLEntity</code> with the information
1104
         *
1105
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error setting the object.
1106
         */
1107
        public void setXMLEntity(XMLEntity xml) throws XMLException{
1108
                super.setXMLEntity(xml);
1109
                //LoadLayerException loadLayerException=new LoadLayerException();
1110
                int numLayers = xml.getIntProperty("numLayers");
1111

    
1112
                String[] s = xml.getStringArrayProperty("LayerNames");
1113
                // try {
1114
                fmap.clearErrors();
1115
                for (int i = 0; i < numLayers; i++) {
1116
                        try {
1117
                                this.addLayerFromXML(xml.getChild(i),s[i]);
1118
                        } catch (LoadLayerException e) {
1119
                                throw new XMLException(e);
1120
                        }
1121

    
1122

    
1123
                }
1124
        }
1125
        /*
1126
         * (non-Javadoc)
1127
         * @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)
1128
         */
1129
        public void process(FeatureVisitor visitor, FBitSet subset)
1130
        throws ReadDriverException, ExpansionFileReadException, VisitorException {
1131
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1132
                        FLayer layer = (FLayer) iter.next();
1133

    
1134
                        if (layer instanceof VectorialData) {
1135
                                ((VectorialData) layer).process(visitor, subset);
1136
                        }
1137
                }
1138
        }
1139
        /*
1140
         * (non-Javadoc)
1141
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
1142
         */
1143
        public void process(FeatureVisitor visitor)
1144
        throws ReadDriverException, VisitorException {
1145
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1146
                        FLayer layer = (FLayer) iter.next();
1147

    
1148
                        if (layer instanceof FLayers){
1149
                                FLayers lyrs=(FLayers)layer;
1150
                                for (int i=0;i<lyrs.getLayersCount();i++){
1151
                                        FLayer lyr=lyrs.getLayer(i);
1152
                                        if (lyr.isActive()) {
1153
                                                if (lyr instanceof VectorialData) {
1154
                                                        ((VectorialData) lyr).process(visitor);
1155
                                                }
1156
                                        }
1157
                                }
1158
                        }
1159
                        if (layer.isActive()) {
1160
                                if (layer instanceof VectorialData) {
1161
                                        ((VectorialData) layer).process(visitor);
1162
                                }
1163
                        }
1164
                }
1165
        }
1166
        /*
1167
         * (non-Javadoc)
1168
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, java.awt.geom.Rectangle2D)
1169
         */
1170
        public void process(FeatureVisitor visitor, Rectangle2D rect) throws ReadDriverException, ExpansionFileReadException, VisitorException {
1171
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1172
                        FLayer layer = (FLayer) iter.next();
1173

    
1174
                        if (layer.isActive()) {
1175
                                if (layer instanceof VectorialData) {
1176
                                        ((VectorialData) layer).process(visitor, rect);
1177
                                }
1178
                        }
1179
                }
1180

    
1181
        }
1182
        /*
1183
         * (non-Javadoc)
1184
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
1185
         */
1186
        public MapContext getMapContext() {
1187
                return fmap;
1188
        }
1189
        /*
1190
         * (non-Javadoc)
1191
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setCoordTrans(org.cresques.cts.ICoordTrans)
1192
         */
1193
        public void setCoordTrans(ICoordTrans ct) {
1194
                super.setCoordTrans(ct);
1195

    
1196
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1197
                        FLayer layer = (FLayer) iter.next();
1198
                        layer.setCoordTrans(ct);
1199
                }
1200
        }
1201
        /*
1202
         * (non-Javadoc)
1203
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
1204
         */
1205
        public void setAllActives(boolean active) {
1206
                FLayer lyr;
1207

    
1208
                for (int i = 0; i < layers.size(); i++) {
1209
                        lyr = ((FLayer) layers.get(i));
1210
                        lyr.setActive(active);
1211

    
1212
                        if (lyr instanceof LayerCollection) {
1213
                                ((LayerCollection) lyr).setAllActives(active);
1214
                        }
1215
                }
1216
        }
1217

    
1218
        /*
1219
         * (non-Javadoc)
1220
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
1221
         */
1222
        public FLayer[] getActives() {
1223
                ArrayList ret = new ArrayList();
1224
                LayersIterator it = new LayersIterator(this) {
1225

    
1226
                        public boolean evaluate(FLayer layer) {
1227
                                return layer.isActive();
1228
                        }
1229

    
1230
                };
1231

    
1232
                while (it.hasNext())
1233
                {
1234
                        ret.add(it.next());
1235
                }
1236
                return (FLayer[]) ret.toArray(new FLayer[0]);
1237
        }
1238

    
1239
        /*
1240
         * (non-Javadoc)
1241
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMinScale()
1242
         */
1243
        public double getMinScale() {
1244
                return -1; // La visibilidad o no la controla cada capa
1245
                // dentro de una colecci?n
1246
        }
1247
        /*
1248
         * (non-Javadoc)
1249
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMaxScale()
1250
         */
1251
        public double getMaxScale() {
1252
                return -1;
1253
        }
1254
        /*
1255
         * (non-Javadoc)
1256
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMinScale(double)
1257
         */
1258
        public void setMinScale(double minScale)
1259
        {
1260
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1261
                        FLayer lyr = (FLayer) iter.next();
1262
                        lyr.setMinScale(minScale);
1263
                }
1264
        }
1265
        /*
1266
         * (non-Javadoc)
1267
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMaxScale(double)
1268
         */
1269
        public void setMaxScale(double maxScale)
1270
        {
1271
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1272
                        FLayer lyr = (FLayer) iter.next();
1273
                        lyr.setMinScale(maxScale);
1274
                }
1275
        }
1276
        /*
1277
         * (non-Javadoc)
1278
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setActive(boolean)
1279
         */
1280
        public void setActive(boolean b){
1281
                super.setActive(b);
1282
                for (int i=0;i<layers.size();i++){
1283
                        ((FLayer)layers.get(i)).setActive(b);
1284
                }
1285
        }
1286
        /* (non-Javadoc)
1287
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
1288
         */
1289
        public boolean addLayerListener(LayerListener o) {
1290
                for (int i = 0; i < layers.size(); i++)
1291
                        ((FLayer) layers.get(i)).addLayerListener(o);
1292
                return true;
1293
        }
1294
        /*
1295
         * (non-Javadoc)
1296
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo(java.awt.Point, double, com.iver.utiles.swing.threads.Cancellable)
1297
         */
1298
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws ReadDriverException, VisitorException, LoadLayerException {
1299
                int i;
1300
                Vector items = new Vector();
1301
                FLayer layer;
1302
                XMLItem[] aux;
1303
                for (i = 0; i < this.layers.size(); i++){
1304
                        layer = (FLayer)layers.get(i);
1305
                        if (layer instanceof InfoByPoint){
1306
                                InfoByPoint queryable_layer = (InfoByPoint) layer;
1307
                                aux = queryable_layer.getInfo(p, tolerance, null);
1308
                                if (!(queryable_layer instanceof FLayers)){
1309
                                        for(int j = 0; j < aux.length; j++){
1310
                                                items.add(aux[j]);
1311
                                        }
1312
                                }
1313
                        }
1314
                }
1315
                return (XMLItem[])items.toArray(new XMLItem[0]);
1316

    
1317
//        for (i = 0; i < this.layers.size(); i++){
1318
//        FLayer laCapa = (FLayer) layers.get(i);
1319
//        if (laCapa instanceof FLyrVect){
1320
//        }
1321
//        else if (laCapa instanceof RasterOperations) {
1322
//        try {
1323
//        RasterOperations layer = (RasterOperations) laCapa;
1324
//        sb.append(layer.getInfo(p, tolerance));
1325
//        } catch (DriverException e) {
1326
//        e.printStackTrace();
1327
//        }
1328
//        }
1329
//        else if (laCapa instanceof InfoByPoint) {
1330
//        try {
1331
//        InfoByPoint layer = (InfoByPoint) laCapa;
1332
//        sb.append(layer.getInfo(p, tolerance));
1333
//        } catch (DriverException e) {
1334
//        e.printStackTrace();
1335
//        }
1336
//        }
1337
//        }
1338

    
1339
}
1340

    
1341
/*
1342
 * (non-Javadoc)
1343
 * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
1344
 */
1345
public ImageIcon getTocImageIcon() {
1346
        return new ImageIcon(MapControl.class.getResource("images/layerGroup.png"));
1347
}
1348
/*
1349
 * (non-Javadoc)
1350
 * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#isDirty()
1351
 */
1352
public boolean isDirty() {
1353
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
1354
                FLayer lyr = (FLayer) iter.next();
1355
                if (lyr.isDirty())
1356
                        return true;
1357
        }
1358
        return false;
1359
}
1360
/*
1361
 * (non-Javadoc)
1362
 * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setDirty(boolean)
1363
 */
1364
public void setDirty(boolean dirty) {
1365
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
1366
                FLayer lyr = (FLayer) iter.next();
1367
                lyr.setDirty(dirty);
1368
        }
1369

    
1370
}
1371
/**
1372
 * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers. And
1373
 *  returns <code>true</code> if there hasn't been errors.</p>
1374
 *
1375
 * @see FLayers#addLayerFromXML(XMLEntity, String)
1376
 *
1377
 * @param xml tree-node structure with information about layers
1378
 * @param name name of the layer to add
1379
 * @return <code>true</code> if there were no errors adding the layer, <code>false</code> otherwise
1380
 */
1381
public boolean addLayerFromXMLEntity(XMLEntity xml, String name) throws LoadLayerException {
1382
        fmap.clearErrors();
1383
        this.addLayerFromXML(xml,name);
1384

    
1385
        return (fmap.getLayersError().size() == 0);
1386

    
1387
}
1388
/**
1389
 * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
1390
 *
1391
 * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
1392
 *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
1393
 *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
1394
 *
1395
 * @param xml tree-node structure with information about layers
1396
 * @param name name of the layer to add
1397
 */
1398
private void addLayerFromXML(XMLEntity xml, String name) throws LoadLayerException {
1399
        FLayer layer = null;
1400

    
1401
        try {
1402
                if (name == null) name = xml.getName();
1403

    
1404

    
1405
                String className = xml.getStringProperty("className");
1406
                //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,
1407
                //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1408
                if (className.equals(FLyrVect.class.getName()) || className.equals(FLyrAnnotation.class.getName())) {
1409
                        String type = xml.getStringProperty("type");
1410
                        if ("vectorial".equals(type)){
1411
                                //String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
1412
                                IProjection proj = null;
1413
                                if (xml.contains("proj")) {
1414
                                        proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
1415
                                }
1416
                                else
1417
                                {
1418
                                        proj = this.getMapContext().getViewPort().getProjection();
1419
                                }
1420
                                if (xml.contains("file")) {
1421
                                        Driver d;
1422
                                        try {
1423
                                                d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
1424
                                        } catch (DriverLoadException e1) {
1425
                                                throw new DriverLayerException(name,e1);
1426
                                        }
1427
                                        layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
1428
                                                        new File(xml.getStringProperty("file")),
1429
                                                        proj);
1430

    
1431

    
1432
                                }
1433
                                if (xml.contains("db")) {
1434

    
1435
                                        String driverName = xml.getStringProperty("db");
1436
                                        IVectorialDatabaseDriver driver;
1437
                                        try {
1438
                                                driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
1439
                                                //Hay que separar la carga de los datos del XMLEntity del load.
1440
                                                driver.setXMLEntity(xml.getChild(2));
1441

    
1442
                                                boolean loadOk = false;
1443
                                                ((DefaultJDBCDriver)driver).load();
1444
                                                if (((DefaultJDBCDriver)driver).getConnection() != null) {
1445
                                                        loadOk = true;
1446
                                                }
1447
                                                layer = LayerFactory.createDBLayer(driver, name, proj);
1448
                                                if (!loadOk) {
1449
                                                        layer.setAvailable(false);
1450
                                                }
1451

    
1452
                                        } catch (DriverLoadException e) {
1453
                                                throw new DriverLayerException(name,e);
1454
                                        } catch (XMLException e) {
1455
                                                throw new DriverLayerException(name,e);
1456
                                        } catch (ReadDriverException e) {
1457
                                                throw new DriverLayerException(name,e);
1458
                                        }
1459

    
1460
                                }
1461
                                // Clases con algun driver gen?rico creado por otro
1462
                                // programador
1463
                                if (xml.contains("other")) {
1464

    
1465
                                        String driverName = xml.getStringProperty("other");
1466
                                        VectorialDriver driver = null;
1467
                                        try {
1468
                                                driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
1469
                                        } catch (DriverLoadException e) {
1470
                                                // Si no existe ese driver, no pasa nada.
1471
                                                // Puede que el desarrollador no quiera que
1472
                                                // aparezca en el cuadro de di?logo y ha metido
1473
                                                // el jar con sus clases en nuestro directorio lib.
1474
                                                // Intentamos cargar esa clase "a pelo".
1475
                                                if (xml.getChild(2).contains("className"))
1476
                                                {
1477
                                                        String className2 = xml.getChild(2).getStringProperty("className");
1478
                                                        try {
1479
                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1480
                                                        } catch (Exception e1) {
1481
                                                                throw new DriverLayerException(name,e);
1482
                                                        }
1483
                                                }
1484
                                        } catch (NullPointerException npe) {
1485
                                                // Si no existe ese driver, no pasa nada.
1486
                                                // Puede que el desarrollador no quiera que
1487
                                                // aparezca en el cuadro de di?logo y ha metido
1488
                                                // el jar con sus clases en nuestro directorio lib.
1489
                                                // Intentamos cargar esa clase "a pelo".
1490
                                                if (xml.getChild(2).contains("className"))
1491
                                                {
1492
                                                        String className2 = xml.getChild(2).getStringProperty("className");
1493
                                                        try {
1494
                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1495
                                                        } catch (Exception e1) {
1496
                                                                throw new DriverLayerException(name,e1);
1497
                                                        }
1498
                                                }
1499
                                        }
1500
                                        if (driver instanceof IPersistence)
1501
                                        {
1502
                                                IPersistence persist = (IPersistence) driver;
1503
                                                persist.setXMLEntity(xml.getChild(2));
1504
                                        }
1505
                                        layer = LayerFactory.createLayer(name, driver, proj);
1506
                                }
1507

    
1508
                        }
1509

    
1510
                        //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,
1511
                        //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1512
                        if (className.equals(FLyrAnnotation.class.getName())){
1513
                                layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
1514
                        }
1515

    
1516

    
1517
                        layer.setXMLEntity(xml);
1518

    
1519
                } else if (className.equals((FLayers.class.getName()))) {
1520
                        layer = new FLayers(getMapContext(),this);
1521
                        layer.setXMLEntity(xml);
1522
                } else {
1523
                        // Capas Nuevas (externas)
1524
                        Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1525
                        layer = (FLayer) clase.newInstance();
1526
                        layer.setName(name);
1527
                        layer.setXMLEntity(xml);
1528
                        layer.load();
1529
                }
1530

    
1531
                this.addLayer(layer);
1532
                logger.debug("layer: "+ layer.getName() +" loaded");
1533
                // Comprobar que la proyecci?n es la misma que la de FMap
1534
                // Si no lo es, es una capa que est? reproyectada al vuelo
1535
                IProjection proj = layer.getProjection();
1536
                if ((proj != null))
1537
                        if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode()))
1538
                        {
1539
                                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1540
                                // TODO: REVISAR CON LUIS
1541
                                // Se lo fijamos a todas, luego cada una que se reproyecte
1542
                                // si puede, o que no haga nada
1543

    
1544
                                layer.setCoordTrans(ct);
1545
                        }
1546
        } catch (XMLException e) {
1547
                fmap.addLayerError(xml.getStringProperty("name"));
1548
                throw new LoadLayerException(name,e);
1549
        } catch (ClassNotFoundException e) {
1550
                fmap.addLayerError(xml.getStringProperty("name"));
1551
                throw new LoadLayerException(name,e);
1552
        } catch (InstantiationException e) {
1553
                fmap.addLayerError(xml.getStringProperty("name"));
1554
                throw new LoadLayerException(name,e);
1555
        } catch (IllegalAccessException e) {
1556
                fmap.addLayerError(xml.getStringProperty("name"));
1557
                throw new LoadLayerException(name,e);
1558
        } catch (ReadDriverException e) {
1559
                fmap.addLayerError(xml.getStringProperty("name"));
1560
                throw new LoadLayerException(name,e);
1561
        } catch (LoadLayerException e){
1562
                fmap.addLayerError(xml.getStringProperty("name"));
1563
                throw e;
1564
        }
1565
}
1566

    
1567
/**
1568
 * <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
1569
 *  and loads the layer. Then, adds the layer to this collection of layers, and if there is a projection defined,
1570
 *  inserts the transformation coordinates to the layer.</p>
1571
 *
1572
 * <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>
1573
 *
1574
 * @param xml tree-node structure with information about layers
1575
 * @param name name of the layer to add
1576
 */
1577
private void addLayerFromXMLNew(XMLEntity xml, String name) {
1578
        FLayer layer = null;
1579

    
1580

    
1581
        try {
1582
                String className = xml.getStringProperty("className");
1583

    
1584
                if (className.equals((FLayers.class.getName()))){
1585
                        layer = new FLayers(getMapContext(),this);
1586
                } else {
1587
//                        Por compatibilidad
1588
                        if (className.equals(FLyrVect.class.getName())) {
1589
                                if (xml.contains("file")) {
1590
                                        layer = new FLayerFileVectorial();
1591
                                } else if (xml.contains("db")) {
1592
                                        try {
1593
                                                layer = (FLayer)((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create("com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial");
1594
                                        } catch (Exception e) {
1595
                                                throw new XMLException(new Exception("No se tiene registrada la capa de tipo JDBC"));
1596
                                        }
1597
                                        //className = FLayerJDBCVectorial.class.getName();
1598
                                } else if (xml.contains("other")){
1599
                                        layer = new FLayerGenericVectorial();
1600
                                } else {
1601
                                        throw new XMLException(new Exception("Capa vectorial de tipo no reconocido"));
1602
                                }
1603
//                                Fin por compatibilidad
1604
                        } else {
1605
                                try {
1606
                                        layer = (FLayer)(((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create(className));
1607
                                } catch (Exception e) {
1608
                                        //puende que no este registrada como punto de extension
1609
                                        Class clase = Class.forName(className);
1610
                                        layer = (FLayer) clase.newInstance();
1611
                                        // FIXME: Hacemos algo aqui o dejamos que suba el error?
1612
                                }
1613
                        }
1614

    
1615
                }
1616
                layer.setXMLEntity(xml);
1617
                if (name != null) layer.setName(name);
1618
                layer.load();
1619

    
1620
                this.addLayer(layer);
1621
                logger.debug("layer: "+ layer.getName() +" loaded");
1622
                // Comprobar que la proyecci?n es la misma que la de FMap
1623
                // Si no lo es, es una capa que est? reproyectada al vuelo
1624
                IProjection proj = layer.getProjection();
1625
                if ((proj != null))
1626
                        if (proj != getMapContext().getProjection())
1627
                        {
1628
                                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1629
                                // TODO: REVISAR CON LUIS
1630
                                // Se lo fijamos a todas, luego cada una que se reproyecte
1631
                                // si puede, o que no haga nada
1632
                                layer.setCoordTrans(ct);
1633

    
1634
                        }
1635
        }catch (Exception e) {
1636
                fmap.addLayerError(xml.getStringProperty("name"));
1637
                logger.debug(Messages.getString("could_not_load_layer")+": "+xml.getStringProperty("name") + ".\n"
1638
                                +Messages.getString("reason")+":", e);
1639
        }
1640
}
1641

    
1642
}