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 @ 44599

History | View | Annotate | Download (44.1 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.dal.feature.FeatureSelection;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.fmap.mapcontext.MapContext;
52
import org.gvsig.fmap.mapcontext.MapContextLocator;
53
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
54
import org.gvsig.fmap.mapcontext.Messages;
55
import org.gvsig.fmap.mapcontext.ViewPort;
56
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
57
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
58
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
59
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
60
import org.gvsig.fmap.mapcontext.layers.operations.LayerNotFoundInCollectionException;
61
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitable;
62
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
63
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
64
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
65
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
66
import org.gvsig.metadata.exceptions.MetadataException;
67
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dynobject.DynObjectSet;
69
import org.gvsig.tools.dynobject.DynStruct;
70
import org.gvsig.tools.dynobject.impl.MultiDynObjectSet;
71
import org.gvsig.tools.exception.BaseException;
72
import org.gvsig.tools.persistence.PersistenceManager;
73
import org.gvsig.tools.persistence.PersistentState;
74
import org.gvsig.tools.persistence.exception.PersistenceException;
75
import org.gvsig.tools.task.Cancellable;
76
import org.gvsig.tools.util.Callable;
77
import org.gvsig.tools.visitor.Visitor;
78

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

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

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

    
106
    protected MapContext fmap;
107

    
108
    /**
109
     * Useful for debug the problems during the implementation.
110
     */
111
    private static final Logger LOGGER = LoggerFactory.getLogger(FLayers.class);
112
    
113
    public static final Predicate<FLayer> LAYER_ACTIVE = new Predicate<FLayer>() {
114
        @Override
115
        public boolean test(FLayer layer) {
116
            return layer.isActive();
117
        }
118
    };
119

    
120
    public static final Predicate<FLayer> LAYER_AVALAIBLE = new Predicate<FLayer>() {
121
        @Override
122
        public boolean test(FLayer layer) {
123
            return layer.isAvailable();
124
        }
125
    };
126

    
127
    public static Predicate<FLayer> LAYER_FLYRVECT = new Predicate<FLayer>() {
128
        @Override
129
        public boolean test(FLayer layer) {
130
            return layer instanceof FLyrVect;
131
        }
132
    };
133
    public static Predicate<FLayer> LAYER_SELECTION_AVAILABLE = new Predicate<FLayer>() {
134
        @Override
135
        public boolean test(FLayer layer) {
136
            if (layer instanceof FLyrVect) {
137
                try {
138
                    FeatureStore store = ((FLyrVect) layer).getFeatureStore();
139
                    if (store.getFeatureSelection().isAvailable()) {
140
                        return true;
141
                    }
142
                } catch (Exception ex) {
143
                    return false;
144
                }
145
            }
146
            return false;
147
        }
148
    };
149
    
150
    public static Predicate<FLayer> LAYER_SELECTION_EMPTY = new Predicate<FLayer>() {
151
        @Override
152
        public boolean test(FLayer layer) {
153
            if (layer instanceof FLyrVect) {
154
                try {
155
                    FeatureStore store = ((FLyrVect) layer).getFeatureStore();
156
                    FeatureSelection selection = store.getFeatureSelection();
157
                    if (!selection.isAvailable() || selection.isEmpty()) {
158
                        return true;
159
                    }
160
                    return false;
161
                } catch (Exception ex) {
162
                    return true;
163
                }
164
            } else {
165
                return true;
166
            }
167
        }
168
    };
169

    
170
    public FLayers() {
171
        super();
172
        layerCollectionListeners = new ArrayList();
173
        layers = Collections.synchronizedList(new ArrayList());
174
    }
175

    
176
    @Override
177
    public void addLayerCollectionListener(LayerCollectionListener listener) {
178
        if (!layerCollectionListeners.contains(listener)) {
179
            layerCollectionListeners.add(listener);
180
        }
181
    }
182

    
183
    /*
184
     * (non-Javadoc)
185
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
186
     */
187
    @Override
188
    public void setAllVisibles(boolean visible) {
189
        FLayer lyr;
190

    
191
        for (int i = 0; i < layers.size(); i++) {
192
            lyr = ((FLayer) layers.get(i));
193
            lyr.setVisible(visible);
194

    
195
            if (lyr instanceof LayerCollection) {
196
                ((LayerCollection) lyr).setAllVisibles(visible);
197
            }
198
        }
199
    }
200

    
201
    @Override
202
    public void removeLayerCollectionListener(LayerCollectionListener listener) {
203
        layerCollectionListeners.remove(listener);
204
    }
205

    
206
    /**
207
     * Adds a layer on an specified position in this node.
208
     *
209
     * @param pos position in the inner list where the layer will be added
210
     * @param layer a layer
211
     */
212
    private void doAddLayer(int pos, FLayer layer) {
213
        layers.add(pos, layer);
214
        ToolsLocator.getDisposableManager().bind(layer);
215
        layer.setParentLayer(this);
216
        IProjection layerProj = layer.getProjection();
217
        if (layerProj != null && fmap != null) {
218
            IProjection mapContextProj = fmap.getProjection();
219
            // TODO REVISAR ESTO !!!!
220
            // Esta condici?n puede que no fuese exacta para todos los casos
221
            if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
222
                ICoordTrans ct = layerProj.getCT(mapContextProj);
223
                layer.setCoordTrans(ct);
224
            } else {
225
                layer.setCoordTrans(null);
226
            }
227
        }
228
        this.updateDrawVersion();
229
    }
230

    
231
    @Override
232
    public void addLayer(FLayer layer) {
233

    
234
        MapContext mco = this.getMapContext();
235

    
236
        if (mco != null) {
237
            /*
238
             * Get order manager from map context
239
             */
240
            int position = mco.getOrderManager().getPosition(this, layer);
241
            addLayer(position, layer);
242
        } else {
243
            /*
244
             * This happens when FLayers object is not in a
245
             * map context, so no order manager is available.
246
             */
247
            addLayer(layers.size(), layer);
248
        }
249
    }
250
    
251
    public void addLayer(DataStore store) {
252
        FLayer layer;
253
        try {
254
            layer = MapContextLocator.getMapContextManager().createLayer(
255
                    store.getName(),
256
                    store
257
            );
258
            
259
        } catch (LoadLayerException ex) {
260
            throw new RuntimeException("Can't create layer from store.", ex);
261
        }
262
        this.addLayer(layer);
263
    }
264

    
265
    /**
266
     * Adds a layer in an specified position in this node.
267
     *
268
     * @param pos
269
     * @param layer a layer
270
     */
271
    public void addLayer(int pos, FLayer layer) {
272
        try {
273
            if (layer instanceof FLayers) {
274
                FLayers layersToAdd = (FLayers) layer;
275
                if( fmap != null ) {
276
                    fmap.addAsCollectionListener(layersToAdd);
277
                }
278
            }
279
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
280

    
281
            doAddLayer(pos, layer);
282

    
283
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
284
        } catch (CancelationException e) {
285
            LOGGER.warn(e.getMessage());
286
        }
287
    }
288

    
289
    @Override
290
    public void moveTo(int from, int to) throws CancelationException {
291
        int newfrom = layers.size() - from - 1;
292
        int newto = layers.size() - to - 1;
293
        if (newfrom < 0 || newfrom >= layers.size() || newto < 0 || newto >= layers.size()) {
294
            return;
295
        }
296
        FLayer aux = (FLayer) layers.get(newfrom);
297
        callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
298
        layers.remove(newfrom);
299
        layers.add(newto, aux);
300
        this.updateDrawVersion();
301
        callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
302
    }
303

    
304
    /**
305
     * Removes an inner layer.
306
     *
307
     * @param lyr a layer
308
     */
309
    private void doRemoveLayer(FLayer lyr) {
310
        layers.remove(lyr);
311
        lyr.dispose();
312
        this.updateDrawVersion();
313
    }
314

    
315
    @Override
316
    public void removeLayer(FLayer lyr) throws CancelationException {
317
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
318
        doRemoveLayer(lyr);
319
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
320
    }
321

    
322
    @Override
323
    public void removeLayer(int idLayer) {
324
        FLayer lyr = (FLayer) layers.get(idLayer);
325
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
326
        this.doRemoveLayer(lyr);
327
//                layers.remove(idLayer);
328
//                this.updateDrawVersion();
329
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
330
    }
331

    
332
    @Override
333
    public void removeLayer(String layerName) {
334
        FLayer lyr;
335

    
336
        for (int i = 0; i < layers.size(); i++) {
337
            lyr = ((FLayer) layers.get(i));
338

    
339
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
340
                removeLayer(i);
341

    
342
                break;
343
            }
344
        }
345
    }
346

    
347
    /**
348
     * Replace a layer identified by its name, by another.
349
     *
350
     * @param layerName the name of the layer to be replaced
351
     * @param layer the new layer
352
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
353
     * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
354
     */
355
    public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException {
356
        replaceLayer(getLayer(layerName), layer);
357
    }
358

    
359
    /**
360
     * Replace a layer by another layer. It search recursively by all the
361
     * ILayerCollection nodes
362
     *
363
     * @param layer the layer to be replaced
364
     * @param newLayer the new layer
365
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
366
     */
367
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
368
        replaceLayer(this, layer, newLayer);
369
    }
370

    
371
    /**
372
     * Replace a layer by other layer. It search recursively by all the
373
     * ILayerCollection nodes
374
     *
375
     * @param parentLayer the parent layer
376
     * @param layer the layer to be replaced
377
     * @param newLayer the new layer
378
     * @throws LoadLayerException
379
     */
380
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
381
        FLayer lyr;
382
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
383
            lyr = ((FLayer) parentLayer.getLayer(i));
384
            if (lyr.equals(layer)) {
385
                parentLayer.removeLayer(i);
386
                parentLayer.addLayer(i, newLayer);
387
                break;
388
            }
389
            if (lyr instanceof LayerCollection) {
390
                replaceLayer((FLayers) lyr, layer, newLayer);
391
            }
392
        }
393
    }
394

    
395
    @Override
396
    public FLayer[] getVisibles() {
397
        ArrayList array = new ArrayList();
398
        LayersIterator iter = new LayersIterator(this) {
399
            @Override
400
            public boolean evaluate(FLayer layer) {
401
                return layer.isVisible();
402
            }
403

    
404
        };
405

    
406
        while (iter.hasNext()) {
407
            array.add(iter.nextLayer());
408
        }
409

    
410
        return (FLayer[]) array.toArray(new FLayer[0]);
411
    }
412

    
413
    @Override
414
    public FLayer getLayer(int index) {
415
        return (FLayer) layers.get(index);
416
    }
417

    
418
    @Override
419
    public FLayer getLayer(final String layerName) {
420
        if( StringUtils.isBlank(layerName) ) {
421
            return null;
422
        }
423
        for (FLayer layer : this.layers) {
424
            if( layer!=null && StringUtils.equalsIgnoreCase(layer.getName(), layerName) ) {
425
                return layer;
426
            }
427
            if( layer instanceof FLayers ) {
428
                List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
429
                    @Override
430
                    public boolean test(FLayer layer) {
431
                        return StringUtils.equalsIgnoreCase(layer.getName(), layerName);                        
432
                    }
433
                });
434
                if( theLayers.isEmpty() ) {
435
                    return null;
436
                }
437
                return theLayers.get(0);
438
            }
439
        }
440
        return null;
441
    }
442

    
443
    @Override
444
    public FLayer getLayer(final DataStore store) {
445
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
446
            @Override
447
            public boolean test(FLayer layer) {
448
                return  layer instanceof SingleLayer && store == ((SingleLayer) layer).getDataStore();
449
            }
450
        });
451
        if( theLayers.isEmpty() ) {
452
            return null;
453
        }
454
        return theLayers.get(0);
455
    }
456
    
457
    private List<FLayer> toPlainList(FLayer layer) {
458
        return toPlainList(layer, new ArrayList<FLayer>(), null);
459
    }
460

    
461
    private List<FLayer> toPlainList(FLayer layer, Predicate<FLayer> filter) {
462
        return toPlainList(layer, new ArrayList<FLayer>(), filter);
463
    }
464

    
465
    private List<FLayer> toPlainList(FLayer layer, List<FLayer> result, Predicate<FLayer> filter) {
466
        if (layer instanceof FLayers) {
467
            FLayers layerGroup = (FLayers) layer;
468
            if (filter == null || filter.test(layer)) {
469
                result.add(layer);
470
            }
471
            for (int i = 0; i < layerGroup.getLayersCount(); i++) {
472
                toPlainList(layerGroup.getLayer(i), result, filter);
473
            }
474
        } else if( filter==null || filter.test(layer) ) {
475
            result.add(layer);
476
        }
477
        return result;
478
    }
479

    
480
    @Override
481
    public int getLayersCount() {
482
        return layers.size();
483
    }
484

    
485
    @Override
486
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
487
            Cancellable cancel, double scale) throws ReadException {
488
        // FIXME Arreglar este error
489
        throw new RuntimeException("Esto no deberia de llamarse");
490
    }
491

    
492
    /*
493
     * (non-Javadoc)
494
     *
495
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
496
     * com.iver.cit.gvsig.fmap.ViewPort,
497
     * com.iver.utiles.swing.threads.Cancellable, double,
498
     * javax.print.attribute.PrintAttributes)
499
     */
500
    @Override
501
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
502
            double scale, PrintAttributes properties)
503
            throws ReadException {
504
        throw new RuntimeException("No deberia pasar por aqui");
505
    }
506

    
507
    public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
508
            double scale, PrintAttributes properties)
509
            throws ReadException {
510
        this.print_old(g, viewPort, cancel, scale, properties, null);
511
    }
512

    
513
    /**
514
     * <p>
515
     * Checks all layers (each one as a sub-node of this node <i>collection of
516
     * layers</i>) of this collection and draws their requested properties. If a
517
     * node is a group of layers (<code>ComposedLayer</code>), executes it's
518
     * drawn.</p>
519
     *
520
     * <p>
521
     * All nodes which could group with the composed layer <code>group</code>,
522
     * will be drawn together. And once the <code>
523
     * group</code> is drawn, will be set to <code>null</code> if hasn't a
524
     * parent layer.</p>
525
     *
526
     * <p>
527
     * The particular implementation depends on the kind of each layer and
528
     * composed layer. And this process can be cancelled at any time by the
529
     * shared object <code>cancel</code>.</p>
530
     *
531
     * <p>
532
     * According the print quality, labels will be printed in different
533
     * resolution:
534
     * <ul>
535
     * <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
536
     * <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
537
     * <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
538
     * </ul>
539
     * </p>
540
     *
541
     * @param g for rendering 2-dimensional shapes, text and images on the
542
     * Java(tm) platform
543
     * @param viewPort the information for drawing the layers
544
     * @param cancel shared object that determines if this layer can continue
545
     * being drawn
546
     * @param scale the scale of the view. Must be between
547
     * {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
548
     * @param properties properties that will be print
549
     * @param group a composed layer pending to paint; if this parameter is
550
     * <code>null</code>, the composed layer
551
     *
552
     * @return <code>null</code> if the layers in <code>group</code> had been
553
     * drawn or were <code>null</code>; otherwise, the <code>group</code>
554
     * @throws org.gvsig.fmap.dal.exception.ReadException
555
     *
556
     * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double,
557
     * PrintAttributes)
558
     */
559
    public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
560
            throws ReadException {
561
        double dpi = 72;
562

    
563
        int resolution = properties.getPrintQuality();
564
        switch (resolution) {
565
            case PrintAttributes.PRINT_QUALITY_NORMAL:
566
                dpi = 300;
567
                break;
568
            case PrintAttributes.PRINT_QUALITY_HIGH:
569
                dpi = 600;
570
                break;
571
            case PrintAttributes.PRINT_QUALITY_DRAFT:
572
                dpi = 72;
573
                break;
574
            default:
575
                break;
576
        }
577

    
578
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
579
        // calculando la escala en pantalla, no para el layout.
580
        // Revisar esto.
581
        // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
582
        for (int i = 0; i < layers.size(); i++) {
583
            FLayer lyr = (FLayer) layers.get(i);
584
            if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
585
                continue;
586
            }
587

    
588
            try {
589

    
590
                // Checks for draw group (ComposedLayer)
591
                if (group != null) {
592
                    if (lyr instanceof FLayers) {
593
                        group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
594
                    } else {
595
                        // If layer can be added to the group, does it
596
                        if (lyr instanceof ILabelable
597
                                && ((ILabelable) lyr).isLabeled()
598
                                && ((ILabelable) lyr).getLabelingStrategy() != null
599
                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
600
                            group.add(lyr);
601
                        } else {
602
                            // draw the 'pending to draw' layer group
603
                            group.print(g, viewPort, cancel, scale, properties);
604

    
605
                            // gets a new group instance
606
                            if (lyr instanceof ILabelable
607
                                    && ((ILabelable) lyr).isLabeled()
608
                                    && ((ILabelable) lyr).getLabelingStrategy() != null
609
                                    && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
610
                                group = lyr.newComposedLayer();
611
                            } else {
612
                                group = null;
613
                            }
614
                            // if layer hasn't group, draws it inmediately
615
                            if (group == null) {
616
                                if (lyr instanceof FLayers) {
617
                                    group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
618
                                } else {
619
                                    lyr.print(g, viewPort, cancel, scale, properties);
620
                                    if (lyr instanceof ILabelable
621
                                            && ((ILabelable) lyr).isLabeled()
622
                                            && ((ILabelable) lyr).getLabelingStrategy() != null
623
                                            && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
624
                                        ILabelable lLayer = (ILabelable) lyr;
625
                                        lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
626
                                    }
627
                                }
628
                            } else {
629
                                // add the layer to the group
630
                                group.setMapContext(fmap);
631
                                group.add(lyr);
632

    
633
                            }
634

    
635
                        }
636
                    }
637
                } else {
638
                    // gets a new group instance
639
                    group = lyr.newComposedLayer();
640
                    // if layer hasn't group, draws it inmediately
641
                    if (group == null) {
642
                        if (lyr instanceof FLayers) {
643
                            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
644
                        } else {
645
                            lyr.print(g, viewPort, cancel, scale, properties);
646
                            if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
647
                                ILabelable lLayer = (ILabelable) lyr;
648

    
649
                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
650
                            }
651
                        }
652
                    } else {
653
                        // add the layer to the group
654
                        group.setMapContext(fmap);
655
                        group.add(lyr);
656

    
657
                    }
658
                }
659

    
660
            } catch (Exception e) {
661
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
662
                if( fmap!=null ) {
663
                    fmap.addLayerError(mesg);
664
                }
665
                LOGGER.error(mesg, e);
666
            }
667

    
668
        }
669

    
670
        if (group != null && this.getParentLayer() == null) {
671
            //si tenemos un grupo pendiente de pintar, pintamos
672
            group.print(g, viewPort, cancel, scale, properties);
673
            group = null;
674

    
675
        }
676
        return group;
677
    }
678

    
679
    @Override
680
    @SuppressWarnings("UseSpecificCatch")
681
    public Envelope getFullEnvelope() {
682
        Envelope rAux = null;
683
        boolean first = true;
684

    
685
        for (FLayer capa : layers) {
686
            try {
687
                if (first) {
688
                    rAux = (Envelope) capa.getFullEnvelope().clone();
689
                    first = false;
690
                } else {
691
                    rAux.add(capa.getFullEnvelope());
692
                }
693
            } catch (Exception e) {
694
                LOGGER.warn("Can't calculate the envelope of the layer group '"+this.getName()+"'.",e);
695
            }
696
        }
697

    
698
        return rAux;
699
    }
700

    
701
    /**
702
     * Notifies all listeners associated to this collection of layers, that
703
     * another layer is going to be added or replaced in the internal list of
704
     * layers.
705
     *
706
     * @param event a layer collection event with the new layer
707
     */
708
    protected void callLayerAdding(LayerCollectionEvent event)
709
            throws CancelationException {
710
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
711
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
712
            ((LayerCollectionListener) iter.next()).layerAdding(event);
713
        }
714
    }
715

    
716
    /**
717
     * Notifies all listeners associated to this collection of layers, that a
718
     * layer is going to be removed from the internal list of layers.
719
     *
720
     * @param event a layer collection event with the layer being removed
721
     *
722
     * @throws CancelationException any exception produced during the
723
     * cancellation of the driver.
724
     */
725
    protected void callLayerRemoving(LayerCollectionEvent event)
726
            throws CancelationException {
727
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
728
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
729
            ((LayerCollectionListener) iter.next()).layerRemoving(event);
730
        }
731
    }
732

    
733
    /**
734
     * Notifies all listeners associated to this collection of layers, that a
735
     * layer is going to be moved in the internal list of layers.
736
     *
737
     * @param event a layer collection event with the layer being moved, and the
738
     * initial and final positions
739
     *
740
     * @throws CancelationException any exception produced during the
741
     * cancellation of the driver.
742
     */
743
    protected void callLayerMoving(LayerPositionEvent event)
744
            throws CancelationException {
745
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
746
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
747
            ((LayerCollectionListener) iter.next()).layerMoving(event);
748
        }
749
    }
750

    
751
    /**
752
     * Notifies all listeners associated to this collection of layers, that
753
     * another layer has been added or replaced in the internal list of layers.
754
     *
755
     * @param event a layer collection event with the new layer
756
     */
757
    protected void callLayerAdded(LayerCollectionEvent event) {
758
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
759
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
760
            ((LayerCollectionListener) iter.next()).layerAdded(event);
761
        }
762
    }
763

    
764
    /**
765
     * Notifies all listeners associated to this collection of layers, that
766
     * another layer has been removed from the internal list of layers.
767
     *
768
     * @param event a layer collection event with the layer removed
769
     */
770
    protected void callLayerRemoved(LayerCollectionEvent event) {
771
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
772
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
773
            ((LayerCollectionListener) iter.next()).layerRemoved(event);
774
        }
775
    }
776

    
777
    /**
778
     * Notifies all listeners associated to this collection of layers, that
779
     * another layer has been moved in the internal list of layers.
780
     *
781
     * @param event a layer collection event with the layer moved, and the initial
782
     * and final positions
783
     */
784
    protected void callLayerMoved(LayerPositionEvent event) {
785
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
786
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
787
            ((LayerCollectionListener) iter.next()).layerMoved(event);
788
        }
789
    }
790

    
791
    @Override
792
    public void saveToState(PersistentState state) throws PersistenceException {
793

    
794
        super.saveToState(state);
795

    
796
        state.set("mapContext", fmap);
797

    
798
        List layersToSave = new ArrayList();
799
        for (FLayer layer : this.layers) {
800
            if (!layer.isTemporary()) {
801
                layersToSave.add(layer);
802
            }
803
        }
804
        state.set("layers", layersToSave);
805
    }
806

    
807
    @Override
808
    public void loadFromState(PersistentState state) throws PersistenceException {
809

    
810
        super.loadFromState(state);
811

    
812
        setMapContext((MapContext) state.get("mapContext"));
813
        Iterator iter = state.getIterator("layers");
814
        while (iter.hasNext()) {
815
            FLayer item = (FLayer) iter.next();
816
            // Cuando falla la carga de la capa desde la persistencia, puede
817
            // haberse perdido quien es el parent. Asi que lo asignamos aqui
818
            // por si acaso.
819
            item.setParentLayer(this);
820
            layers.add(item);
821
        }
822
    }
823

    
824
    @Override
825
    public MapContext getMapContext() {
826
        return fmap;
827
    }
828

    
829
    @Override
830
    public void setAllActives(boolean active) {
831
        FLayer lyr;
832

    
833
        for (int i = 0; i < layers.size(); i++) {
834
            lyr = ((FLayer) layers.get(i));
835
            lyr.setActive(active);
836

    
837
            if (lyr instanceof LayerCollection) {
838
                ((LayerCollection) lyr).setAllActives(active);
839
            }
840
        }
841
    }
842

    
843
    @Override
844
    public List<FLayer> getLayers(Predicate<FLayer> filter) {
845
        List<FLayer> theLayers = this.toPlainList(this, filter);
846
        return theLayers;
847
    }
848
    
849
    @Override
850
    public FLayer[] getActives() {
851
        List<FLayer> activeLayers = this.getLayers(new Predicate<FLayer>() {
852
            @Override
853
            public boolean test(FLayer layer) {
854
                return layer.isActive();
855
            }
856
        });
857
        return (FLayer[]) activeLayers.toArray(new FLayer[activeLayers.size()]);
858
    }
859
                   
860
    @Override
861
    public double getMinScale() {
862
        return -1; // La visibilidad o no la controla cada capa
863
        // dentro de una colecci?n
864
    }
865

    
866
    @Override
867
    public double getMaxScale() {
868
        return -1;
869
    }
870

    
871
    @Override
872
    public void setMinScale(double minScale) {
873
        for (FLayer lyr : layers) {
874
            lyr.setMinScale(minScale);
875
        }
876
    }
877

    
878
    @Override
879
    public void setMaxScale(double maxScale) {
880
        for (FLayer lyr : layers) {
881
            lyr.setMinScale(maxScale);
882
        }
883
    }
884

    
885
    @Override
886
    public void setActive(boolean b) {
887
        super.setActive(b);
888
        for (FLayer layer : this.layers) {
889
            layer.setActive(b);
890
        }
891
    }
892

    
893
    @Override
894
    public boolean addLayerListener(LayerListener o) {
895
        for (FLayer layer : this.layers) {
896
            layer.addLayerListener(o);
897
        }
898
        return true;
899
    }
900

    
901
    @Override
902
    public DynObjectSet getInfo(Point p, double tolerance,
903
            Cancellable cancel) throws LoadLayerException, DataException {
904
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
905
    }
906

    
907
    @Override
908
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel,
909
            boolean fast) throws LoadLayerException, DataException {
910
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
911
    }
912

    
913
    @Override
914
    public DynObjectSet getInfo(org.gvsig.fmap.geom.primitive.Point p,
915
            double tolerance) throws LoadLayerException, DataException {
916
        int i;
917
        FLayer layer;
918
        List res = new ArrayList();
919
        for (i = 0; i < this.layers.size(); i++) {
920
            layer = (FLayer) layers.get(i);
921
            if (layer instanceof InfoByPoint) {
922
                InfoByPoint queryable_layer = (InfoByPoint) layer;
923
                res.add(queryable_layer.getInfo(p, tolerance));
924
            }
925
        }
926
        DynObjectSet[] innerSets
927
                = (DynObjectSet[]) res.toArray(new DynObjectSet[res.size()]);
928
        return new MultiDynObjectSet(innerSets);
929
    }
930

    
931
    @Override
932
    public String getTocImageIcon() {
933
        return "layer-icon-group";
934
    }
935

    
936
    /**
937
     * <p>
938
     * Sets the <code>MapContext</code> that contains this layer node.</p>
939
     *
940
     * @param mapContext the <code>MapContext</code> that contains this layer
941
     * node
942
     */
943
    public void setMapContext(MapContext mapContext) {
944
        this.fmap = mapContext;
945
    }
946

    
947
    @Override
948
    public void accept(Visitor visitor) throws BaseException {
949
        for (int i = 0; i < this.getLayersCount(); i++) {
950
            FLayer layer = this.getLayer(i);
951
//            try {
952
                if (layer instanceof LayersVisitable) {
953
                    ((LayersVisitable) layer).accept(visitor);
954
                } else {
955
                    visitor.visit(layer);
956
                }
957
//            } catch (VisitCanceledException ex) {
958
//                break;
959
//            }
960
        }
961
    }
962

    
963
    @Override
964
    public void accept(LayersVisitor visitor) throws BaseException {
965
        for (int i = 0; i < this.getLayersCount(); i++) {
966
            FLayer layer = this.getLayer(i);
967
            if (layer instanceof LayersVisitable) {
968
                ((LayersVisitable) layer).accept(visitor);
969
            } else {
970
                visitor.visit(layer);
971
            }
972
        }
973
    }
974

    
975
    @Override
976
    public Object getMetadataID() throws MetadataException {
977
        StringBuilder strb = new StringBuilder();
978
        strb.append("Layers(");
979
        strb.append(this.getName());
980
        strb.append("):{");
981
        Iterator iter = this.layers.iterator();
982
        while (iter.hasNext()) {
983
            strb.append(((FLayer) iter.next()).getMetadataID());
984
            strb.append(",");
985
        }
986
        strb.append("}");
987
        return strb.toString();
988

    
989
    }
990

    
991
    @Override
992
    public Set getMetadataChildren() {
993
        Set ret = new TreeSet();
994
        Iterator iter = this.layers.iterator();
995
        while (iter.hasNext()) {
996
            ret.add(iter.next());
997
        }
998
        return ret;
999
    }
1000

    
1001
    @Override
1002
    public String getMetadataName() throws MetadataException {
1003
        StringBuilder strb = new StringBuilder();
1004
        strb.append("Layer Group '");
1005
        strb.append(this.getName());
1006
        strb.append("': {");
1007
        Iterator iter = this.layers.iterator();
1008
        while (iter.hasNext()) {
1009
            strb.append(((FLayer) iter.next()).getMetadataName());
1010
            strb.append(",");
1011
        }
1012
        strb.append("}");
1013
        return strb.toString();
1014
    }
1015

    
1016
    @Override
1017
    public void beginDraw(Graphics2D g, ViewPort viewPort) {
1018
        if( fmap == null ) {
1019
            return;
1020
        }
1021
        LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
1022
        fmap.fireLayerDrawingEvent(beforeEvent);
1023
    }
1024

    
1025
    @Override
1026
    public void endDraw(Graphics2D g, ViewPort viewPort) {
1027
        if( fmap == null ) {
1028
            return;
1029
        }
1030
        LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
1031
        fmap.fireLayerDrawingEvent(afterEvent);
1032
    }
1033

    
1034
    public static class RegisterPersistence implements Callable {
1035

    
1036
        @Override
1037
        public Object call() {
1038

    
1039
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1040
            DynStruct definition = manager.addDefinition(
1041
                    FLayers.class,
1042
                    "FLayers",
1043
                    "FLayers Persistence definition",
1044
                    null,
1045
                    null
1046
            );
1047
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, "FLyrDefault");
1048

    
1049
            definition.addDynFieldObject("mapContext").setClassOfValue(MapContext.class).setMandatory(true);
1050
            definition.addDynFieldList("layers").setClassOfItems(FLayer.class).setMandatory(true);
1051

    
1052
            return Boolean.TRUE;
1053
        }
1054
    }
1055

    
1056
    @Override
1057
    protected void doDispose() throws BaseException {
1058
        if (layers != null) {
1059
            for (FLayer layer : this.layers) {
1060
                dispose(layer);
1061
            }
1062
        }
1063
    }
1064

    
1065
    @Override
1066
    public void move(FLayer layer, LayerCollection group, int where, FLayer adjoiningLayer) throws LayerNotFoundInCollectionException {
1067

    
1068
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(layer));
1069
        group.addLayer(layer, where, adjoiningLayer);
1070
        removeLayer(layer);
1071
        this.updateDrawVersion();
1072
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(layer));
1073

    
1074
    }
1075

    
1076
    public void join(FLayer layer, LayerCollection group) {
1077
        try {
1078
            layers.remove(layer);
1079
            group.addLayer(layer, END, null);
1080
            this.updateDrawVersion();
1081
        } catch (LayerNotFoundInCollectionException e) {
1082
            throw new MapContextRuntimeException(e);
1083
        }
1084
    }
1085

    
1086
    @Override
1087
    public void move(FLayer layer, LayerCollection group) {
1088
        try {
1089
            move(layer, group, END, null);
1090
        } catch (LayerNotFoundInCollectionException e) {
1091
            throw new MapContextRuntimeException(e);
1092
        }
1093
    }
1094

    
1095
    @Override
1096
    public void addLayer(FLayer layer, int where, FLayer adjoiningLayer)
1097
            throws LayerNotFoundInCollectionException {
1098

    
1099
        switch (where) {
1100
            case BEGIN:
1101
                addLayer(0, layer);
1102
                break;
1103
            case BEFORE:
1104
                if (adjoiningLayer != null) {
1105
                    if (this.layers.contains(adjoiningLayer)) {
1106
                        for (int i = 0; i < this.getLayersCount(); i++) {
1107
                            if (adjoiningLayer == this.getLayer(i)) {
1108
                                addLayer(i, layer);
1109
                                break;
1110
                            }
1111
                        }
1112
                    } else {
1113
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1114
                    }
1115
                } else {
1116
                    addLayer(0, layer);
1117
                }
1118
                break;
1119
            case AFTER:
1120
                if (adjoiningLayer != null) {
1121
                    if (this.layers.contains(adjoiningLayer)) {
1122
                        for (int i = 0; i < this.getLayersCount(); i++) {
1123
                            if (adjoiningLayer == this.getLayer(i)) {
1124
                                addLayer(i + 1, layer);
1125
                                break;
1126
                            }
1127
                        }
1128
                    } else {
1129
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1130
                    }
1131
                } else {
1132
                    this.addLayer(layer);
1133
                }
1134
                break;
1135
            default: // By default add layer an the end of the collection
1136
                this.addLayer(layer);
1137
                break;
1138
        }
1139

    
1140
    }
1141

    
1142
    public FLayer getFirstActiveLayer() {
1143
        List<FLayer> theLayers = this.getLayers(LAYER_ACTIVE);
1144
        if( theLayers.isEmpty() ) {
1145
            return null;
1146
        }
1147
        return theLayers.get(0);
1148
    }
1149

    
1150
    public FLyrVect getFirstActiveVectorLayer() {
1151
        List<FLayer> theLayers = this.getLayers(LAYER_ACTIVE.and(LAYER_FLYRVECT));
1152
        if( theLayers.isEmpty() ) {
1153
            return null;
1154
        }
1155
        return (FLyrVect) theLayers.get(0);
1156
    }
1157
    
1158
    @Override
1159
    public Iterator<FLayer> iterator() {
1160
        return this.layers.iterator();
1161
    }
1162

    
1163
    public Iterator<FLayer> deepiterator() {
1164
        List theLayers = toPlainList(this);
1165
        return theLayers.iterator();
1166
    }
1167
    
1168
    @Override
1169
    public int size() {
1170
        return this.layers.size();
1171
    }
1172
    
1173
    @Override
1174
    public FLayer get(int index) {
1175
        return this.layers.get(index);
1176
    }
1177
    
1178
    @Override
1179
    public boolean isEmpty() {
1180
        return this.layers.isEmpty();
1181
    }
1182

    
1183
    @Override
1184
    public boolean contains(Object o) {
1185
        return this.layers.contains(o);
1186
    }
1187

    
1188
    @Override
1189
    public Object[] toArray() {
1190
        return this.layers.toArray();
1191
    }
1192

    
1193
    @Override
1194
    public Object[] toArray(Object[] ts) {
1195
        return this.layers.toArray(ts);
1196
    }
1197

    
1198
    @Override
1199
    public boolean add(FLayer e) {
1200
        this.addLayer(e);
1201
        return true;
1202
    }
1203

    
1204
    @Override
1205
    public boolean remove(Object o) {
1206
        this.removeLayer((FLayer) o);
1207
        return true;
1208
    }
1209

    
1210
    @Override
1211
    public boolean containsAll(Collection clctn) {
1212
        return this.layers.containsAll(clctn);
1213
    }
1214

    
1215
    @Override
1216
    public void add(int i, FLayer e) {
1217
        this.addLayer(i, (FLayer) e);
1218
    }
1219

    
1220
    @Override
1221
    public FLayer remove(int i) {
1222
        FLayer o = this.getLayer(i);
1223
        this.removeLayer(i);
1224
        return o;
1225
    }
1226

    
1227
    @Override
1228
    public int indexOf(Object o) {
1229
        return this.layers.indexOf(o);
1230
    }
1231

    
1232
    @Override
1233
    public int lastIndexOf(Object o) {
1234
        return this.layers.lastIndexOf(o);
1235
    }
1236

    
1237
    @Override
1238
    public ListIterator listIterator() {
1239
        return this.layers.listIterator();
1240
    }
1241

    
1242
    @Override
1243
    public ListIterator listIterator(int i) {
1244
        return this.layers.listIterator(i);
1245
    }
1246

    
1247
    @Override
1248
    public List subList(int i, int i1) {
1249
        return this.layers.subList(i, i1);
1250
    }
1251
    
1252
    @Override
1253
    public boolean addAll(Collection clctn) {
1254
        Iterator it = clctn.iterator();
1255
        while( it.hasNext() ) {
1256
            this.add((FLayer) it.next());
1257
        }
1258
        return true;
1259
    }
1260

    
1261
    @Override
1262
    public boolean addAll(int i, Collection clctn) {
1263
        Iterator it = clctn.iterator();
1264
        while( it.hasNext() ) {
1265
            this.add(i, (FLayer) it.next());
1266
        }
1267
        return true;
1268
    }
1269

    
1270
    @Override
1271
    public boolean removeAll(Collection clctn) {
1272
        Iterator it = clctn.iterator();
1273
        while( it.hasNext() ) {
1274
            this.remove((FLayer) it.next());
1275
        }
1276
        return true;
1277
    }
1278

    
1279
    @Override
1280
    public boolean retainAll(Collection clctn) {
1281
        Iterator it = this.layers.iterator();
1282
        while( it.hasNext() ) {
1283
            Object o = it.next();
1284
            if( !clctn.contains(o) ) {
1285
                this.remove((FLayer) o);
1286
            }
1287
        }
1288
        return true;
1289
    }
1290

    
1291
    @Override
1292
    public FLayer set(int i, FLayer e) {
1293
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1294
    }
1295

    
1296
    public FLayers createFLayerGroup(String name) {            
1297
        FLayers group = new FLayers();
1298
        group.setMapContext(this.getMapContext());
1299
        group.setParentLayer(this);
1300
        group.setName(name);
1301
        return group;
1302
    }
1303
}