Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / FLayers.java @ 44548

History | View | Annotate | Download (42.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.mapcontext.layers;
24

    
25
import java.awt.Graphics2D;
26
import java.awt.Point;
27
import java.awt.image.BufferedImage;
28
import java.util.ArrayList;
29
import java.util.Collection;
30
import java.util.Collections;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.ListIterator;
34
import java.util.Set;
35
import java.util.TreeSet;
36
import java.util.function.Predicate;
37
import org.apache.commons.lang3.StringUtils;
38

    
39
import org.cresques.cts.ICoordTrans;
40
import org.cresques.cts.IProjection;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.compat.print.PrintAttributes;
45
import org.gvsig.fmap.dal.DataStore;
46
import org.gvsig.fmap.dal.exception.DataException;
47
import org.gvsig.fmap.dal.exception.ReadException;
48
import org.gvsig.fmap.geom.primitive.Envelope;
49
import org.gvsig.fmap.mapcontext.MapContext;
50
import org.gvsig.fmap.mapcontext.MapContextLocator;
51
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
52
import org.gvsig.fmap.mapcontext.Messages;
53
import org.gvsig.fmap.mapcontext.ViewPort;
54
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
55
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
56
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
57
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
58
import org.gvsig.fmap.mapcontext.layers.operations.LayerNotFoundInCollectionException;
59
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitable;
60
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
61
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
64
import org.gvsig.metadata.exceptions.MetadataException;
65
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.dynobject.DynObjectSet;
67
import org.gvsig.tools.dynobject.DynStruct;
68
import org.gvsig.tools.dynobject.impl.MultiDynObjectSet;
69
import org.gvsig.tools.exception.BaseException;
70
import org.gvsig.tools.persistence.PersistenceManager;
71
import org.gvsig.tools.persistence.PersistentState;
72
import org.gvsig.tools.persistence.exception.PersistenceException;
73
import org.gvsig.tools.task.Cancellable;
74
import org.gvsig.tools.util.Callable;
75
import org.gvsig.tools.visitor.Visitor;
76

    
77
/**
78
 * <p>
79
 * Represents a generic collection of layers, that can be represented as a node
80
 * in a tree of nodes of layers.</p>
81
 *
82
 * <p>
83
 * Adapts the basic functionality implemented for a layer in the abstract class
84
 * <code>FLyrDefault</code>, to a collection of layers, implementing, as well,
85
 * specific methods for this kind of object, defined in the interfaces
86
 * <code>VectorialData</code>, <code>LayerCollection</code>, and
87
 * <code>InfoByPoint</code>.</p>
88
 *
89
 * @see FLyrDefault
90
 */
91
public class FLayers extends FLyrDefault implements LayerCollection,
92
        InfoByPoint, List<FLayer> {
93

    
94
    /**
95
     * List with all listeners registered for this kind of node.
96
     */
97
    protected ArrayList layerCollectionListeners = null;
98

    
99
    /**
100
     * A synchronized list with the layers.
101
     */
102
    protected List<FLayer> layers = null;
103

    
104
    protected MapContext fmap;
105

    
106
    /**
107
     * Useful for debug the problems during the implementation.
108
     */
109
    private static final Logger LOGGER = LoggerFactory.getLogger(FLayers.class);
110

    
111
    public FLayers() {
112
        super();
113
        layerCollectionListeners = new ArrayList();
114
        layers = Collections.synchronizedList(new ArrayList());
115
    }
116

    
117
    @Override
118
    public void addLayerCollectionListener(LayerCollectionListener listener) {
119
        if (!layerCollectionListeners.contains(listener)) {
120
            layerCollectionListeners.add(listener);
121
        }
122
    }
123

    
124
    /*
125
     * (non-Javadoc)
126
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
127
     */
128
    @Override
129
    public void setAllVisibles(boolean visible) {
130
        FLayer lyr;
131

    
132
        for (int i = 0; i < layers.size(); i++) {
133
            lyr = ((FLayer) layers.get(i));
134
            lyr.setVisible(visible);
135

    
136
            if (lyr instanceof LayerCollection) {
137
                ((LayerCollection) lyr).setAllVisibles(visible);
138
            }
139
        }
140
    }
141

    
142
    @Override
143
    public void removeLayerCollectionListener(LayerCollectionListener listener) {
144
        layerCollectionListeners.remove(listener);
145
    }
146

    
147
    /**
148
     * Adds a layer on an specified position in this node.
149
     *
150
     * @param pos position in the inner list where the layer will be added
151
     * @param layer a layer
152
     */
153
    private void doAddLayer(int pos, FLayer layer) {
154
        layers.add(pos, layer);
155
        ToolsLocator.getDisposableManager().bind(layer);
156
        layer.setParentLayer(this);
157
        IProjection layerProj = layer.getProjection();
158
        if (layerProj != null && fmap != null) {
159
            IProjection mapContextProj = fmap.getProjection();
160
            // TODO REVISAR ESTO !!!!
161
            // Esta condici?n puede que no fuese exacta para todos los casos
162
            if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
163
                ICoordTrans ct = layerProj.getCT(mapContextProj);
164
                layer.setCoordTrans(ct);
165
            } else {
166
                layer.setCoordTrans(null);
167
            }
168
        }
169
        this.updateDrawVersion();
170
    }
171

    
172
    @Override
173
    public void addLayer(FLayer layer) {
174

    
175
        MapContext mco = this.getMapContext();
176

    
177
        if (mco != null) {
178
            /*
179
             * Get order manager from map context
180
             */
181
            int position = mco.getOrderManager().getPosition(this, layer);
182
            addLayer(position, layer);
183
        } else {
184
            /*
185
             * This happens when FLayers object is not in a
186
             * map context, so no order manager is available.
187
             */
188
            addLayer(layers.size(), layer);
189
        }
190
    }
191
    
192
    public void addLayer(DataStore store) {
193
        FLayer layer;
194
        try {
195
            layer = MapContextLocator.getMapContextManager().createLayer(
196
                    store.getName(),
197
                    store
198
            );
199
            
200
        } catch (LoadLayerException ex) {
201
            throw new RuntimeException("Can't create layer from store.", ex);
202
        }
203
        this.addLayer(layer);
204
    }
205

    
206
    /**
207
     * Adds a layer in an specified position in this node.
208
     *
209
     * @param pos
210
     * @param layer a layer
211
     */
212
    public void addLayer(int pos, FLayer layer) {
213
        try {
214
            if (layer instanceof FLayers) {
215
                FLayers layers = (FLayers) layer;
216
                if( fmap != null ) {
217
                    fmap.addAsCollectionListener(layers);
218
                }
219
            }
220
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
221

    
222
            doAddLayer(pos, layer);
223

    
224
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
225
        } catch (CancelationException e) {
226
            LOGGER.warn(e.getMessage());
227
        }
228
    }
229

    
230
    @Override
231
    public void moveTo(int from, int to) throws CancelationException {
232
        int newfrom = layers.size() - from - 1;
233
        int newto = layers.size() - to - 1;
234
        if (newfrom < 0 || newfrom >= layers.size() || newto < 0 || newto >= layers.size()) {
235
            return;
236
        }
237
        FLayer aux = (FLayer) layers.get(newfrom);
238
        callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
239
        layers.remove(newfrom);
240
        layers.add(newto, aux);
241
        this.updateDrawVersion();
242
        callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
243
    }
244

    
245
    /**
246
     * Removes an inner layer.
247
     *
248
     * @param lyr a layer
249
     */
250
    private void doRemoveLayer(FLayer lyr) {
251
        layers.remove(lyr);
252
        lyr.dispose();
253
        this.updateDrawVersion();
254
    }
255

    
256
    @Override
257
    public void removeLayer(FLayer lyr) throws CancelationException {
258
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
259
        doRemoveLayer(lyr);
260
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
261
    }
262

    
263
    @Override
264
    public void removeLayer(int idLayer) {
265
        FLayer lyr = (FLayer) layers.get(idLayer);
266
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
267
        this.doRemoveLayer(lyr);
268
//                layers.remove(idLayer);
269
//                this.updateDrawVersion();
270
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
271
    }
272

    
273
    @Override
274
    public void removeLayer(String layerName) {
275
        FLayer lyr;
276

    
277
        for (int i = 0; i < layers.size(); i++) {
278
            lyr = ((FLayer) layers.get(i));
279

    
280
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
281
                removeLayer(i);
282

    
283
                break;
284
            }
285
        }
286
    }
287

    
288
    /**
289
     * Replace a layer identified by its name, by another.
290
     *
291
     * @param layerName the name of the layer to be replaced
292
     * @param layer the new layer
293
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
294
     * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
295
     */
296
    public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException {
297
        replaceLayer(getLayer(layerName), layer);
298
    }
299

    
300
    /**
301
     * Replace a layer by another layer. It search recursively by all the
302
     * ILayerCollection nodes
303
     *
304
     * @param layer the layer to be replaced
305
     * @param newLayer the new layer
306
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
307
     */
308
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
309
        replaceLayer(this, layer, newLayer);
310
    }
311

    
312
    /**
313
     * Replace a layer by other layer. It search recursively by all the
314
     * ILayerCollection nodes
315
     *
316
     * @param parentLayer the parent layer
317
     * @param layer the layer to be replaced
318
     * @param newLayer the new layer
319
     * @throws LoadLayerException
320
     */
321
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
322
        FLayer lyr;
323
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
324
            lyr = ((FLayer) parentLayer.getLayer(i));
325
            if (lyr.equals(layer)) {
326
                parentLayer.removeLayer(i);
327
                parentLayer.addLayer(i, newLayer);
328
                break;
329
            }
330
            if (lyr instanceof LayerCollection) {
331
                replaceLayer((FLayers) lyr, layer, newLayer);
332
            }
333
        }
334
    }
335

    
336
    @Override
337
    public FLayer[] getVisibles() {
338
        ArrayList array = new ArrayList();
339
        LayersIterator iter = new LayersIterator(this) {
340
            @Override
341
            public boolean evaluate(FLayer layer) {
342
                return layer.isVisible();
343
            }
344

    
345
        };
346

    
347
        while (iter.hasNext()) {
348
            array.add(iter.nextLayer());
349
        }
350

    
351
        return (FLayer[]) array.toArray(new FLayer[0]);
352
    }
353

    
354
    @Override
355
    public FLayer getLayer(int index) {
356
        return (FLayer) layers.get(index);
357
    }
358

    
359
    @Override
360
    public FLayer getLayer(final String layerName) {
361
        if( StringUtils.isBlank(layerName) ) {
362
            return null;
363
        }
364
        for (FLayer layer : this.layers) {
365
            if( layer!=null && StringUtils.equalsIgnoreCase(layer.getName(), layerName) ) {
366
                return layer;
367
            }
368
            if( layer instanceof FLayers ) {
369
                List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
370
                    @Override
371
                    public boolean test(FLayer layer) {
372
                        return StringUtils.equalsIgnoreCase(layer.getName(), layerName);                        
373
                    }
374
                });
375
                if( theLayers.isEmpty() ) {
376
                    return null;
377
                }
378
                return theLayers.get(0);
379
            }
380
        }
381
        return null;
382
    }
383

    
384
    public FLayer getLayer(final DataStore store) {
385
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
386
            @Override
387
            public boolean test(FLayer layer) {
388
                return  layer instanceof SingleLayer && store == ((SingleLayer) layer).getDataStore();
389
            }
390
        });
391
        if( theLayers.isEmpty() ) {
392
            return null;
393
        }
394
        return theLayers.get(0);
395
    }
396
    
397
    private List<FLayer> toPlainList(FLayer layer) {
398
        return toPlainList(layer, new ArrayList<FLayer>(), null);
399
    }
400

    
401
    private List<FLayer> toPlainList(FLayer layer, Predicate<FLayer> filter) {
402
        return toPlainList(layer, new ArrayList<FLayer>(), filter);
403
    }
404

    
405
    private List<FLayer> toPlainList(FLayer layer, List<FLayer> result, Predicate<FLayer> filter) {
406
        if (layer instanceof FLayers) {
407
            FLayers layerGroup = (FLayers) layer;
408
            for (int i = 0; i < layerGroup.getLayersCount(); i++) {
409
                toPlainList(layerGroup.getLayer(i), result, filter);
410
            }
411
        } else if( filter==null || filter.test(layer) ) {
412
            result.add(layer);
413
        }
414
        return result;
415
    }
416

    
417
    @Override
418
    public int getLayersCount() {
419
        return layers.size();
420
    }
421

    
422
    @Override
423
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
424
            Cancellable cancel, double scale) throws ReadException {
425
        // FIXME Arreglar este error
426
        throw new RuntimeException("Esto no deberia de llamarse");
427
    }
428

    
429
    /*
430
     * (non-Javadoc)
431
     *
432
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
433
     * com.iver.cit.gvsig.fmap.ViewPort,
434
     * com.iver.utiles.swing.threads.Cancellable, double,
435
     * javax.print.attribute.PrintAttributes)
436
     */
437
    @Override
438
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
439
            double scale, PrintAttributes properties)
440
            throws ReadException {
441
        throw new RuntimeException("No deberia pasar por aqui");
442
    }
443

    
444
    public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
445
            double scale, PrintAttributes properties)
446
            throws ReadException {
447
        this.print_old(g, viewPort, cancel, scale, properties, null);
448
    }
449

    
450
    /**
451
     * <p>
452
     * Checks all layers (each one as a sub-node of this node <i>collection of
453
     * layers</i>) of this collection and draws their requested properties. If a
454
     * node is a group of layers (<code>ComposedLayer</code>), executes it's
455
     * drawn.</p>
456
     *
457
     * <p>
458
     * All nodes which could group with the composed layer <code>group</code>,
459
     * will be drawn together. And once the <code>
460
     * group</code> is drawn, will be set to <code>null</code> if hasn't a
461
     * parent layer.</p>
462
     *
463
     * <p>
464
     * The particular implementation depends on the kind of each layer and
465
     * composed layer. And this process can be cancelled at any time by the
466
     * shared object <code>cancel</code>.</p>
467
     *
468
     * <p>
469
     * According the print quality, labels will be printed in different
470
     * resolution:
471
     * <ul>
472
     * <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
473
     * <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
474
     * <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
475
     * </ul>
476
     * </p>
477
     *
478
     * @param g for rendering 2-dimensional shapes, text and images on the
479
     * Java(tm) platform
480
     * @param viewPort the information for drawing the layers
481
     * @param cancel shared object that determines if this layer can continue
482
     * being drawn
483
     * @param scale the scale of the view. Must be between
484
     * {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
485
     * @param properties properties that will be print
486
     * @param group a composed layer pending to paint; if this parameter is
487
     * <code>null</code>, the composed layer
488
     *
489
     * @return <code>null</code> if the layers in <code>group</code> had been
490
     * drawn or were <code>null</code>; otherwise, the <code>group</code>
491
     * @throws org.gvsig.fmap.dal.exception.ReadException
492
     *
493
     * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double,
494
     * PrintAttributes)
495
     */
496
    public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
497
            throws ReadException {
498
        double dpi = 72;
499

    
500
        int resolution = properties.getPrintQuality();
501
        switch (resolution) {
502
            case PrintAttributes.PRINT_QUALITY_NORMAL:
503
                dpi = 300;
504
                break;
505
            case PrintAttributes.PRINT_QUALITY_HIGH:
506
                dpi = 600;
507
                break;
508
            case PrintAttributes.PRINT_QUALITY_DRAFT:
509
                dpi = 72;
510
                break;
511
            default:
512
                break;
513
        }
514

    
515
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
516
        // calculando la escala en pantalla, no para el layout.
517
        // Revisar esto.
518
        // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
519
        for (int i = 0; i < layers.size(); i++) {
520
            FLayer lyr = (FLayer) layers.get(i);
521
            if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
522
                continue;
523
            }
524

    
525
            try {
526

    
527
                // Checks for draw group (ComposedLayer)
528
                if (group != null) {
529
                    if (lyr instanceof FLayers) {
530
                        group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
531
                    } else {
532
                        // If layer can be added to the group, does it
533
                        if (lyr instanceof ILabelable
534
                                && ((ILabelable) lyr).isLabeled()
535
                                && ((ILabelable) lyr).getLabelingStrategy() != null
536
                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
537
                            group.add(lyr);
538
                        } else {
539
                            // draw the 'pending to draw' layer group
540
                            group.print(g, viewPort, cancel, scale, properties);
541

    
542
                            // gets a new group instance
543
                            if (lyr instanceof ILabelable
544
                                    && ((ILabelable) lyr).isLabeled()
545
                                    && ((ILabelable) lyr).getLabelingStrategy() != null
546
                                    && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
547
                                group = lyr.newComposedLayer();
548
                            } else {
549
                                group = null;
550
                            }
551
                            // if layer hasn't group, draws it inmediately
552
                            if (group == null) {
553
                                if (lyr instanceof FLayers) {
554
                                    group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
555
                                } else {
556
                                    lyr.print(g, viewPort, cancel, scale, properties);
557
                                    if (lyr instanceof ILabelable
558
                                            && ((ILabelable) lyr).isLabeled()
559
                                            && ((ILabelable) lyr).getLabelingStrategy() != null
560
                                            && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
561
                                        ILabelable lLayer = (ILabelable) lyr;
562
                                        lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
563
                                    }
564
                                }
565
                            } else {
566
                                // add the layer to the group
567
                                group.setMapContext(fmap);
568
                                group.add(lyr);
569

    
570
                            }
571

    
572
                        }
573
                    }
574
                } else {
575
                    // gets a new group instance
576
                    group = lyr.newComposedLayer();
577
                    // if layer hasn't group, draws it inmediately
578
                    if (group == null) {
579
                        if (lyr instanceof FLayers) {
580
                            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
581
                        } else {
582
                            lyr.print(g, viewPort, cancel, scale, properties);
583
                            if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
584
                                ILabelable lLayer = (ILabelable) lyr;
585

    
586
                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
587
                            }
588
                        }
589
                    } else {
590
                        // add the layer to the group
591
                        group.setMapContext(fmap);
592
                        group.add(lyr);
593

    
594
                    }
595
                }
596

    
597
            } catch (Exception e) {
598
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
599
                if( fmap!=null ) {
600
                    fmap.addLayerError(mesg);
601
                }
602
                LOGGER.error(mesg, e);
603
            }
604

    
605
        }
606

    
607
        if (group != null && this.getParentLayer() == null) {
608
            //si tenemos un grupo pendiente de pintar, pintamos
609
            group.print(g, viewPort, cancel, scale, properties);
610
            group = null;
611

    
612
        }
613
        return group;
614
    }
615

    
616
    @Override
617
    @SuppressWarnings("UseSpecificCatch")
618
    public Envelope getFullEnvelope() {
619
        Envelope rAux = null;
620
        boolean first = true;
621

    
622
        for (FLayer capa : layers) {
623
            try {
624
                if (first) {
625
                    rAux = (Envelope) capa.getFullEnvelope().clone();
626
                    first = false;
627
                } else {
628
                    rAux.add(capa.getFullEnvelope());
629
                }
630
            } catch (Exception e) {
631
                LOGGER.warn("Can't calculate the envelope of the layer group '"+this.getName()+"'.",e);
632
            }
633
        }
634

    
635
        return rAux;
636
    }
637

    
638
    /**
639
     * Notifies all listeners associated to this collection of layers, that
640
     * another layer is going to be added or replaced in the internal list of
641
     * layers.
642
     *
643
     * @param event a layer collection event with the new layer
644
     */
645
    protected void callLayerAdding(LayerCollectionEvent event)
646
            throws CancelationException {
647
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
648
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
649
            ((LayerCollectionListener) iter.next()).layerAdding(event);
650
        }
651
    }
652

    
653
    /**
654
     * Notifies all listeners associated to this collection of layers, that a
655
     * layer is going to be removed from the internal list of layers.
656
     *
657
     * @param event a layer collection event with the layer being removed
658
     *
659
     * @throws CancelationException any exception produced during the
660
     * cancellation of the driver.
661
     */
662
    protected void callLayerRemoving(LayerCollectionEvent event)
663
            throws CancelationException {
664
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
665
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
666
            ((LayerCollectionListener) iter.next()).layerRemoving(event);
667
        }
668
    }
669

    
670
    /**
671
     * Notifies all listeners associated to this collection of layers, that a
672
     * layer is going to be moved in the internal list of layers.
673
     *
674
     * @param event a layer collection event with the layer being moved, and the
675
     * initial and final positions
676
     *
677
     * @throws CancelationException any exception produced during the
678
     * cancellation of the driver.
679
     */
680
    protected void callLayerMoving(LayerPositionEvent event)
681
            throws CancelationException {
682
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
683
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
684
            ((LayerCollectionListener) iter.next()).layerMoving(event);
685
        }
686
    }
687

    
688
    /**
689
     * Notifies all listeners associated to this collection of layers, that
690
     * another layer has been added or replaced in the internal list of layers.
691
     *
692
     * @param event a layer collection event with the new layer
693
     */
694
    protected void callLayerAdded(LayerCollectionEvent event) {
695
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
696
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
697
            ((LayerCollectionListener) iter.next()).layerAdded(event);
698
        }
699
    }
700

    
701
    /**
702
     * Notifies all listeners associated to this collection of layers, that
703
     * another layer has been removed from the internal list of layers.
704
     *
705
     * @param event a layer collection event with the layer removed
706
     */
707
    protected void callLayerRemoved(LayerCollectionEvent event) {
708
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
709
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
710
            ((LayerCollectionListener) iter.next()).layerRemoved(event);
711
        }
712
    }
713

    
714
    /**
715
     * Notifies all listeners associated to this collection of layers, that
716
     * another layer has been moved in the internal list of layers.
717
     *
718
     * @param event a layer collection event with the layer moved, and the initial
719
     * and final positions
720
     */
721
    protected void callLayerMoved(LayerPositionEvent event) {
722
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
723
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
724
            ((LayerCollectionListener) iter.next()).layerMoved(event);
725
        }
726
    }
727

    
728
    @Override
729
    public void saveToState(PersistentState state) throws PersistenceException {
730

    
731
        super.saveToState(state);
732

    
733
        state.set("mapContext", fmap);
734

    
735
        List layersToSave = new ArrayList();
736
        for (FLayer layer : this.layers) {
737
            if (!layer.isTemporary()) {
738
                layersToSave.add(layer);
739
            }
740
        }
741
        state.set("layers", layersToSave);
742
    }
743

    
744
    @Override
745
    public void loadFromState(PersistentState state) throws PersistenceException {
746

    
747
        super.loadFromState(state);
748

    
749
        setMapContext((MapContext) state.get("mapContext"));
750
        Iterator iter = state.getIterator("layers");
751
        while (iter.hasNext()) {
752
            FLayer item = (FLayer) iter.next();
753
            // Cuando falla la carga de la capa desde la persistencia, puede
754
            // haberse perdido quien es el parent. Asi que lo asignamos aqui
755
            // por si acaso.
756
            item.setParentLayer(this);
757
            layers.add(item);
758
        }
759
    }
760

    
761
    @Override
762
    public MapContext getMapContext() {
763
        return fmap;
764
    }
765

    
766
    @Override
767
    public void setAllActives(boolean active) {
768
        FLayer lyr;
769

    
770
        for (int i = 0; i < layers.size(); i++) {
771
            lyr = ((FLayer) layers.get(i));
772
            lyr.setActive(active);
773

    
774
            if (lyr instanceof LayerCollection) {
775
                ((LayerCollection) lyr).setAllActives(active);
776
            }
777
        }
778
    }
779

    
780
    public List<FLayer> getLayers(Predicate<FLayer> filter) {
781
        List<FLayer> theLayers = this.toPlainList(this, filter);
782
        return theLayers;
783
    }
784
    
785
    @Override
786
    public FLayer[] getActives() {
787
        List<FLayer> activeLayers = this.getLayers(new Predicate<FLayer>() {
788
            @Override
789
            public boolean test(FLayer layer) {
790
                return layer.isActive();
791
            }
792
        });
793
        return (FLayer[]) activeLayers.toArray(new FLayer[activeLayers.size()]);
794
    }
795

    
796
    @Override
797
    public double getMinScale() {
798
        return -1; // La visibilidad o no la controla cada capa
799
        // dentro de una colecci?n
800
    }
801

    
802
    @Override
803
    public double getMaxScale() {
804
        return -1;
805
    }
806

    
807
    @Override
808
    public void setMinScale(double minScale) {
809
        for (FLayer lyr : layers) {
810
            lyr.setMinScale(minScale);
811
        }
812
    }
813

    
814
    @Override
815
    public void setMaxScale(double maxScale) {
816
        for (FLayer lyr : layers) {
817
            lyr.setMinScale(maxScale);
818
        }
819
    }
820

    
821
    @Override
822
    public void setActive(boolean b) {
823
        super.setActive(b);
824
        for (FLayer layer : this.layers) {
825
            layer.setActive(b);
826
        }
827
    }
828

    
829
    @Override
830
    public boolean addLayerListener(LayerListener o) {
831
        for (FLayer layer : this.layers) {
832
            layer.addLayerListener(o);
833
        }
834
        return true;
835
    }
836

    
837
    @Override
838
    public DynObjectSet getInfo(Point p, double tolerance,
839
            Cancellable cancel) throws LoadLayerException, DataException {
840
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
841
    }
842

    
843
    @Override
844
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel,
845
            boolean fast) throws LoadLayerException, DataException {
846
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
847
    }
848

    
849
    @Override
850
    public DynObjectSet getInfo(org.gvsig.fmap.geom.primitive.Point p,
851
            double tolerance) throws LoadLayerException, DataException {
852
        int i;
853
        FLayer layer;
854
        List res = new ArrayList();
855
        for (i = 0; i < this.layers.size(); i++) {
856
            layer = (FLayer) layers.get(i);
857
            if (layer instanceof InfoByPoint) {
858
                InfoByPoint queryable_layer = (InfoByPoint) layer;
859
                res.add(queryable_layer.getInfo(p, tolerance));
860
            }
861
        }
862
        DynObjectSet[] innerSets
863
                = (DynObjectSet[]) res.toArray(new DynObjectSet[res.size()]);
864
        return new MultiDynObjectSet(innerSets);
865
    }
866

    
867
    @Override
868
    public String getTocImageIcon() {
869
        return "layer-icon-group";
870
    }
871

    
872
    /**
873
     * <p>
874
     * Sets the <code>MapContext</code> that contains this layer node.</p>
875
     *
876
     * @param mapContext the <code>MapContext</code> that contains this layer
877
     * node
878
     */
879
    public void setMapContext(MapContext mapContext) {
880
        this.fmap = mapContext;
881
    }
882

    
883
    @Override
884
    public void accept(Visitor visitor) throws BaseException {
885
        for (int i = 0; i < this.getLayersCount(); i++) {
886
            FLayer layer = this.getLayer(i);
887
//            try {
888
                if (layer instanceof LayersVisitable) {
889
                    ((LayersVisitable) layer).accept(visitor);
890
                } else {
891
                    visitor.visit(layer);
892
                }
893
//            } catch (VisitCanceledException ex) {
894
//                break;
895
//            }
896
        }
897
    }
898

    
899
    @Override
900
    public void accept(LayersVisitor visitor) throws BaseException {
901
        for (int i = 0; i < this.getLayersCount(); i++) {
902
            FLayer layer = this.getLayer(i);
903
            if (layer instanceof LayersVisitable) {
904
                ((LayersVisitable) layer).accept(visitor);
905
            } else {
906
                visitor.visit(layer);
907
            }
908
        }
909
    }
910

    
911
    @Override
912
    public Object getMetadataID() throws MetadataException {
913
        StringBuilder strb = new StringBuilder();
914
        strb.append("Layers(");
915
        strb.append(this.getName());
916
        strb.append("):{");
917
        Iterator iter = this.layers.iterator();
918
        while (iter.hasNext()) {
919
            strb.append(((FLayer) iter.next()).getMetadataID());
920
            strb.append(",");
921
        }
922
        strb.append("}");
923
        return strb.toString();
924

    
925
    }
926

    
927
    @Override
928
    public Set getMetadataChildren() {
929
        Set ret = new TreeSet();
930
        Iterator iter = this.layers.iterator();
931
        while (iter.hasNext()) {
932
            ret.add(iter.next());
933
        }
934
        return ret;
935
    }
936

    
937
    @Override
938
    public String getMetadataName() throws MetadataException {
939
        StringBuilder strb = new StringBuilder();
940
        strb.append("Layer Group '");
941
        strb.append(this.getName());
942
        strb.append("': {");
943
        Iterator iter = this.layers.iterator();
944
        while (iter.hasNext()) {
945
            strb.append(((FLayer) iter.next()).getMetadataName());
946
            strb.append(",");
947
        }
948
        strb.append("}");
949
        return strb.toString();
950
    }
951

    
952
    @Override
953
    public void beginDraw(Graphics2D g, ViewPort viewPort) {
954
        if( fmap == null ) {
955
            return;
956
        }
957
        LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
958
        fmap.fireLayerDrawingEvent(beforeEvent);
959
    }
960

    
961
    @Override
962
    public void endDraw(Graphics2D g, ViewPort viewPort) {
963
        if( fmap == null ) {
964
            return;
965
        }
966
        LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
967
        fmap.fireLayerDrawingEvent(afterEvent);
968
    }
969

    
970
    public static class RegisterPersistence implements Callable {
971

    
972
        @Override
973
        public Object call() {
974

    
975
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
976
            DynStruct definition = manager.addDefinition(
977
                    FLayers.class,
978
                    "FLayers",
979
                    "FLayers Persistence definition",
980
                    null,
981
                    null
982
            );
983
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, "FLyrDefault");
984

    
985
            definition.addDynFieldObject("mapContext").setClassOfValue(MapContext.class).setMandatory(true);
986
            definition.addDynFieldList("layers").setClassOfItems(FLayer.class).setMandatory(true);
987

    
988
            return Boolean.TRUE;
989
        }
990
    }
991

    
992
    @Override
993
    protected void doDispose() throws BaseException {
994
        if (layers != null) {
995
            for (FLayer layer : this.layers) {
996
                dispose(layer);
997
            }
998
        }
999
    }
1000

    
1001
    @Override
1002
    public void move(FLayer layer, LayerCollection group, int where, FLayer adjoiningLayer) throws LayerNotFoundInCollectionException {
1003

    
1004
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(layer));
1005
        group.addLayer(layer, where, adjoiningLayer);
1006
        removeLayer(layer);
1007
        this.updateDrawVersion();
1008
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(layer));
1009

    
1010
    }
1011

    
1012
    public void join(FLayer layer, LayerCollection group) {
1013
        try {
1014
            layers.remove(layer);
1015
            group.addLayer(layer, END, null);
1016
            this.updateDrawVersion();
1017
        } catch (LayerNotFoundInCollectionException e) {
1018
            throw new MapContextRuntimeException(e);
1019
        }
1020
    }
1021

    
1022
    @Override
1023
    public void move(FLayer layer, LayerCollection group) {
1024
        try {
1025
            move(layer, group, END, null);
1026
        } catch (LayerNotFoundInCollectionException e) {
1027
            throw new MapContextRuntimeException(e);
1028
        }
1029
    }
1030

    
1031
    @Override
1032
    public void addLayer(FLayer layer, int where, FLayer adjoiningLayer)
1033
            throws LayerNotFoundInCollectionException {
1034

    
1035
        switch (where) {
1036
            case BEGIN:
1037
                addLayer(0, layer);
1038
                break;
1039
            case BEFORE:
1040
                if (adjoiningLayer != null) {
1041
                    if (this.layers.contains(adjoiningLayer)) {
1042
                        for (int i = 0; i < this.getLayersCount(); i++) {
1043
                            if (adjoiningLayer == this.getLayer(i)) {
1044
                                addLayer(i, layer);
1045
                                break;
1046
                            }
1047
                        }
1048
                    } else {
1049
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1050
                    }
1051
                } else {
1052
                    addLayer(0, layer);
1053
                }
1054
                break;
1055
            case AFTER:
1056
                if (adjoiningLayer != null) {
1057
                    if (this.layers.contains(adjoiningLayer)) {
1058
                        for (int i = 0; i < this.getLayersCount(); i++) {
1059
                            if (adjoiningLayer == this.getLayer(i)) {
1060
                                addLayer(i + 1, layer);
1061
                                break;
1062
                            }
1063
                        }
1064
                    } else {
1065
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1066
                    }
1067
                } else {
1068
                    this.addLayer(layer);
1069
                }
1070
                break;
1071
            default: // By default add layer an the end of the collection
1072
                this.addLayer(layer);
1073
                break;
1074
        }
1075

    
1076
    }
1077

    
1078
    public FLayer getFirstActiveLayer() {
1079
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
1080
            @Override
1081
            public boolean test(FLayer layer) {
1082
                return layer.isActive();
1083
            }
1084
        });
1085
        if( theLayers.isEmpty() ) {
1086
            return null;
1087
        }
1088
        return theLayers.get(0);
1089
    }
1090

    
1091
    public FLyrVect getFirstActiveVectorLayer() {
1092
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
1093
            @Override
1094
            public boolean test(FLayer layer) {
1095
                return layer.isActive() && layer instanceof FLyrVect;
1096
            }
1097
        });
1098
        if( theLayers.isEmpty() ) {
1099
            return null;
1100
        }
1101
        return (FLyrVect) theLayers.get(0);
1102
    }
1103
    
1104
    @Override
1105
    public Iterator<FLayer> iterator() {
1106
        return this.layers.iterator();
1107
    }
1108

    
1109
    public Iterator<FLayer> deepiterator() {
1110
        List theLayers = toPlainList(this);
1111
        return theLayers.iterator();
1112
    }
1113
    
1114
    @Override
1115
    public int size() {
1116
        return this.layers.size();
1117
    }
1118
    
1119
    @Override
1120
    public FLayer get(int index) {
1121
        return this.layers.get(index);
1122
    }
1123
    
1124
    @Override
1125
    public boolean isEmpty() {
1126
        return this.layers.isEmpty();
1127
    }
1128

    
1129
    @Override
1130
    public boolean contains(Object o) {
1131
        return this.layers.contains(o);
1132
    }
1133

    
1134
    @Override
1135
    public Object[] toArray() {
1136
        return this.layers.toArray();
1137
    }
1138

    
1139
    @Override
1140
    public Object[] toArray(Object[] ts) {
1141
        return this.layers.toArray(ts);
1142
    }
1143

    
1144
    @Override
1145
    public boolean add(FLayer e) {
1146
        this.addLayer(e);
1147
        return true;
1148
    }
1149

    
1150
    @Override
1151
    public boolean remove(Object o) {
1152
        this.removeLayer((FLayer) o);
1153
        return true;
1154
    }
1155

    
1156
    @Override
1157
    public boolean containsAll(Collection clctn) {
1158
        return this.layers.containsAll(clctn);
1159
    }
1160

    
1161
    @Override
1162
    public void add(int i, FLayer e) {
1163
        this.addLayer(i, (FLayer) e);
1164
    }
1165

    
1166
    @Override
1167
    public FLayer remove(int i) {
1168
        FLayer o = this.getLayer(i);
1169
        this.removeLayer(i);
1170
        return o;
1171
    }
1172

    
1173
    @Override
1174
    public int indexOf(Object o) {
1175
        return this.layers.indexOf(o);
1176
    }
1177

    
1178
    @Override
1179
    public int lastIndexOf(Object o) {
1180
        return this.layers.lastIndexOf(o);
1181
    }
1182

    
1183
    @Override
1184
    public ListIterator listIterator() {
1185
        return this.layers.listIterator();
1186
    }
1187

    
1188
    @Override
1189
    public ListIterator listIterator(int i) {
1190
        return this.layers.listIterator(i);
1191
    }
1192

    
1193
    @Override
1194
    public List subList(int i, int i1) {
1195
        return this.layers.subList(i, i1);
1196
    }
1197
    
1198
    @Override
1199
    public boolean addAll(Collection clctn) {
1200
        Iterator it = clctn.iterator();
1201
        while( it.hasNext() ) {
1202
            this.add((FLayer) it.next());
1203
        }
1204
        return true;
1205
    }
1206

    
1207
    @Override
1208
    public boolean addAll(int i, Collection clctn) {
1209
        Iterator it = clctn.iterator();
1210
        while( it.hasNext() ) {
1211
            this.add(i, (FLayer) it.next());
1212
        }
1213
        return true;
1214
    }
1215

    
1216
    @Override
1217
    public boolean removeAll(Collection clctn) {
1218
        Iterator it = clctn.iterator();
1219
        while( it.hasNext() ) {
1220
            this.remove((FLayer) it.next());
1221
        }
1222
        return true;
1223
    }
1224

    
1225
    @Override
1226
    public boolean retainAll(Collection clctn) {
1227
        Iterator it = this.layers.iterator();
1228
        while( it.hasNext() ) {
1229
            Object o = it.next();
1230
            if( !clctn.contains(o) ) {
1231
                this.remove((FLayer) o);
1232
            }
1233
        }
1234
        return true;
1235
    }
1236

    
1237
    @Override
1238
    public FLayer set(int i, FLayer e) {
1239
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1240
    }
1241

    
1242
    public FLayers createFLayerGroup(String name) {            
1243
        FLayers group = new FLayers();
1244
        group.setMapContext(this.getMapContext());
1245
        group.setParentLayer(this);
1246
        group.setName(name);
1247
        return group;
1248
    }
1249
}