Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayers.java @ 33261

History | View | Annotate | Download (49.7 KB)

1
/* 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 com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.Point;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47
import java.io.File;
48
import java.util.ArrayList;
49
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.Iterator;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.SortedMap;
55
import java.util.TreeMap;
56
import java.util.Vector;
57

    
58
import javax.print.attribute.PrintRequestAttributeSet;
59
import javax.print.attribute.standard.PrintQuality;
60
import javax.swing.ImageIcon;
61

    
62
import org.apache.log4j.Logger;
63
import org.cresques.cts.ICoordTrans;
64
import org.cresques.cts.IProjection;
65
import org.gvsig.tools.file.PathGenerator;
66

    
67
import com.hardcode.driverManager.Driver;
68
import com.hardcode.driverManager.DriverLoadException;
69
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
70
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
71
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
72
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
73
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
74
import com.iver.cit.gvsig.fmap.MapContext;
75
import com.iver.cit.gvsig.fmap.MapControl;
76
import com.iver.cit.gvsig.fmap.Messages;
77
import com.iver.cit.gvsig.fmap.ViewPort;
78
import com.iver.cit.gvsig.fmap.core.ILabelable;
79
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
80
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver;
81
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
82
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
83
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
84
import com.iver.cit.gvsig.fmap.layers.layerOperations.ComposedLayer;
85
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
86
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
87
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
88
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
89
import com.iver.cit.gvsig.fmap.layers.order.DefaultOrderManager;
90
import com.iver.cit.gvsig.fmap.layers.order.OrderManager;
91
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
92
import com.iver.utiles.IPersistence;
93
import com.iver.utiles.XMLEntity;
94
import com.iver.utiles.extensionPoints.ExtensionPoint;
95
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
96
import com.iver.utiles.swing.threads.Cancellable;
97

    
98
/**
99
 * <p>Represents a generic collection of layers, that can be represented as a node in a tree of nodes of layers.</p>
100
 *
101
 * <p>Adapts the basic functionality implemented for a layer in the abstract class <code>FLyrDefault</code>, to
102
 *  a collection of layers, implementing, as well, specific methods for this kind of object, defined in the
103
 *  interfaces <code>VectorialData</code>, <code>LayerCollection</code>, and <code>InfoByPoint</code>.</p>
104
 *
105
 * @see FLyrDefault
106
 */
107
public class FLayers extends FLyrDefault implements VectorialData, LayerCollection, InfoByPoint
108
{
109
        /**
110
         * List with all listeners registered for this kind of node.
111
         *
112
         * @see #addLayerCollectionListener(LayerCollectionListener)
113
         * @see #removeLayerCollectionListener(LayerCollectionListener)
114
         * @see #callLayerAdded(LayerCollectionEvent)
115
         * @see #callLayerAdding(LayerCollectionEvent)
116
         * @see #callLayerMoved(LayerPositionEvent)
117
         * @see #callLayerMoving(LayerPositionEvent)
118
         * @see #callLayerRemoved(LayerCollectionEvent)
119
         * @see #callLayerRemoving(LayerCollectionEvent)
120
         */
121
        protected ArrayList layerCollectionListeners = new ArrayList();
122

    
123
        /**
124
         * A synchronized list with the layers.
125
         *
126
         * @see #setAllVisibles(boolean)
127
         * @see #addLayer(FLayer)
128
         * @see #addLayer(int, FLayer)
129
         * @see #moveTo(int, int)
130
         * @see #removeLayer(FLayer)
131
         * @see #removeLayer(int)
132
         * @see #removeLayer(String)
133
         * @see #replaceLayer(String, FLayer)
134
         * @see #getVisibles()
135
         * @see #getLayer(int)
136
         * @see #getLayer(String)
137
         * @see #getLayersCount()
138
         * @see #getFullExtent()
139
         */
140
        protected List layers = Collections.synchronizedList(new ArrayList());
141

    
142
        protected Map orderedLayers = Collections.synchronizedSortedMap(new TreeMap());
143

    
144
        /**
145
         * The model of the layer.
146
         *
147
         * @see #getMapContext()
148
         */
149
        protected MapContext fmap;
150

    
151
        /**
152
         * Useful for debug the problems during the implementation.
153
         */
154
        protected static Logger logger = Logger.getLogger(FLayers.class.getName());
155

    
156
        //        /**
157
        //         * Default <code>FLayers</code> constructor.
158
        //         *
159
        //         * @param fmap reference to the <code>MapContext</code> instance that contains this node of layers
160
        //         * @param parent parent node of this one
161
        //         */
162
        //        public FLayers(MapContext fmap, FLayers parent) {
163
        //                setParentLayer(parent);
164
        //                this.fmap = fmap;
165
        //        }
166
        private static PathGenerator pathGenerator=PathGenerator.getInstance();
167
        /*
168
         * (non-Javadoc)
169
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
170
         */
171
        public void addLayerCollectionListener(LayerCollectionListener listener) {
172
                if (!layerCollectionListeners.contains(listener))
173
                        layerCollectionListeners.add(listener);
174
        }
175

    
176
        /*
177
         * (non-Javadoc)
178
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
179
         */
180
        public void setAllVisibles(boolean visible) {
181
                FLayer lyr;
182

    
183
                for (int i = 0; i < layers.size(); i++) {
184
                        lyr = ((FLayer) layers.get(i));
185
                        lyr.setVisible(visible);
186

    
187
                        if (lyr instanceof LayerCollection) {
188
                                ((LayerCollection) lyr).setAllVisibles(visible);
189
                        }
190
                }
191
        }
192

    
193
        /*
194
         * (non-Javadoc)
195
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
196
         */
197
        public void removeLayerCollectionListener(LayerCollectionListener listener) {
198
                layerCollectionListeners.remove(listener);
199
        }
200

    
201
        //        private void doAddLayer(FLayer layer) {
202
        //        layers.add(layer);
203
        //        layer.setParentLayer(this);
204
        //        }
205

    
206
        /**
207
         * Adds a layer on an specified position in this node. If there is a previous layer that
208
         * has been not added the layer is saved and will bi added where the previous layer
209
         * will be added.
210
         *
211
         * @param pos position in the inner list where the layer will be added
212
         * @param layer a layer
213
         */
214
        private void doAddLayer(int pos,FLayer layer) {
215
                //if there are not layers before the added layer, we have to
216
                //save the layer until all the layers are added
217
                if (pos > layers.size()){
218
                        orderedLayers.put(new Integer(pos), layer);
219
                }else{                        
220
                        doAddLayerSecure(pos, layer);
221
                        //Check if there are layers to add after
222
                        if (orderedLayers.size() > 0){
223
                                Iterator it = orderedLayers.keySet().iterator();
224
                                while (it.hasNext()){
225
                                        Integer position = (Integer)it.next();
226
                                        if (position == layers.size()){
227
                                                FLayer orderedLayer = (FLayer)orderedLayers.get(position);
228
                                                doAddLayerSecure(position, orderedLayer);
229
                                                orderedLayers.remove(position);
230
                                        }                                        
231
                                }
232
                        }
233
                }
234
        }
235

    
236
        /**
237
         * Adds a layer on an specified position in this node, but not checks if
238
         * there are layers added before it.
239
         * @param pos position in the inner list where the layer will be added
240
         * @param layer a layer
241
         */
242
        private void doAddLayerSecure(int pos,FLayer layer) {
243
                layers.add(pos,layer);
244
                layer.setParentLayer(this);
245
                if(layer.getProjection() != null && fmap != null)
246
                        layer.setCoordTrans(layer.getProjection().getCT(fmap.getProjection()));
247
                this.updateDrawVersion();
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
253
         */
254
        public void addLayer(FLayer layer) {
255
                int position = getOrderManager().getPosition(this, layer);
256
                addLayer(position,layer);
257
        }
258

    
259
        private OrderManager getOrderManager() {
260
                if (getMapContext()!=null) {
261
                        // just get it from MapContext each time is needed
262
                        return getMapContext().getOrderManager();
263
                }
264
                else {
265
                        return new DefaultOrderManager();
266
                }
267
        }
268

    
269
        /**
270
         * Adds a layer in an specified position in this node.
271
         *
272
         * @param layer a layer
273
         */
274
        public void addLayer(int pos,FLayer layer) {
275
                try {
276
                        //Notificamos a la capa que va a ser a?adida
277
                        //FLyrDefault layerDef = (FLyrDefault)layer;
278
                        //if (!layerDef.isUnavailable()) {
279
                        if (layer instanceof FLyrDefault)
280
                                ((FLyrDefault)layer).wakeUp();
281

    
282
                        if (layer instanceof FLayers){
283
                                FLayers layers=(FLayers)layer;
284
                                fmap.addAsCollectionListener(layers);
285
                        }
286
                        callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
287
                        //}
288

    
289
                        doAddLayer(pos,layer);
290

    
291
                        //if (!layerDef.isUnavailable()) {
292
                        callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
293
                        //}
294
                } catch (CancelationException e) {
295
                        logger.warn(e);
296
                } catch (LoadLayerException e) {
297
                        layer.setAvailable(false);
298
                        layer.addError(e);
299
                }
300
        }
301

    
302
        /*
303
         * (non-Javadoc)
304
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
305
         */
306
        public void moveTo(int from, int to) throws CancelationException {
307
                int newfrom=layers.size()-from-1;
308
                int newto=layers.size()-to-1;
309
                if ( newfrom < 0 || newfrom >=layers.size() || newto < 0 || newto >= layers.size()) return;
310
                FLayer aux = (FLayer) layers.get(newfrom);
311
                callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
312
                layers.remove(newfrom);
313
                layers.add(newto, aux);
314
                this.updateDrawVersion();
315
                callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
316

    
317
        }
318

    
319
        /**
320
         * Removes an inner layer.
321
         *
322
         * @param lyr a layer
323
         */
324
        private void doRemoveLayer(FLayer lyr) {
325
                layers.remove(lyr);
326
                this.updateDrawVersion();
327
        }
328

    
329
        /*
330
         * (non-Javadoc)
331
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
332
         */
333
        public void removeLayer(FLayer lyr) throws CancelationException {
334
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
335
                doRemoveLayer(lyr);
336
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
337
        }
338

    
339
        /*
340
         * (non-Javadoc)
341
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
342
         */
343
        public void removeLayer(int idLayer) {
344
                FLayer lyr = (FLayer) layers.get(idLayer);
345
                callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
346
                layers.remove(idLayer);
347
                this.updateDrawVersion();
348
                callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
349
        }
350

    
351
        /*
352
         * (non-Javadoc)
353
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
354
         */
355
        public void removeLayer(String layerName) {
356
                FLayer lyr;
357

    
358
                for (int i = 0; i < layers.size(); i++) {
359
                        lyr = ((FLayer) layers.get(i));
360

    
361
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
362
                                removeLayer(i);
363

    
364
                                break;
365
                        }
366
                }
367
        }
368

    
369
        /**
370
         * Replace a layer identified by its name, by another.
371
         *
372
         * @param layerName the name of the layer to be replaced
373
         * @param layer the new layer
374
         */
375
        public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException
376
        {
377
                FLayer lyr;
378
                FLayer parent;
379
                for (int i = 0; i < layers.size(); i++) {
380
                        lyr = ((FLayer) layers.get(i));
381

    
382
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
383
                                parent = lyr.getParentLayer();
384
                                removeLayer(i);
385
                                if (parent != null)
386
                                        //Notificamos a la capa que va a ser a?adida
387
                                        if (layer instanceof FLyrDefault)
388
                                                ((FLyrDefault)layer).wakeUp();
389

    
390
                                if (layer instanceof FLayers){
391
                                        FLayers layers=(FLayers)layer;
392
                                        fmap.addAsCollectionListener(layers);
393
                                }
394
                                callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
395

    
396
                                layers.add(i,layer);
397
                                layer.setParentLayer(this);
398

    
399
                                callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
400
                                break;
401
                        }
402
                }
403
        }
404

    
405
        /*
406
         * (non-Javadoc)
407
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
408
         */
409
        public FLayer[] getVisibles() {
410
                ArrayList array = new ArrayList();
411
                LayersIterator iter = new LayersIterator(this) {
412
                        public boolean evaluate(FLayer layer) {
413
                                return layer.isVisible();
414
                        }
415

    
416
                };
417

    
418
                while (iter.hasNext()) {
419
                        array.add(iter.nextLayer());
420
                }
421

    
422
                return (FLayer[]) array.toArray(new FLayer[0]);
423
        }
424

    
425
        /*
426
         * (non-Javadoc)
427
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
428
         */
429
        public FLayer getLayer(int index) {
430
                return (FLayer) layers.get(index);
431
        }
432

    
433
        /*
434
         * (non-Javadoc)
435
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
436
         */
437
        public FLayer getLayer(String layerName) {
438
                FLayer lyr;
439
                FLayer lyr2;
440
                ArrayList layerList;
441

    
442
                for (int i = 0; i < layers.size(); i++) {
443
                        lyr = ((FLayer) layers.get(i));
444

    
445
                        if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
446
                                return lyr;
447
                        }
448

    
449
                        layerList = new ArrayList();
450
                        splitLayerGroup(lyr,layerList);
451
                        for(int j = 0; j<layerList.size(); j++ )
452
                        {
453
                                lyr2 = ((FLayer)layerList.get(j));
454
                                if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
455
                                        return lyr2;
456
                                }
457
                        }
458
                }
459

    
460
                return null;
461
        }
462

    
463
        /**
464
         * <p> Splits up a layer group in order to get a layer by name when there are layer groups</p>
465
         *
466
         * <p>In <code>result</code> always will be at least one layer.</p>
467
         *
468
         * @param layer the layer we are looking for
469
         * @param result an array list that will have the results of the search
470
         */
471
        private void splitLayerGroup(FLayer layer, ArrayList result)
472
        {
473
                int i;
474
                FLayers layerGroup;
475
                if (layer instanceof FLayers)
476
                {
477
                        layerGroup = (FLayers)layer;
478
                        for (i=0; i < layerGroup.getLayersCount(); i++ )
479
                        {
480
                                splitLayerGroup(layerGroup.getLayer(i),result);
481
                        }
482
                }
483
                else
484
                {
485
                        result.add(layer);
486
                }
487
        }
488

    
489
        /*
490
         * (non-Javadoc)
491
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
492
         */
493
        public int getLayersCount() {
494
                return layers.size();
495
        }
496

    
497
        /*
498
         * (non-Javadoc)
499
         * @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)
500
         */
501
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
502
                        Cancellable cancel,double scale) throws ReadDriverException {
503
                // FIXME Arreglar este error
504
                throw new RuntimeException("Esto no deberia de llamarse");
505
        }
506

    
507

    
508

    
509
        /*
510
         * (non-Javadoc)
511
         * @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)
512
         */
513
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties)
514
        throws ReadDriverException {
515
                throw new RuntimeException("No deberia pasar por aqui");
516
        }
517

    
518
        public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties)
519
        throws ReadDriverException {
520
                this.print_old(g, viewPort, cancel, scale, properties, null);
521
        }
522

    
523
        /**
524
         * <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
525
         *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
526
         *
527
         * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
528
         * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.</p>
529
         *
530
         * <p>The particular implementation depends on the kind of each layer and composed layer. And this process can be cancelled at any
531
         *  time by the shared object <code>cancel</code>.</p>
532
         *
533
         * <p>According the print quality, labels will be printed in different resolution:
534
         *  <ul>
535
         *   <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
536
         *   <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
537
         *   <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
538
         *  </ul>
539
         * </p>
540
         *
541
         * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
542
         * @param viewPort the information for drawing the layers
543
         * @param cancel shared object that determines if this layer can continue being drawn
544
         * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
545
         * @param properties properties that will be print
546
         * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
547
         *
548
         * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
549
         *
550
         * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintRequestAttributeSet)
551
         *
552
         * @throws ReadDriverException if fails the driver reading the data.
553
         */
554
        public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties, ComposedLayer group)
555
        throws ReadDriverException {
556
                double dpi = 72;
557

    
558
                PrintQuality resolution=(PrintQuality)properties.get(PrintQuality.class);
559
                if (resolution.equals(PrintQuality.NORMAL)){
560
                        dpi = 300;
561
                } else if (resolution.equals(PrintQuality.HIGH)){
562
                        dpi = 600;
563
                } else if (resolution.equals(PrintQuality.DRAFT)){
564
                        dpi = 72;
565
                }
566

    
567
                // TODO: A la hora de imprimir, isWithinScale falla, porque est?
568
                // calculando la escala en pantalla, no para el layout.
569
                // Revisar esto.
570

    
571
                // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
572
                for (int i=0; i < layers.size(); i++) {
573
                        FLayer lyr = (FLayer) layers.get(i);
574
                        if (!lyr.isVisible() || !lyr.isWithinScale(scale)) continue;
575

    
576
                        try{
577

    
578
                                ///// CHEMA ComposedLayer
579
                                // Checks for draw group (ComposedLayer)
580
                                if (group != null) {
581
                                        if (lyr instanceof FLayers){
582
                                                group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
583
                                        } else {
584
                                                // If layer can be added to the group, does it
585
                                                if (lyr instanceof ILabelable
586
                                                                && ((ILabelable) lyr).isLabeled()
587
                                                                && ((ILabelable) lyr).getLabelingStrategy() != null
588
                                                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
589
                                                        group.add(lyr);
590
                                                } else {
591
                                                        // draw the 'pending to draw' layer group
592
                                                        group.print(g,viewPort,cancel,scale,properties);
593

    
594
                                                        // gets a new group instance
595
                                                        if (lyr instanceof ILabelable
596
                                                                        && ((ILabelable) lyr).isLabeled()
597
                                                                        && ((ILabelable) lyr).getLabelingStrategy() != null
598
                                                                        && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
599
                                                                group = lyr.newComposedLayer();
600
                                                        } else {
601
                                                                group = null;
602
                                                        }
603
                                                        // if layer hasn't group, draws it inmediately
604
                                                        if (group == null) {
605
                                                                if (lyr instanceof FLayers){
606
                                                                        group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
607
                                                                } else {
608
                                                                        lyr.print(g, viewPort, cancel,scale,properties);
609
                                                                        if (lyr instanceof ILabelable
610
                                                                                        && ((ILabelable) lyr).isLabeled()
611
                                                                                        && ((ILabelable) lyr).getLabelingStrategy() != null
612
                                                                                        && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
613
                                                                                ILabelable lLayer = (ILabelable) lyr;
614
                                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
615
                                                                        }
616
                                                                }
617
                                                        } else {
618
                                                                // add the layer to the group
619
                                                                group.setMapContext(fmap);
620
                                                                group.add(lyr);
621

    
622
                                                        }
623

    
624
                                                }
625
                                        }
626
                                } else {
627
                                        // gets a new group instance
628
                                        group = lyr.newComposedLayer();
629
                                        // if layer hasn't group, draws it inmediately
630
                                        if (group == null) {
631
                                                if (lyr instanceof FLayers){
632
                                                        group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
633
                                                } else {
634
                                                        lyr.print(g, viewPort, cancel,scale,properties);
635
                                                        if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
636
                                                                ILabelable lLayer = (ILabelable) lyr;
637

    
638
                                                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
639
                                                        }
640
                                                }
641
                                        } else {
642
                                                // add the layer to the group
643
                                                group.setMapContext(fmap);
644
                                                group.add(lyr);
645

    
646
                                        }
647
                                }
648
                                ///// CHEMA ComposedLayer
649

    
650
                        } catch (Exception e){
651
                                String mesg = Messages.getString("error_printing_layer")+" "+ lyr.getName() + ": " + e.getMessage();
652
                                fmap.addLayerError(mesg);
653
                                logger.error(mesg, e);
654
                        }
655

    
656
                }
657

    
658
                ///// CHEMA ComposedLayer
659
                if (group != null && this.getParentLayer() == null) {
660
                        //si tenemos un grupo pendiente de pintar, pintamos
661
                        group.print(g, viewPort, cancel,scale,properties);
662
                        group = null;
663

    
664
                }
665
                ///// CHEMA ComposedLayer
666

    
667
                if (getVirtualLayers() != null) {
668
                        getVirtualLayers().print( g, viewPort, cancel, scale, properties);
669
                }
670

    
671
                ///// CHEMA ComposedLayer
672
                return group;
673
                ///// CHEMA ComposedLayer
674
        }
675

    
676
        /*
677
         * (non-Javadoc)
678
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
679
         */
680
        public Rectangle2D getFullExtent() {
681
                Rectangle2D rAux = null;
682
                boolean first = true;
683

    
684

    
685
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
686
                        FLayer capa = (FLayer) iter.next();
687
                        try{
688
                                if (first) {
689
                                        rAux = (Rectangle2D)capa.getFullExtent().clone();
690
                                        first=false;
691
                                } else {
692
                                        rAux.add(capa.getFullExtent());
693
                                }
694
                        }catch (Exception e) {
695
                                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
696
                        }
697
                }
698

    
699
                return rAux;
700
        }
701

    
702
        /**
703
         * Notifies all listeners associated to this collection of layers,
704
         *  that another layer is going to be added or replaced in the internal
705
         *  list of layers.
706
         *
707
         * @param e a layer collection event with the new layer
708
         */
709
        protected void callLayerAdding(LayerCollectionEvent event)
710
        throws CancelationException {
711
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
712
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
713
                        ((LayerCollectionListener) iter.next()).layerAdding(event);
714
                }
715
        }
716

    
717
        /**
718
         * Notifies all listeners associated to this collection of layers,
719
         *  that a layer is going to be removed from the internal list of layers.
720
         *
721
         * @param event a layer collection event with the layer being removed
722
         *
723
         * @throws CancelationException any exception produced during the cancellation of the driver.
724
         */
725
        protected void callLayerRemoving(LayerCollectionEvent event)
726
        throws CancelationException {
727
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
728
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
729
                        ((LayerCollectionListener) iter.next()).layerRemoving(event);
730
                }
731
        }
732

    
733
        /**
734
         * Notifies all listeners associated to this collection of layers,
735
         *  that a layer is going to be moved in the internal list of layers.
736
         *
737
         * @param event a layer collection event with the layer being moved, and the initial and final positions
738
         *
739
         * @throws CancelationException any exception produced during the cancellation of the driver.
740
         */
741
        protected void callLayerMoving(LayerPositionEvent event)
742
        throws CancelationException {
743
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
744
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
745
                        ((LayerCollectionListener) iter.next()).layerMoving(event);
746
                }
747
        }
748

    
749
        /**
750
         * Notifies all listeners associated to this collection of layers,
751
         *  that another layer has been added or replaced in the internal
752
         *  list of layers.
753
         *
754
         * @param e a layer collection event with the new layer
755
         */
756
        protected void callLayerAdded(LayerCollectionEvent event) {
757
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
758
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
759
                        ((LayerCollectionListener) iter.next()).layerAdded(event);
760
                }
761
        }
762

    
763
        /**
764
         * Notifies all listeners associated to this collection of layers,
765
         *  that another layer has been removed from the internal list of layers.
766
         *
767
         * @param e a layer collection event with the layer removed
768
         */
769
        protected void callLayerRemoved(LayerCollectionEvent event) {
770
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
771
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
772
                        ((LayerCollectionListener) iter.next()).layerRemoved(event);
773
                }
774
        }
775

    
776
        /**
777
         * Notifies all listeners associated to this collection of layers,
778
         *  that another layer has been moved in the internal list of layers.
779
         *
780
         * @param e a layer collection event with the layer moved, and the initial and final positions
781
         */
782
        protected void callLayerMoved(LayerPositionEvent event) {
783
                ArrayList aux = (ArrayList) layerCollectionListeners.clone();
784
                for (Iterator iter = aux.iterator(); iter.hasNext();) {
785
                        ((LayerCollectionListener) iter.next()).layerMoved(event);
786
                }
787
        }
788

    
789
        /**
790
         * <p>Returns an entity that represents this collection of layers stored as a tree-node with children that are also layers.</p>
791
         *
792
         * <p>The root node has the same properties that <code>FlyrDefault#getXMLEntity()</code> returns, and adds:
793
         *  <ul>
794
         *          <li> <i>numLayers</i> : number of layers of this collection (direct children of this node)
795
         *   <li> <i>LayerNames</i> : an array list with the name of the layers of this collection (direct children of this node)
796
         *    <code>FLayer.getXMLEntity()</code>
797
         *  </ul>
798
         * </p>
799
         *
800
         * <p>All XML elements returned represent the information about this layer.</p>
801
         *
802
         * @return an XML entity with information to this collection of layers
803
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is any error creating the XML from the layers.
804
         */
805
        public XMLEntity getXMLEntity() throws XMLException {
806
                XMLEntity xml = super.getXMLEntity();
807
                xml.putProperty("numLayers", layers.size());
808

    
809
                String[] s = new String[layers.size()];
810

    
811
                for (int i = 0; i < layers.size(); i++) {
812
                        s[i] = ((FLayer) layers.get(i)).getName();
813
                }
814

    
815
                xml.putProperty("LayerNames", s);
816

    
817
                for (int i = 0; i < layers.size(); i++) {
818
                        try {
819
                                XMLEntity entity = ((FLayer) layers.get(i)).getXMLEntity();
820
                                if(entity != null)
821
                                        xml.addChild(entity);
822
                        }catch (XMLException e) {
823
                                e.printStackTrace();
824
                        }
825
                }
826

    
827
                return xml;
828
        }
829

    
830
        /**
831
         * <p>Inserts layers and their properties to this collection, from an XML entity. Also adds properties to the collection of layers (root node).
832
         *
833
         * <p>The XML entity as parameter must have a tree-node structure, the root must have at least two properties, and each (first-level)
834
         *  child can be a raster layer <i>(<code>FLyrRaster</code>)</i>, a vectorial layer <i>(<code>FLyrVect</code>)</i>,
835
         *  another collection of layers <i>(<code>FLayers</code>)</i>, or another kind of layer <i>(<code>FLayer</code>)</i> .</p>
836
         *
837
         * <p> <b>Root node properties:</b>
838
         *  <ul>
839
         *   <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
840
         *   <li> numLayers : number of layers
841
         *   <li> LayerNames : name of the layers
842
         *  </ul>
843
         * </p>
844
         *
845
         * <p> <b>Layers: each first-level child: </b>
846
         *  <ul>
847
         *   <li> className : name of the class
848
         *   <li> <b> Capa Raster: </b>
849
         *   <ul>
850
         *    <li> name : name of the layer
851
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
852
         *   </ul>
853
         *   <li> <b> Capa Vectorial: </b>
854
         *   <ul>
855
         *    <li> file : the projection of this layer (only if it's defined)
856
         *    <li> driverName : name of the driver used to access to the file
857
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
858
         *   </ul>
859
         *   <li> <b> Collection of layers: </b>
860
         *   <ul>
861
         *    <li> that node
862
         *   </ul>
863
         *   <li> <b> Another kind of layer: </b>
864
         *   <ul>
865
         *    <li> name : the name of the layer
866
         *    <li> properties described in <code>FLyrDefault#getXMLEntity03()</code>
867
         *   </ul>
868
         *  </ul>
869
         * </p>
870
         *
871
         * @see FLyrDefault#getXMLEntity03()
872
         *
873
         * @param xml an <code>XMLEntity</code> with the information
874
         *
875
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error obtaining the object.
876
         */
877
        public void setXMLEntity03(XMLEntity xml) throws XMLException{
878
                super.setXMLEntity03(xml);
879
                int numLayers = xml.getIntProperty("numLayers");
880

    
881
                String[] s = xml.getStringArrayProperty("LayerNames");
882
                try {
883
                        for (int i = 0; i < numLayers; i++) {
884
                                FLayer layer = null;
885

    
886
                                String className = xml.getChild(i).getStringProperty("className");
887

    
888
                                if (className.equals(FLyrVect.class.getName())) {
889
                                        if (xml.getChild(i).contains("file")) {
890

    
891
                                                layer = LayerFactory.createLayer(s[i],
892
                                                                (VectorialFileDriver)LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName")),
893
                                                                new File(xml.getChild(i).getStringProperty("file")),
894
                                                                this.getMapContext().getViewPort().getProjection());
895

    
896
                                        } else if (true) {
897
                                                //TODO falta por implementar
898
                                        } else if (true) {
899
                                                //TODO falta por implementar
900
                                        }
901

    
902
                                        layer.setXMLEntity03(xml.getChild(i));
903
                                        // Comprobar que la proyecci?n es la misma que la de FMap
904
                                        // Si no lo es, es una capa que est? reproyectada al vuelo
905
                                        IProjection proj = layer.getProjection();
906
                                        if (proj != null)
907
                                                if (proj != fmap.getProjection())
908
                                                {
909
                                                        ICoordTrans ct = proj.getCT(fmap.getProjection());
910
                                                        layer.setCoordTrans(ct);
911
                                                        logger.info("coordTrans = " +
912
                                                                        proj.getAbrev() + " " +
913
                                                                        fmap.getProjection().getAbrev());
914
                                                }
915

    
916
                                } else {
917
                                        try {
918
                                                Class clazz = Class.forName(className);
919
                                                if (FLayers.class.isAssignableFrom(clazz)) {
920
                                                        layer = (FLayer) clazz.newInstance();
921
                                                        ((FLayers)layer).setMapContext(getMapContext());
922
                                                        ((FLayers)layer).setParentLayer(this);
923
                                                        //                                                        if (className.equals((FLayers.class.getName()))) {
924
                                                        //                                                        layer = new FLayers(getMapContext(),this);
925
                                                        layer.setXMLEntity(xml.getChild(i));
926
                                                } else {
927
                                                        // Capas Nuevas (externas)
928
                                                        Class clase = Class.forName(className);
929
                                                        layer = (FLayer) clase.newInstance();
930
                                                        layer.setName(s[i]);
931
                                                        layer.setXMLEntity03(xml.getChild(i));
932
                                                        layer.load();
933

    
934
                                                }
935
                                        } catch (Exception e) {
936
                                                //e.printStackTrace();
937
                                                throw new XMLException(e);
938
                                        }
939
                                }
940
                                this.addLayer(layer);
941
                        }
942
                }
943
                catch (DriverLoadException e) {
944
                        throw new XMLException(e);
945
                }
946

    
947
        }
948

    
949
        /**
950
         * <p>Inserts layers and properties to this collection of layers.</p>
951
         *
952
         * <p>This root node has the same properties that return <code>FlyrDefault#getXMLEntity()</code> adding:
953
         *  <ul>
954
         *          <li> <i>numLayers</i> : number of first-level layers of this collection
955
         *   <li> <i>LayerNames</i> : an array list with the name of the first-level layers of this collection
956
         *    <code>FLayer.getXMLEntity()</code>
957
         *  </ul>
958
         * </p>
959
         *
960
         * @see FLyrDefault#setXMLEntity()
961
         * @see FLyrDefault#getXMLEntity()
962
         * @see CopyOfFLayers#addLayerFromXML(XMLEntity, String)
963
         *
964
         * @param xml an <code>XMLEntity</code> with the information
965
         *
966
         * @throws com.iver.cit.gvsig.fmap.layers.XMLException if there is an error setting the object.
967
         */
968
        public void setXMLEntity(XMLEntity xml) throws XMLException{
969
                super.setXMLEntity(xml);
970
                //LoadLayerException loadLayerException=new LoadLayerException();
971
                int numLayers = xml.getIntProperty("numLayers");
972

    
973
                String[] s = xml.getStringArrayProperty("LayerNames");
974
                // try {
975
                fmap.clearErrors();
976
                for (int i = 0; i < numLayers; i++) {
977
                        try {
978
                                this.addLayerFromXML(xml.getChild(i),s[i]);
979
                        } catch (LoadLayerException e) {
980
                                //If a layer can't be loaded: continue with next layer
981
                                //throw new XMLException(e);
982
                        }
983

    
984

    
985
                }
986
        }
987

    
988
        /*
989
         * (non-Javadoc)
990
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, com.iver.cit.gvsig.fmap.layers.FBitSet)
991
         */
992
        public void process(FeatureVisitor visitor, FBitSet subset)
993
        throws ReadDriverException, ExpansionFileReadException, VisitorException {
994
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
995
                        FLayer layer = (FLayer) iter.next();
996

    
997
                        if (layer instanceof VectorialData) {
998
                                ((VectorialData) layer).process(visitor, subset);
999
                        }
1000
                }
1001
        }
1002

    
1003
        /*
1004
         * (non-Javadoc)
1005
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
1006
         */
1007
        public void process(FeatureVisitor visitor)
1008
        throws ReadDriverException, VisitorException {
1009
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1010
                        FLayer layer = (FLayer) iter.next();
1011

    
1012
                        if (layer instanceof FLayers){
1013
                                FLayers lyrs=(FLayers)layer;
1014
                                for (int i=0;i<lyrs.getLayersCount();i++){
1015
                                        FLayer lyr=lyrs.getLayer(i);
1016
                                        if (lyr.isActive()) {
1017
                                                if (lyr instanceof VectorialData) {
1018
                                                        ((VectorialData) lyr).process(visitor);
1019
                                                }
1020
                                        }
1021
                                }
1022
                        }
1023
                        if (layer.isActive()) {
1024
                                if (layer instanceof VectorialData) {
1025
                                        ((VectorialData) layer).process(visitor);
1026
                                }
1027
                        }
1028
                }
1029
        }
1030
        /*
1031
         * (non-Javadoc)
1032
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor, java.awt.geom.Rectangle2D)
1033
         */
1034
        public void process(FeatureVisitor visitor, Rectangle2D rect) throws ReadDriverException, ExpansionFileReadException, VisitorException {
1035
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1036
                        FLayer layer = (FLayer) iter.next();
1037

    
1038
                        if (layer.isActive()) {
1039
                                if (layer instanceof VectorialData) {
1040
                                        ((VectorialData) layer).process(visitor, rect);
1041
                                }
1042
                        }
1043
                }
1044

    
1045
        }
1046
        /*
1047
         * (non-Javadoc)
1048
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
1049
         */
1050
        public MapContext getMapContext() {
1051
                return fmap;
1052
        }
1053
        /*
1054
         * (non-Javadoc)
1055
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setCoordTrans(org.cresques.cts.ICoordTrans)
1056
         */
1057
        public void setCoordTrans(ICoordTrans ct) {
1058
                super.setCoordTrans(ct);
1059

    
1060
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1061
                        FLayer layer = (FLayer) iter.next();
1062
                        layer.setCoordTrans(ct);
1063
                }
1064
        }
1065
        /*
1066
         * (non-Javadoc)
1067
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
1068
         */
1069
        public void setAllActives(boolean active) {
1070
                FLayer lyr;
1071

    
1072
                for (int i = 0; i < layers.size(); i++) {
1073
                        lyr = ((FLayer) layers.get(i));
1074
                        lyr.setActive(active);
1075

    
1076
                        if (lyr instanceof LayerCollection) {
1077
                                ((LayerCollection) lyr).setAllActives(active);
1078
                        }
1079
                }
1080
        }
1081

    
1082
        /*
1083
         * (non-Javadoc)
1084
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
1085
         */
1086
        public FLayer[] getActives() {
1087
                ArrayList ret = new ArrayList();
1088
                LayersIterator it = new LayersIterator(this) {
1089

    
1090
                        public boolean evaluate(FLayer layer) {
1091
                                return layer.isActive();
1092
                        }
1093

    
1094
                };
1095

    
1096
                while (it.hasNext())
1097
                {
1098
                        ret.add(it.next());
1099
                }
1100
                return (FLayer[]) ret.toArray(new FLayer[0]);
1101
        }
1102

    
1103
        /*
1104
         * (non-Javadoc)
1105
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMinScale()
1106
         */
1107
        public double getMinScale() {
1108
                return -1; // La visibilidad o no la controla cada capa
1109
                // dentro de una colecci?n
1110
        }
1111
        /*
1112
         * (non-Javadoc)
1113
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMaxScale()
1114
         */
1115
        public double getMaxScale() {
1116
                return -1;
1117
        }
1118
        /*
1119
         * (non-Javadoc)
1120
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMinScale(double)
1121
         */
1122
        public void setMinScale(double minScale)
1123
        {
1124
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1125
                        FLayer lyr = (FLayer) iter.next();
1126
                        lyr.setMinScale(minScale);
1127
                }
1128
        }
1129
        /*
1130
         * (non-Javadoc)
1131
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setMaxScale(double)
1132
         */
1133
        public void setMaxScale(double maxScale)
1134
        {
1135
                for (Iterator iter = layers.iterator(); iter.hasNext();) {
1136
                        FLayer lyr = (FLayer) iter.next();
1137
                        lyr.setMinScale(maxScale);
1138
                }
1139
        }
1140
        /*
1141
         * (non-Javadoc)
1142
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setActive(boolean)
1143
         */
1144
        public void setActive(boolean b){
1145
                super.setActive(b);
1146
                for (int i=0;i<layers.size();i++){
1147
                        ((FLayer)layers.get(i)).setActive(b);
1148
                }
1149
        }
1150
        /* (non-Javadoc)
1151
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
1152
         */
1153
        public boolean addLayerListener(LayerListener o) {
1154
                for (int i = 0; i < layers.size(); i++)
1155
                        ((FLayer) layers.get(i)).addLayerListener(o);
1156
                return true;
1157
        }
1158
        /*
1159
         * (non-Javadoc)
1160
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#getInfo(java.awt.Point, double, com.iver.utiles.swing.threads.Cancellable)
1161
         */
1162
        public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws ReadDriverException, VisitorException, LoadLayerException {
1163
                int i;
1164
                Vector items = new Vector();
1165
                FLayer layer;
1166
                XMLItem[] aux;
1167
                for (i = 0; i < this.layers.size(); i++){
1168
                        layer = (FLayer)layers.get(i);
1169
                        if (layer instanceof InfoByPoint){
1170
                                InfoByPoint queryable_layer = (InfoByPoint) layer;
1171
                                aux = queryable_layer.getInfo(p, tolerance, null);
1172
                                if (!(queryable_layer instanceof FLayers)){
1173
                                        for(int j = 0; j < aux.length; j++){
1174
                                                items.add(aux[j]);
1175
                                        }
1176
                                }
1177
                        }
1178
                }
1179
                return (XMLItem[])items.toArray(new XMLItem[0]);
1180

    
1181
                //        for (i = 0; i < this.layers.size(); i++){
1182
                //        FLayer laCapa = (FLayer) layers.get(i);
1183
                //        if (laCapa instanceof FLyrVect){
1184
                //        }
1185
                //        else if (laCapa instanceof RasterOperations) {
1186
                //        try {
1187
                //        RasterOperations layer = (RasterOperations) laCapa;
1188
                //        sb.append(layer.getInfo(p, tolerance));
1189
                //        } catch (DriverException e) {
1190
                //        e.printStackTrace();
1191
                //        }
1192
                //        }
1193
                //        else if (laCapa instanceof InfoByPoint) {
1194
                //        try {
1195
                //        InfoByPoint layer = (InfoByPoint) laCapa;
1196
                //        sb.append(layer.getInfo(p, tolerance));
1197
                //        } catch (DriverException e) {
1198
                //        e.printStackTrace();
1199
                //        }
1200
                //        }
1201
                //        }
1202

    
1203
        }
1204

    
1205
        /*
1206
         * (non-Javadoc)
1207
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getTocImageIcon()
1208
         */
1209
        public ImageIcon getTocImageIcon() {
1210
                return new ImageIcon(MapControl.class.getResource("images/layerGroup.png"));
1211
        }
1212

    
1213
        /**
1214
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers. And
1215
         *  returns <code>true</code> if there hasn't been errors.</p>
1216
         *
1217
         * @see CopyOfFLayers#addLayerFromXML(XMLEntity, String)
1218
         *
1219
         * @param xml tree-node structure with information about layers
1220
         * @param name name of the layer to add
1221
         * @return <code>true</code> if there were no errors adding the layer, <code>false</code> otherwise
1222
         *
1223
         * @throws LoadLayerException if fails loading this layer.
1224
         */
1225
        public boolean addLayerFromXMLEntity(XMLEntity xml, String name) throws LoadLayerException {
1226
                fmap.clearErrors();
1227
                this.addLayerFromXML(xml,name);
1228

    
1229
                return (fmap.getLayersError().size() == 0);
1230

    
1231
        }
1232

    
1233
        /**
1234
         * <p>Adds the layer with the information in an XML entity and the specified name, to this collection of layers.</p>
1235
         *
1236
         * <p>This method really executes the addition, considering the kind of layer (<code>FLyrVect</code>,
1237
         *  <code>FLyrAnnotation</code>, <code>FLyrRaster</code>, a collection of layers (<code>FLayers</code>),
1238
         *  or another kind of layer (<code>FLayer</code>)), and the driver in the layer.</p>
1239
         *
1240
         * @param xml tree-node structure with information about layers
1241
         * @param name name of the layer to add
1242
         *
1243
         * @throws LoadLayerException if fails loading this layer.
1244
         */
1245
        private void addLayerFromXML(XMLEntity xml, String name) throws LoadLayerException {
1246
                FLayer layer = null;
1247

    
1248
                try {
1249
                        if (name == null) name = xml.getName();
1250

    
1251

    
1252
                        String className = xml.getStringProperty("className");
1253
                        //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,
1254
                        //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1255
                        if (className.equals(FLyrVect.class.getName()) || className.equals(FLyrAnnotation.class.getName())) {
1256
                                String type = xml.getStringProperty("type");
1257
                                if ("vectorial".equals(type)){
1258
                                        //String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
1259
                                        IProjection proj = null;
1260
                                        if (xml.contains("proj")) {
1261
                                                proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
1262
                                        } else {
1263
                                                proj = this.getMapContext().getViewPort().getProjection();
1264
                                        }
1265
                                        if (xml.contains("file")) {
1266
                                                Driver d;
1267
                                                try {
1268
                                                        d = LayerFactory.getDM().getDriver(xml.getStringProperty("driverName"));
1269
                                                } catch (DriverLoadException e1) {
1270
                                                        throw new DriverLayerException(name,e1);
1271
                                                }
1272
                                                layer = LayerFactory.createLayer(name, (VectorialFileDriver) d,
1273
                                                                new File(pathGenerator.getAbsolutePath((String)xml.getStringProperty("file"))),
1274
                                                                proj);
1275

    
1276

    
1277

    
1278
                                        }
1279
                                        if (xml.contains("db")) {
1280

    
1281
                                                String driverName = xml.getStringProperty("db");
1282
                                                IVectorialDatabaseDriver driver;
1283
                                                try {
1284
                                                        driver = (IVectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
1285
                                                        //Hay que separar la carga de los datos del XMLEntity del load.
1286
                                                        if (xml.firstIndexOfChild("className", "com.iver.cit.gvsig.fmap.layers.SelectionSupport")==2)
1287
                                                                driver.setXMLEntity(xml.getChild(3));
1288
                                                        else
1289
                                                                driver.setXMLEntity(xml.getChild(2));
1290

    
1291
                                                        //                                                boolean loadOk = false;
1292
                                                        try {
1293
                                                                ((DefaultJDBCDriver)driver).load();
1294
                                                                //                                                        loadOk = (((DefaultJDBCDriver)driver).getConnection() != null);
1295
                                                                layer = LayerFactory.createDBLayer(driver, name, proj);
1296
                                                                layer.setAvailable((((DefaultJDBCDriver)driver).getConnection() != null));
1297
                                                        } catch (ReadDriverException e) {
1298
                                                                layer = LayerFactory.createDBLayer(driver, name, proj);
1299
                                                                layer.addError(e);
1300
                                                                layer.setAvailable(false);
1301
                                                        }
1302
                                                } catch (DriverLoadException e) {
1303
                                                        throw new DriverLayerException(name,e);
1304
                                                } catch (XMLException e) {
1305
                                                        throw new DriverLayerException(name,e);
1306
                                                        //                                                } catch (ReadDriverException e) {
1307
                                                        //                                                throw new DriverLayerException(name,e);
1308
                                                }
1309

    
1310
                                        }
1311
                                        // Clases con algun driver gen?rico creado por otro
1312
                                        // programador
1313
                                        if (xml.contains("other")) {
1314
                                                int classChild = 2;
1315
                                                if(xml.contains("isLabeled") && xml.getBooleanProperty("isLabeled")){
1316
                                                        classChild++;
1317
                                                }
1318

    
1319
                                                String driverName = xml.getStringProperty("other");
1320
                                                VectorialDriver driver = null;
1321
                                                try {
1322
                                                        driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
1323
                                                } catch (DriverLoadException e) {
1324
                                                        // Si no existe ese driver, no pasa nada.
1325
                                                        // Puede que el desarrollador no quiera que
1326
                                                        // aparezca en el cuadro de di?logo y ha metido
1327
                                                        // el jar con sus clases en nuestro directorio lib.
1328
                                                        // Intentamos cargar esa clase "a pelo".
1329
                                                        if (xml.getChild(classChild).contains("className"))
1330
                                                        {
1331
                                                                String className2 = xml.getChild(classChild).getStringProperty("className");
1332
                                                                try {
1333
                                                                        driver = (VectorialDriver) Class.forName(className2).newInstance();
1334
                                                                } catch (Exception e1) {
1335
                                                                        throw new DriverLayerException(name,e);
1336
                                                                }
1337
                                                        }
1338
                                                } catch (NullPointerException npe) {
1339
                                                        // Si no existe ese driver, no pasa nada.
1340
                                                        // Puede que el desarrollador no quiera que
1341
                                                        // aparezca en el cuadro de di?logo y ha metido
1342
                                                        // el jar con sus clases en nuestro directorio lib.
1343
                                                        // Intentamos cargar esa clase "a pelo".
1344
                                                        if (xml.getChild(2).contains("className"))
1345
                                                        {
1346
                                                                String className2 = xml.getChild(classChild).getStringProperty("className");
1347
                                                                try {
1348
                                                                        driver = (VectorialDriver) Class.forName(className2).newInstance();
1349
                                                                } catch (Exception e1) {
1350
                                                                        throw new DriverLayerException(name,e1);
1351
                                                                }
1352
                                                        }
1353
                                                }
1354
                                                if (driver instanceof IPersistence)
1355
                                                {
1356
                                                        IPersistence persist = (IPersistence) driver;
1357
                                                        persist.setXMLEntity(xml.getChild(classChild));
1358
                                                }
1359
                                                layer = LayerFactory.createLayer(name, driver, proj);
1360
                                        }
1361

    
1362
                                }
1363

    
1364
                                //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,
1365
                                //ya que los drivers de una FLyrAnnotation no sabemos cual es puede ser cualquier Driver Vectorial.
1366
                                if (className.equals(FLyrAnnotation.class.getName())){
1367
                                        layer=FLyrAnnotation.createLayerFromVect((FLyrVect)layer);
1368
                                }
1369

    
1370

    
1371
                                layer.setXMLEntity(xml);
1372

    
1373
                        } else {
1374
                                Class clase = LayerFactory.getLayerClassForLayerClassName(className);
1375
                                layer = (FLayer) clase.newInstance();
1376
                                if (clase.isAssignableFrom(FLayers.class)) {
1377
                                        ((FLayers)layer).setMapContext(getMapContext());
1378
                                        ((FLayers)layer).setParentLayer(this);
1379
                                        //                                layer = new FLayers(getMapContext(),this);
1380
                                        layer.setXMLEntity(xml);
1381
                                } else {
1382
                                        // Capas Nuevas (externas)
1383
                                        layer.setName(name);
1384
                                        layer.setXMLEntity(xml);
1385
                                        layer.load();
1386
                                }
1387
                        }
1388
                        this.addLayer(layer);
1389
                        logger.debug("layer: "+ layer.getName() +" loaded");
1390
                        // Comprobar que la proyecci?n es la misma que la de FMap
1391
                        // Si no lo es, es una capa que est? reproyectada al vuelo
1392
                        IProjection proj = layer.getProjection();
1393
                        if ((proj != null))
1394
                                if (!proj.getFullCode().equals(getMapContext().getProjection().getFullCode()))
1395
                                {
1396
                                        ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1397
                                        // TODO: REVISAR CON LUIS
1398
                                        // Se lo fijamos a todas, luego cada una que se reproyecte
1399
                                        // si puede, o que no haga nada
1400

    
1401
                                        layer.setCoordTrans(ct);
1402
                                }
1403
                } catch (XMLException e) {
1404
                        fmap.addLayerError(xml.getStringProperty("name"));
1405
                        throw new LoadLayerException(name,e);
1406
                } catch (ClassNotFoundException e) {
1407
                        fmap.addLayerError(xml.getStringProperty("name"));
1408
                        throw new LoadLayerException(name,e);
1409
                } catch (InstantiationException e) {
1410
                        fmap.addLayerError(xml.getStringProperty("name"));
1411
                        throw new LoadLayerException(name,e);
1412
                } catch (IllegalAccessException e) {
1413
                        fmap.addLayerError(xml.getStringProperty("name"));
1414
                        throw new LoadLayerException(name,e);
1415
                } catch (ReadDriverException e) {
1416
                        fmap.addLayerError(xml.getStringProperty("name"));
1417
                        throw new LoadLayerException(name,e);
1418
                } catch (LoadLayerException e){
1419
                        fmap.addLayerError(xml.getStringProperty("name"));
1420
                        throw e;
1421
                }
1422
        }
1423

    
1424
        /**
1425
         * <p>Sets the <code>MapContext</code> that contains this layer node.</p>
1426
         *
1427
         * @param mapContext the <code>MapContext</code> that contains this layer node
1428
         */
1429
        public void setMapContext(MapContext mapContext) {
1430
                this.fmap = mapContext;
1431
        }
1432

    
1433
        /**
1434
         * <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
1435
         *  and loads the layer. Then, adds the layer to this collection of layers, and if there is a projection defined,
1436
         *  inserts the transformation coordinates to the layer.</p>
1437
         *
1438
         * <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>
1439
         *
1440
         * @param xml tree-node structure with information about layers
1441
         * @param name name of the layer to add
1442
         */
1443
        private void addLayerFromXMLNew(XMLEntity xml, String name) {
1444
                FLayer layer = null;
1445

    
1446

    
1447
                try {
1448
                        String className = xml.getStringProperty("className");
1449
                        Class clazz = Class.forName(className);
1450
                        if (FLayers.class.isAssignableFrom(clazz)) {
1451
                                layer = (FLayer) clazz.newInstance();
1452
                                ((FLayers)layer).setMapContext(getMapContext());
1453
                                ((FLayers)layer).setParentLayer(this);
1454
                                //                if (className.equals((FLayers.class.getName()))){
1455
                                //                        layer = new FLayers(getMapContext(),this);
1456
                        } else {
1457
                                //                        Por compatibilidad
1458
                                if (className.equals(FLyrVect.class.getName())) {
1459
                                        if (xml.contains("file")) {
1460
                                                layer = new FLayerFileVectorial();
1461
                                        } else if (xml.contains("db")) {
1462
                                                try {
1463
                                                        layer = (FLayer)((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create("com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial");
1464
                                                } catch (Exception e) {
1465
                                                        throw new XMLException(new Exception("No se tiene registrada la capa de tipo JDBC"));
1466
                                                }
1467
                                                //className = FLayerJDBCVectorial.class.getName();
1468
                                        } else if (xml.contains("other")){
1469
                                                layer = new FLayerGenericVectorial();
1470
                                        } else {
1471
                                                throw new XMLException(new Exception("Capa vectorial de tipo no reconocido"));
1472
                                        }
1473
                                        //                                Fin por compatibilidad
1474
                                } else {
1475
                                        try {
1476
                                                layer = (FLayer)(((ExtensionPoint)ExtensionPointsSingleton.getInstance().get("Layers")).create(className));
1477
                                        } catch (Exception e) {
1478
                                                //puende que no este registrada como punto de extension
1479
                                                Class clase = Class.forName(className);
1480
                                                layer = (FLayer) clase.newInstance();
1481
                                                // FIXME: Hacemos algo aqui o dejamos que suba el error?
1482
                                        }
1483
                                }
1484

    
1485
                        }
1486
                        layer.setXMLEntity(xml);
1487
                        if (name != null) layer.setName(name);
1488
                        layer.load();
1489

    
1490
                        this.addLayer(layer);
1491
                        logger.debug("layer: "+ layer.getName() +" loaded");
1492
                        // Comprobar que la proyecci?n es la misma que la de FMap
1493
                        // Si no lo es, es una capa que est? reproyectada al vuelo
1494
                        IProjection proj = layer.getProjection();
1495
                        if ((proj != null))
1496
                                if (proj != getMapContext().getProjection())
1497
                                {
1498
                                        ICoordTrans ct = proj.getCT(getMapContext().getProjection());
1499
                                        // TODO: REVISAR CON LUIS
1500
                                        // Se lo fijamos a todas, luego cada una que se reproyecte
1501
                                        // si puede, o que no haga nada
1502
                                        layer.setCoordTrans(ct);
1503

    
1504
                                }
1505
                }catch (Exception e) {
1506
                        fmap.addLayerError(xml.getStringProperty("name"));
1507
                        logger.debug(Messages.getString("could_not_load_layer")+": "+xml.getStringProperty("name") + ".\n"
1508
                                        +Messages.getString("reason")+":", e);
1509
                }
1510
        }
1511

    
1512
        public void beginDraw(Graphics2D g, ViewPort viewPort) {
1513
                LayerDrawEvent beforeEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_BEFORE_DRAW);
1514
                fmap.fireLayerDrawingEvent(beforeEvent);
1515
        }
1516

    
1517
        public void endDraw(Graphics2D g, ViewPort viewPort) {
1518
                LayerDrawEvent afterEvent = new LayerDrawEvent(this, g, viewPort, LayerDrawEvent.LAYER_AFTER_DRAW);
1519
                fmap.fireLayerDrawingEvent(afterEvent);
1520
        }
1521
}