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

History | View | Annotate | Download (42 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 42287 jjdelcerro
 * 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 40435 jjdelcerro
 *
11 42287 jjdelcerro
 * 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 40435 jjdelcerro
 *
16 42287 jjdelcerro
 * 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 40435 jjdelcerro
 *
20 42287 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
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 42293 jjdelcerro
import java.util.Collection;
30 40435 jjdelcerro
import java.util.Collections;
31
import java.util.Iterator;
32
import java.util.List;
33 42293 jjdelcerro
import java.util.ListIterator;
34 40435 jjdelcerro
import java.util.Set;
35
import java.util.TreeSet;
36 44073 jjdelcerro
import java.util.function.Predicate;
37
import org.apache.commons.lang3.StringUtils;
38 40435 jjdelcerro
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 43075 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
46 40435 jjdelcerro
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 43075 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextLocator;
51 40435 jjdelcerro
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 44073 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
62 42513 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63 40435 jjdelcerro
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 41840 jjdelcerro
import org.gvsig.tools.util.Callable;
75 40435 jjdelcerro
import org.gvsig.tools.visitor.Visitor;
76
77
/**
78 42287 jjdelcerro
 * <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 40435 jjdelcerro
 *
82 42287 jjdelcerro
 * <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 40435 jjdelcerro
 *
89
 * @see FLyrDefault
90
 */
91
public class FLayers extends FLyrDefault implements LayerCollection,
92 42513 jjdelcerro
        InfoByPoint, List<FLayer> {
93 40435 jjdelcerro
94 42287 jjdelcerro
    /**
95
     * List with all listeners registered for this kind of node.
96
     */
97
    protected ArrayList layerCollectionListeners = null;
98 40435 jjdelcerro
99 42287 jjdelcerro
    /**
100
     * A synchronized list with the layers.
101
     */
102 42513 jjdelcerro
    protected List<FLayer> layers = null;
103 40435 jjdelcerro
104 42287 jjdelcerro
    protected MapContext fmap;
105 40435 jjdelcerro
106 42287 jjdelcerro
    /**
107
     * Useful for debug the problems during the implementation.
108
     */
109 44073 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(FLayers.class);
110 40435 jjdelcerro
111 42287 jjdelcerro
    public FLayers() {
112
        super();
113
        layerCollectionListeners = new ArrayList();
114
        layers = Collections.synchronizedList(new ArrayList());
115
    }
116 40435 jjdelcerro
117 42543 jjdelcerro
    @Override
118 42287 jjdelcerro
    public void addLayerCollectionListener(LayerCollectionListener listener) {
119
        if (!layerCollectionListeners.contains(listener)) {
120
            layerCollectionListeners.add(listener);
121
        }
122
    }
123 40435 jjdelcerro
124 42287 jjdelcerro
    /*
125
     * (non-Javadoc)
126
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
127
     */
128 42543 jjdelcerro
    @Override
129 42287 jjdelcerro
    public void setAllVisibles(boolean visible) {
130
        FLayer lyr;
131 40435 jjdelcerro
132 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
133
            lyr = ((FLayer) layers.get(i));
134
            lyr.setVisible(visible);
135 40435 jjdelcerro
136 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
137
                ((LayerCollection) lyr).setAllVisibles(visible);
138
            }
139
        }
140
    }
141
142 42543 jjdelcerro
    @Override
143 42287 jjdelcerro
    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 42293 jjdelcerro
            // TODO REVISAR ESTO !!!!
161 42287 jjdelcerro
            // 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 40435 jjdelcerro
172 42543 jjdelcerro
    @Override
173 42287 jjdelcerro
    public void addLayer(FLayer layer) {
174 40435 jjdelcerro
175 42287 jjdelcerro
        MapContext mco = this.getMapContext();
176 40435 jjdelcerro
177 42287 jjdelcerro
        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 43075 jjdelcerro
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 40435 jjdelcerro
206 42287 jjdelcerro
    /**
207
     * Adds a layer in an specified position in this node.
208
     *
209 42543 jjdelcerro
     * @param pos
210 42287 jjdelcerro
     * @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 42651 jjdelcerro
                if( fmap != null ) {
217
                    fmap.addAsCollectionListener(layers);
218
                }
219 42287 jjdelcerro
            }
220
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
221 40435 jjdelcerro
222 42287 jjdelcerro
            doAddLayer(pos, layer);
223 40435 jjdelcerro
224 42287 jjdelcerro
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
225
        } catch (CancelationException e) {
226 44073 jjdelcerro
            LOGGER.warn(e.getMessage());
227 42287 jjdelcerro
        }
228
    }
229 40435 jjdelcerro
230 42543 jjdelcerro
    @Override
231 42287 jjdelcerro
    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 40435 jjdelcerro
245 42287 jjdelcerro
    /**
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 42543 jjdelcerro
    @Override
257 42287 jjdelcerro
    public void removeLayer(FLayer lyr) throws CancelationException {
258
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
259
        doRemoveLayer(lyr);
260
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
261
    }
262
263 42543 jjdelcerro
    @Override
264 42287 jjdelcerro
    public void removeLayer(int idLayer) {
265
        FLayer lyr = (FLayer) layers.get(idLayer);
266
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
267
        this.doRemoveLayer(lyr);
268 40435 jjdelcerro
//                layers.remove(idLayer);
269 41840 jjdelcerro
//                this.updateDrawVersion();
270 42287 jjdelcerro
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
271
    }
272 40435 jjdelcerro
273 42543 jjdelcerro
    @Override
274 42287 jjdelcerro
    public void removeLayer(String layerName) {
275
        FLayer lyr;
276 40435 jjdelcerro
277 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
278
            lyr = ((FLayer) layers.get(i));
279 40435 jjdelcerro
280 42287 jjdelcerro
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
281
                removeLayer(i);
282 40435 jjdelcerro
283 42287 jjdelcerro
                break;
284
            }
285
        }
286
    }
287 40435 jjdelcerro
288 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
294 42287 jjdelcerro
     * @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 40435 jjdelcerro
     * ILayerCollection nodes
303
     *
304 42287 jjdelcerro
     * @param layer the layer to be replaced
305
     * @param newLayer the new layer
306 42543 jjdelcerro
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
307 40435 jjdelcerro
     */
308 42287 jjdelcerro
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
309 40435 jjdelcerro
        replaceLayer(this, layer, newLayer);
310
    }
311 42287 jjdelcerro
312 40435 jjdelcerro
    /**
313 42287 jjdelcerro
     * Replace a layer by other layer. It search recursively by all the
314 40435 jjdelcerro
     * ILayerCollection nodes
315 42287 jjdelcerro
     *
316
     * @param parentLayer the parent layer
317
     * @param layer the layer to be replaced
318
     * @param newLayer the new layer
319 40435 jjdelcerro
     * @throws LoadLayerException
320
     */
321 42287 jjdelcerro
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
322
        FLayer lyr;
323 40435 jjdelcerro
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
324
            lyr = ((FLayer) parentLayer.getLayer(i));
325 42287 jjdelcerro
            if (lyr.equals(layer)) {
326
                parentLayer.removeLayer(i);
327
                parentLayer.addLayer(i, newLayer);
328 40435 jjdelcerro
                break;
329
            }
330 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
331
                replaceLayer((FLayers) lyr, layer, newLayer);
332 40435 jjdelcerro
            }
333
        }
334
    }
335
336 42543 jjdelcerro
    @Override
337 42287 jjdelcerro
    public FLayer[] getVisibles() {
338
        ArrayList array = new ArrayList();
339
        LayersIterator iter = new LayersIterator(this) {
340 42543 jjdelcerro
            @Override
341 42287 jjdelcerro
            public boolean evaluate(FLayer layer) {
342
                return layer.isVisible();
343
            }
344 40435 jjdelcerro
345 42287 jjdelcerro
        };
346 40435 jjdelcerro
347 42287 jjdelcerro
        while (iter.hasNext()) {
348
            array.add(iter.nextLayer());
349
        }
350 40435 jjdelcerro
351 42287 jjdelcerro
        return (FLayer[]) array.toArray(new FLayer[0]);
352
    }
353 40435 jjdelcerro
354 42543 jjdelcerro
    @Override
355 42287 jjdelcerro
    public FLayer getLayer(int index) {
356
        return (FLayer) layers.get(index);
357
    }
358 40435 jjdelcerro
359 42543 jjdelcerro
    @Override
360 44073 jjdelcerro
    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 42287 jjdelcerro
            }
368 44073 jjdelcerro
            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 42293 jjdelcerro
                    }
374 44073 jjdelcerro
                });
375
                if( theLayers.isEmpty() ) {
376
                    return null;
377 42287 jjdelcerro
                }
378 44073 jjdelcerro
                return theLayers.get(0);
379 42287 jjdelcerro
            }
380
        }
381
        return null;
382
    }
383 40435 jjdelcerro
384 44073 jjdelcerro
    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 42293 jjdelcerro
    }
396 44073 jjdelcerro
397
    private List<FLayer> toPlainList(FLayer layer) {
398
        return toPlainList(layer, new ArrayList<FLayer>(), null);
399
    }
400 42293 jjdelcerro
401 44073 jjdelcerro
    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 42287 jjdelcerro
        if (layer instanceof FLayers) {
407 42293 jjdelcerro
            FLayers layerGroup = (FLayers) layer;
408
            for (int i = 0; i < layerGroup.getLayersCount(); i++) {
409 44073 jjdelcerro
                toPlainList(layerGroup.getLayer(i), result, filter);
410 42287 jjdelcerro
            }
411 44073 jjdelcerro
        } else if( filter==null || filter.test(layer) ) {
412 42287 jjdelcerro
            result.add(layer);
413
        }
414 42293 jjdelcerro
        return result;
415 42287 jjdelcerro
    }
416 40435 jjdelcerro
417 42543 jjdelcerro
    @Override
418 42287 jjdelcerro
    public int getLayersCount() {
419
        return layers.size();
420
    }
421 40435 jjdelcerro
422 42543 jjdelcerro
    @Override
423 42287 jjdelcerro
    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 40435 jjdelcerro
429 42287 jjdelcerro
    /*
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 42543 jjdelcerro
    @Override
438 42287 jjdelcerro
    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 40435 jjdelcerro
444 42287 jjdelcerro
    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 40435 jjdelcerro
450 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @throws org.gvsig.fmap.dal.exception.ReadException
492 42287 jjdelcerro
     *
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 40435 jjdelcerro
500 42287 jjdelcerro
        int resolution = properties.getPrintQuality();
501 44073 jjdelcerro
        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 42287 jjdelcerro
        }
514 40435 jjdelcerro
515 42293 jjdelcerro
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
516 42287 jjdelcerro
        // 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 40435 jjdelcerro
525 42287 jjdelcerro
            try {
526 40435 jjdelcerro
527 42287 jjdelcerro
                // 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 40435 jjdelcerro
542 42287 jjdelcerro
                            // 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 40435 jjdelcerro
570 42287 jjdelcerro
                            }
571 40435 jjdelcerro
572 42287 jjdelcerro
                        }
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 40435 jjdelcerro
586 42287 jjdelcerro
                                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 40435 jjdelcerro
594 42287 jjdelcerro
                    }
595
                }
596 40435 jjdelcerro
597 42287 jjdelcerro
            } catch (Exception e) {
598
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
599 42651 jjdelcerro
                if( fmap!=null ) {
600
                    fmap.addLayerError(mesg);
601
                }
602 44073 jjdelcerro
                LOGGER.error(mesg, e);
603 42287 jjdelcerro
            }
604 40435 jjdelcerro
605 42287 jjdelcerro
        }
606 40435 jjdelcerro
607 42287 jjdelcerro
        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 40435 jjdelcerro
612 42287 jjdelcerro
        }
613
        return group;
614
    }
615 40435 jjdelcerro
616 42543 jjdelcerro
    @Override
617 44073 jjdelcerro
    @SuppressWarnings("UseSpecificCatch")
618 42287 jjdelcerro
    public Envelope getFullEnvelope() {
619
        Envelope rAux = null;
620
        boolean first = true;
621 40435 jjdelcerro
622 44073 jjdelcerro
        for (FLayer capa : layers) {
623 42287 jjdelcerro
            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 44073 jjdelcerro
                LOGGER.warn("Can't calculate the envelope of the layer group '"+this.getName()+"'.",e);
632 42287 jjdelcerro
            }
633
        }
634 40435 jjdelcerro
635 42287 jjdelcerro
        return rAux;
636
    }
637 40435 jjdelcerro
638 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @param event a layer collection event with the new layer
644 42287 jjdelcerro
     */
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 40435 jjdelcerro
653 42287 jjdelcerro
    /**
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 40435 jjdelcerro
670 42287 jjdelcerro
    /**
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 40435 jjdelcerro
688 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @param event a layer collection event with the new layer
693 42287 jjdelcerro
     */
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 40435 jjdelcerro
701 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @param event a layer collection event with the layer removed
706 42287 jjdelcerro
     */
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 40435 jjdelcerro
714 42287 jjdelcerro
    /**
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 42543 jjdelcerro
     * @param event a layer collection event with the layer moved, and the initial
719 42287 jjdelcerro
     * 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 40435 jjdelcerro
728 42543 jjdelcerro
    @Override
729 42287 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
730 40435 jjdelcerro
731 42287 jjdelcerro
        super.saveToState(state);
732 40435 jjdelcerro
733 42287 jjdelcerro
        state.set("mapContext", fmap);
734 40435 jjdelcerro
735 42287 jjdelcerro
        List layersToSave = new ArrayList();
736 44073 jjdelcerro
        for (FLayer layer : this.layers) {
737 42287 jjdelcerro
            if (!layer.isTemporary()) {
738
                layersToSave.add(layer);
739
            }
740
        }
741
        state.set("layers", layersToSave);
742
    }
743 40435 jjdelcerro
744 42543 jjdelcerro
    @Override
745 42287 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
746 40435 jjdelcerro
747 42287 jjdelcerro
        super.loadFromState(state);
748 40435 jjdelcerro
749 42287 jjdelcerro
        setMapContext((MapContext) state.get("mapContext"));
750
        Iterator iter = state.getIterator("layers");
751
        while (iter.hasNext()) {
752
            FLayer item = (FLayer) iter.next();
753 43376 jjdelcerro
            // 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 42287 jjdelcerro
            layers.add(item);
758
        }
759
    }
760 40435 jjdelcerro
761 42543 jjdelcerro
    @Override
762 42287 jjdelcerro
    public MapContext getMapContext() {
763
        return fmap;
764
    }
765 40435 jjdelcerro
766 42543 jjdelcerro
    @Override
767 42287 jjdelcerro
    public void setAllActives(boolean active) {
768
        FLayer lyr;
769 40435 jjdelcerro
770 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
771
            lyr = ((FLayer) layers.get(i));
772
            lyr.setActive(active);
773 40435 jjdelcerro
774 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
775
                ((LayerCollection) lyr).setAllActives(active);
776
            }
777
        }
778
    }
779 40435 jjdelcerro
780 44073 jjdelcerro
    public List<FLayer> getLayers(Predicate<FLayer> filter) {
781
        List<FLayer> theLayers = this.toPlainList(this, filter);
782
        return theLayers;
783
    }
784
785 42543 jjdelcerro
    @Override
786 42287 jjdelcerro
    public FLayer[] getActives() {
787 44073 jjdelcerro
        List<FLayer> activeLayers = this.getLayers(new Predicate<FLayer>() {
788 42543 jjdelcerro
            @Override
789 44073 jjdelcerro
            public boolean test(FLayer layer) {
790 42287 jjdelcerro
                return layer.isActive();
791
            }
792 44073 jjdelcerro
        });
793
        return (FLayer[]) activeLayers.toArray(new FLayer[activeLayers.size()]);
794 42287 jjdelcerro
    }
795
796 42543 jjdelcerro
    @Override
797 42287 jjdelcerro
    public double getMinScale() {
798
        return -1; // La visibilidad o no la controla cada capa
799
        // dentro de una colecci?n
800
    }
801
802 42543 jjdelcerro
    @Override
803 42287 jjdelcerro
    public double getMaxScale() {
804
        return -1;
805
    }
806
807 42543 jjdelcerro
    @Override
808 42287 jjdelcerro
    public void setMinScale(double minScale) {
809 44073 jjdelcerro
        for (FLayer lyr : layers) {
810 42287 jjdelcerro
            lyr.setMinScale(minScale);
811
        }
812
    }
813
814 42543 jjdelcerro
    @Override
815 42287 jjdelcerro
    public void setMaxScale(double maxScale) {
816 44073 jjdelcerro
        for (FLayer lyr : layers) {
817 42287 jjdelcerro
            lyr.setMinScale(maxScale);
818
        }
819
    }
820
821 42543 jjdelcerro
    @Override
822 42287 jjdelcerro
    public void setActive(boolean b) {
823
        super.setActive(b);
824 44073 jjdelcerro
        for (FLayer layer : this.layers) {
825
            layer.setActive(b);
826 42287 jjdelcerro
        }
827
    }
828
829 42543 jjdelcerro
    @Override
830 42287 jjdelcerro
    public boolean addLayerListener(LayerListener o) {
831 44073 jjdelcerro
        for (FLayer layer : this.layers) {
832
            layer.addLayerListener(o);
833 42287 jjdelcerro
        }
834
        return true;
835
    }
836
837 42543 jjdelcerro
    @Override
838 40435 jjdelcerro
    public DynObjectSet getInfo(Point p, double tolerance,
839 42287 jjdelcerro
            Cancellable cancel) throws LoadLayerException, DataException {
840
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
841 40435 jjdelcerro
    }
842
843 42543 jjdelcerro
    @Override
844 40435 jjdelcerro
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel,
845 42287 jjdelcerro
            boolean fast) throws LoadLayerException, DataException {
846
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
847 40435 jjdelcerro
    }
848 42287 jjdelcerro
849 42543 jjdelcerro
    @Override
850 40435 jjdelcerro
    public DynObjectSet getInfo(org.gvsig.fmap.geom.primitive.Point p,
851 42287 jjdelcerro
            double tolerance) throws LoadLayerException, DataException {
852 40435 jjdelcerro
        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 42287 jjdelcerro
        DynObjectSet[] innerSets
863
                = (DynObjectSet[]) res.toArray(new DynObjectSet[res.size()]);
864 40435 jjdelcerro
        return new MultiDynObjectSet(innerSets);
865
    }
866
867 42543 jjdelcerro
    @Override
868 42287 jjdelcerro
    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 40435 jjdelcerro
883 42543 jjdelcerro
    @Override
884 42287 jjdelcerro
    public void accept(Visitor visitor) throws BaseException {
885
        for (int i = 0; i < this.getLayersCount(); i++) {
886
            FLayer layer = this.getLayer(i);
887 43877 jjdelcerro
//            try {
888 42287 jjdelcerro
                if (layer instanceof LayersVisitable) {
889
                    ((LayersVisitable) layer).accept(visitor);
890
                } else {
891
                    visitor.visit(layer);
892 42002 jjdelcerro
                }
893 43877 jjdelcerro
//            } catch (VisitCanceledException ex) {
894
//                break;
895
//            }
896 42002 jjdelcerro
        }
897 42287 jjdelcerro
    }
898 40435 jjdelcerro
899 42543 jjdelcerro
    @Override
900 42287 jjdelcerro
    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 40435 jjdelcerro
911 42543 jjdelcerro
    @Override
912 42287 jjdelcerro
    public Object getMetadataID() throws MetadataException {
913 42543 jjdelcerro
        StringBuilder strb = new StringBuilder();
914 42287 jjdelcerro
        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 40435 jjdelcerro
925 42287 jjdelcerro
    }
926 40435 jjdelcerro
927 42543 jjdelcerro
    @Override
928 42287 jjdelcerro
    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 40435 jjdelcerro
937 42543 jjdelcerro
    @Override
938 42287 jjdelcerro
    public String getMetadataName() throws MetadataException {
939 42543 jjdelcerro
        StringBuilder strb = new StringBuilder();
940 42287 jjdelcerro
        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 40435 jjdelcerro
952 42543 jjdelcerro
    @Override
953 42287 jjdelcerro
    public void beginDraw(Graphics2D g, ViewPort viewPort) {
954 42651 jjdelcerro
        if( fmap == null ) {
955
            return;
956
        }
957 42287 jjdelcerro
        LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
958
        fmap.fireLayerDrawingEvent(beforeEvent);
959
    }
960 40435 jjdelcerro
961 42543 jjdelcerro
    @Override
962 42287 jjdelcerro
    public void endDraw(Graphics2D g, ViewPort viewPort) {
963 42651 jjdelcerro
        if( fmap == null ) {
964
            return;
965
        }
966 42287 jjdelcerro
        LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
967
        fmap.fireLayerDrawingEvent(afterEvent);
968
    }
969 40435 jjdelcerro
970 41840 jjdelcerro
    public static class RegisterPersistence implements Callable {
971
972 42543 jjdelcerro
        @Override
973 41840 jjdelcerro
        public Object call() {
974 42287 jjdelcerro
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 41840 jjdelcerro
            return Boolean.TRUE;
989
        }
990
    }
991 40435 jjdelcerro
992 42543 jjdelcerro
    @Override
993 42287 jjdelcerro
    protected void doDispose() throws BaseException {
994
        if (layers != null) {
995 44073 jjdelcerro
            for (FLayer layer : this.layers) {
996
                dispose(layer);
997 42287 jjdelcerro
            }
998
        }
999
    }
1000 40435 jjdelcerro
1001 42543 jjdelcerro
    @Override
1002 42287 jjdelcerro
    public void move(FLayer layer, LayerCollection group, int where, FLayer adjoiningLayer) throws LayerNotFoundInCollectionException {
1003 40435 jjdelcerro
1004 42287 jjdelcerro
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(layer));
1005
        group.addLayer(layer, where, adjoiningLayer);
1006
        removeLayer(layer);
1007
        this.updateDrawVersion();
1008
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(layer));
1009 40435 jjdelcerro
1010 42287 jjdelcerro
    }
1011 40435 jjdelcerro
1012 42287 jjdelcerro
    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 40435 jjdelcerro
1022 42543 jjdelcerro
    @Override
1023 42287 jjdelcerro
    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 42543 jjdelcerro
    @Override
1032 42287 jjdelcerro
    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 42293 jjdelcerro
    public FLayer getFirstActiveLayer() {
1079 44073 jjdelcerro
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
1080 42543 jjdelcerro
            @Override
1081 44073 jjdelcerro
            public boolean test(FLayer layer) {
1082 42293 jjdelcerro
                return layer.isActive();
1083
            }
1084 44073 jjdelcerro
        });
1085
        if( theLayers.isEmpty() ) {
1086
            return null;
1087 42293 jjdelcerro
        }
1088 44073 jjdelcerro
        return theLayers.get(0);
1089 42293 jjdelcerro
    }
1090 42513 jjdelcerro
1091
    public FLyrVect getFirstActiveVectorLayer() {
1092 44073 jjdelcerro
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
1093 42543 jjdelcerro
            @Override
1094 44073 jjdelcerro
            public boolean test(FLayer layer) {
1095 42513 jjdelcerro
                return layer.isActive() && layer instanceof FLyrVect;
1096
            }
1097 44073 jjdelcerro
        });
1098
        if( theLayers.isEmpty() ) {
1099
            return null;
1100 42513 jjdelcerro
        }
1101 44073 jjdelcerro
        return (FLyrVect) theLayers.get(0);
1102 42513 jjdelcerro
    }
1103 42293 jjdelcerro
1104 42543 jjdelcerro
    @Override
1105 43608 jjdelcerro
    public Iterator<FLayer> iterator() {
1106 42293 jjdelcerro
        return this.layers.iterator();
1107
    }
1108
1109 43608 jjdelcerro
    public Iterator<FLayer> deepiterator() {
1110 44073 jjdelcerro
        List theLayers = toPlainList(this);
1111
        return theLayers.iterator();
1112 42293 jjdelcerro
    }
1113
1114 42543 jjdelcerro
    @Override
1115 42293 jjdelcerro
    public int size() {
1116
        return this.layers.size();
1117
    }
1118
1119 42543 jjdelcerro
    @Override
1120 42513 jjdelcerro
    public FLayer get(int index) {
1121 42293 jjdelcerro
        return this.layers.get(index);
1122
    }
1123
1124 42543 jjdelcerro
    @Override
1125 42293 jjdelcerro
    public boolean isEmpty() {
1126
        return this.layers.isEmpty();
1127
    }
1128
1129 42543 jjdelcerro
    @Override
1130 42293 jjdelcerro
    public boolean contains(Object o) {
1131
        return this.layers.contains(o);
1132
    }
1133
1134 42543 jjdelcerro
    @Override
1135 42293 jjdelcerro
    public Object[] toArray() {
1136
        return this.layers.toArray();
1137
    }
1138
1139 42543 jjdelcerro
    @Override
1140 42293 jjdelcerro
    public Object[] toArray(Object[] ts) {
1141
        return this.layers.toArray(ts);
1142
    }
1143
1144 42543 jjdelcerro
    @Override
1145 42513 jjdelcerro
    public boolean add(FLayer e) {
1146
        this.addLayer(e);
1147 42293 jjdelcerro
        return true;
1148
    }
1149
1150 42543 jjdelcerro
    @Override
1151 42293 jjdelcerro
    public boolean remove(Object o) {
1152
        this.removeLayer((FLayer) o);
1153
        return true;
1154
    }
1155
1156 42543 jjdelcerro
    @Override
1157 42293 jjdelcerro
    public boolean containsAll(Collection clctn) {
1158
        return this.layers.containsAll(clctn);
1159
    }
1160
1161 42543 jjdelcerro
    @Override
1162 42513 jjdelcerro
    public void add(int i, FLayer e) {
1163 42293 jjdelcerro
        this.addLayer(i, (FLayer) e);
1164
    }
1165
1166 42543 jjdelcerro
    @Override
1167 42513 jjdelcerro
    public FLayer remove(int i) {
1168 42293 jjdelcerro
        FLayer o = this.getLayer(i);
1169
        this.removeLayer(i);
1170
        return o;
1171
    }
1172
1173 42543 jjdelcerro
    @Override
1174 42293 jjdelcerro
    public int indexOf(Object o) {
1175
        return this.layers.indexOf(o);
1176
    }
1177
1178 42543 jjdelcerro
    @Override
1179 42293 jjdelcerro
    public int lastIndexOf(Object o) {
1180
        return this.layers.lastIndexOf(o);
1181
    }
1182
1183 42543 jjdelcerro
    @Override
1184 42293 jjdelcerro
    public ListIterator listIterator() {
1185
        return this.layers.listIterator();
1186
    }
1187
1188 42543 jjdelcerro
    @Override
1189 42293 jjdelcerro
    public ListIterator listIterator(int i) {
1190
        return this.layers.listIterator(i);
1191
    }
1192
1193 42543 jjdelcerro
    @Override
1194 42293 jjdelcerro
    public List subList(int i, int i1) {
1195
        return this.layers.subList(i, i1);
1196
    }
1197
1198 42543 jjdelcerro
    @Override
1199 42293 jjdelcerro
    public boolean addAll(Collection clctn) {
1200
        Iterator it = clctn.iterator();
1201
        while( it.hasNext() ) {
1202 42513 jjdelcerro
            this.add((FLayer) it.next());
1203 42293 jjdelcerro
        }
1204
        return true;
1205
    }
1206
1207 42543 jjdelcerro
    @Override
1208 42293 jjdelcerro
    public boolean addAll(int i, Collection clctn) {
1209
        Iterator it = clctn.iterator();
1210
        while( it.hasNext() ) {
1211 42513 jjdelcerro
            this.add(i, (FLayer) it.next());
1212 42293 jjdelcerro
        }
1213
        return true;
1214
    }
1215
1216 42543 jjdelcerro
    @Override
1217 42293 jjdelcerro
    public boolean removeAll(Collection clctn) {
1218
        Iterator it = clctn.iterator();
1219
        while( it.hasNext() ) {
1220 42513 jjdelcerro
            this.remove((FLayer) it.next());
1221 42293 jjdelcerro
        }
1222
        return true;
1223
    }
1224
1225 42543 jjdelcerro
    @Override
1226 42293 jjdelcerro
    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 42513 jjdelcerro
                this.remove((FLayer) o);
1232 42293 jjdelcerro
            }
1233
        }
1234
        return true;
1235
    }
1236
1237 42543 jjdelcerro
    @Override
1238 42513 jjdelcerro
    public FLayer set(int i, FLayer e) {
1239 42293 jjdelcerro
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1240
    }
1241
1242 40435 jjdelcerro
}