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

History | View | Annotate | Download (57 KB)

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

    
25
import java.awt.Graphics2D;
26
import java.awt.Point;
27
import java.awt.image.BufferedImage;
28
import java.util.ArrayList;
29
import java.util.Collection;
30
import java.util.Collections;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.ListIterator;
34
import java.util.Set;
35
import java.util.TreeSet;
36

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

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

    
91
    /**
92
     * List with all listeners registered for this kind of node.
93
     *
94
     * @see #addLayerCollectionListener(LayerCollectionListener)
95
     * @see #removeLayerCollectionListener(LayerCollectionListener)
96
     * @see #callLayerAdded(LayerCollectionEvent)
97
     * @see #callLayerAdding(LayerCollectionEvent)
98
     * @see #callLayerMoved(LayerPositionEvent)
99
     * @see #callLayerMoving(LayerPositionEvent)
100
     * @see #callLayerRemoved(LayerCollectionEvent)
101
     * @see #callLayerRemoving(LayerCollectionEvent)
102
     */
103
    protected ArrayList layerCollectionListeners = null;
104

    
105
    /**
106
     * A synchronized list with the layers.
107
     *
108
     * @see #setAllVisibles(boolean)
109
     * @see #addLayer(FLayer)
110
     * @see #addLayer(int, FLayer)
111
     * @see #moveTo(int, int)
112
     * @see #removeLayer(FLayer)
113
     * @see #removeLayer(int)
114
     * @see #removeLayer(String)
115
     * @see #replaceLayer(String, FLayer)
116
     * @see #getVisibles()
117
     * @see #getLayer(int)
118
     * @see #getLayer(String)
119
     * @see #getLayersCount()
120
     * @see #getFullEnvelope()
121
     */
122
    protected List<FLayer> layers = null;
123

    
124
    /**
125
     * The model of the layer.
126
     *
127
     * @see #getMapContext()
128
     */
129
    protected MapContext fmap;
130

    
131
    /**
132
     * Useful for debug the problems during the implementation.
133
     */
134
    private static Logger logger = LoggerFactory.getLogger(FLayers.class);
135

    
136
    public FLayers() {
137
        super();
138
        layerCollectionListeners = new ArrayList();
139
        layers = Collections.synchronizedList(new ArrayList());
140

    
141
        logger = LoggerFactory.getLogger(FLayers.class);
142
    }
143
    /*
144
     * (non-Javadoc)
145
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
146
     */
147

    
148
    public void addLayerCollectionListener(LayerCollectionListener listener) {
149
        if (!layerCollectionListeners.contains(listener)) {
150
            layerCollectionListeners.add(listener);
151
        }
152
    }
153

    
154
    /*
155
     * (non-Javadoc)
156
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
157
     */
158
    public void setAllVisibles(boolean visible) {
159
        FLayer lyr;
160

    
161
        for (int i = 0; i < layers.size(); i++) {
162
            lyr = ((FLayer) layers.get(i));
163
            lyr.setVisible(visible);
164

    
165
            if (lyr instanceof LayerCollection) {
166
                ((LayerCollection) lyr).setAllVisibles(visible);
167
            }
168
        }
169
    }
170

    
171
    /*
172
     * (non-Javadoc)
173
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
174
     */
175
    public void removeLayerCollectionListener(LayerCollectionListener listener) {
176
        layerCollectionListeners.remove(listener);
177
    }
178

    
179
    /**
180
     * Adds a layer on an specified position in this node.
181
     *
182
     * @param pos position in the inner list where the layer will be added
183
     * @param layer a layer
184
     */
185
    private void doAddLayer(int pos, FLayer layer) {
186
        layers.add(pos, layer);
187
        ToolsLocator.getDisposableManager().bind(layer);
188
        layer.setParentLayer(this);
189
        IProjection layerProj = layer.getProjection();
190
        if (layerProj != null && fmap != null) {
191
            IProjection mapContextProj = fmap.getProjection();
192
            // TODO REVISAR ESTO !!!!
193
            // Esta condici?n puede que no fuese exacta para todos los casos
194
            if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
195
                ICoordTrans ct = layerProj.getCT(mapContextProj);
196
                layer.setCoordTrans(ct);
197
            } else {
198
                layer.setCoordTrans(null);
199
            }
200
        }
201
        this.updateDrawVersion();
202
    }
203

    
204
    /*
205
     * (non-Javadoc)
206
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
207
     */
208
    public void addLayer(FLayer layer) {
209

    
210
        MapContext mco = this.getMapContext();
211

    
212
        if (mco != null) {
213
            /*
214
             * Get order manager from map context
215
             */
216
            int position = mco.getOrderManager().getPosition(this, layer);
217
            addLayer(position, layer);
218
        } else {
219
            /*
220
             * This happens when FLayers object is not in a
221
             * map context, so no order manager is available.
222
             */
223
            addLayer(layers.size(), layer);
224
        }
225
    }
226

    
227
    /**
228
     * Adds a layer in an specified position in this node.
229
     *
230
     * @param layer a layer
231
     */
232
    public void addLayer(int pos, FLayer layer) {
233
        try {
234
            //Notificamos a la capa que va a ser a?adida
235
            if (layer instanceof FLyrDefault) {
236
                ((FLyrDefault) layer).wakeUp();
237
            }
238

    
239
            if (layer instanceof FLayers) {
240
                FLayers layers = (FLayers) layer;
241
                fmap.addAsCollectionListener(layers);
242
            }
243
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
244

    
245
            doAddLayer(pos, layer);
246

    
247
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
248
        } catch (CancelationException e) {
249
            logger.warn(e.getMessage());
250
        } catch (LoadLayerException e) {
251
            layer.setAvailable(false);
252
            layer.addError(e);
253
        }
254
    }
255

    
256
    /*
257
     * (non-Javadoc)
258
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
259
     */
260
    public void moveTo(int from, int to) throws CancelationException {
261
        int newfrom = layers.size() - from - 1;
262
        int newto = layers.size() - to - 1;
263
        if (newfrom < 0 || newfrom >= layers.size() || newto < 0 || newto >= layers.size()) {
264
            return;
265
        }
266
        FLayer aux = (FLayer) layers.get(newfrom);
267
        callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
268
        layers.remove(newfrom);
269
        layers.add(newto, aux);
270
        this.updateDrawVersion();
271
        callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
272
    }
273

    
274
    /**
275
     * Removes an inner layer.
276
     *
277
     * @param lyr a layer
278
     */
279
    private void doRemoveLayer(FLayer lyr) {
280
        layers.remove(lyr);
281
        lyr.dispose();
282
        this.updateDrawVersion();
283
    }
284

    
285
    /*
286
     * (non-Javadoc)
287
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
288
     */
289
    public void removeLayer(FLayer lyr) throws CancelationException {
290
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
291
        doRemoveLayer(lyr);
292
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
293
    }
294

    
295
    /*
296
     * (non-Javadoc)
297
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
298
     */
299
    public void removeLayer(int idLayer) {
300
        FLayer lyr = (FLayer) layers.get(idLayer);
301
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
302
        this.doRemoveLayer(lyr);
303
//                layers.remove(idLayer);
304
//                this.updateDrawVersion();
305
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
306
    }
307

    
308
    /*
309
     * (non-Javadoc)
310
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
311
     */
312
    public void removeLayer(String layerName) {
313
        FLayer lyr;
314

    
315
        for (int i = 0; i < layers.size(); i++) {
316
            lyr = ((FLayer) layers.get(i));
317

    
318
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
319
                removeLayer(i);
320

    
321
                break;
322
            }
323
        }
324
    }
325

    
326
    /**
327
     * Replace a layer identified by its name, by another.
328
     *
329
     * @param layerName the name of the layer to be replaced
330
     * @param layer the new layer
331
     * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
332
     */
333
    public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException {
334
        replaceLayer(getLayer(layerName), layer);
335
    }
336

    
337
    /**
338
     * Replace a layer by another layer. It search recursively by all the
339
     * ILayerCollection nodes
340
     *
341
     * @param layer the layer to be replaced
342
     * @param newLayer the new layer
343
     */
344
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
345
        replaceLayer(this, layer, newLayer);
346
    }
347

    
348
    /**
349
     * Replace a layer by other layer. It search recursively by all the
350
     * ILayerCollection nodes
351
     *
352
     * @param parentLayer the parent layer
353
     * @param layer the layer to be replaced
354
     * @param newLayer the new layer
355
     * @throws LoadLayerException
356
     */
357
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
358
        FLayer lyr;
359
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
360
            lyr = ((FLayer) parentLayer.getLayer(i));
361
            if (lyr.equals(layer)) {
362
                parentLayer.removeLayer(i);
363
                parentLayer.addLayer(i, newLayer);
364
                break;
365
            }
366
            if (lyr instanceof LayerCollection) {
367
                replaceLayer((FLayers) lyr, layer, newLayer);
368
            }
369
        }
370
    }
371

    
372
    /*
373
     * (non-Javadoc)
374
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
375
     */
376
    public FLayer[] getVisibles() {
377
        ArrayList array = new ArrayList();
378
        LayersIterator iter = new LayersIterator(this) {
379
            public boolean evaluate(FLayer layer) {
380
                return layer.isVisible();
381
            }
382

    
383
        };
384

    
385
        while (iter.hasNext()) {
386
            array.add(iter.nextLayer());
387
        }
388

    
389
        return (FLayer[]) array.toArray(new FLayer[0]);
390
    }
391

    
392
    /*
393
     * (non-Javadoc)
394
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
395
     */
396
    public FLayer getLayer(int index) {
397
        return (FLayer) layers.get(index);
398
    }
399

    
400
    /*
401
     * (non-Javadoc)
402
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
403
     */
404
    public FLayer getLayer(String layerName) {
405
        for (int i = 0; i < layers.size(); i++) {
406
            FLayer lyr = ((FLayer) layers.get(i));
407
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
408
                return lyr;
409
            }
410
            if (lyr instanceof FLayers) {
411
                List layerList = toPlainList(lyr);
412
                for (int j = 0; j < layerList.size(); j++) {
413
                    FLayer lyr2 = ((FLayer) layerList.get(j));
414
                    if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
415
                        return lyr2;
416
                    }
417
                }
418
            }
419
        }
420
        return null;
421
    }
422

    
423
    private List toPlainList(FLayer layer) {
424
        return toPlainList(layer, new ArrayList());
425
    }
426

    
427
    private List toPlainList(FLayer layer, List result) {
428
        if (layer instanceof FLayers) {
429
            FLayers layerGroup = (FLayers) layer;
430
            for (int i = 0; i < layerGroup.getLayersCount(); i++) {
431
                toPlainList(layerGroup.getLayer(i), result);
432
            }
433
        } else {
434
            result.add(layer);
435
        }
436
        return result;
437
    }
438

    
439
    /*
440
     * (non-Javadoc)
441
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
442
     */
443
    public int getLayersCount() {
444
        return layers.size();
445
    }
446

    
447
    /*
448
     * (non-Javadoc)
449
     * @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)
450
     */
451
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
452
            Cancellable cancel, double scale) throws ReadException {
453
        // FIXME Arreglar este error
454
        throw new RuntimeException("Esto no deberia de llamarse");
455
    }
456

    
457
    /*
458
     * (non-Javadoc)
459
     *
460
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
461
     * com.iver.cit.gvsig.fmap.ViewPort,
462
     * com.iver.utiles.swing.threads.Cancellable, double,
463
     * javax.print.attribute.PrintAttributes)
464
     */
465
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
466
            double scale, PrintAttributes properties)
467
            throws ReadException {
468
        throw new RuntimeException("No deberia pasar por aqui");
469
    }
470

    
471
    public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
472
            double scale, PrintAttributes properties)
473
            throws ReadException {
474
        this.print_old(g, viewPort, cancel, scale, properties, null);
475
    }
476

    
477
    /**
478
     * <p>
479
     * Checks all layers (each one as a sub-node of this node <i>collection of
480
     * layers</i>) of this collection and draws their requested properties. If a
481
     * node is a group of layers (<code>ComposedLayer</code>), executes it's
482
     * drawn.</p>
483
     *
484
     * <p>
485
     * All nodes which could group with the composed layer <code>group</code>,
486
     * will be drawn together. And once the <code>
487
     * group</code> is drawn, will be set to <code>null</code> if hasn't a
488
     * parent layer.</p>
489
     *
490
     * <p>
491
     * The particular implementation depends on the kind of each layer and
492
     * composed layer. And this process can be cancelled at any time by the
493
     * shared object <code>cancel</code>.</p>
494
     *
495
     * <p>
496
     * According the print quality, labels will be printed in different
497
     * resolution:
498
     * <ul>
499
     * <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
500
     * <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
501
     * <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
502
     * </ul>
503
     * </p>
504
     *
505
     * @param g for rendering 2-dimensional shapes, text and images on the
506
     * Java(tm) platform
507
     * @param viewPort the information for drawing the layers
508
     * @param cancel shared object that determines if this layer can continue
509
     * being drawn
510
     * @param scale the scale of the view. Must be between
511
     * {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
512
     * @param properties properties that will be print
513
     * @param group a composed layer pending to paint; if this parameter is
514
     * <code>null</code>, the composed layer
515
     *
516
     * @return <code>null</code> if the layers in <code>group</code> had been
517
     * drawn or were <code>null</code>; otherwise, the <code>group</code>
518
     *
519
     * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double,
520
     * PrintAttributes)
521
     *
522
     * @throws ReadDriverException if fails the driver reading the data.
523
     */
524
    public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
525
            throws ReadException {
526
        double dpi = 72;
527

    
528
        int resolution = properties.getPrintQuality();
529
        if (resolution == PrintAttributes.PRINT_QUALITY_NORMAL) {
530
            dpi = 300;
531
        } else if (resolution == PrintAttributes.PRINT_QUALITY_HIGH) {
532
            dpi = 600;
533
        } else if (resolution == PrintAttributes.PRINT_QUALITY_DRAFT) {
534
            dpi = 72;
535
        }
536

    
537
        // TODO: A la hora de imprimir, isWithinScale falla, porque est?
538
        // calculando la escala en pantalla, no para el layout.
539
        // Revisar esto.
540
        // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
541
        for (int i = 0; i < layers.size(); i++) {
542
            FLayer lyr = (FLayer) layers.get(i);
543
            if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
544
                continue;
545
            }
546

    
547
            try {
548

    
549
                ///// CHEMA ComposedLayer
550
                // Checks for draw group (ComposedLayer)
551
                if (group != null) {
552
                    if (lyr instanceof FLayers) {
553
                        group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
554
                    } else {
555
                        // If layer can be added to the group, does it
556
                        if (lyr instanceof ILabelable
557
                                && ((ILabelable) lyr).isLabeled()
558
                                && ((ILabelable) lyr).getLabelingStrategy() != null
559
                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
560
                            group.add(lyr);
561
                        } else {
562
                            // draw the 'pending to draw' layer group
563
                            group.print(g, viewPort, cancel, scale, properties);
564

    
565
                            // gets a new group instance
566
                            if (lyr instanceof ILabelable
567
                                    && ((ILabelable) lyr).isLabeled()
568
                                    && ((ILabelable) lyr).getLabelingStrategy() != null
569
                                    && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
570
                                group = lyr.newComposedLayer();
571
                            } else {
572
                                group = null;
573
                            }
574
                            // if layer hasn't group, draws it inmediately
575
                            if (group == null) {
576
                                if (lyr instanceof FLayers) {
577
                                    group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
578
                                } else {
579
                                    lyr.print(g, viewPort, cancel, scale, properties);
580
                                    if (lyr instanceof ILabelable
581
                                            && ((ILabelable) lyr).isLabeled()
582
                                            && ((ILabelable) lyr).getLabelingStrategy() != null
583
                                            && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
584
                                        ILabelable lLayer = (ILabelable) lyr;
585
                                        lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
586
                                    }
587
                                }
588
                            } else {
589
                                // add the layer to the group
590
                                group.setMapContext(fmap);
591
                                group.add(lyr);
592

    
593
                            }
594

    
595
                        }
596
                    }
597
                } else {
598
                    // gets a new group instance
599
                    group = lyr.newComposedLayer();
600
                    // if layer hasn't group, draws it inmediately
601
                    if (group == null) {
602
                        if (lyr instanceof FLayers) {
603
                            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
604
                        } else {
605
                            lyr.print(g, viewPort, cancel, scale, properties);
606
                            if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
607
                                ILabelable lLayer = (ILabelable) lyr;
608

    
609
                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
610
                            }
611
                        }
612
                    } else {
613
                        // add the layer to the group
614
                        group.setMapContext(fmap);
615
                        group.add(lyr);
616

    
617
                    }
618
                }
619
                ///// CHEMA ComposedLayer
620

    
621
            } catch (Exception e) {
622
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
623
                fmap.addLayerError(mesg);
624
                logger.error(mesg, e);
625
            }
626

    
627
        }
628

    
629
        ///// CHEMA ComposedLayer
630
        if (group != null && this.getParentLayer() == null) {
631
            //si tenemos un grupo pendiente de pintar, pintamos
632
            group.print(g, viewPort, cancel, scale, properties);
633
            group = null;
634

    
635
        }
636
                ///// CHEMA ComposedLayer
637

    
638
        //                if (getVirtualLayers() != null) {
639
        //                        getVirtualLayers().print( g, viewPort, cancel, scale, properties);
640
        //                }
641
        ///// CHEMA ComposedLayer
642
        return group;
643
        ///// CHEMA ComposedLayer
644
    }
645

    
646
    /*
647
     * (non-Javadoc)
648
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
649
     */
650
    public Envelope getFullEnvelope() {
651
        Envelope rAux = null;
652
        boolean first = true;
653

    
654
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
655
            FLayer capa = (FLayer) iter.next();
656
            try {
657
                if (first) {
658
                    rAux = (Envelope) capa.getFullEnvelope().clone();
659
                    first = false;
660
                } else {
661
                    rAux.add(capa.getFullEnvelope());
662
                }
663
            } catch (Exception e) {
664
                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
665
            }
666
        }
667

    
668
        return rAux;
669
    }
670

    
671
    /**
672
     * Notifies all listeners associated to this collection of layers, that
673
     * another layer is going to be added or replaced in the internal list of
674
     * layers.
675
     *
676
     * @param e a layer collection event with the new layer
677
     */
678
    protected void callLayerAdding(LayerCollectionEvent event)
679
            throws CancelationException {
680
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
681
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
682
            ((LayerCollectionListener) iter.next()).layerAdding(event);
683
        }
684
    }
685

    
686
    /**
687
     * Notifies all listeners associated to this collection of layers, that a
688
     * layer is going to be removed from the internal list of layers.
689
     *
690
     * @param event a layer collection event with the layer being removed
691
     *
692
     * @throws CancelationException any exception produced during the
693
     * cancellation of the driver.
694
     */
695
    protected void callLayerRemoving(LayerCollectionEvent event)
696
            throws CancelationException {
697
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
698
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
699
            ((LayerCollectionListener) iter.next()).layerRemoving(event);
700
        }
701
    }
702

    
703
    /**
704
     * Notifies all listeners associated to this collection of layers, that a
705
     * layer is going to be moved in the internal list of layers.
706
     *
707
     * @param event a layer collection event with the layer being moved, and the
708
     * initial and final positions
709
     *
710
     * @throws CancelationException any exception produced during the
711
     * cancellation of the driver.
712
     */
713
    protected void callLayerMoving(LayerPositionEvent event)
714
            throws CancelationException {
715
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
716
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
717
            ((LayerCollectionListener) iter.next()).layerMoving(event);
718
        }
719
    }
720

    
721
    /**
722
     * Notifies all listeners associated to this collection of layers, that
723
     * another layer has been added or replaced in the internal list of layers.
724
     *
725
     * @param e a layer collection event with the new layer
726
     */
727
    protected void callLayerAdded(LayerCollectionEvent event) {
728
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
729
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
730
            ((LayerCollectionListener) iter.next()).layerAdded(event);
731
        }
732
    }
733

    
734
    /**
735
     * Notifies all listeners associated to this collection of layers, that
736
     * another layer has been removed from the internal list of layers.
737
     *
738
     * @param e a layer collection event with the layer removed
739
     */
740
    protected void callLayerRemoved(LayerCollectionEvent event) {
741
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
742
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
743
            ((LayerCollectionListener) iter.next()).layerRemoved(event);
744
        }
745
    }
746

    
747
    /**
748
     * Notifies all listeners associated to this collection of layers, that
749
     * another layer has been moved in the internal list of layers.
750
     *
751
     * @param e a layer collection event with the layer moved, and the initial
752
     * and final positions
753
     */
754
    protected void callLayerMoved(LayerPositionEvent event) {
755
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
756
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
757
            ((LayerCollectionListener) iter.next()).layerMoved(event);
758
        }
759
    }
760

    
761
    public void saveToState(PersistentState state) throws PersistenceException {
762

    
763
        super.saveToState(state);
764

    
765
        state.set("mapContext", fmap);
766

    
767
        List layersToSave = new ArrayList();
768
        Iterator iter = this.layers.iterator();
769
        while (iter.hasNext()) {
770
            FLayer layer = (FLayer) iter.next();
771
            if (!layer.isTemporary()) {
772
                layersToSave.add(layer);
773
            }
774
        }
775
        state.set("layers", layersToSave);
776
    }
777

    
778
    public void loadFromState(PersistentState state) throws PersistenceException {
779

    
780
        super.loadFromState(state);
781

    
782
        setMapContext((MapContext) state.get("mapContext"));
783
        Iterator iter = state.getIterator("layers");
784
        while (iter.hasNext()) {
785
            FLayer item = (FLayer) iter.next();
786
            layers.add(item);
787
        }
788
    }
789

    
790
    /*
791
     * (non-Javadoc)
792
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
793
     */
794
    public MapContext getMapContext() {
795
        return fmap;
796
    }
797

    
798
    /*
799
     * I don't think this implementation makes any sense.
800
     * We can group layers with different transformations,
801
     * we cannot set the ICoordTrans for all (?)
802
     * 
803
     public void setCoordTrans(ICoordTrans ct) {
804
     super.setCoordTrans(ct);
805

806
     for (Iterator iter = layers.iterator(); iter.hasNext();) {
807
     FLayer layer = (FLayer) iter.next();
808
     layer.setCoordTrans(ct);
809
     }
810
     }
811
     */
812
    /*
813
     * (non-Javadoc)
814
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
815
     */
816
    public void setAllActives(boolean active) {
817
        FLayer lyr;
818

    
819
        for (int i = 0; i < layers.size(); i++) {
820
            lyr = ((FLayer) layers.get(i));
821
            lyr.setActive(active);
822

    
823
            if (lyr instanceof LayerCollection) {
824
                ((LayerCollection) lyr).setAllActives(active);
825
            }
826
        }
827
    }
828

    
829
    /*
830
     * (non-Javadoc)
831
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
832
     */
833
    public FLayer[] getActives() {
834
        List ret = new ArrayList();
835
        LayersIterator it = new LayersIterator(this) {
836

    
837
            public boolean evaluate(FLayer layer) {
838
                return layer.isActive();
839
            }
840

    
841
        };
842

    
843
        while (it.hasNext()) {
844
            ret.add(it.next());
845
        }
846
        return (FLayer[]) ret.toArray(new FLayer[0]);
847
    }
848

    
849
    /*
850
     * (non-Javadoc)
851
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMinScale()
852
     */
853
    public double getMinScale() {
854
        return -1; // La visibilidad o no la controla cada capa
855
        // dentro de una colecci?n
856
    }
857
    /*
858
     * (non-Javadoc)
859
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMaxScale()
860
     */
861

    
862
    public double getMaxScale() {
863
        return -1;
864
    }
865
    /*
866
     * (non-Javadoc)
867
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMinScale(double)
868
     */
869

    
870
    public void setMinScale(double minScale) {
871
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
872
            FLayer lyr = (FLayer) iter.next();
873
            lyr.setMinScale(minScale);
874
        }
875
    }
876
    /*
877
     * (non-Javadoc)
878
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMaxScale(double)
879
     */
880

    
881
    public void setMaxScale(double maxScale) {
882
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
883
            FLayer lyr = (FLayer) iter.next();
884
            lyr.setMinScale(maxScale);
885
        }
886
    }
887
    /*
888
     * (non-Javadoc)
889
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setActive(boolean)
890
     */
891

    
892
    public void setActive(boolean b) {
893
        super.setActive(b);
894
        for (int i = 0; i < layers.size(); i++) {
895
            ((FLayer) layers.get(i)).setActive(b);
896
        }
897
    }
898
    /* (non-Javadoc)
899
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
900
     */
901

    
902
    public boolean addLayerListener(LayerListener o) {
903
        for (int i = 0; i < layers.size(); i++) {
904
            ((FLayer) layers.get(i)).addLayerListener(o);
905
        }
906
        return true;
907
    }
908

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

    
914
    public DynObjectSet getInfo(Point p, double tolerance, Cancellable cancel,
915
            boolean fast) throws LoadLayerException, DataException {
916
        return getInfo(this.getMapContext().getViewPort().convertToMapPoint(p), tolerance);
917
    }
918

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

    
936
    /*
937
     * (non-Javadoc)
938
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
939
     */
940
    public String getTocImageIcon() {
941
        return "layer-icon-group";
942
    }
943

    
944
    /**
945
     * <p>
946
     * Adds the layer with the information in an XML entity and the specified
947
     * name, to this collection of layers. And returns <code>true</code> if
948
     * there hasn't been errors.</p>
949
     *
950
     * @see CopyOfFLayers#addLayerFromXML(XMLEntity, String)
951
     *
952
     * @param xml tree-node structure with information about layers
953
     * @param name name of the layer to add
954
     * @return <code>true</code> if there were no errors adding the layer,
955
     * <code>false</code> otherwise
956
     *
957
     * @throws LoadLayerException if fails loading this layer.
958
     */
959
    //        public boolean addLayerFromXMLEntity(XMLEntity xml, String name) throws LoadLayerException {
960
    //                fmap.clearErrors();
961
    //                this.addLayerFromXML(xml,name);
962
    //
963
    //                return (fmap.getLayersError().size() == 0);
964
    //
965
    //        }
966
//        /**
967
//         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
968
//         *
969
//         * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
970
//         *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
971
//         *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
972
//         *
973
//         * @param xml tree-node structure with information about layers
974
//         * @param name name of the layer to add
975
//         *
976
//         * @throws LoadLayerException if fails loading this layer.
977
//         */
978
//        private void addLayerFromXML(XMLEntity xml, String name) throws LoadLayerException {
979
//                FLayer layer = null;
980
//
981
//                try {
982
//                        if (name == null) {
983
//                                name = xml.getName();
984
//                        }
985
//
986
//
987
//                        String className = xml.getStringProperty("className");
988
//                        Class clase = Class.forName(className);
989
//                        layer = (FLayer) clase.newInstance();
990
//                        if (FLayers.class.isAssignableFrom(clase)) {
991
//                                ((FLayers)layer).setMapContext(getMapContext());
992
//                                ((FLayers)layer).setParentLayer(this);
993
//                                //                        layer = new FLayers(getMapContext(),this);
994
//                                layer.setXMLEntity(xml);
995
//                        } else {
996
//                                // Capas Nuevas (externas)
997
//                                layer.setName(name);
998
//                                layer.setXMLEntity(xml);
999
//                                layer.load();
1000
//                        }
1001
//
1002
//                        //                //TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el c?digo de aq? y de los diferentes m?todos de LayerFactory,
1003
//                        //                //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1004
//                        //                if (className.equals(FLyrVect.class.getName())){// || className.equals(FLyrAnnotation.class.getName())) {
1005
//                        //                        String type = xml.getStringProperty("type");
1006
//                        //                        if ("vectorial".equals(type)){
1007
//                        //                                //String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
1008
//                        //                                IProjection proj = null;
1009
//                        //                                if (xml.contains("proj")) {
1010
//                        //                                        proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
1011
//                        //                                }
1012
//                        //                                else
1013
//                        //                                {
1014
//                        //                                        proj = this.getMapContext().getViewPort().getProjection();
1015
//                        //                                }
1016
//                        //                                if (xml.contains("file")) {
1017
//                        //                                        Driver d;
1018
//                        //                                        try {
1019
//                        //                                                d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
1020
//                        //                                        } catch (DriverLoadException e1) {
1021
//                        //                                                throw new DriverLayerException(name,e1);
1022
//                        //                                        }
1023
//                        //                                        layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
1024
//                        //                                                        new File(xml.getStringProperty("file")),
1025
//                        //                                                        proj);
1026
//                        //
1027
//                        //
1028
//                        //                                }
1029
//                        //                                if (xml.contains("db")) {
1030
//                        //
1031
//                        //                                        String driverName = xml.getStringProperty("db");
1032
//                        //                                        IVectorialDatabaseDriver driver;
1033
//                        //                                        try {
1034
//                        //                                                driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
1035
//                        //                                                //Hay que separar la carga de los datos del XMLEntity del load.
1036
//                        //                                                driver.setXMLEntity(xml.getChild(2));
1037
//                        //
1038
//                        //                                                boolean loadOk = false;
1039
//                        //                                                ((DefaultJDBCDriver)driver).load();
1040
//                        //                                                if (((DefaultJDBCDriver)driver).getConnection() != null) {
1041
//                        //                                                        loadOk = true;
1042
//                        //                                                }
1043
//                        //                                                layer = LayerFactory.createDBLayer(driver, name, proj);
1044
//                        //                                                if (!loadOk) {
1045
//                        //                                                        layer.setAvailable(false);
1046
//                        //                                                }
1047
//                        //
1048
//                        //                                        } catch (DriverLoadException e) {
1049
//                        //                                                throw new DriverLayerException(name,e);
1050
//                        //                                        } catch (XMLException e) {
1051
//                        //                                                throw new DriverLayerException(name,e);
1052
//                        //                                        } catch (ReadException e) {
1053
//                        //                                                throw new DriverLayerException(name,e);
1054
//                        //                                        }
1055
//                        //
1056
//                        //                                }
1057
//                        //                                // Clases con algun driver gen?rico creado por otro
1058
//                        //                                // programador
1059
//                        //                                if (xml.contains("other")) {
1060
//                        //
1061
//                        //                                        String driverName = xml.getStringProperty("other");
1062
//                        //                                        VectorialDriver driver = null;
1063
//                        //                                        try {
1064
//                        //                                                driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
1065
//                        //                                        } catch (DriverLoadException e) {
1066
//                        //                                                // Si no existe ese driver, no pasa nada.
1067
//                        //                                                // Puede que el desarrollador no quiera que
1068
//                        //                                                // aparezca en el cuadro de di?logo y ha metido
1069
//                        //                                                // el jar con sus clases en nuestro directorio lib.
1070
//                        //                                                // Intentamos cargar esa clase "a pelo".
1071
//                        //                                                if (xml.getChild(2).contains("className"))
1072
//                        //                                                {
1073
//                        //                                                        String className2 = xml.getChild(2).getStringProperty("className");
1074
//                        //                                                        try {
1075
//                        //                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1076
//                        //                                                        } catch (Exception e1) {
1077
//                        //                                                                throw new DriverLayerException(name,e);
1078
//                        //                                                        }
1079
//                        //                                                }
1080
//                        //                                        } catch (NullPointerException npe) {
1081
//                        //                                                // Si no existe ese driver, no pasa nada.
1082
//                        //                                                // Puede que el desarrollador no quiera que
1083
//                        //                                                // aparezca en el cuadro de di?logo y ha metido
1084
//                        //                                                // el jar con sus clases en nuestro directorio lib.
1085
//                        //                                                // Intentamos cargar esa clase "a pelo".
1086
//                        //                                                if (xml.getChild(2).contains("className"))
1087
//                        //                                                {
1088
//                        //                                                        String className2 = xml.getChild(2).getStringProperty("className");
1089
//                        //                                                        try {
1090
//                        //                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1091
//                        //                                                        } catch (Exception e1) {
1092
//                        //                                                                throw new DriverLayerException(name,e1);
1093
//                        //                                                        }
1094
//                        //                                                }
1095
//                        //                                        }
1096
//                        //                                        if (driver instanceof IPersistence)
1097
//                        //                                        {
1098
//                        //                                                IPersistence persist = (IPersistence) driver;
1099
//                        //                                                persist.setXMLEntity(xml.getChild(2));
1100
//                        //                                        }
1101
//                        //                                        layer = LayerFactory.createLayer(name, driver, proj);
1102
//                        //                                }
1103
//                        //
1104
//                        //                        }
1105
//                        //
1106
//                        //                        //TODO VCN FLyrAnnotation es un parche para no tener que duplicar todo el c?digo de aq? y de los diferentes m?todos de LayerFactory,
1107
//                        //                        //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1108
//                        //                        if (className.equals(FLyrAnnotation.class.getName())){
1109
//                        //                                layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
1110
//                        //                        }
1111
//                        //
1112
//                        //
1113
//                        //                        layer.setXMLEntity(xml);
1114
//                        //
1115
//                        //                } else {
1116
//                        //                        Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1117
//                        //                        layer = (FLayer) clase.newInstance();
1118
//                        //                        if (clase.isAssignableFrom(FLayers.class)) {
1119
//                        //                                ((FLayers)layer).setMapContext(getMapContext());
1120
//                        //                                ((FLayers)layer).setParentLayer(this);
1121
//                        ////                                layer = new FLayers(getMapContext(),this);
1122
//                        //                                layer.setXMLEntity(xml);
1123
//                        //                        } else {
1124
//                        //                                // Capas Nuevas (externas)
1125
//                        //                                layer.setName(name);
1126
//                        //                                layer.setXMLEntity(xml);
1127
//                        //                                layer.load();
1128
//                        //                        }
1129
//                        //                }
1130
//                        this.addLayer(layer);
1131
//                        logger.debug("layer: "+ layer.getName() +" loaded");
1132
//                        // Comprobar que la proyecci?n es la misma que la de FMap
1133
//                        // Si no lo es, es una capa que est? reproyectada al vuelo
1134
//                        IProjection proj = layer.getProjection();
1135
//                        if ((proj != null)) {
1136
//                                if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode()))
1137
//                                {
1138
//                                        ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1139
//                                        // TODO: REVISAR CON LUIS
1140
//                                        // Se lo fijamos a todas, luego cada una que se reproyecte
1141
//                                        // si puede, o que no haga nada
1142
//
1143
//                                        layer.setCoordTrans(ct);
1144
//                                }
1145
//                        }
1146
//                } catch (XMLException e) {
1147
//                        fmap.addLayerError(xml.getStringProperty("name"));
1148
//                        throw new LoadLayerException(name,e);
1149
//                } catch (ClassNotFoundException e) {
1150
//                        fmap.addLayerError(xml.getStringProperty("name"));
1151
//                        throw new LoadLayerException(name,e);
1152
//                } catch (InstantiationException e) {
1153
//                        fmap.addLayerError(xml.getStringProperty("name"));
1154
//                        throw new LoadLayerException(name,e);
1155
//                } catch (IllegalAccessException e) {
1156
//                        fmap.addLayerError(xml.getStringProperty("name"));
1157
//                        throw new LoadLayerException(name,e);
1158
//                } catch (LoadLayerException e){
1159
//                        fmap.addLayerError(xml.getStringProperty("name"));
1160
//                        throw e;
1161
//                }
1162
//        }
1163
    /**
1164
     * <p>
1165
     * Sets the <code>MapContext</code> that contains this layer node.</p>
1166
     *
1167
     * @param mapContext the <code>MapContext</code> that contains this layer
1168
     * node
1169
     */
1170
    public void setMapContext(MapContext mapContext) {
1171
        this.fmap = mapContext;
1172
    }
1173

    
1174
//        /**
1175
//         * <p>Creates a new layer of the same class as the property <i>className</i> of the XML, after, adds the XML entity to that layer
1176
//         *  and loads the layer. Then, adds the layer to this collection of layers, and if there is a projection defined,
1177
//         *  inserts the transformation coordinates to the layer.</p>
1178
//         *
1179
//         * <p>If the new layer is an instance of <code>FLyrVect</code>, and has a label field, creates a label layer on the layer.</p>
1180
//         *
1181
//         * @param xml tree-node structure with information about layers
1182
//         * @param name name of the layer to add
1183
//         */
1184
//        private void addLayerFromXMLNew(XMLEntity xml, String name) {
1185
//                //                FLayer layer = null;
1186
//                //
1187
//                //
1188
//                //                try {
1189
//                //                        String className = xml.getStringProperty("className");
1190
//                //                        Class clazz = Class.forName(className);
1191
//                //                        if (clazz.isAssignableFrom(FLayers.class)) {
1192
//                //                                layer = (FLayer) clazz.newInstance();
1193
//                //                                ((FLayers)layer).setMapContext(getMapContext());
1194
//                //                                ((FLayers)layer).setParentLayer(this);
1195
//                //        //                if (className.equals((FLayers.class.getName()))){
1196
//                //        //                        layer = new FLayers(getMapContext(),this);
1197
//                //                        } else {
1198
//                //        //                        Por compatibilidad
1199
//                //                                if (className.equals(FLyrVect.class.getName())) {
1200
//                //                                        if (xml.contains("file")) {
1201
//                //                                                layer = new FLayerFileVectorial();
1202
//                //                                        } else if (xml.contains("db")) {
1203
//                //                                                try {
1204
//                //                                                        layer = (FLayer)((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create("com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial");
1205
//                //                                                } catch (Exception e) {
1206
//                //                                                        throw new XMLException(new Exception("No se tiene registrada la capa de tipo JDBC"));
1207
//                //                                                }
1208
//                //                                                //className = FLayerJDBCVectorial.class.getName();
1209
//                //                                        } else if (xml.contains("other")){
1210
//                //                                                layer = new FLayerGenericVectorial();
1211
//                //                                        } else {
1212
//                //                                                throw new XMLException(new Exception("Capa vectorial de tipo no reconocido"));
1213
//                //                                        }
1214
//                //        //                                Fin por compatibilidad
1215
//                //                                } else {
1216
//                //                                        try {
1217
//                //                                                layer = (FLayer)(((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create(className));
1218
//                //                                        } catch (Exception e) {
1219
//                //                                                //puende que no este registrada como punto de extension
1220
//                //                                                Class clase = Class.forName(className);
1221
//                //                                                layer = (FLayer) clase.newInstance();
1222
//                //                                                // FIXME: Hacemos algo aqui o dejamos que suba el error?
1223
//                //                                        }
1224
//                //                                }
1225
//                //
1226
//                //                        }
1227
//                //                        layer.setXMLEntity(xml);
1228
//                //                        if (name != null) layer.setName(name);
1229
//                //                        layer.load();
1230
//                //
1231
//                //                        this.addLayer(layer);
1232
//                //                        logger.debug("layer: "+ layer.getName() +" loaded");
1233
//                //                        // Comprobar que la proyecci?n es la misma que la de FMap
1234
//                //                        // Si no lo es, es una capa que est? reproyectada al vuelo
1235
//                //                        IProjection proj = layer.getProjection();
1236
//                //                        if ((proj != null))
1237
//                //                                if (proj != getMapContext().getProjection())
1238
//                //                                {
1239
//                //                                        ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1240
//                //                                        // TODO: REVISAR CON LUIS
1241
//                //                                        // Se lo fijamos a todas, luego cada una que se reproyecte
1242
//                //                                        // si puede, o que no haga nada
1243
//                //                                        layer.setCoordTrans(ct);
1244
//                //
1245
//                //                                }
1246
//                //                }catch (Exception e) {
1247
//                //                        fmap.addLayerError(xml.getStringProperty("name"));
1248
//                //                        logger.debug(Messages.getString("could_not_load_layer")+": "+xml.getStringProperty("name") + ".\n"
1249
//                //                                        +Messages.getString("reason")+":", e);
1250
//                //                }
1251
//        }
1252
    public void accept(Visitor visitor) throws BaseException {
1253
        for (int i = 0; i < this.getLayersCount(); i++) {
1254
            FLayer layer = this.getLayer(i);
1255
            try {
1256
                if (layer instanceof LayersVisitable) {
1257
                    ((LayersVisitable) layer).accept(visitor);
1258
                } else {
1259
                    visitor.visit(layer);
1260
                }
1261
            } catch (VisitCanceledException ex) {
1262
                break;
1263
            }
1264
        }
1265
    }
1266

    
1267
    public void accept(LayersVisitor visitor) throws BaseException {
1268
        for (int i = 0; i < this.getLayersCount(); i++) {
1269
            FLayer layer = this.getLayer(i);
1270
            if (layer instanceof LayersVisitable) {
1271
                ((LayersVisitable) layer).accept(visitor);
1272
            } else {
1273
                visitor.visit(layer);
1274
            }
1275
        }
1276
    }
1277

    
1278
    /*
1279
     * (non-Javadoc)
1280
     *
1281
     * @see org.gvsig.metadata.Metadata#getMetadataID()
1282
     */
1283
    public Object getMetadataID() throws MetadataException {
1284
        StringBuffer strb = new StringBuffer();
1285
        strb.append("Layers(");
1286
        strb.append(this.getName());
1287
        strb.append("):{");
1288
        Iterator iter = this.layers.iterator();
1289
        while (iter.hasNext()) {
1290
            strb.append(((FLayer) iter.next()).getMetadataID());
1291
            strb.append(",");
1292
        }
1293
        strb.append("}");
1294
        return strb.toString();
1295

    
1296
    }
1297

    
1298
    /*
1299
     * (non-Javadoc)
1300
     *
1301
     * @see org.gvsig.metadata.Metadata#getMetadataChildren()
1302
     */
1303
    public Set getMetadataChildren() {
1304
        Set ret = new TreeSet();
1305
        Iterator iter = this.layers.iterator();
1306
        while (iter.hasNext()) {
1307
            ret.add(iter.next());
1308
        }
1309
        return ret;
1310
    }
1311

    
1312
    /*
1313
     * (non-Javadoc)
1314
     *
1315
     * @see org.gvsig.metadata.Metadata#getMetadataName()
1316
     */
1317
    public String getMetadataName() throws MetadataException {
1318
        StringBuffer strb = new StringBuffer();
1319
        strb.append("Layer Group '");
1320
        strb.append(this.getName());
1321
        strb.append("': {");
1322
        Iterator iter = this.layers.iterator();
1323
        while (iter.hasNext()) {
1324
            strb.append(((FLayer) iter.next()).getMetadataName());
1325
            strb.append(",");
1326
        }
1327
        strb.append("}");
1328
        return strb.toString();
1329
    }
1330

    
1331
    public void beginDraw(Graphics2D g, ViewPort viewPort) {
1332
        LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
1333
        fmap.fireLayerDrawingEvent(beforeEvent);
1334
    }
1335

    
1336
    public void endDraw(Graphics2D g, ViewPort viewPort) {
1337
        LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
1338
        fmap.fireLayerDrawingEvent(afterEvent);
1339
    }
1340

    
1341
    public static class RegisterPersistence implements Callable {
1342

    
1343
        public Object call() {
1344

    
1345
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1346
            DynStruct definition = manager.addDefinition(
1347
                    FLayers.class,
1348
                    "FLayers",
1349
                    "FLayers Persistence definition",
1350
                    null,
1351
                    null
1352
            );
1353
            definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, "FLyrDefault");
1354

    
1355
            definition.addDynFieldObject("mapContext").setClassOfValue(MapContext.class).setMandatory(true);
1356
            definition.addDynFieldList("layers").setClassOfItems(FLayer.class).setMandatory(true);
1357

    
1358
            return Boolean.TRUE;
1359
        }
1360
    }
1361

    
1362
    protected void doDispose() throws BaseException {
1363
        if (layers != null) {
1364
            for (int i = 0; i < layers.size(); i++) {
1365
                dispose((Disposable) layers.get(i));
1366
            }
1367
        }
1368
    }
1369

    
1370
    public void move(FLayer layer, LayerCollection group, int where, FLayer adjoiningLayer) throws LayerNotFoundInCollectionException {
1371

    
1372
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(layer));
1373
        group.addLayer(layer, where, adjoiningLayer);
1374
        removeLayer(layer);
1375
        this.updateDrawVersion();
1376
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(layer));
1377

    
1378
    }
1379

    
1380
    public void join(FLayer layer, LayerCollection group) {
1381
        try {
1382
            layers.remove(layer);
1383
            group.addLayer(layer, END, null);
1384
            this.updateDrawVersion();
1385
        } catch (LayerNotFoundInCollectionException e) {
1386
            throw new MapContextRuntimeException(e);
1387
        }
1388
    }
1389

    
1390
    public void move(FLayer layer, LayerCollection group) {
1391
        try {
1392
            move(layer, group, END, null);
1393
        } catch (LayerNotFoundInCollectionException e) {
1394
            throw new MapContextRuntimeException(e);
1395
        }
1396
    }
1397

    
1398
    public void addLayer(FLayer layer, int where, FLayer adjoiningLayer)
1399
            throws LayerNotFoundInCollectionException {
1400

    
1401
        switch (where) {
1402
            case BEGIN:
1403
                addLayer(0, layer);
1404
                break;
1405
            case BEFORE:
1406
                if (adjoiningLayer != null) {
1407
                    if (this.layers.contains(adjoiningLayer)) {
1408
                        for (int i = 0; i < this.getLayersCount(); i++) {
1409
                            if (adjoiningLayer == this.getLayer(i)) {
1410
                                addLayer(i, layer);
1411
                                break;
1412
                            }
1413
                        }
1414
                    } else {
1415
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1416
                    }
1417
                } else {
1418
                    addLayer(0, layer);
1419
                }
1420
                break;
1421
            case AFTER:
1422
                if (adjoiningLayer != null) {
1423
                    if (this.layers.contains(adjoiningLayer)) {
1424
                        for (int i = 0; i < this.getLayersCount(); i++) {
1425
                            if (adjoiningLayer == this.getLayer(i)) {
1426
                                addLayer(i + 1, layer);
1427
                                break;
1428
                            }
1429
                        }
1430
                    } else {
1431
                        throw new LayerNotFoundInCollectionException(adjoiningLayer, this);
1432
                    }
1433
                } else {
1434
                    this.addLayer(layer);
1435
                }
1436
                break;
1437
            default: // By default add layer an the end of the collection
1438
                this.addLayer(layer);
1439
                break;
1440
        }
1441

    
1442
    }
1443

    
1444
    public FLayer getFirstActiveLayer() {
1445
        LayersIterator it = new LayersIterator(this) {
1446
            public boolean evaluate(FLayer layer) {
1447
                return layer.isActive();
1448
            }
1449
        };
1450
        if( it.hasNext() ) {
1451
            return (FLayer) it.next();
1452
        }
1453
        return null;
1454
    }
1455

    
1456
    public FLyrVect getFirstActiveVectorLayer() {
1457
        LayersIterator it = new LayersIterator(this) {
1458
            public boolean evaluate(FLayer layer) {
1459
                return layer.isActive() && layer instanceof FLyrVect;
1460
            }
1461
        };
1462
        if( it.hasNext() ) {
1463
            return (FLyrVect) it.next();
1464
        }
1465
        return null;
1466
    }
1467
    
1468
    public Iterator iterator() {
1469
        return this.layers.iterator();
1470
    }
1471

    
1472
    public Iterator deepiterator() {
1473
        List layers = toPlainList(this);
1474
        return layers.iterator();
1475
    }
1476
    
1477
    public int size() {
1478
        return this.layers.size();
1479
    }
1480
    
1481
    public FLayer get(int index) {
1482
        return this.layers.get(index);
1483
    }
1484
    
1485
    public boolean isEmpty() {
1486
        return this.layers.isEmpty();
1487
    }
1488

    
1489
    public boolean contains(Object o) {
1490
        return this.layers.contains(o);
1491
    }
1492

    
1493
    public Object[] toArray() {
1494
        return this.layers.toArray();
1495
    }
1496

    
1497
    public Object[] toArray(Object[] ts) {
1498
        return this.layers.toArray(ts);
1499
    }
1500

    
1501
    public boolean add(FLayer e) {
1502
        this.addLayer(e);
1503
        return true;
1504
    }
1505

    
1506
    public boolean remove(Object o) {
1507
        this.removeLayer((FLayer) o);
1508
        return true;
1509
    }
1510

    
1511
    public boolean containsAll(Collection clctn) {
1512
        return this.layers.containsAll(clctn);
1513
    }
1514

    
1515
    public void add(int i, FLayer e) {
1516
        this.addLayer(i, (FLayer) e);
1517
    }
1518

    
1519
    public FLayer remove(int i) {
1520
        FLayer o = this.getLayer(i);
1521
        this.removeLayer(i);
1522
        return o;
1523
    }
1524

    
1525
    public int indexOf(Object o) {
1526
        return this.layers.indexOf(o);
1527
    }
1528

    
1529
    public int lastIndexOf(Object o) {
1530
        return this.layers.lastIndexOf(o);
1531
    }
1532

    
1533
    public ListIterator listIterator() {
1534
        return this.layers.listIterator();
1535
    }
1536

    
1537
    public ListIterator listIterator(int i) {
1538
        return this.layers.listIterator(i);
1539
    }
1540

    
1541
    public List subList(int i, int i1) {
1542
        return this.layers.subList(i, i1);
1543
    }
1544
    
1545
    public boolean addAll(Collection clctn) {
1546
        Iterator it = clctn.iterator();
1547
        while( it.hasNext() ) {
1548
            this.add((FLayer) it.next());
1549
        }
1550
        return true;
1551
    }
1552

    
1553
    public boolean addAll(int i, Collection clctn) {
1554
        Iterator it = clctn.iterator();
1555
        while( it.hasNext() ) {
1556
            this.add(i, (FLayer) it.next());
1557
        }
1558
        return true;
1559
    }
1560

    
1561
    public boolean removeAll(Collection clctn) {
1562
        Iterator it = clctn.iterator();
1563
        while( it.hasNext() ) {
1564
            this.remove((FLayer) it.next());
1565
        }
1566
        return true;
1567
    }
1568

    
1569
    public boolean retainAll(Collection clctn) {
1570
        Iterator it = this.layers.iterator();
1571
        while( it.hasNext() ) {
1572
            Object o = it.next();
1573
            if( !clctn.contains(o) ) {
1574
                this.remove((FLayer) o);
1575
            }
1576
        }
1577
        return true;
1578
    }
1579

    
1580
    public FLayer set(int i, FLayer e) {
1581
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1582
    }
1583

    
1584
}