Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / FLayers.java @ 23750

History | View | Annotate | Download (50.7 KB)

1 21200 vcaballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.mapcontext.layers;
42
43
import java.awt.Graphics2D;
44
import java.awt.Point;
45
import java.awt.image.BufferedImage;
46
import java.awt.image.WritableRaster;
47
import java.util.ArrayList;
48
import java.util.Collections;
49
import java.util.Iterator;
50
import java.util.List;
51
import java.util.Vector;
52
53
import javax.print.attribute.PrintRequestAttributeSet;
54
import javax.print.attribute.standard.PrintQuality;
55
import javax.swing.ImageIcon;
56
57
import org.apache.log4j.Logger;
58
import org.cresques.cts.ICoordTrans;
59 23183 vcaballero
import org.cresques.cts.IProjection;
60 23750 jjdelcerro
import org.gvsig.fmap.data.exceptions.ReadException;
61 21426 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
62 21200 vcaballero
import org.gvsig.fmap.mapcontext.MapContext;
63
import org.gvsig.fmap.mapcontext.Messages;
64
import org.gvsig.fmap.mapcontext.ViewPort;
65
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
66 23183 vcaballero
import org.gvsig.fmap.mapcontext.exceptions.XMLLayerException;
67 21200 vcaballero
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
68
import org.gvsig.fmap.mapcontext.layers.operations.ILabelable;
69
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
70
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
71 22252 jmvivo
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitable;
72 23183 vcaballero
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
73 21200 vcaballero
import org.gvsig.fmap.mapcontext.layers.operations.XMLItem;
74 23303 jmvivo
import org.gvsig.tools.exception.BaseException;
75 23066 jmvivo
import org.gvsig.tools.visitor.NotSupportedOperationException;
76
import org.gvsig.tools.visitor.Visitor;
77 21200 vcaballero
78
import com.iver.utiles.XMLEntity;
79 23183 vcaballero
import com.iver.utiles.XMLException;
80 21200 vcaballero
import com.iver.utiles.swing.threads.Cancellable;
81
82
/**
83
 * <p>Represents a generic collection of layers, that can be represented as a node in a tree of nodes of layers.</p>
84
 *
85
 * <p>Adapts the basic functionality implemented for a layer in the abstract class <code>FLyrDefault</code>, to
86
 *  a collection of layers, implementing, as well, specific methods for this kind of object, defined in the
87
 *  interfaces <code>VectorialData</code>, <code>LayerCollection</code>, and <code>InfoByPoint</code>.</p>
88
 *
89
 * @see FLyrDefault
90
 */
91 22252 jmvivo
public class FLayers extends FLyrDefault implements LayerCollection,
92
                InfoByPoint {
93 21200 vcaballero
        /**
94
         * List with all listeners registered for this kind of node.
95
         *
96
         * @see #addLayerCollectionListener(LayerCollectionListener)
97
         * @see #removeLayerCollectionListener(LayerCollectionListener)
98
         * @see #callLayerAdded(LayerCollectionEvent)
99
         * @see #callLayerAdding(LayerCollectionEvent)
100
         * @see #callLayerMoved(LayerPositionEvent)
101
         * @see #callLayerMoving(LayerPositionEvent)
102
         * @see #callLayerRemoved(LayerCollectionEvent)
103
         * @see #callLayerRemoving(LayerCollectionEvent)
104
         */
105
        protected ArrayList layerCollectionListeners = new ArrayList();
106
107
        /**
108
         * A synchronized list with the layers.
109
         *
110
         * @see #setAllVisibles(boolean)
111
         * @see #addLayer(FLayer)
112
         * @see #addLayer(int, FLayer)
113
         * @see #moveTo(int, int)
114
         * @see #removeLayer(FLayer)
115
         * @see #removeLayer(int)
116
         * @see #removeLayer(String)
117
         * @see #replaceLayer(String, FLayer)
118
         * @see #getVisibles()
119
         * @see #getLayer(int)
120
         * @see #getLayer(String)
121
         * @see #getLayersCount()
122 21426 vcaballero
         * @see #getFullEnvelope()
123 21200 vcaballero
         */
124
        protected List layers = Collections.synchronizedList(new ArrayList());
125
126
        /**
127
         * The model of the layer.
128
         *
129
         * @see #getMapContext()
130
         */
131
        protected MapContext fmap;
132
133
        /**
134
         * Useful for debug the problems during the implementation.
135
         */
136
        protected static Logger logger = Logger.getLogger(FLayers.class.getName());
137
138
        /*
139
         * (non-Javadoc)
140
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
141
         */
142
        public void addLayerCollectionListener(LayerCollectionListener listener) {
143 22252 jmvivo
                if (!layerCollectionListeners.contains(listener)) {
144 21200 vcaballero
                        layerCollectionListeners.add(listener);
145 22252 jmvivo
                }
146 21200 vcaballero
        }
147
148
        /*
149
         * (non-Javadoc)
150
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
151
         */
152
        public void setAllVisibles(boolean visible) {
153
                FLayer lyr;
154
155
                for (int i = 0; i < layers.size(); i++) {
156
                        lyr = ((FLayer) layers.get(i));
157
                        lyr.setVisible(visible);
158
159
                        if (lyr instanceof LayerCollection) {
160
                                ((LayerCollection) lyr).setAllVisibles(visible);
161
                        }
162
                }
163
        }
164
165
        /*
166
         * (non-Javadoc)
167
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
168
         */
169
        public void removeLayerCollectionListener(LayerCollectionListener listener) {
170
                layerCollectionListeners.remove(listener);
171
        }
172
173
        /**
174
         * Adds a layer on an specified position in this node.
175
         *
176
         * @param pos position in the inner list where the layer will be added
177
         * @param layer a layer
178
         */
179
        private void doAddLayer(int pos,FLayer layer) {
180
                layers.add(pos,layer);
181
                layer.setParentLayer(this);
182 22252 jmvivo
                if(layer.getProjection() != null && fmap != null) {
183 21200 vcaballero
                        layer.setCoordTrans(layer.getProjection().getCT(fmap.getProjection()));
184 22252 jmvivo
                }
185 21200 vcaballero
        }
186
187
        /*
188
         * (non-Javadoc)
189
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
190
         */
191
        public void addLayer(FLayer layer) {
192
                addLayer(layers.size(),layer);
193
        }
194
195
        /**
196
         * Adds a layer in an specified position in this node.
197
         *
198
         * @param layer a layer
199
         */
200
        public void addLayer(int pos,FLayer layer) {
201
                try {
202
                        //Notificamos a la capa que va a ser a?adida
203 22252 jmvivo
                        if (layer instanceof FLyrDefault) {
204 21200 vcaballero
                                ((FLyrDefault)layer).wakeUp();
205 22252 jmvivo
                        }
206 21200 vcaballero
207
                        if (layer instanceof FLayers){
208
                                FLayers layers=(FLayers)layer;
209
                                fmap.addAsCollectionListener(layers);
210
                        }
211
                        callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
212
213
                        doAddLayer(pos,layer);
214
215
                        callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
216
                } catch (CancelationException e) {
217
                        logger.warn(e);
218
                } catch (LoadLayerException e) {
219
                        layer.setAvailable(false);
220
                        layer.addError(e);
221
                }
222
        }
223
224
        /*
225
         * (non-Javadoc)
226
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
227
         */
228
        public void moveTo(int from, int to) throws CancelationException {
229
                int newfrom=layers.size()-from-1;
230
                int newto=layers.size()-to-1;
231 22252 jmvivo
                if ( newfrom < 0 || newfrom >=layers.size() || newto < 0 || newto >= layers.size()) {
232
                        return;
233
                }
234 21200 vcaballero
                FLayer aux = (FLayer) layers.get(newfrom);
235
                callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
236
                layers.remove(newfrom);
237
                layers.add(newto, aux);
238
                callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
239
        }
240
241
        /**
242
         * Removes an inner layer.
243
         *
244
         * @param lyr a layer
245
         */
246
        private void doRemoveLayer(FLayer lyr) {
247
                layers.remove(lyr);
248
        }
249
250
        /*
251
         * (non-Javadoc)
252
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
253
         */
254
        public void removeLayer(FLayer lyr) throws CancelationException {
255
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
256
                doRemoveLayer(lyr);
257
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
258
        }
259
260
        /*
261
         * (non-Javadoc)
262
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
263
         */
264
        public void removeLayer(int idLayer) {
265
                FLayer lyr = (FLayer) layers.get(idLayer);
266
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
267
                layers.remove(idLayer);
268
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
269
        }
270
271
        /*
272
         * (non-Javadoc)
273
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
274
         */
275
        public void removeLayer(String layerName) {
276
                FLayer lyr;
277
278
                for (int i = 0; i < layers.size(); i++) {
279
                        lyr = ((FLayer) layers.get(i));
280
281
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
282
                                removeLayer(i);
283
284
                                break;
285
                        }
286
                }
287
        }
288
289
        /**
290
         * Replace a layer identified by its name, by another.
291
         *
292
         * @param layerName the name of the layer to be replaced
293
         * @param layer the new layer
294
         */
295
        public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException
296
        {
297
                FLayer lyr;
298
                FLayer parent;
299
                for (int i = 0; i < layers.size(); i++) {
300
                        lyr = ((FLayer) layers.get(i));
301
302
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
303
                                parent = lyr.getParentLayer();
304
                                removeLayer(i);
305 22252 jmvivo
                                if (parent != null) {
306 21200 vcaballero
                                        //Notificamos a la capa que va a ser a?adida
307 22252 jmvivo
                                        if (layer instanceof FLyrDefault) {
308 21200 vcaballero
                                                ((FLyrDefault)layer).wakeUp();
309 22252 jmvivo
                                        }
310
                                }
311 21200 vcaballero
312
                                if (layer instanceof FLayers){
313
                                        FLayers layers=(FLayers)layer;
314
                                        fmap.addAsCollectionListener(layers);
315
                                }
316
                                callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
317
318
                                layers.add(i,layer);
319
                                layer.setParentLayer(this);
320
321
                                callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
322
                                break;
323
                        }
324
                }
325
        }
326
327
        /*
328
         * (non-Javadoc)
329
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
330
         */
331
        public FLayer[] getVisibles() {
332
                ArrayList array = new ArrayList();
333
                LayersIterator iter = new LayersIterator(this) {
334
                        public boolean evaluate(FLayer layer) {
335
                                return layer.isVisible();
336
                        }
337
338
                };
339
340
                while (iter.hasNext()) {
341
                        array.add(iter.nextLayer());
342
                }
343
344
                return (FLayer[]) array.toArray(new FLayer[0]);
345
        }
346
347
        /*
348
         * (non-Javadoc)
349
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
350
         */
351
        public FLayer getLayer(int index) {
352
                return (FLayer) layers.get(index);
353
        }
354
355
        /*
356
         * (non-Javadoc)
357
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
358
         */
359
        public FLayer getLayer(String layerName) {
360
                FLayer lyr;
361
                FLayer lyr2;
362
                ArrayList layerList;
363
364
                for (int i = 0; i < layers.size(); i++) {
365
                        lyr = ((FLayer) layers.get(i));
366
367
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
368
                                return lyr;
369
                        }
370
371
                        layerList = new ArrayList();
372
                        splitLayerGroup(lyr,layerList);
373
                        for(int j = 0; j<layerList.size(); j++ )
374
                        {
375
                                lyr2 = ((FLayer)layerList.get(j));
376
                                if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
377
                                        return lyr2;
378
                                }
379
                        }
380
                }
381
382
                return null;
383
        }
384
385
        /**
386
         * <p> Splits up a layer group in order to get a layer by name when there are layer groups</p>
387
         *
388
         * <p>In <code>result</code> always will be at least one layer.</p>
389
         *
390
         * @param layer the layer we are looking for
391
         * @param result an array list that will have the results of the search
392
         */
393
        private void splitLayerGroup(FLayer layer, ArrayList result)
394
        {
395
                int i;
396
                FLayers layerGroup;
397
                if (layer instanceof FLayers)
398
                {
399
                        layerGroup = (FLayers)layer;
400
                        for (i=0; i < layerGroup.getLayersCount(); i++ )
401
                        {
402
                                splitLayerGroup(layerGroup.getLayer(i),result);
403
                        }
404
                }
405
                else
406
                {
407
                        result.add(layer);
408
                }
409
        }
410
411
        /*
412
         * (non-Javadoc)
413
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
414
         */
415
        public int getLayersCount() {
416
                return layers.size();
417
        }
418
419
        /*
420
         * (non-Javadoc)
421
         * @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)
422
         */
423
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
424
                        Cancellable cancel,double scale) throws ReadException {
425
                draw(image,g,viewPort,cancel, scale, null);
426
        }
427
428
        /**
429
         * <p>Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this collection and draws which are visible, and dirty (in cache) or aren't in cache. If a node is
430
         *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
431
         *
432
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
433
         * group</code> is drawn, will be set to <code>null</code>.</p>
434
         *
435
         * <p>The particular behavior depends on the kind of each layer and composed layer. And this process can be cancelled at any
436
         *  time by the shared object <code>cancel</code>.</p>
437
         *
438
         * @param image buffer used sometimes instead <code>g</code> to accelerate the draw. For example, if two points are as closed that can't be distinguished, draws only one.
439
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
440
         * @param viewPort the information for drawing the layers
441
         * @param cancel shared object that determines if this layer can continue being drawn
442
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
443
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
444
         *  will also be drawn
445
         *
446
         * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
447
         *
448
         * @throws ReadDriverException if fails the driver reading the data.
449
         *
450
         * @see ComposedLayer#draw(BufferedImage, Graphics2D, ViewPort, Cancellable, double)
451
         */
452
        public ComposedLayer draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
453
                        Cancellable cancel,double scale, ComposedLayer group) throws ReadException {
454
                boolean bNeedRecalculateCache = false;
455
                double dpi = MapContext.getScreenDPI();
456
                ///// CHEMA ComposedLayer
457
                long tg1 = System.currentTimeMillis();
458
                ///// CHEMA ComposedLayer
459
460
461
                for (int i=0; i < layers.size(); i++) {
462 22252 jmvivo
                        if (cancel.isCanceled()) {
463 21200 vcaballero
                                break; // M?s que nada porque las capas raster no son interrumpibles por ahora.
464 22252 jmvivo
                        }
465 21200 vcaballero
                        FLayer lyr = (FLayer) layers.get(i);
466
467 22252 jmvivo
                        if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
468
                                continue;
469
                        }
470 21200 vcaballero
471
                        LayerDrawEvent beforeEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
472
                        fmap.fireLayerDrawingEvent(beforeEvent);
473 22252 jmvivo
                        if ((lyr.isDirty()) || (lyr.isCachingDrawnLayers() == false)) {
474 21200 vcaballero
                                bNeedRecalculateCache = true;
475 22252 jmvivo
                        }
476 21200 vcaballero
477
                        if (lyr.isVisible()) {
478
479
                                long t1 = System.currentTimeMillis();
480
                                // synchronized (this) {
481
                                //Sincronizaci?n del m?todo dibujar de cada Layer, esto es posible hacerlo de otra forma,
482
                                //pero de momento se queda as?, para solucionar el refresco de una misma vista que est? a?adida en el Layout.
483
                                try{
484
                                        /* if (lyr instanceof IComposedDrawing)
485
                                             {
486
                                             // Acumulamos las peticiones hasta que sea
487
                                              // otro servidor o otro tipo de capa o la ?ltima
488
                                               // Si hay que dibujar:
489
                                                lyr.draw(image, g, viewPort, cancel,scale);
490
                                                // Si hay que acumlar:
491
                                                 lyr.acumulateDrawing(antLayer);
492

493
                                                 }
494
                                                 else */
495
496
                                        if (lyr.isCachingDrawnLayers())
497
                                        {
498
                                                if ((bNeedRecalculateCache) || (lyr.getCacheImageDrawnLayers()==null))
499
                                                {
500
                                                        //if (bNeedRecalculateCache)
501
                                                        // {
502
503
                                                        ///// CHEMA ComposedLayer
504
                                                        if (group != null) {
505
                                                                //si tenemos un grupo pendiente de pintar, pintamos
506
                                                                // para que la cache sea fiable
507
                                                                group.draw(image, g, viewPort, cancel,scale);
508
                                                                group = null;
509
                                                        }
510
                                                        ///// CHEMA ComposedLayer
511
512
513
514
515
516
                                                        // Copiamos la imagen actual
517
                                                        BufferedImage buff = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
518
                                                        WritableRaster w = buff.getRaster();
519
                                                        image.copyData(w);
520
                                                        lyr.setCacheImageDrawnLayers(buff);
521
                                                }
522
//                                                }
523
//                                                else
524
//                                                {
525
                                                if (lyr.getCacheImageDrawnLayers() != null)
526
                                                {
527
                                                        // Previo a esto hemos tenido que fijar
528
                                                        // la imagen que queremos usar, en el
529
                                                        // prepareDrawing de FMap.
530
                                                        if (lyr.isDirty())
531
                                                        {
532
533
534
                                                                ///// CHEMA ComposedLayer
535
                                                                // Checks for draw group (ComposedLayer)
536
                                                                if (group != null) {
537
                                                                        // it's going to load a cache image,
538
                                                                        // the current draw isn't needed
539
                                                                        group = null;
540
                                                                }
541
                                                                ///// CHEMA ComposedLayer
542
543
                                                                g.drawImage(lyr.getCacheImageDrawnLayers(), 0, 0, null);
544
                                                        }
545
                                                }
546
//                                                }
547
                                        }
548
                                        // Si la capa est? "sucia" o alguna de las de abajo est? sucia
549
                                        // hay que volver a dibujar.
550
                                        if (lyr.isDirty() || bNeedRecalculateCache)
551
                                        {
552
                                                if (!lyr.getFLayerStatus().isDriverLoaded()){
553
                                                        continue;
554
                                        }
555
                                                ///// CHEMA ComposedLayer
556
                                                //lyr.draw(image, g, viewPort, cancel,scale);
557
                                                ///// CHEMA ComposedLayer
558
559
                                                // Checks for draw group (ComposedLayer)
560
                                                if (group != null) {
561
                                                        if (lyr instanceof FLayers){
562
                                                                group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
563
                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
564
                                                                fmap.fireLayerDrawingEvent(afterEvent);
565
                                                        } else {
566
                                                                //If layer can be added to the group, does it
567
                                                                if (group.canAdd(lyr)) {
568
                                                                        group.add(lyr);
569
                                                                } else {
570
                                                                        // draw the 'pending to draw' layer group
571
                                                                        group.draw(image, g, viewPort, cancel,scale);
572
573
                                                                        // gets a new group instance
574
                                                                        group = lyr.newComposedLayer();
575
                                                                        // if layer hasn't group, draws it inmediately
576
                                                                        if (group == null) {
577
                                                                                if (lyr instanceof FLayers){
578
                                                                                        group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
579
                                                                                } else {
580
                                                                                        lyr.draw(image, g, viewPort, cancel,scale);
581
                                                                                }
582
                                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
583
                                                                                fmap.fireLayerDrawingEvent(afterEvent);
584
                                                                        } else {
585
                                                                                // add the layer to the group
586
                                                                                group.setMapContext(fmap);
587
                                                                                group.add(lyr);
588
589
                                                                        }
590
                                                                }
591
                                                        }
592
                                                } else {
593
                                                        // gets a new group instance
594
                                                        group = lyr.newComposedLayer();
595
                                                        // if layer hasn't group, draws it inmediately
596
                                                        if (group == null) {
597
                                                                if (lyr instanceof FLayers){
598
                                                                        group = ((FLayers)lyr).draw(image, g, viewPort, cancel,scale,group);
599
                                                                } else {
600
                                                                        lyr.draw(image, g, viewPort, cancel,scale);
601
                                                                }
602
                                                                LayerDrawEvent afterEvent = new LayerDrawEvent(lyr, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
603
                                                                fmap.fireLayerDrawingEvent(afterEvent);
604
                                                        } else {
605
                                                                // adds the layer to the group
606
                                                                group.setMapContext(fmap);
607
                                                                group.add(lyr);
608
                                                        }
609
                                                }
610
                                                ///// CHEMA ComposedLayer
611
612
                                                /*
613
                                                 * (Jaume)
614
                                                 * If the layer is instance of ILabelable then it may have labels.
615
                                                 */
616
                                                if (lyr instanceof ILabelable
617
                                                        && ((ILabelable) lyr).isLabeled()
618
                                                        && ((ILabelable) lyr).getLabelingStrategy() != null
619
                                                        && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
620
621
                                                        ///// CHEMA ComposedLayer
622
                                                        if (group != null) {
623
                                                                //si tenemos un grupo pendiente de pintar, pintamos
624
                                                                // para que se pinten correctamente las etiquetas
625
                                                                group.draw(image, g, viewPort, cancel,scale);
626
                                                                group = null;
627
                                                        }
628
                                                        ///// CHEMA ComposedLayer
629
630
631
                                                        ((ILabelable) lyr).drawLabels(image, g, viewPort, cancel, scale, dpi);
632
                                                }
633
                                                bNeedRecalculateCache = true;
634
                                        }
635
                                } catch (ReadException e){
636
                                        // fmap.callNewErrorEvent(new ErrorEvent(lyr.getName(), e));
637
                                        fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
638
                                        lyr.addError(e);
639
                                        e.printStackTrace();
640
                                        lyr.setAvailable(false);
641
                                        // this.removeLayer(lyr);
642
                                } catch (Exception e) {
643
                                        fmap.addLayerError("La capa " + lyr.getName() + " es err?nea.");
644
                                        e.printStackTrace();
645
                                        lyr.setAvailable(false);
646
                                }
647
                                // }
648
                                long t2 = System.currentTimeMillis();
649
                                logger.info("Layer " + lyr.getName() + " "
650
                                                + (t2-t1) + " milisecs.");
651
                                lyr.setDirty(false);
652
                        }
653
654
                }
655
                ///// CHEMA ComposedLayer
656
                if (group != null && this.getParentLayer() == null) {
657
                        //si tenemos un grupo pendiente de pintar, pintamos
658
                        group.draw(image, g, viewPort, cancel,scale);
659
                        group = null;
660
661
                }
662
                ///// CHEMA ComposedLayer
663
                if (getVirtualLayers() != null) {
664
                        getVirtualLayers().draw(image, g, viewPort, cancel,scale);
665
                }
666
667
                long tg2 = System.currentTimeMillis();
668
669
                logger.debug("Draw all layer " +this.getName() + " " +
670
                                + (tg2-tg1) + " milisecs.");
671
                ///// CHEMA ComposedLayer
672
                return group;
673
                ///// CHEMA ComposedLayer
674
        }
675
676
        /*
677
         * (non-Javadoc)
678
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double, javax.print.attribute.PrintRequestAttributeSet)
679
         */
680
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties)
681
        throws ReadException {
682
                this.print(g, viewPort, cancel, scale, properties, null);
683
        }
684
685
        /**
686
         * <p>Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this collection and draws their requested properties. If a node is
687
         *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
688
         *
689
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
690
         * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.</p>
691
         *
692
         * <p>The particular implementation depends on the kind of each layer and composed layer. And this process can be cancelled at any
693
         *  time by the shared object <code>cancel</code>.</p>
694
         *
695
         * <p>According the print quality, labels will be printed in different resolution:
696
         *  <ul>
697
         *   <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
698
         *   <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
699
         *   <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
700
         *  </ul>
701
         * </p>
702
         *
703
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
704
         * @param viewPort the information for drawing the layers
705
         * @param cancel shared object that determines if this layer can continue being drawn
706
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
707
         * @param properties properties that will be print
708
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
709
         *
710
         * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
711
         *
712
         * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintRequestAttributeSet)
713
         *
714
         * @throws ReadDriverException if fails the driver reading the data.
715
         */
716
        public ComposedLayer print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties, ComposedLayer group)
717
        throws ReadException {
718
        double dpi = 72;
719
720
                PrintQuality resolution=(PrintQuality)properties.get(PrintQuality.class);
721
                if (resolution.equals(PrintQuality.NORMAL)){
722
                        dpi = 300;
723
                } else if (resolution.equals(PrintQuality.HIGH)){
724
                        dpi = 600;
725
                } else if (resolution.equals(PrintQuality.DRAFT)){
726
                        dpi = 72;
727
                }
728
729
                // TODO: A la hora de imprimir, isWithinScale falla, porque est?
730
                // calculando la escala en pantalla, no para el layout.
731
                // Revisar esto.
732
733
                // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
734
                for (int i=0; i < layers.size(); i++) {
735
                        FLayer lyr = (FLayer) layers.get(i);
736 22252 jmvivo
                        if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
737
                                continue;
738
                        }
739 21200 vcaballero
740
                        try{
741
742
                                ///// CHEMA ComposedLayer
743
                                // Checks for draw group (ComposedLayer)
744
                                if (group != null) {
745
                                        if (lyr instanceof FLayers){
746
                                                group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
747
                                        } else {
748
                                                // If layer can be added to the group, does it
749
                                                if (lyr instanceof ILabelable
750
                                                                && ((ILabelable) lyr).isLabeled()
751
                                                                && ((ILabelable) lyr).getLabelingStrategy() != null
752
                                                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
753
                                                        group.add(lyr);
754
                                                } else {
755
                                                        // draw the 'pending to draw' layer group
756
                                                        group.print(g,viewPort,cancel,scale,properties);
757
758
                                                        // gets a new group instance
759
                                                        if (lyr instanceof ILabelable
760
                                                                        && ((ILabelable) lyr).isLabeled()
761
                                                                        && ((ILabelable) lyr).getLabelingStrategy() != null
762
                                                                        && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
763
                                                                group = lyr.newComposedLayer();
764
                                                        } else {
765
                                                                group = null;
766
                                                        }
767
                                                        // if layer hasn't group, draws it inmediately
768
                                                        if (group == null) {
769
                                                                if (lyr instanceof FLayers){
770
                                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
771
                                                                } else {
772
                                                                        lyr.print(g, viewPort, cancel,scale,properties);
773
                                                                        if (lyr instanceof ILabelable
774
                                                                                        && ((ILabelable) lyr).isLabeled()
775
                                                                                        && ((ILabelable) lyr).getLabelingStrategy() != null
776
                                                                                        && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
777
                                                                                ILabelable lLayer = (ILabelable) lyr;
778
                                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
779
                                                                        }
780
                                                                }
781
                                                        } else {
782
                                                                // add the layer to the group
783
                                                                group.setMapContext(fmap);
784
                                                                group.add(lyr);
785
786
                                                        }
787
788
                                                }
789
                                        }
790
                                } else {
791
                                        // gets a new group instance
792
                                        group = lyr.newComposedLayer();
793
                                        // if layer hasn't group, draws it inmediately
794
                                        if (group == null) {
795
                                                if (lyr instanceof FLayers){
796
                                                        group = ((FLayers)lyr).print(g, viewPort, cancel,scale,properties,group);
797
                                                } else {
798
                                                        lyr.print(g, viewPort, cancel,scale,properties);
799
                                                        if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
800
                                                                ILabelable lLayer = (ILabelable) lyr;
801
802
                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
803
                                                        }
804
                                                }
805
                                        } else {
806
                                                // add the layer to the group
807
                                                group.setMapContext(fmap);
808
                                                group.add(lyr);
809
810
                                        }
811
                                }
812
                                ///// CHEMA ComposedLayer
813
814
                        } catch (Exception e){
815
                                String mesg = Messages.getString("error_printing_layer")+" "+ lyr.getName() + ": " + e.getMessage();
816
                                fmap.addLayerError(mesg);
817
                                logger.error(mesg, e);
818
                        }
819
820
                }
821
822
                ///// CHEMA ComposedLayer
823
                if (group != null && this.getParentLayer() == null) {
824
                        //si tenemos un grupo pendiente de pintar, pintamos
825
                        group.print(g, viewPort, cancel,scale,properties);
826
                        group = null;
827
828
                }
829
                ///// CHEMA ComposedLayer
830
831
                if (getVirtualLayers() != null) {
832
                        getVirtualLayers().print( g, viewPort, cancel, scale, properties);
833
                }
834
835
                ///// CHEMA ComposedLayer
836
                return group;
837
                ///// CHEMA ComposedLayer
838
        }
839
840
        /*
841
         * (non-Javadoc)
842
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
843
         */
844 21426 vcaballero
        public Envelope getFullEnvelope() {
845
                Envelope rAux = null;
846 21200 vcaballero
                boolean first = true;
847
848
849
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
850
                        FLayer capa = (FLayer) iter.next();
851
                        try{
852
                                if (first) {
853 21426 vcaballero
                                        rAux = capa.getFullEnvelope();
854 21200 vcaballero
                                        first=false;
855
                                } else {
856 21426 vcaballero
                                        rAux.add(capa.getFullEnvelope());
857 21200 vcaballero
                                }
858
                        }catch (Exception e) {
859
                                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
860
                        }
861
                }
862
863
                return rAux;
864
        }
865
866
        /**
867
         * Notifies all listeners associated to this collection of layers,
868
         *  that another layer is going to be added or replaced in the internal
869
         *  list of layers.
870
         *
871
         * @param e a layer collection event with the new layer
872
         */
873
        protected void callLayerAdding(LayerCollectionEvent event)
874
        throws CancelationException {
875
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
876
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
877
                        ((LayerCollectionListener) iter.next()).layerAdding(event);
878
                }
879
        }
880
881
        /**
882
         * Notifies all listeners associated to this collection of layers,
883
         *  that a layer is going to be removed from the internal list of layers.
884
         *
885
         * @param event a layer collection event with the layer being removed
886
         *
887
         * @throws CancelationException any exception produced during the cancellation of the driver.
888
         */
889
        protected void callLayerRemoving(LayerCollectionEvent event)
890
        throws CancelationException {
891
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
892
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
893
                        ((LayerCollectionListener) iter.next()).layerRemoving(event);
894
                }
895
        }
896
897
        /**
898
         * Notifies all listeners associated to this collection of layers,
899
         *  that a layer is going to be moved in the internal list of layers.
900
         *
901
         * @param event a layer collection event with the layer being moved, and the initial and final positions
902
         *
903
         * @throws CancelationException any exception produced during the cancellation of the driver.
904
         */
905
        protected void callLayerMoving(LayerPositionEvent event)
906
        throws CancelationException {
907
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
908
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
909
                        ((LayerCollectionListener) iter.next()).layerMoving(event);
910
                }
911
        }
912
913
        /**
914
         * Notifies all listeners associated to this collection of layers,
915
         *  that another layer has been added or replaced in the internal
916
         *  list of layers.
917
         *
918
         * @param e a layer collection event with the new layer
919
         */
920
        protected void callLayerAdded(LayerCollectionEvent event) {
921
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
922
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
923
                        ((LayerCollectionListener) iter.next()).layerAdded(event);
924
                }
925
        }
926
927
        /**
928
         * Notifies all listeners associated to this collection of layers,
929
         *  that another layer has been removed from the internal list of layers.
930
         *
931
         * @param e a layer collection event with the layer removed
932
         */
933
        protected void callLayerRemoved(LayerCollectionEvent event) {
934
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
935
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
936
                        ((LayerCollectionListener) iter.next()).layerRemoved(event);
937
                }
938
        }
939
940
        /**
941
         * Notifies all listeners associated to this collection of layers,
942
         *  that another layer has been moved in the internal list of layers.
943
         *
944
         * @param e a layer collection event with the layer moved, and the initial and final positions
945
         */
946
        protected void callLayerMoved(LayerPositionEvent event) {
947
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
948
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
949
                        ((LayerCollectionListener) iter.next()).layerMoved(event);
950
                }
951
        }
952
953
        /**
954
         * <p>Returns an entity that represents this collection of layers stored as a tree-node with children that are also layers.</p>
955
         *
956
         * <p>The root node has the same properties that <code>FlyrDefault#getXMLEntity()</code> returns, and adds:
957
         *  <ul>
958
         *          <li> <i>numLayers</i> : number of layers of this collection (direct children of this node)
959
         *   <li> <i>LayerNames</i> : an array list with the name of the layers of this collection (direct children of this node)
960
         *    <code>FLayer.getXMLEntity()</code>
961
         *  </ul>
962
         * </p>
963
         *
964
         * <p>All XML elements returned represent the information about this layer.</p>
965
         *
966
         * @return an XML entity with information to this collection of layers
967 23183 vcaballero
         * @throws XMLException if there is any error creating the XML from the layers.
968 21200 vcaballero
         */
969
        public XMLEntity getXMLEntity() throws XMLException {
970
                XMLEntity xml = super.getXMLEntity();
971
                xml.putProperty("numLayers", layers.size());
972
973
                String[] s = new String[layers.size()];
974
975
                for (int i = 0; i < layers.size(); i++) {
976
                        s[i] = ((FLayer) layers.get(i)).getName();
977
                }
978
979
                xml.putProperty("LayerNames", s);
980
981
                for (int i = 0; i < layers.size(); i++) {
982
                        try {
983 23183 vcaballero
                                XMLEntity xmlLayer=((FLayer) layers.get(i)).getXMLEntity();
984 23303 jmvivo
                                xmlLayer.putProperty("tagName", "layer");
985 23183 vcaballero
                                xml.addChild(xmlLayer);
986 21200 vcaballero
                        }catch (XMLException e) {
987
                                e.printStackTrace();
988
                        }
989
                }
990
991
                return xml;
992
        }
993
994
        /**
995
         * <p>Inserts layers and properties to this collection of layers.</p>
996
         *
997
         * <p>This root node has the same properties that return <code>FlyrDefault#getXMLEntity()</code> adding:
998
         *  <ul>
999
         *          <li> <i>numLayers</i> : number of first-level layers of this collection
1000
         *   <li> <i>LayerNames</i> : an array list with the name of the first-level layers of this collection
1001
         *    <code>FLayer.getXMLEntity()</code>
1002
         *  </ul>
1003
         * </p>
1004
         *
1005
         * @see FLyrDefault#setXMLEntity()
1006
         * @see FLyrDefault#getXMLEntity()
1007
         * @see CopyOfFLayers#addLayerFromXML(XMLEntity, String)
1008
         *
1009
         * @param xml an <code>XMLEntity</code> with the information
1010
         *
1011 23183 vcaballero
         * @throws XMLException if there is an error setting the object.
1012 21200 vcaballero
         */
1013 23183 vcaballero
        public void setXMLEntity(XMLEntity xml) throws XMLException{
1014
                super.setXMLEntity(xml);
1015
                //LoadLayerException loadLayerException=new LoadLayerException();
1016
1017
                String[] s = xml.getStringArrayProperty("LayerNames");
1018
                // try {
1019
                fmap.clearErrors();
1020
                int numLayers=xml.getIntProperty("numLayers");
1021 23303 jmvivo
                Iterator iter = xml.findChildren("tagName","layer");
1022
                XMLEntity xmlLayer;
1023
                while (iter.hasNext()) {
1024
                        xmlLayer=(XMLEntity) iter.next();
1025 23183 vcaballero
                        try {
1026 23303 jmvivo
                                this.addLayerFromXML(xmlLayer, null);
1027 23183 vcaballero
                        } catch (LoadLayerException e) {
1028
                                throw new XMLLayerException(getName(),e);
1029
                        }
1030
                }
1031 23303 jmvivo
                if (numLayers != this.layers.size()) {
1032
                        logger.warn(this.getName() + ": layer count no match");
1033
                }
1034 23183 vcaballero
        }
1035 21200 vcaballero
        /*
1036
         * (non-Javadoc)
1037
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
1038
         */
1039
        public MapContext getMapContext() {
1040
                return fmap;
1041
        }
1042
        /*
1043
         * (non-Javadoc)
1044
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setCoordTrans(org.cresques.cts.ICoordTrans)
1045
         */
1046
        public void setCoordTrans(ICoordTrans ct) {
1047
                super.setCoordTrans(ct);
1048
1049
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1050
                        FLayer layer = (FLayer) iter.next();
1051
                        layer.setCoordTrans(ct);
1052
                }
1053
        }
1054
        /*
1055
         * (non-Javadoc)
1056
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
1057
         */
1058
        public void setAllActives(boolean active) {
1059
                FLayer lyr;
1060
1061
                for (int i = 0; i < layers.size(); i++) {
1062
                        lyr = ((FLayer) layers.get(i));
1063
                        lyr.setActive(active);
1064
1065
                        if (lyr instanceof LayerCollection) {
1066
                                ((LayerCollection) lyr).setAllActives(active);
1067
                        }
1068
                }
1069
        }
1070
1071
        /*
1072
         * (non-Javadoc)
1073
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
1074
         */
1075
        public FLayer[] getActives() {
1076
                ArrayList ret = new ArrayList();
1077
                LayersIterator it = new LayersIterator(this) {
1078
1079
                        public boolean evaluate(FLayer layer) {
1080
                                return layer.isActive();
1081
                        }
1082
1083
                };
1084
1085
                while (it.hasNext())
1086
                {
1087
                        ret.add(it.next());
1088
                }
1089
                return (FLayer[]) ret.toArray(new FLayer[0]);
1090
        }
1091
1092
        /*
1093
         * (non-Javadoc)
1094
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMinScale()
1095
         */
1096
        public double getMinScale() {
1097
                return -1; // La visibilidad o no la controla cada capa
1098
                // dentro de una colecci?n
1099
        }
1100
        /*
1101
         * (non-Javadoc)
1102
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMaxScale()
1103
         */
1104
        public double getMaxScale() {
1105
                return -1;
1106
        }
1107
        /*
1108
         * (non-Javadoc)
1109
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMinScale(double)
1110
         */
1111
        public void setMinScale(double minScale)
1112
        {
1113
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1114
                        FLayer lyr = (FLayer) iter.next();
1115
                        lyr.setMinScale(minScale);
1116
                }
1117
        }
1118
        /*
1119
         * (non-Javadoc)
1120
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMaxScale(double)
1121
         */
1122
        public void setMaxScale(double maxScale)
1123
        {
1124
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1125
                        FLayer lyr = (FLayer) iter.next();
1126
                        lyr.setMinScale(maxScale);
1127
                }
1128
        }
1129
        /*
1130
         * (non-Javadoc)
1131
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setActive(boolean)
1132
         */
1133
        public void setActive(boolean b){
1134
                super.setActive(b);
1135
                for (int i=0;i<layers.size();i++){
1136
                        ((FLayer)layers.get(i)).setActive(b);
1137
                }
1138
        }
1139
        /* (non-Javadoc)
1140
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
1141
         */
1142
        public boolean addLayerListener(LayerListener o) {
1143 22252 jmvivo
                for (int i = 0; i < layers.size(); i++) {
1144 21200 vcaballero
                        ((FLayer) layers.get(i)).addLayerListener(o);
1145 22252 jmvivo
                }
1146 21200 vcaballero
                return true;
1147
        }
1148
        /*
1149
         * (non-Javadoc)
1150
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo(java.awt.Point, double, com.iver.utiles.swing.threads.Cancellable)
1151
         */
1152
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws ReadException, LoadLayerException {
1153
                int i;
1154
                Vector items = new Vector();
1155
                FLayer layer;
1156
                XMLItem[] aux;
1157
                for (i = 0; i < this.layers.size(); i++){
1158
                        layer = (FLayer)layers.get(i);
1159
                        if (layer instanceof InfoByPoint){
1160
                                InfoByPoint queryable_layer = (InfoByPoint) layer;
1161
                                aux = queryable_layer.getInfo(p, tolerance, null);
1162
                                if (!(queryable_layer instanceof FLayers)){
1163
                                        for(int j = 0; j < aux.length; j++){
1164
                                                items.add(aux[j]);
1165
                                        }
1166
                                }
1167
                        }
1168
                }
1169
                return (XMLItem[])items.toArray(new XMLItem[0]);
1170
1171
        }
1172
1173
        /*
1174
         * (non-Javadoc)
1175
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
1176
         */
1177
        public ImageIcon getTocImageIcon() {
1178
                return new ImageIcon(MapContext.class.getResource("images/layerGroup.png"));
1179
        }
1180
        /*
1181
         * (non-Javadoc)
1182
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#isDirty()
1183
         */
1184
        public boolean isDirty() {
1185
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1186
                        FLayer lyr = (FLayer) iter.next();
1187 22252 jmvivo
                        if (lyr.isDirty()) {
1188 21200 vcaballero
                                return true;
1189 22252 jmvivo
                        }
1190 21200 vcaballero
                }
1191
                return false;
1192
        }
1193
        /*
1194
         * (non-Javadoc)
1195
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setDirty(boolean)
1196
         */
1197
        public void setDirty(boolean dirty) {
1198
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1199
                        FLayer lyr = (FLayer) iter.next();
1200
                        lyr.setDirty(dirty);
1201
                }
1202
1203
        }
1204
1205
        /**
1206
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers. And
1207
         *  returns <code>true</code> if there hasn't been errors.</p>
1208
         *
1209
         * @see CopyOfFLayers#addLayerFromXML(XMLEntity, String)
1210
         *
1211
         * @param xml tree-node structure with information about layers
1212
         * @param name name of the layer to add
1213
         * @return <code>true</code> if there were no errors adding the layer, <code>false</code> otherwise
1214
         *
1215
         * @throws LoadLayerException if fails loading this layer.
1216
         */
1217
//        public boolean addLayerFromXMLEntity(XMLEntity xml, String name) throws LoadLayerException {
1218
//                fmap.clearErrors();
1219
//                this.addLayerFromXML(xml,name);
1220
//
1221
//                return (fmap.getLayersError().size() == 0);
1222
//
1223
//        }
1224
1225
        /**
1226
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
1227
         *
1228
         * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
1229
         *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
1230
         *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
1231
         *
1232
         * @param xml tree-node structure with information about layers
1233
         * @param name name of the layer to add
1234
         *
1235
         * @throws LoadLayerException if fails loading this layer.
1236
         */
1237 23183 vcaballero
        private void addLayerFromXML(XMLEntity xml, String name) throws LoadLayerException {
1238
        FLayer layer = null;
1239
1240
        try {
1241 23303 jmvivo
                if (name == null) {
1242
                        name = xml.getName();
1243
                }
1244 23183 vcaballero
1245
1246
                String className = xml.getStringProperty("className");
1247
                Class clase = Class.forName(className);
1248
                layer = (FLayer) clase.newInstance();
1249
                if (clase.isAssignableFrom(FLayers.class)) {
1250
                        ((FLayers)layer).setMapContext(getMapContext());
1251
                        ((FLayers)layer).setParentLayer(this);
1252
//                        layer = new FLayers(getMapContext(),this);
1253
                        layer.setXMLEntity(xml);
1254
                } else {
1255
                        // Capas Nuevas (externas)
1256
                        layer.setName(name);
1257
                        layer.setXMLEntity(xml);
1258
                        layer.load();
1259
                }
1260
1261 21200 vcaballero
//                //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,
1262
//                //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1263 23183 vcaballero
//                if (className.equals(FLyrVect.class.getName())){// || className.equals(FLyrAnnotation.class.getName())) {
1264 21200 vcaballero
//                        String type = xml.getStringProperty("type");
1265
//                        if ("vectorial".equals(type)){
1266
//                                //String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
1267
//                                IProjection proj = null;
1268
//                                if (xml.contains("proj")) {
1269
//                                        proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
1270
//                                }
1271
//                                else
1272
//                                {
1273
//                                        proj = this.getMapContext().getViewPort().getProjection();
1274
//                                }
1275
//                                if (xml.contains("file")) {
1276
//                                        Driver d;
1277
//                                        try {
1278
//                                                d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
1279
//                                        } catch (DriverLoadException e1) {
1280
//                                                throw new DriverLayerException(name,e1);
1281
//                                        }
1282
//                                        layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
1283
//                                                        new File(xml.getStringProperty("file")),
1284
//                                                        proj);
1285
//
1286
//
1287
//                                }
1288
//                                if (xml.contains("db")) {
1289
//
1290
//                                        String driverName = xml.getStringProperty("db");
1291
//                                        IVectorialDatabaseDriver driver;
1292
//                                        try {
1293
//                                                driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
1294
//                                                //Hay que separar la carga de los datos del XMLEntity del load.
1295
//                                                driver.setXMLEntity(xml.getChild(2));
1296
//
1297
//                                                boolean loadOk = false;
1298
//                                                ((DefaultJDBCDriver)driver).load();
1299
//                                                if (((DefaultJDBCDriver)driver).getConnection() != null) {
1300
//                                                        loadOk = true;
1301
//                                                }
1302
//                                                layer = LayerFactory.createDBLayer(driver, name, proj);
1303
//                                                if (!loadOk) {
1304
//                                                        layer.setAvailable(false);
1305
//                                                }
1306
//
1307
//                                        } catch (DriverLoadException e) {
1308
//                                                throw new DriverLayerException(name,e);
1309
//                                        } catch (XMLException e) {
1310
//                                                throw new DriverLayerException(name,e);
1311
//                                        } catch (ReadException e) {
1312
//                                                throw new DriverLayerException(name,e);
1313
//                                        }
1314
//
1315
//                                }
1316
//                                // Clases con algun driver gen?rico creado por otro
1317
//                                // programador
1318
//                                if (xml.contains("other")) {
1319
//
1320
//                                        String driverName = xml.getStringProperty("other");
1321
//                                        VectorialDriver driver = null;
1322
//                                        try {
1323
//                                                driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
1324
//                                        } catch (DriverLoadException e) {
1325
//                                                // Si no existe ese driver, no pasa nada.
1326
//                                                // Puede que el desarrollador no quiera que
1327
//                                                // aparezca en el cuadro de di?logo y ha metido
1328
//                                                // el jar con sus clases en nuestro directorio lib.
1329
//                                                // Intentamos cargar esa clase "a pelo".
1330
//                                                if (xml.getChild(2).contains("className"))
1331
//                                                {
1332
//                                                        String className2 = xml.getChild(2).getStringProperty("className");
1333
//                                                        try {
1334
//                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1335
//                                                        } catch (Exception e1) {
1336
//                                                                throw new DriverLayerException(name,e);
1337
//                                                        }
1338
//                                                }
1339
//                                        } catch (NullPointerException npe) {
1340
//                                                // Si no existe ese driver, no pasa nada.
1341
//                                                // Puede que el desarrollador no quiera que
1342
//                                                // aparezca en el cuadro de di?logo y ha metido
1343
//                                                // el jar con sus clases en nuestro directorio lib.
1344
//                                                // Intentamos cargar esa clase "a pelo".
1345
//                                                if (xml.getChild(2).contains("className"))
1346
//                                                {
1347
//                                                        String className2 = xml.getChild(2).getStringProperty("className");
1348
//                                                        try {
1349
//                                                                driver = (VectorialDriver) Class.forName(className2).newInstance();
1350
//                                                        } catch (Exception e1) {
1351
//                                                                throw new DriverLayerException(name,e1);
1352
//                                                        }
1353
//                                                }
1354
//                                        }
1355
//                                        if (driver instanceof IPersistence)
1356
//                                        {
1357
//                                                IPersistence persist = (IPersistence) driver;
1358
//                                                persist.setXMLEntity(xml.getChild(2));
1359
//                                        }
1360
//                                        layer = LayerFactory.createLayer(name, driver, proj);
1361
//                                }
1362
//
1363
//                        }
1364
//
1365
//                        //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,
1366
//                        //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1367
//                        if (className.equals(FLyrAnnotation.class.getName())){
1368
//                                layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
1369
//                        }
1370
//
1371
//
1372
//                        layer.setXMLEntity(xml);
1373
//
1374
//                } else {
1375
//                        Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1376
//                        layer = (FLayer) clase.newInstance();
1377
//                        if (clase.isAssignableFrom(FLayers.class)) {
1378
//                                ((FLayers)layer).setMapContext(getMapContext());
1379
//                                ((FLayers)layer).setParentLayer(this);
1380
////                                layer = new FLayers(getMapContext(),this);
1381
//                                layer.setXMLEntity(xml);
1382
//                        } else {
1383
//                                // Capas Nuevas (externas)
1384
//                                layer.setName(name);
1385
//                                layer.setXMLEntity(xml);
1386
//                                layer.load();
1387
//                        }
1388
//                }
1389 23183 vcaballero
                this.addLayer(layer);
1390
                logger.debug("layer: "+ layer.getName() +" loaded");
1391
                // Comprobar que la proyecci?n es la misma que la de FMap
1392
                // Si no lo es, es una capa que est? reproyectada al vuelo
1393
                IProjection proj = layer.getProjection();
1394 23303 jmvivo
                if ((proj != null)) {
1395 23183 vcaballero
                        if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode()))
1396
                        {
1397
                                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1398
                                // TODO: REVISAR CON LUIS
1399
                                // Se lo fijamos a todas, luego cada una que se reproyecte
1400
                                // si puede, o que no haga nada
1401 21200 vcaballero
1402 23183 vcaballero
                                layer.setCoordTrans(ct);
1403
                        }
1404 23303 jmvivo
                }
1405 23183 vcaballero
        } catch (XMLException e) {
1406
                fmap.addLayerError(xml.getStringProperty("name"));
1407
                throw new LoadLayerException(name,e);
1408
        } catch (ClassNotFoundException e) {
1409
                fmap.addLayerError(xml.getStringProperty("name"));
1410
                throw new LoadLayerException(name,e);
1411
        } catch (InstantiationException e) {
1412
                fmap.addLayerError(xml.getStringProperty("name"));
1413
                throw new LoadLayerException(name,e);
1414
        } catch (IllegalAccessException e) {
1415
                fmap.addLayerError(xml.getStringProperty("name"));
1416
                throw new LoadLayerException(name,e);
1417
        } catch (LoadLayerException e){
1418
                fmap.addLayerError(xml.getStringProperty("name"));
1419
                throw e;
1420
        }
1421
        }
1422
1423 21200 vcaballero
        /**
1424
         * <p>Sets the <code>MapContext</code> that contains this layer node.</p>
1425
         *
1426
         * @param mapContext the <code>MapContext</code> that contains this layer node
1427
         */
1428
        public void setMapContext(MapContext mapContext) {
1429
                this.fmap = mapContext;
1430
        }
1431
1432
        /**
1433
         * <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
1434
         *  and loads the layer. Then, adds the layer to this collection of layers, and if there is a projection defined,
1435
         *  inserts the transformation coordinates to the layer.</p>
1436
         *
1437
         * <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>
1438
         *
1439
         * @param xml tree-node structure with information about layers
1440
         * @param name name of the layer to add
1441
         */
1442
        private void addLayerFromXMLNew(XMLEntity xml, String name) {
1443
//                FLayer layer = null;
1444
//
1445
//
1446
//                try {
1447
//                        String className = xml.getStringProperty("className");
1448
//                        Class clazz = Class.forName(className);
1449
//                        if (clazz.isAssignableFrom(FLayers.class)) {
1450
//                                layer = (FLayer) clazz.newInstance();
1451
//                                ((FLayers)layer).setMapContext(getMapContext());
1452
//                                ((FLayers)layer).setParentLayer(this);
1453
//        //                if (className.equals((FLayers.class.getName()))){
1454
//        //                        layer = new FLayers(getMapContext(),this);
1455
//                        } else {
1456
//        //                        Por compatibilidad
1457
//                                if (className.equals(FLyrVect.class.getName())) {
1458
//                                        if (xml.contains("file")) {
1459
//                                                layer = new FLayerFileVectorial();
1460
//                                        } else if (xml.contains("db")) {
1461
//                                                try {
1462
//                                                        layer = (FLayer)((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create("com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial");
1463
//                                                } catch (Exception e) {
1464
//                                                        throw new XMLException(new Exception("No se tiene registrada la capa de tipo JDBC"));
1465
//                                                }
1466
//                                                //className = FLayerJDBCVectorial.class.getName();
1467
//                                        } else if (xml.contains("other")){
1468
//                                                layer = new FLayerGenericVectorial();
1469
//                                        } else {
1470
//                                                throw new XMLException(new Exception("Capa vectorial de tipo no reconocido"));
1471
//                                        }
1472
//        //                                Fin por compatibilidad
1473
//                                } else {
1474
//                                        try {
1475
//                                                layer = (FLayer)(((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create(className));
1476
//                                        } catch (Exception e) {
1477
//                                                //puende que no este registrada como punto de extension
1478
//                                                Class clase = Class.forName(className);
1479
//                                                layer = (FLayer) clase.newInstance();
1480
//                                                // FIXME: Hacemos algo aqui o dejamos que suba el error?
1481
//                                        }
1482
//                                }
1483
//
1484
//                        }
1485
//                        layer.setXMLEntity(xml);
1486
//                        if (name != null) layer.setName(name);
1487
//                        layer.load();
1488
//
1489
//                        this.addLayer(layer);
1490
//                        logger.debug("layer: "+ layer.getName() +" loaded");
1491
//                        // Comprobar que la proyecci?n es la misma que la de FMap
1492
//                        // Si no lo es, es una capa que est? reproyectada al vuelo
1493
//                        IProjection proj = layer.getProjection();
1494
//                        if ((proj != null))
1495
//                                if (proj != getMapContext().getProjection())
1496
//                                {
1497
//                                        ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1498
//                                        // TODO: REVISAR CON LUIS
1499
//                                        // Se lo fijamos a todas, luego cada una que se reproyecte
1500
//                                        // si puede, o que no haga nada
1501
//                                        layer.setCoordTrans(ct);
1502
//
1503
//                                }
1504
//                }catch (Exception e) {
1505
//                        fmap.addLayerError(xml.getStringProperty("name"));
1506
//                        logger.debug(Messages.getString("could_not_load_layer")+": "+xml.getStringProperty("name") + ".\n"
1507
//                                        +Messages.getString("reason")+":", e);
1508
//                }
1509
        }
1510 22252 jmvivo
1511
        public void accept(Visitor visitor) throws BaseException {
1512
                throw new NotSupportedOperationException(visitor, this);
1513
        }
1514
1515
        public void accept(LayersVisitor visitor) throws BaseException {
1516
                for (int i = 0; i < this.getLayersCount(); i++) {
1517
                        FLayer layer = this.getLayer(i);
1518
                        if (layer instanceof LayersVisitable) {
1519
                                ((LayersVisitable) layer).accept(visitor);
1520
                        } else {
1521
                                visitor.visit(layer);
1522
                        }
1523
                }
1524
        }
1525
1526
1527 21200 vcaballero
}