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

History | View | Annotate | Download (46.4 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
import org.cresques.cts.ICoordTrans;
39
import org.cresques.cts.IProjection;
40
import org.gvsig.compat.print.PrintAttributes;
41 43075 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
42 40435 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.exception.ReadException;
44 44599 omartinez
import org.gvsig.fmap.dal.feature.FeatureSelection;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46 40435 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
47
import org.gvsig.fmap.mapcontext.MapContext;
48 43075 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextLocator;
49 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
50
import org.gvsig.fmap.mapcontext.Messages;
51
import org.gvsig.fmap.mapcontext.ViewPort;
52
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
53
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
54
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
55
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
56
import org.gvsig.fmap.mapcontext.layers.operations.LayerNotFoundInCollectionException;
57
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitable;
58
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
59 44073 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
60 42513 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable;
62
import org.gvsig.metadata.exceptions.MetadataException;
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.dynobject.DynObjectSet;
65
import org.gvsig.tools.dynobject.DynStruct;
66
import org.gvsig.tools.dynobject.impl.MultiDynObjectSet;
67
import org.gvsig.tools.exception.BaseException;
68
import org.gvsig.tools.persistence.PersistenceManager;
69
import org.gvsig.tools.persistence.PersistentState;
70
import org.gvsig.tools.persistence.exception.PersistenceException;
71
import org.gvsig.tools.task.Cancellable;
72 41840 jjdelcerro
import org.gvsig.tools.util.Callable;
73 40435 jjdelcerro
import org.gvsig.tools.visitor.Visitor;
74 45535 fdiaz
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
76 40435 jjdelcerro
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 45535 fdiaz
    protected ArrayList<LayerCollectionListener> 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 44599 omartinez
111
    public static final Predicate<FLayer> LAYER_ACTIVE = new Predicate<FLayer>() {
112
        @Override
113
        public boolean test(FLayer layer) {
114
            return layer.isActive();
115
        }
116
    };
117 40435 jjdelcerro
118 44599 omartinez
    public static final Predicate<FLayer> LAYER_AVALAIBLE = new Predicate<FLayer>() {
119
        @Override
120
        public boolean test(FLayer layer) {
121
            return layer.isAvailable();
122
        }
123
    };
124
125
    public static Predicate<FLayer> LAYER_FLYRVECT = new Predicate<FLayer>() {
126
        @Override
127
        public boolean test(FLayer layer) {
128
            return layer instanceof FLyrVect;
129
        }
130
    };
131
    public static Predicate<FLayer> LAYER_SELECTION_AVAILABLE = new Predicate<FLayer>() {
132
        @Override
133
        public boolean test(FLayer layer) {
134
            if (layer instanceof FLyrVect) {
135
                try {
136
                    FeatureStore store = ((FLyrVect) layer).getFeatureStore();
137 46277 jjdelcerro
                    if (store.isFeatureSelectionAvailable()) {
138 44599 omartinez
                        return true;
139
                    }
140
                } catch (Exception ex) {
141
                    return false;
142
                }
143
            }
144
            return false;
145
        }
146
    };
147
148
    public static Predicate<FLayer> LAYER_SELECTION_EMPTY = new Predicate<FLayer>() {
149
        @Override
150
        public boolean test(FLayer layer) {
151
            if (layer instanceof FLyrVect) {
152
                try {
153
                    FeatureStore store = ((FLyrVect) layer).getFeatureStore();
154 46277 jjdelcerro
                    if (!store.isFeatureSelectionAvailable() || store.isFeatureSelectionEmpty()) {
155 44599 omartinez
                        return true;
156
                    }
157
                    return false;
158
                } catch (Exception ex) {
159
                    return true;
160
                }
161
            } else {
162
                return true;
163
            }
164
        }
165
    };
166
167 42287 jjdelcerro
    public FLayers() {
168
        super();
169
        layerCollectionListeners = new ArrayList();
170
        layers = Collections.synchronizedList(new ArrayList());
171
    }
172 40435 jjdelcerro
173 42543 jjdelcerro
    @Override
174 42287 jjdelcerro
    public void addLayerCollectionListener(LayerCollectionListener listener) {
175
        if (!layerCollectionListeners.contains(listener)) {
176
            layerCollectionListeners.add(listener);
177
        }
178
    }
179 40435 jjdelcerro
180 42287 jjdelcerro
    /*
181
     * (non-Javadoc)
182
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
183
     */
184 42543 jjdelcerro
    @Override
185 42287 jjdelcerro
    public void setAllVisibles(boolean visible) {
186
        FLayer lyr;
187 40435 jjdelcerro
188 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
189
            lyr = ((FLayer) layers.get(i));
190
            lyr.setVisible(visible);
191 40435 jjdelcerro
192 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
193
                ((LayerCollection) lyr).setAllVisibles(visible);
194
            }
195
        }
196
    }
197
198 42543 jjdelcerro
    @Override
199 42287 jjdelcerro
    public void removeLayerCollectionListener(LayerCollectionListener listener) {
200
        layerCollectionListeners.remove(listener);
201
    }
202
203
    /**
204
     * Adds a layer on an specified position in this node.
205
     *
206
     * @param pos position in the inner list where the layer will be added
207
     * @param layer a layer
208
     */
209
    private void doAddLayer(int pos, FLayer layer) {
210
        layers.add(pos, layer);
211
        ToolsLocator.getDisposableManager().bind(layer);
212
        layer.setParentLayer(this);
213
        IProjection layerProj = layer.getProjection();
214
        if (layerProj != null && fmap != null) {
215
            IProjection mapContextProj = fmap.getProjection();
216 42293 jjdelcerro
            // TODO REVISAR ESTO !!!!
217 42287 jjdelcerro
            // Esta condici?n puede que no fuese exacta para todos los casos
218
            if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
219
                ICoordTrans ct = layerProj.getCT(mapContextProj);
220
                layer.setCoordTrans(ct);
221
            } else {
222
                layer.setCoordTrans(null);
223
            }
224
        }
225
        this.updateDrawVersion();
226
    }
227 40435 jjdelcerro
228 42543 jjdelcerro
    @Override
229 42287 jjdelcerro
    public void addLayer(FLayer layer) {
230 40435 jjdelcerro
231 42287 jjdelcerro
        MapContext mco = this.getMapContext();
232 40435 jjdelcerro
233 42287 jjdelcerro
        if (mco != null) {
234
            /*
235
             * Get order manager from map context
236
             */
237
            int position = mco.getOrderManager().getPosition(this, layer);
238
            addLayer(position, layer);
239
        } else {
240
            /*
241
             * This happens when FLayers object is not in a
242
             * map context, so no order manager is available.
243
             */
244
            addLayer(layers.size(), layer);
245
        }
246
    }
247 43075 jjdelcerro
248
    public void addLayer(DataStore store) {
249
        FLayer layer;
250
        try {
251
            layer = MapContextLocator.getMapContextManager().createLayer(
252
                    store.getName(),
253
                    store
254
            );
255
256
        } catch (LoadLayerException ex) {
257
            throw new RuntimeException("Can't create layer from store.", ex);
258
        }
259
        this.addLayer(layer);
260
    }
261 40435 jjdelcerro
262 42287 jjdelcerro
    /**
263
     * Adds a layer in an specified position in this node.
264
     *
265 42543 jjdelcerro
     * @param pos
266 42287 jjdelcerro
     * @param layer a layer
267
     */
268
    public void addLayer(int pos, FLayer layer) {
269
        try {
270
            if (layer instanceof FLayers) {
271 44599 omartinez
                FLayers layersToAdd = (FLayers) layer;
272 42651 jjdelcerro
                if( fmap != null ) {
273 44599 omartinez
                    fmap.addAsCollectionListener(layersToAdd);
274 42651 jjdelcerro
                }
275 42287 jjdelcerro
            }
276
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
277 40435 jjdelcerro
278 42287 jjdelcerro
            doAddLayer(pos, layer);
279 40435 jjdelcerro
280 42287 jjdelcerro
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
281
        } catch (CancelationException e) {
282 44073 jjdelcerro
            LOGGER.warn(e.getMessage());
283 42287 jjdelcerro
        }
284
    }
285 40435 jjdelcerro
286 42543 jjdelcerro
    @Override
287 42287 jjdelcerro
    public void moveTo(int from, int to) throws CancelationException {
288
        int newfrom = layers.size() - from - 1;
289
        int newto = layers.size() - to - 1;
290
        if (newfrom < 0 || newfrom >= layers.size() || newto < 0 || newto >= layers.size()) {
291
            return;
292
        }
293
        FLayer aux = (FLayer) layers.get(newfrom);
294
        callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
295
        layers.remove(newfrom);
296
        layers.add(newto, aux);
297
        this.updateDrawVersion();
298
        callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
299
    }
300 40435 jjdelcerro
301 42287 jjdelcerro
    /**
302
     * Removes an inner layer.
303
     *
304
     * @param lyr a layer
305
     */
306
    private void doRemoveLayer(FLayer lyr) {
307
        layers.remove(lyr);
308
        lyr.dispose();
309
        this.updateDrawVersion();
310
    }
311
312 42543 jjdelcerro
    @Override
313 42287 jjdelcerro
    public void removeLayer(FLayer lyr) throws CancelationException {
314
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
315
        doRemoveLayer(lyr);
316
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
317
    }
318
319 42543 jjdelcerro
    @Override
320 42287 jjdelcerro
    public void removeLayer(int idLayer) {
321
        FLayer lyr = (FLayer) layers.get(idLayer);
322
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
323
        this.doRemoveLayer(lyr);
324 40435 jjdelcerro
//                layers.remove(idLayer);
325 41840 jjdelcerro
//                this.updateDrawVersion();
326 42287 jjdelcerro
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
327
    }
328 40435 jjdelcerro
329 42543 jjdelcerro
    @Override
330 42287 jjdelcerro
    public void removeLayer(String layerName) {
331
        FLayer lyr;
332 40435 jjdelcerro
333 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
334
            lyr = ((FLayer) layers.get(i));
335 40435 jjdelcerro
336 42287 jjdelcerro
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
337
                removeLayer(i);
338 40435 jjdelcerro
339 42287 jjdelcerro
                break;
340
            }
341
        }
342
    }
343 40435 jjdelcerro
344 42287 jjdelcerro
    /**
345
     * Replace a layer identified by its name, by another.
346
     *
347
     * @param layerName the name of the layer to be replaced
348
     * @param layer the new layer
349 42543 jjdelcerro
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
350 42287 jjdelcerro
     * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
351
     */
352
    public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException {
353
        replaceLayer(getLayer(layerName), layer);
354
    }
355
356
    /**
357
     * Replace a layer by another layer. It search recursively by all the
358 40435 jjdelcerro
     * ILayerCollection nodes
359
     *
360 42287 jjdelcerro
     * @param layer the layer to be replaced
361
     * @param newLayer the new layer
362 42543 jjdelcerro
     * @throws org.gvsig.fmap.mapcontext.exceptions.LoadLayerException
363 40435 jjdelcerro
     */
364 42287 jjdelcerro
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
365 40435 jjdelcerro
        replaceLayer(this, layer, newLayer);
366
    }
367 42287 jjdelcerro
368 40435 jjdelcerro
    /**
369 42287 jjdelcerro
     * Replace a layer by other layer. It search recursively by all the
370 40435 jjdelcerro
     * ILayerCollection nodes
371 42287 jjdelcerro
     *
372
     * @param parentLayer the parent layer
373
     * @param layer the layer to be replaced
374
     * @param newLayer the new layer
375 40435 jjdelcerro
     * @throws LoadLayerException
376
     */
377 42287 jjdelcerro
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
378
        FLayer lyr;
379 40435 jjdelcerro
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
380
            lyr = ((FLayer) parentLayer.getLayer(i));
381 42287 jjdelcerro
            if (lyr.equals(layer)) {
382
                parentLayer.removeLayer(i);
383
                parentLayer.addLayer(i, newLayer);
384 40435 jjdelcerro
                break;
385
            }
386 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
387
                replaceLayer((FLayers) lyr, layer, newLayer);
388 40435 jjdelcerro
            }
389
        }
390
    }
391
392 42543 jjdelcerro
    @Override
393 42287 jjdelcerro
    public FLayer[] getVisibles() {
394
        ArrayList array = new ArrayList();
395
        LayersIterator iter = new LayersIterator(this) {
396 42543 jjdelcerro
            @Override
397 42287 jjdelcerro
            public boolean evaluate(FLayer layer) {
398
                return layer.isVisible();
399
            }
400 40435 jjdelcerro
401 42287 jjdelcerro
        };
402 40435 jjdelcerro
403 42287 jjdelcerro
        while (iter.hasNext()) {
404
            array.add(iter.nextLayer());
405
        }
406 40435 jjdelcerro
407 42287 jjdelcerro
        return (FLayer[]) array.toArray(new FLayer[0]);
408
    }
409 40435 jjdelcerro
410 42543 jjdelcerro
    @Override
411 42287 jjdelcerro
    public FLayer getLayer(int index) {
412
        return (FLayer) layers.get(index);
413
    }
414 40435 jjdelcerro
415 42543 jjdelcerro
    @Override
416 44073 jjdelcerro
    public FLayer getLayer(final String layerName) {
417
        if( StringUtils.isBlank(layerName) ) {
418
            return null;
419
        }
420
        for (FLayer layer : this.layers) {
421
            if( layer!=null && StringUtils.equalsIgnoreCase(layer.getName(), layerName) ) {
422
                return layer;
423 42287 jjdelcerro
            }
424 44073 jjdelcerro
            if( layer instanceof FLayers ) {
425
                List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
426
                    @Override
427
                    public boolean test(FLayer layer) {
428
                        return StringUtils.equalsIgnoreCase(layer.getName(), layerName);
429 42293 jjdelcerro
                    }
430 44073 jjdelcerro
                });
431
                if( theLayers.isEmpty() ) {
432
                    return null;
433 42287 jjdelcerro
                }
434 44073 jjdelcerro
                return theLayers.get(0);
435 42287 jjdelcerro
            }
436
        }
437
        return null;
438
    }
439 40435 jjdelcerro
440 44599 omartinez
    @Override
441 44073 jjdelcerro
    public FLayer getLayer(final DataStore store) {
442
        List<FLayer> theLayers = this.getLayers(new Predicate<FLayer>() {
443
            @Override
444
            public boolean test(FLayer layer) {
445
                return  layer instanceof SingleLayer && store == ((SingleLayer) layer).getDataStore();
446
            }
447
        });
448
        if( theLayers.isEmpty() ) {
449
            return null;
450
        }
451
        return theLayers.get(0);
452 42293 jjdelcerro
    }
453 44073 jjdelcerro
454
    private List<FLayer> toPlainList(FLayer layer) {
455
        return toPlainList(layer, new ArrayList<FLayer>(), null);
456
    }
457 42293 jjdelcerro
458 44073 jjdelcerro
    private List<FLayer> toPlainList(FLayer layer, Predicate<FLayer> filter) {
459
        return toPlainList(layer, new ArrayList<FLayer>(), filter);
460
    }
461
462
    private List<FLayer> toPlainList(FLayer layer, List<FLayer> result, Predicate<FLayer> filter) {
463 42287 jjdelcerro
        if (layer instanceof FLayers) {
464 42293 jjdelcerro
            FLayers layerGroup = (FLayers) layer;
465 44599 omartinez
            if (filter == null || filter.test(layer)) {
466
                result.add(layer);
467
            }
468 42293 jjdelcerro
            for (int i = 0; i < layerGroup.getLayersCount(); i++) {
469 44073 jjdelcerro
                toPlainList(layerGroup.getLayer(i), result, filter);
470 42287 jjdelcerro
            }
471 44073 jjdelcerro
        } else if( filter==null || filter.test(layer) ) {
472 42287 jjdelcerro
            result.add(layer);
473
        }
474 42293 jjdelcerro
        return result;
475 42287 jjdelcerro
    }
476 40435 jjdelcerro
477 42543 jjdelcerro
    @Override
478 42287 jjdelcerro
    public int getLayersCount() {
479
        return layers.size();
480
    }
481 40435 jjdelcerro
482 42543 jjdelcerro
    @Override
483 42287 jjdelcerro
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
484
            Cancellable cancel, double scale) throws ReadException {
485
        // FIXME Arreglar este error
486
        throw new RuntimeException("Esto no deberia de llamarse");
487
    }
488 40435 jjdelcerro
489 42287 jjdelcerro
    /*
490
     * (non-Javadoc)
491
     *
492
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
493
     * com.iver.cit.gvsig.fmap.ViewPort,
494
     * com.iver.utiles.swing.threads.Cancellable, double,
495
     * javax.print.attribute.PrintAttributes)
496
     */
497 42543 jjdelcerro
    @Override
498 42287 jjdelcerro
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
499
            double scale, PrintAttributes properties)
500
            throws ReadException {
501
        throw new RuntimeException("No deberia pasar por aqui");
502
    }
503 40435 jjdelcerro
504 42287 jjdelcerro
    public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
505
            double scale, PrintAttributes properties)
506
            throws ReadException {
507
        this.print_old(g, viewPort, cancel, scale, properties, null);
508
    }
509 40435 jjdelcerro
510 42287 jjdelcerro
    /**
511
     * <p>
512
     * Checks all layers (each one as a sub-node of this node <i>collection of
513
     * layers</i>) of this collection and draws their requested properties. If a
514
     * node is a group of layers (<code>ComposedLayer</code>), executes it's
515
     * drawn.</p>
516
     *
517
     * <p>
518
     * All nodes which could group with the composed layer <code>group</code>,
519
     * will be drawn together. And once the <code>
520
     * group</code> is drawn, will be set to <code>null</code> if hasn't a
521
     * parent layer.</p>
522
     *
523
     * <p>
524
     * The particular implementation depends on the kind of each layer and
525
     * composed layer. And this process can be cancelled at any time by the
526
     * shared object <code>cancel</code>.</p>
527
     *
528
     * <p>
529
     * According the print quality, labels will be printed in different
530
     * resolution:
531
     * <ul>
532
     * <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
533
     * <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
534
     * <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
535
     * </ul>
536
     * </p>
537
     *
538
     * @param g for rendering 2-dimensional shapes, text and images on the
539
     * Java(tm) platform
540
     * @param viewPort the information for drawing the layers
541
     * @param cancel shared object that determines if this layer can continue
542
     * being drawn
543
     * @param scale the scale of the view. Must be between
544
     * {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
545
     * @param properties properties that will be print
546
     * @param group a composed layer pending to paint; if this parameter is
547
     * <code>null</code>, the composed layer
548
     *
549
     * @return <code>null</code> if the layers in <code>group</code> had been
550
     * drawn or were <code>null</code>; otherwise, the <code>group</code>
551 42543 jjdelcerro
     * @throws org.gvsig.fmap.dal.exception.ReadException
552 42287 jjdelcerro
     *
553
     * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double,
554
     * PrintAttributes)
555
     */
556
    public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
557
            throws ReadException {
558
        double dpi = 72;
559 40435 jjdelcerro
560 42287 jjdelcerro
        int resolution = properties.getPrintQuality();
561 44073 jjdelcerro
        switch (resolution) {
562
            case PrintAttributes.PRINT_QUALITY_NORMAL:
563
                dpi = 300;
564
                break;
565
            case PrintAttributes.PRINT_QUALITY_HIGH:
566
                dpi = 600;
567
                break;
568
            case PrintAttributes.PRINT_QUALITY_DRAFT:
569
                dpi = 72;
570
                break;
571
            default:
572
                break;
573 42287 jjdelcerro
        }
574 40435 jjdelcerro
575 42293 jjdelcerro
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
576 42287 jjdelcerro
        // calculando la escala en pantalla, no para el layout.
577
        // Revisar esto.
578
        // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
579
        for (int i = 0; i < layers.size(); i++) {
580
            FLayer lyr = (FLayer) layers.get(i);
581
            if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
582
                continue;
583
            }
584 40435 jjdelcerro
585 42287 jjdelcerro
            try {
586 40435 jjdelcerro
587 42287 jjdelcerro
                // Checks for draw group (ComposedLayer)
588
                if (group != null) {
589
                    if (lyr instanceof FLayers) {
590
                        group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
591
                    } else {
592
                        // If layer can be added to the group, does it
593
                        if (lyr instanceof ILabelable
594
                                && ((ILabelable) lyr).isLabeled()
595
                                && ((ILabelable) lyr).getLabelingStrategy() != null
596
                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
597
                            group.add(lyr);
598
                        } else {
599
                            // draw the 'pending to draw' layer group
600
                            group.print(g, viewPort, cancel, scale, properties);
601 40435 jjdelcerro
602 42287 jjdelcerro
                            // gets a new group instance
603
                            if (lyr instanceof ILabelable
604
                                    && ((ILabelable) lyr).isLabeled()
605
                                    && ((ILabelable) lyr).getLabelingStrategy() != null
606
                                    && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
607
                                group = lyr.newComposedLayer();
608
                            } else {
609
                                group = null;
610
                            }
611
                            // if layer hasn't group, draws it inmediately
612
                            if (group == null) {
613
                                if (lyr instanceof FLayers) {
614
                                    group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
615
                                } else {
616
                                    lyr.print(g, viewPort, cancel, scale, properties);
617
                                    if (lyr instanceof ILabelable
618
                                            && ((ILabelable) lyr).isLabeled()
619
                                            && ((ILabelable) lyr).getLabelingStrategy() != null
620
                                            && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
621
                                        ILabelable lLayer = (ILabelable) lyr;
622
                                        lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
623
                                    }
624
                                }
625
                            } else {
626
                                // add the layer to the group
627
                                group.setMapContext(fmap);
628
                                group.add(lyr);
629 40435 jjdelcerro
630 42287 jjdelcerro
                            }
631 40435 jjdelcerro
632 42287 jjdelcerro
                        }
633
                    }
634
                } else {
635
                    // gets a new group instance
636
                    group = lyr.newComposedLayer();
637
                    // if layer hasn't group, draws it inmediately
638
                    if (group == null) {
639
                        if (lyr instanceof FLayers) {
640
                            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
641
                        } else {
642
                            lyr.print(g, viewPort, cancel, scale, properties);
643
                            if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
644
                                ILabelable lLayer = (ILabelable) lyr;
645 40435 jjdelcerro
646 42287 jjdelcerro
                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
647
                            }
648
                        }
649
                    } else {
650
                        // add the layer to the group
651
                        group.setMapContext(fmap);
652
                        group.add(lyr);
653 40435 jjdelcerro
654 42287 jjdelcerro
                    }
655
                }
656 40435 jjdelcerro
657 42287 jjdelcerro
            } catch (Exception e) {
658
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
659 42651 jjdelcerro
                if( fmap!=null ) {
660
                    fmap.addLayerError(mesg);
661
                }
662 44073 jjdelcerro
                LOGGER.error(mesg, e);
663 42287 jjdelcerro
            }
664 40435 jjdelcerro
665 42287 jjdelcerro
        }
666 40435 jjdelcerro
667 42287 jjdelcerro
        if (group != null && this.getParentLayer() == null) {
668
            //si tenemos un grupo pendiente de pintar, pintamos
669
            group.print(g, viewPort, cancel, scale, properties);
670
            group = null;
671 40435 jjdelcerro
672 42287 jjdelcerro
        }
673
        return group;
674
    }
675 40435 jjdelcerro
676 42543 jjdelcerro
    @Override
677 44073 jjdelcerro
    @SuppressWarnings("UseSpecificCatch")
678 42287 jjdelcerro
    public Envelope getFullEnvelope() {
679
        Envelope rAux = null;
680
        boolean first = true;
681 40435 jjdelcerro
682 44073 jjdelcerro
        for (FLayer capa : layers) {
683 42287 jjdelcerro
            try {
684
                if (first) {
685
                    rAux = (Envelope) capa.getFullEnvelope().clone();
686
                    first = false;
687
                } else {
688
                    rAux.add(capa.getFullEnvelope());
689
                }
690
            } catch (Exception e) {
691 44073 jjdelcerro
                LOGGER.warn("Can't calculate the envelope of the layer group '"+this.getName()+"'.",e);
692 42287 jjdelcerro
            }
693
        }
694 40435 jjdelcerro
695 42287 jjdelcerro
        return rAux;
696
    }
697 40435 jjdelcerro
698 42287 jjdelcerro
    /**
699
     * Notifies all listeners associated to this collection of layers, that
700
     * another layer is going to be added or replaced in the internal list of
701
     * layers.
702
     *
703 42543 jjdelcerro
     * @param event a layer collection event with the new layer
704 42287 jjdelcerro
     */
705
    protected void callLayerAdding(LayerCollectionEvent event)
706
            throws CancelationException {
707
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
708
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
709
            ((LayerCollectionListener) iter.next()).layerAdding(event);
710
        }
711
    }
712 40435 jjdelcerro
713 42287 jjdelcerro
    /**
714
     * Notifies all listeners associated to this collection of layers, that a
715
     * layer is going to be removed from the internal list of layers.
716
     *
717
     * @param event a layer collection event with the layer being removed
718
     *
719
     * @throws CancelationException any exception produced during the
720
     * cancellation of the driver.
721
     */
722
    protected void callLayerRemoving(LayerCollectionEvent event)
723
            throws CancelationException {
724
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
725
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
726
            ((LayerCollectionListener) iter.next()).layerRemoving(event);
727
        }
728
    }
729 40435 jjdelcerro
730 42287 jjdelcerro
    /**
731
     * Notifies all listeners associated to this collection of layers, that a
732
     * layer is going to be moved in the internal list of layers.
733
     *
734
     * @param event a layer collection event with the layer being moved, and the
735
     * initial and final positions
736
     *
737
     * @throws CancelationException any exception produced during the
738
     * cancellation of the driver.
739
     */
740
    protected void callLayerMoving(LayerPositionEvent event)
741
            throws CancelationException {
742
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
743
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
744
            ((LayerCollectionListener) iter.next()).layerMoving(event);
745
        }
746
    }
747 40435 jjdelcerro
748 42287 jjdelcerro
    /**
749
     * Notifies all listeners associated to this collection of layers, that
750
     * another layer has been added or replaced in the internal list of layers.
751
     *
752 42543 jjdelcerro
     * @param event a layer collection event with the new layer
753 42287 jjdelcerro
     */
754
    protected void callLayerAdded(LayerCollectionEvent event) {
755
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
756
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
757
            ((LayerCollectionListener) iter.next()).layerAdded(event);
758
        }
759
    }
760 40435 jjdelcerro
761 42287 jjdelcerro
    /**
762
     * Notifies all listeners associated to this collection of layers, that
763
     * another layer has been removed from the internal list of layers.
764
     *
765 42543 jjdelcerro
     * @param event a layer collection event with the layer removed
766 42287 jjdelcerro
     */
767
    protected void callLayerRemoved(LayerCollectionEvent event) {
768
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
769
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
770
            ((LayerCollectionListener) iter.next()).layerRemoved(event);
771
        }
772
    }
773 40435 jjdelcerro
774 42287 jjdelcerro
    /**
775
     * Notifies all listeners associated to this collection of layers, that
776
     * another layer has been moved in the internal list of layers.
777
     *
778 42543 jjdelcerro
     * @param event a layer collection event with the layer moved, and the initial
779 42287 jjdelcerro
     * and final positions
780
     */
781
    protected void callLayerMoved(LayerPositionEvent event) {
782
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
783
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
784
            ((LayerCollectionListener) iter.next()).layerMoved(event);
785
        }
786
    }
787 40435 jjdelcerro
788 42543 jjdelcerro
    @Override
789 42287 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
790 40435 jjdelcerro
791 42287 jjdelcerro
        super.saveToState(state);
792 40435 jjdelcerro
793 42287 jjdelcerro
        state.set("mapContext", fmap);
794 40435 jjdelcerro
795 42287 jjdelcerro
        List layersToSave = new ArrayList();
796 44073 jjdelcerro
        for (FLayer layer : this.layers) {
797 42287 jjdelcerro
            if (!layer.isTemporary()) {
798
                layersToSave.add(layer);
799
            }
800
        }
801
        state.set("layers", layersToSave);
802
    }
803 40435 jjdelcerro
804 42543 jjdelcerro
    @Override
805 42287 jjdelcerro
    public void loadFromState(PersistentState state) throws PersistenceException {
806 40435 jjdelcerro
807 42287 jjdelcerro
        super.loadFromState(state);
808 40435 jjdelcerro
809 42287 jjdelcerro
        setMapContext((MapContext) state.get("mapContext"));
810
        Iterator iter = state.getIterator("layers");
811
        while (iter.hasNext()) {
812
            FLayer item = (FLayer) iter.next();
813 46191 fdiaz
            if(item == null){
814
                continue;
815
            }
816
            if (item instanceof FLayers) {
817
                FLayers layersToAdd = (FLayers) item;
818
                if (fmap != null) {
819
                    fmap.addAsCollectionListener(layersToAdd);
820
                }
821
            }
822
823 43376 jjdelcerro
            // Cuando falla la carga de la capa desde la persistencia, puede
824
            // haberse perdido quien es el parent. Asi que lo asignamos aqui
825
            // por si acaso.
826
            item.setParentLayer(this);
827 42287 jjdelcerro
            layers.add(item);
828
        }
829
    }
830 40435 jjdelcerro
831 42543 jjdelcerro
    @Override
832 42287 jjdelcerro
    public MapContext getMapContext() {
833
        return fmap;
834
    }
835 40435 jjdelcerro
836 42543 jjdelcerro
    @Override
837 42287 jjdelcerro
    public void setAllActives(boolean active) {
838
        FLayer lyr;
839 40435 jjdelcerro
840 42287 jjdelcerro
        for (int i = 0; i < layers.size(); i++) {
841
            lyr = ((FLayer) layers.get(i));
842
            lyr.setActive(active);
843 40435 jjdelcerro
844 42287 jjdelcerro
            if (lyr instanceof LayerCollection) {
845
                ((LayerCollection) lyr).setAllActives(active);
846
            }
847
        }
848
    }
849 40435 jjdelcerro
850 44599 omartinez
    @Override
851 44073 jjdelcerro
    public List<FLayer> getLayers(Predicate<FLayer> filter) {
852
        List<FLayer> theLayers = this.toPlainList(this, filter);
853
        return theLayers;
854
    }
855
856 42543 jjdelcerro
    @Override
857 42287 jjdelcerro
    public FLayer[] getActives() {
858 44073 jjdelcerro
        List<FLayer> activeLayers = this.getLayers(new Predicate<FLayer>() {
859 42543 jjdelcerro
            @Override
860 44073 jjdelcerro
            public boolean test(FLayer layer) {
861 42287 jjdelcerro
                return layer.isActive();
862
            }
863 44073 jjdelcerro
        });
864
        return (FLayer[]) activeLayers.toArray(new FLayer[activeLayers.size()]);
865 42287 jjdelcerro
    }
866 44599 omartinez
867 42543 jjdelcerro
    @Override
868 42287 jjdelcerro
    public double getMinScale() {
869
        return -1; // La visibilidad o no la controla cada capa
870
        // dentro de una colecci?n
871
    }
872
873 42543 jjdelcerro
    @Override
874 42287 jjdelcerro
    public double getMaxScale() {
875
        return -1;
876
    }
877
878 42543 jjdelcerro
    @Override
879 42287 jjdelcerro
    public void setMinScale(double minScale) {
880 44073 jjdelcerro
        for (FLayer lyr : layers) {
881 42287 jjdelcerro
            lyr.setMinScale(minScale);
882
        }
883
    }
884
885 42543 jjdelcerro
    @Override
886 42287 jjdelcerro
    public void setMaxScale(double maxScale) {
887 44073 jjdelcerro
        for (FLayer lyr : layers) {
888 42287 jjdelcerro
            lyr.setMinScale(maxScale);
889
        }
890
    }
891
892 42543 jjdelcerro
    @Override
893 42287 jjdelcerro
    public void setActive(boolean b) {
894
        super.setActive(b);
895 44073 jjdelcerro
        for (FLayer layer : this.layers) {
896
            layer.setActive(b);
897 42287 jjdelcerro
        }
898
    }
899
900 46186 jjdelcerro
    public void setActive(boolean active, boolean applySublayers) {
901
        super.setActive(active);
902 46191 fdiaz
        if(applySublayers){
903 46186 jjdelcerro
            for (FLayer layer : this.layers) {
904
                layer.setActive(active);
905
            }
906
        }
907
    }
908
909 42543 jjdelcerro
    @Override
910 42287 jjdelcerro
    public boolean addLayerListener(LayerListener o) {
911 44073 jjdelcerro
        for (FLayer layer : this.layers) {
912
            layer.addLayerListener(o);
913 42287 jjdelcerro
        }
914
        return true;
915
    }
916
917 42543 jjdelcerro
    @Override
918 40435 jjdelcerro
    public DynObjectSet getInfo(Point p, double tolerance,
919 42287 jjdelcerro
            Cancellable cancel) throws LoadLayerException, DataException {
920
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
921 40435 jjdelcerro
    }
922
923 42543 jjdelcerro
    @Override
924 40435 jjdelcerro
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel,
925 42287 jjdelcerro
            boolean fast) throws LoadLayerException, DataException {
926
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
927 40435 jjdelcerro
    }
928 42287 jjdelcerro
929 42543 jjdelcerro
    @Override
930 40435 jjdelcerro
    public DynObjectSet getInfo(org.gvsig.fmap.geom.primitive.Point p,
931 42287 jjdelcerro
            double tolerance) throws LoadLayerException, DataException {
932 40435 jjdelcerro
        int i;
933
        FLayer layer;
934
        List res = new ArrayList();
935
        for (i = 0; i < this.layers.size(); i++) {
936
            layer = (FLayer) layers.get(i);
937
            if (layer instanceof InfoByPoint) {
938
                InfoByPoint queryable_layer = (InfoByPoint) layer;
939
                res.add(queryable_layer.getInfo(p, tolerance));
940
            }
941
        }
942 42287 jjdelcerro
        DynObjectSet[] innerSets
943
                = (DynObjectSet[]) res.toArray(new DynObjectSet[res.size()]);
944 40435 jjdelcerro
        return new MultiDynObjectSet(innerSets);
945
    }
946
947 42543 jjdelcerro
    @Override
948 42287 jjdelcerro
    public String getTocImageIcon() {
949
        return "layer-icon-group";
950
    }
951
952
    /**
953
     * <p>
954
     * Sets the <code>MapContext</code> that contains this layer node.</p>
955
     *
956
     * @param mapContext the <code>MapContext</code> that contains this layer
957
     * node
958
     */
959
    public void setMapContext(MapContext mapContext) {
960
        this.fmap = mapContext;
961 45535 fdiaz
        for (FLayer lyr : this.layers) {
962
            if(lyr instanceof FLayers){
963
                ((FLayers)lyr).setMapContext(mapContext);
964
            }
965
        }
966 42287 jjdelcerro
    }
967 40435 jjdelcerro
968 42543 jjdelcerro
    @Override
969 42287 jjdelcerro
    public void accept(Visitor visitor) throws BaseException {
970
        for (int i = 0; i < this.getLayersCount(); i++) {
971
            FLayer layer = this.getLayer(i);
972 43877 jjdelcerro
//            try {
973 42287 jjdelcerro
                if (layer instanceof LayersVisitable) {
974
                    ((LayersVisitable) layer).accept(visitor);
975
                } else {
976
                    visitor.visit(layer);
977 42002 jjdelcerro
                }
978 43877 jjdelcerro
//            } catch (VisitCanceledException ex) {
979
//                break;
980
//            }
981 42002 jjdelcerro
        }
982 42287 jjdelcerro
    }
983 40435 jjdelcerro
984 42543 jjdelcerro
    @Override
985 42287 jjdelcerro
    public void accept(LayersVisitor visitor) throws BaseException {
986
        for (int i = 0; i < this.getLayersCount(); i++) {
987
            FLayer layer = this.getLayer(i);
988
            if (layer instanceof LayersVisitable) {
989
                ((LayersVisitable) layer).accept(visitor);
990
            } else {
991
                visitor.visit(layer);
992
            }
993
        }
994
    }
995 40435 jjdelcerro
996 42543 jjdelcerro
    @Override
997 42287 jjdelcerro
    public Object getMetadataID() throws MetadataException {
998 42543 jjdelcerro
        StringBuilder strb = new StringBuilder();
999 42287 jjdelcerro
        strb.append("Layers(");
1000
        strb.append(this.getName());
1001
        strb.append("):{");
1002
        Iterator iter = this.layers.iterator();
1003
        while (iter.hasNext()) {
1004
            strb.append(((FLayer) iter.next()).getMetadataID());
1005
            strb.append(",");
1006
        }
1007
        strb.append("}");
1008
        return strb.toString();
1009 40435 jjdelcerro
1010 42287 jjdelcerro
    }
1011 40435 jjdelcerro
1012 42543 jjdelcerro
    @Override
1013 42287 jjdelcerro
    public Set getMetadataChildren() {
1014
        Set ret = new TreeSet();
1015
        Iterator iter = this.layers.iterator();
1016
        while (iter.hasNext()) {
1017
            ret.add(iter.next());
1018
        }
1019
        return ret;
1020
    }
1021 40435 jjdelcerro
1022 42543 jjdelcerro
    @Override
1023 42287 jjdelcerro
    public String getMetadataName() throws MetadataException {
1024 42543 jjdelcerro
        StringBuilder strb = new StringBuilder();
1025 42287 jjdelcerro
        strb.append("Layer Group '");
1026
        strb.append(this.getName());
1027
        strb.append("': {");
1028
        Iterator iter = this.layers.iterator();
1029
        while (iter.hasNext()) {
1030
            strb.append(((FLayer) iter.next()).getMetadataName());
1031
            strb.append(",");
1032
        }
1033
        strb.append("}");
1034
        return strb.toString();
1035
    }
1036 40435 jjdelcerro
1037 42543 jjdelcerro
    @Override
1038 42287 jjdelcerro
    public void beginDraw(Graphics2D g, ViewPort viewPort) {
1039 42651 jjdelcerro
        if( fmap == null ) {
1040
            return;
1041
        }
1042 42287 jjdelcerro
        LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
1043
        fmap.fireLayerDrawingEvent(beforeEvent);
1044
    }
1045 40435 jjdelcerro
1046 42543 jjdelcerro
    @Override
1047 42287 jjdelcerro
    public void endDraw(Graphics2D g, ViewPort viewPort) {
1048 42651 jjdelcerro
        if( fmap == null ) {
1049
            return;
1050
        }
1051 42287 jjdelcerro
        LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
1052
        fmap.fireLayerDrawingEvent(afterEvent);
1053
    }
1054 40435 jjdelcerro
1055 41840 jjdelcerro
    public static class RegisterPersistence implements Callable {
1056
1057 42543 jjdelcerro
        @Override
1058 41840 jjdelcerro
        public Object call() {
1059 42287 jjdelcerro
1060
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1061
            DynStruct definition = manager.addDefinition(
1062
                    FLayers.class,
1063
                    "FLayers",
1064
                    "FLayers Persistence definition",
1065
                    null,
1066
                    null
1067
            );
1068
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, "FLyrDefault");
1069
1070
            definition.addDynFieldObject("mapContext").setClassOfValue(MapContext.class).setMandatory(true);
1071
            definition.addDynFieldList("layers").setClassOfItems(FLayer.class).setMandatory(true);
1072
1073 41840 jjdelcerro
            return Boolean.TRUE;
1074
        }
1075
    }
1076 40435 jjdelcerro
1077 42543 jjdelcerro
    @Override
1078 42287 jjdelcerro
    protected void doDispose() throws BaseException {
1079
        if (layers != null) {
1080 44073 jjdelcerro
            for (FLayer layer : this.layers) {
1081
                dispose(layer);
1082 42287 jjdelcerro
            }
1083
        }
1084
    }
1085 40435 jjdelcerro
1086 42543 jjdelcerro
    @Override
1087 42287 jjdelcerro
    public void move(FLayer layer, LayerCollection group, int where, FLayer adjoiningLayer) throws LayerNotFoundInCollectionException {
1088 40435 jjdelcerro
1089 42287 jjdelcerro
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(layer));
1090
        group.addLayer(layer, where, adjoiningLayer);
1091
        removeLayer(layer);
1092
        this.updateDrawVersion();
1093
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(layer));
1094 40435 jjdelcerro
1095 42287 jjdelcerro
    }
1096 40435 jjdelcerro
1097 42287 jjdelcerro
    public void join(FLayer layer, LayerCollection group) {
1098
        try {
1099
            layers.remove(layer);
1100
            group.addLayer(layer, END, null);
1101
            this.updateDrawVersion();
1102
        } catch (LayerNotFoundInCollectionException e) {
1103
            throw new MapContextRuntimeException(e);
1104
        }
1105
    }
1106 40435 jjdelcerro
1107 42543 jjdelcerro
    @Override
1108 42287 jjdelcerro
    public void move(FLayer layer, LayerCollection group) {
1109
        try {
1110
            move(layer, group, END, null);
1111
        } catch (LayerNotFoundInCollectionException e) {
1112
            throw new MapContextRuntimeException(e);
1113
        }
1114
    }
1115
1116 42543 jjdelcerro
    @Override
1117 42287 jjdelcerro
    public void addLayer(FLayer layer, int where, FLayer adjoiningLayer)
1118
            throws LayerNotFoundInCollectionException {
1119
1120
        switch (where) {
1121
            case BEGIN:
1122
                addLayer(0, layer);
1123
                break;
1124
            case BEFORE:
1125
                if (adjoiningLayer != null) {
1126
                    if (this.layers.contains(adjoiningLayer)) {
1127
                        for (int i = 0; i < this.getLayersCount(); i++) {
1128
                            if (adjoiningLayer == this.getLayer(i)) {
1129
                                addLayer(i, layer);
1130
                                break;
1131
                            }
1132
                        }
1133
                    } else {
1134
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1135
                    }
1136
                } else {
1137
                    addLayer(0, layer);
1138
                }
1139
                break;
1140
            case AFTER:
1141
                if (adjoiningLayer != null) {
1142
                    if (this.layers.contains(adjoiningLayer)) {
1143
                        for (int i = 0; i < this.getLayersCount(); i++) {
1144
                            if (adjoiningLayer == this.getLayer(i)) {
1145
                                addLayer(i + 1, layer);
1146
                                break;
1147
                            }
1148
                        }
1149
                    } else {
1150
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1151
                    }
1152
                } else {
1153
                    this.addLayer(layer);
1154
                }
1155
                break;
1156
            default: // By default add layer an the end of the collection
1157
                this.addLayer(layer);
1158
                break;
1159
        }
1160
1161
    }
1162
1163 42293 jjdelcerro
    public FLayer getFirstActiveLayer() {
1164 44599 omartinez
        List<FLayer> theLayers = this.getLayers(LAYER_ACTIVE);
1165 44073 jjdelcerro
        if( theLayers.isEmpty() ) {
1166
            return null;
1167 42293 jjdelcerro
        }
1168 44073 jjdelcerro
        return theLayers.get(0);
1169 42293 jjdelcerro
    }
1170 42513 jjdelcerro
1171
    public FLyrVect getFirstActiveVectorLayer() {
1172 44599 omartinez
        List<FLayer> theLayers = this.getLayers(LAYER_ACTIVE.and(LAYER_FLYRVECT));
1173 44073 jjdelcerro
        if( theLayers.isEmpty() ) {
1174
            return null;
1175 42513 jjdelcerro
        }
1176 44073 jjdelcerro
        return (FLyrVect) theLayers.get(0);
1177 42513 jjdelcerro
    }
1178 42293 jjdelcerro
1179 42543 jjdelcerro
    @Override
1180 43608 jjdelcerro
    public Iterator<FLayer> iterator() {
1181 42293 jjdelcerro
        return this.layers.iterator();
1182
    }
1183
1184 43608 jjdelcerro
    public Iterator<FLayer> deepiterator() {
1185 44073 jjdelcerro
        List theLayers = toPlainList(this);
1186
        return theLayers.iterator();
1187 42293 jjdelcerro
    }
1188
1189 42543 jjdelcerro
    @Override
1190 42293 jjdelcerro
    public int size() {
1191
        return this.layers.size();
1192
    }
1193
1194 42543 jjdelcerro
    @Override
1195 42513 jjdelcerro
    public FLayer get(int index) {
1196 42293 jjdelcerro
        return this.layers.get(index);
1197
    }
1198
1199 42543 jjdelcerro
    @Override
1200 42293 jjdelcerro
    public boolean isEmpty() {
1201
        return this.layers.isEmpty();
1202
    }
1203
1204 42543 jjdelcerro
    @Override
1205 42293 jjdelcerro
    public boolean contains(Object o) {
1206
        return this.layers.contains(o);
1207
    }
1208
1209 42543 jjdelcerro
    @Override
1210 42293 jjdelcerro
    public Object[] toArray() {
1211
        return this.layers.toArray();
1212
    }
1213
1214 42543 jjdelcerro
    @Override
1215 42293 jjdelcerro
    public Object[] toArray(Object[] ts) {
1216
        return this.layers.toArray(ts);
1217
    }
1218
1219 42543 jjdelcerro
    @Override
1220 42513 jjdelcerro
    public boolean add(FLayer e) {
1221
        this.addLayer(e);
1222 42293 jjdelcerro
        return true;
1223
    }
1224
1225 42543 jjdelcerro
    @Override
1226 42293 jjdelcerro
    public boolean remove(Object o) {
1227
        this.removeLayer((FLayer) o);
1228
        return true;
1229
    }
1230
1231 42543 jjdelcerro
    @Override
1232 42293 jjdelcerro
    public boolean containsAll(Collection clctn) {
1233
        return this.layers.containsAll(clctn);
1234
    }
1235
1236 42543 jjdelcerro
    @Override
1237 42513 jjdelcerro
    public void add(int i, FLayer e) {
1238 42293 jjdelcerro
        this.addLayer(i, (FLayer) e);
1239
    }
1240
1241 42543 jjdelcerro
    @Override
1242 42513 jjdelcerro
    public FLayer remove(int i) {
1243 42293 jjdelcerro
        FLayer o = this.getLayer(i);
1244
        this.removeLayer(i);
1245
        return o;
1246
    }
1247
1248 42543 jjdelcerro
    @Override
1249 42293 jjdelcerro
    public int indexOf(Object o) {
1250
        return this.layers.indexOf(o);
1251
    }
1252
1253 42543 jjdelcerro
    @Override
1254 42293 jjdelcerro
    public int lastIndexOf(Object o) {
1255
        return this.layers.lastIndexOf(o);
1256
    }
1257
1258 42543 jjdelcerro
    @Override
1259 42293 jjdelcerro
    public ListIterator listIterator() {
1260
        return this.layers.listIterator();
1261
    }
1262
1263 42543 jjdelcerro
    @Override
1264 42293 jjdelcerro
    public ListIterator listIterator(int i) {
1265
        return this.layers.listIterator(i);
1266
    }
1267
1268 42543 jjdelcerro
    @Override
1269 42293 jjdelcerro
    public List subList(int i, int i1) {
1270
        return this.layers.subList(i, i1);
1271
    }
1272
1273 42543 jjdelcerro
    @Override
1274 42293 jjdelcerro
    public boolean addAll(Collection clctn) {
1275
        Iterator it = clctn.iterator();
1276
        while( it.hasNext() ) {
1277 42513 jjdelcerro
            this.add((FLayer) it.next());
1278 42293 jjdelcerro
        }
1279
        return true;
1280
    }
1281
1282 42543 jjdelcerro
    @Override
1283 42293 jjdelcerro
    public boolean addAll(int i, Collection clctn) {
1284
        Iterator it = clctn.iterator();
1285
        while( it.hasNext() ) {
1286 42513 jjdelcerro
            this.add(i, (FLayer) it.next());
1287 42293 jjdelcerro
        }
1288
        return true;
1289
    }
1290
1291 42543 jjdelcerro
    @Override
1292 42293 jjdelcerro
    public boolean removeAll(Collection clctn) {
1293
        Iterator it = clctn.iterator();
1294
        while( it.hasNext() ) {
1295 42513 jjdelcerro
            this.remove((FLayer) it.next());
1296 42293 jjdelcerro
        }
1297
        return true;
1298
    }
1299
1300 42543 jjdelcerro
    @Override
1301 42293 jjdelcerro
    public boolean retainAll(Collection clctn) {
1302
        Iterator it = this.layers.iterator();
1303
        while( it.hasNext() ) {
1304
            Object o = it.next();
1305
            if( !clctn.contains(o) ) {
1306 42513 jjdelcerro
                this.remove((FLayer) o);
1307 42293 jjdelcerro
            }
1308
        }
1309
        return true;
1310
    }
1311
1312 42543 jjdelcerro
    @Override
1313 42513 jjdelcerro
    public FLayer set(int i, FLayer e) {
1314 42293 jjdelcerro
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1315
    }
1316
1317 44548 jjdelcerro
    public FLayers createFLayerGroup(String name) {
1318
        FLayers group = new FLayers();
1319
        group.setMapContext(this.getMapContext());
1320
        group.setParentLayer(this);
1321
        group.setName(name);
1322
        return group;
1323
    }
1324 45535 fdiaz
1325
    @Override
1326
    public FLayers cloneLayer() throws Exception {
1327
        FLayers cloned = new FLayers();
1328
1329
        cloned.fmap = this.fmap;
1330
        for (FLayer layer : this.layers) {
1331
            FLayer l = layer.cloneLayer();
1332
            l.setParentLayer(cloned);
1333
            cloned.add(l);
1334
        }
1335
        for (LayerCollectionListener layerCollectionListener : this.layerCollectionListeners) {
1336
            cloned.addLayerCollectionListener(layerCollectionListener);
1337
        }
1338
        for (LayerListener layerListener : this.layerListeners) {
1339
            cloned.addLayerListener(layerListener);
1340
        }
1341
        cloned.setTransparency(this.getTransparency());
1342
        cloned.setMaxScale(this.getMaxScale());
1343
        cloned.setMinScale(this.getMinScale());
1344
        cloned.setCoordTrans(this.getCoordTrans());
1345
        cloned.setFLayerStatus(this.getFLayerStatus());
1346
        cloned.setTemporary(this.isTemporary());
1347
        cloned.setTocStatusImage(this.getTocStatusImage());
1348
        cloned.metadataContainer = this.metadataContainer;
1349
        cloned.setParentLayer(this.getParentLayer());
1350
        cloned.properties = this.properties;
1351
1352
        return cloned;
1353
    }
1354 45945 fdiaz
1355
    @Override
1356
    public void clear() {
1357
        //Don't clear layer list for compatibility with older code
1358
//        this.layers.clear();
1359
        super.clear();
1360
    }
1361
1362
    public void clearMetadata() {
1363
        super.clear();
1364
    }
1365
1366
    /**
1367
     * Remove all layers of this object.
1368
     */
1369
    public void clearLayers() {
1370
        this.layers.clear();
1371
        this.updateDrawVersion();
1372
    }
1373 45535 fdiaz
1374
1375 45945 fdiaz
1376
1377 40435 jjdelcerro
}