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

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