Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / FLyrDefault.java @ 42877

History | View | Annotate | Download (30.8 KB)

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

    
26
import java.awt.Image;
27
import java.awt.geom.Point2D;
28
import java.net.URI;
29
import java.util.HashSet;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34

    
35
import org.cresques.cts.ICoordTrans;
36
import org.cresques.cts.IProjection;
37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.geom.primitive.Envelope;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.MapContextLocator;
42
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
43
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException;
44
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
45
import org.gvsig.fmap.mapcontext.impl.DefaultMapContextManager;
46
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
47
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
48
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
49
import org.gvsig.metadata.MetadataContainer;
50
import org.gvsig.metadata.MetadataLocator;
51
import org.gvsig.metadata.MetadataManager;
52
import org.gvsig.metadata.exceptions.MetadataException;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.dispose.impl.AbstractDisposable;
55
import org.gvsig.tools.dynobject.DynClass;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.dynobject.DynStruct;
58
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
59
import org.gvsig.tools.dynobject.exception.DynMethodException;
60
import org.gvsig.tools.exception.BaseException;
61
import org.gvsig.tools.persistence.PersistenceManager;
62
import org.gvsig.tools.persistence.Persistent;
63
import org.gvsig.tools.persistence.PersistentState;
64
import org.gvsig.tools.persistence.exception.PersistenceException;
65
import org.gvsig.tools.util.Callable;
66
import org.slf4j.LoggerFactory;
67

    
68

    
69

    
70
/**
71
 * <p>Implementation of the common characteristics of all layers: visibility, activation, name, ...</p>
72
 *
73
 * <p>Represents the definition of a basic layer, implementing {@link FLayer FLayer}, and new functionality:
74
 * <ul>
75
 *  <li>Supports transparency.
76
 *  <li>Notification of evens produced using this layer.
77
 *  <li>Can have internal virtual layers.
78
 *  <li>Can have a text layer.
79
 *  <li>Supports an strategy for visit its geometries.
80
 *  <li>Can have an image in the <i>TOC (table of contents)</i> associated to the state of this layer.
81
 * </ul>
82
 * </p>
83
 *
84
 * <p>Each graphical layer will inherit from this class and adapt to its particular logic and model according
85
 *  its nature.</p>
86
 *
87
 * @see FLayer
88
 * @see FLayerStatus
89
 */
90
public abstract class FLyrDefault extends AbstractDisposable implements FLayer, FLayerHidesArea,
91
                LayerListener {
92
        /**
93
         * Useful for debug the problems during the implementation.
94
         */
95
        final static private org.slf4j.Logger logger = LoggerFactory.getLogger(FLyrDefault.class);
96

    
97
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
98

    
99
        /**
100
         * Flag to set the layer as a temporary layer.
101
         */
102
        private boolean temporary;
103

    
104
        /**
105
         * Path to the upper layer which this layer belongs.
106
         *
107
         * @see #getParentLayer()
108
         * @see #setParentLayer(FLayers)
109
         */
110
        private FLayers parentLayer = null;
111

    
112
        /**
113
         * Transparency level of this layer in the range 0-255. By default 255.
114
         * 0   --> Transparent
115
         * 255 --> Opaque
116
         *
117
         * @see #getTransparency()
118
         * @see #setTransparency(int)
119
         */
120
        private int transparency = 255;
121

    
122
        /**
123
         * Coordinate transformation.
124
         *
125
         * @see #getCoordTrans()
126
         * @see #setCoordTrans(ICoordTrans)
127
         */
128
        private ICoordTrans ct;
129

    
130
        /**
131
         * Minimum scale, >= 0 or -1 if not defined. By default -1.
132
         *
133
         * @see #getMinScale()
134
         * @see #setMinScale(double)
135
         */
136
        private double minScale = -1; // -1 indica que no se usa
137

    
138
        /**
139
         * Maximum scale, >= 0 or -1 if not defined. By default -1.
140
         *
141
         * @see #getMaxScale()
142
         * @see #setMaxScale(double)
143
         */
144
        private double maxScale = -1;
145
        //        private boolean isInTOC = true;
146

    
147
        /**
148
         * Array list with all listeners registered to this layer.
149
         *
150
         * @see #getLayerListeners()
151
         * @see #removeLayerListener(LayerListener)
152
         * @see #callEditionChanged(LayerEvent)
153
         */
154
        protected Set<LayerListener> layerListeners = new HashSet<>();
155

    
156
        //by default, all is active, visible and avalaible
157
        /**
158
         * Status of this layer.
159
         *
160
         * @see #getFLayerStatus()
161
         * @see #setFLayerStatus(FLayerStatus)
162
         * @see #isActive()
163
         * @see #setActive(boolean)
164
         * @see #isVisible()
165
         * @see #setVisible(boolean)
166
         * @see #visibleRequired()
167
         * @see #isEditing()
168
         * @see #setEditing(boolean)
169
         * @see #isInTOC()
170
         * @see #isCachingDrawnLayers()
171
         * @see #setCachingDrawnLayers(boolean)
172
         * @see #isDirty()
173
         * @see #setDirty(boolean)
174
         * @see #isAvailable()
175
         * @see #setAvailable(boolean)
176
         * @see #isOk()
177
         * @see #isWritable()
178
         * @see #getNumErrors()
179
         * @see #getError(int)
180
         * @see #getErrors()
181
         * @see #addError(BaseException)
182
         */
183
        private FLayerStatus status = new FLayerStatus();
184
        /**
185
         * Image drawn shown in the TOC according the status of this layer.
186
         *
187
         * @see #getTocStatusImage()
188
         * @see #setTocStatusImage(Image)
189
         */
190
        private Image tocStatusImage;
191

    
192
        protected MetadataContainer metadataContainer;
193

    
194
        /**
195
         * Draw version of the context. It's used for know when de componend has
196
         * changed any visualization property
197
         *
198
         *  @see getDrawVersion
199
         *  @see updateDrawVersion
200
         */
201
        private long drawVersion= 0L;
202

    
203

    
204
        private ExtendedPropertiesHelper properties = new ExtendedPropertiesHelper();
205

    
206
        public FLyrDefault(MetadataContainer metadataContainer) {
207
                this.metadataContainer = metadataContainer;
208
        }
209

    
210
        public FLyrDefault() {
211
                this(MetadataLocator
212
                        .getMetadataManager()
213
                                .createMetadataContainer(FLayer.METADATA_DEFINITION_NAME)
214
                );
215
        }
216

    
217

    
218
        /*
219
         * (non-Javadoc)
220
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperty(java.lang.Object)
221
         */
222
        public Object getProperty(Object key) {
223
                return properties.getProperty(key);
224
        }
225
        /*
226
         * (non-Javadoc)
227
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setProperty(java.lang.Object, java.lang.Object)
228
         */
229
        public void setProperty(Object key, Object val) {
230
                properties.setProperty(key, val);
231
        }
232
        /*
233
         * (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getExtendedProperties()
235
         */
236
        public Map getExtendedProperties() {
237
                return properties.getExtendedProperties();
238
        }
239
        /*
240
         * (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setActive(boolean)
242
         */
243
        public void setActive(boolean selected) {
244
                status.active = selected;
245
                callActivationChanged(LayerEvent.createActivationChangedEvent(this,
246
                "active"));
247
        }
248

    
249
        /*
250
         * (non-Javadoc)
251
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isActive()
252
         */
253
        public boolean isActive() {
254
                return status.active;
255
        }
256

    
257
        /*
258
         * (non-Javadoc)
259
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setName(java.lang.String)
260
         */
261
        public void setName(String name) {
262
                this.metadataContainer.setDynValue(METADATA_NAME, name);
263
                callNameChanged(LayerEvent.createNameChangedEvent(this, "name"));
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getName()
269
         */
270
        public String getName() {
271
            String name = "(unknow)";
272
            try {
273
                name = (String) this.metadataContainer.getDynValue(METADATA_NAME);
274
            } catch( Throwable th) {
275
                logger.warn("Can't retrive the layer name.");
276
            }
277
            return name;
278
        }
279

    
280
        /*
281
         * (non-Javadoc)
282
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
283
         */
284
    public void load() throws LoadLayerException {
285
        MetadataManager manager = MetadataLocator.getMetadataManager();
286
        try {
287
            manager.loadMetadata(this);
288
        } catch (MetadataException e) {
289
            throw new LoadLayerException("Can't load metadata.", e);
290
        }
291
        DefaultMapContextManager mcmanager = (DefaultMapContextManager) MapContextLocator.getMapContextManager();
292
        mcmanager.notifyLoadLayer(this);
293
    }
294

    
295
        /*
296
         * (non-Javadoc)
297
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setVisible(boolean)
298
         */
299
        public void setVisible(boolean visibility) {
300
                if (status.visible != visibility){
301
                        status.visible = visibility;
302
                        this.updateDrawVersion();
303

    
304
                        //                        if (this.getMapContext() != null){
305
                        //                                this.getMapContext().clearAllCachingImageDrawnLayers();
306
                        //                        }
307
                        callVisibilityChanged(LayerEvent.createVisibilityChangedEvent(this,
308
                        "visible"));
309
                }
310
        }
311

    
312

    
313
        /*
314
         * (non-Javadoc)
315
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isVisible()
316
         */
317
        public boolean isVisible() {
318
                return status.visible && status.available;
319
        }
320

    
321
        /*
322
         * (non-Javadoc)
323
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getParentLayer()
324
         */
325
        public FLayers getParentLayer() {
326
                return parentLayer;
327
        }
328

    
329

    
330
        /*
331
         * (non-Javadoc)
332
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setParentLayer(com.iver.cit.gvsig.fmap.layers.FLayers)
333
         */
334
        public void setParentLayer(FLayers root) {
335
                if (this.parentLayer != root){
336
                        this.parentLayer = root;
337
                        this.updateDrawVersion();
338
                }
339
        }
340

    
341
        /**
342
         * <p>Inserts the projection to this layer.</p>
343
         *
344
         * @param proj information about the new projection
345
         *
346
         * @see #isReprojectable()
347
         * @see #reProject(MapControl)
348
         */
349
        public void setProjection(IProjection proj) {
350
                IProjection curProj = this.getProjection();
351
                if (curProj == proj) {
352
                        return;
353
                }
354
                if (curProj != null && curProj.equals(proj)){
355
                        return;
356
                }
357
                this.updateDrawVersion();
358
                this.metadataContainer.setDynValue(METADATA_CRS, proj);
359
                // Comprobar que la proyecci?n es la misma que la de FMap
360
                // Si no lo es, es una capa que est? reproyectada al vuelo
361
                if ((proj != null) && (getMapContext() != null)) {
362
                        if (proj != getMapContext().getProjection()) {
363
                                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
364
                                setCoordTrans(ct);
365
                                logger.debug("Cambio proyecci?n: FMap con "
366
                                                + getMapContext().getProjection().getAbrev() + " y capa "
367
                                                + getName() + " con " + proj.getAbrev());
368
                        }
369
                }
370
        }
371

    
372
        /*
373
         * (non-Javadoc)
374
         * @see org.cresques.geo.Projected#getProjection()
375
         */
376
        public IProjection getProjection() {
377
                return (IProjection) this.metadataContainer.getDynValue(METADATA_CRS);
378
        }
379

    
380
        /**
381
         * <p>Changes the projection of this layer.</p>
382
         * <p>This method will be overloaded in each kind of layer, according its specific nature.</p>
383
         *
384
         * @param mapC <code>MapControl</code> instance that will reproject this layer
385
         *
386
         * @return <code>true<code> if the layer has been created calling {@link FLayers#addLayer(FLayer) FLayers#addLayer}. But returns <code>false</code>
387
         *  if the load control logic of this layer is in the reprojection method
388
         *
389
         * @see #isReprojectable()
390
         * @see #setProjection(IProjection)
391
         */
392
        public void reProject(ICoordTrans arg0) {
393
        }
394

    
395
        /**
396
         * Returns the transparency level of this layer, in the range 0-255 .
397
         *
398
         * @return the transparency level
399
         *
400
         * @see #setTransparency(int)
401
         */
402
        public int getTransparency() {
403
                return transparency;
404
        }
405

    
406
        /**
407
         * Inserts the transparency level for this layer, the range allowed is 0-255 .
408
         *
409
         * @param trans the transparency level
410
         *
411
         * @see #getTransparency()
412
         */
413
        public void setTransparency(int trans) {
414
                if (this.transparency != trans){
415
                        transparency = trans;
416
                        this.updateDrawVersion();
417
                }
418
        }
419

    
420

    
421
        /*
422
         * (non-Javadoc)
423
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMapContext()
424
         */
425
        public MapContext getMapContext() {
426
                if (getParentLayer() != null) {
427
                        return getParentLayer().getMapContext();
428
                } else {
429
                        return null;
430
                }
431
        }
432

    
433
        /*
434
         * (non-Javadoc)
435
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
436
         */
437
        public boolean addLayerListener(LayerListener o) {
438
            if( o == null ) {
439
                return true;
440
            }
441
            return layerListeners.add(o);
442
        }
443
        /*
444
         * (non-Javadoc)
445
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getLayerListeners()
446
         */
447
        public LayerListener[] getLayerListeners() {
448
                return (LayerListener[])layerListeners.toArray(new LayerListener[0]);
449
        }
450
        /*
451
         * (non-Javadoc)
452
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#removeLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
453
         */
454
        public boolean removeLayerListener(LayerListener o) {
455
                return layerListeners.remove(o);
456
        }
457
        /**
458
         *
459
         */
460
        private void callDrawValueChanged(LayerEvent e) {
461
           for (LayerListener listener : this.layerListeners) {
462
               try {
463
                   listener.drawValueChanged(e);
464
               } catch (Exception ex) {
465
                   logger.warn("Error calling listener '"+listener.toString()+"'.", ex);
466
               }
467
           }
468
        }
469
        /**
470
         * Called by the method {@linkplain #setName(String)}. Notifies all listeners associated to this layer,
471
         *  that its name has changed.
472
         *
473
         * @param e a layer event with the name of the property that has changed
474
         *
475
         * @see #setName(String)
476
         */
477
        private void callNameChanged(LayerEvent e) {
478
           for (LayerListener listener : this.layerListeners) {
479
               try {
480
                   listener.nameChanged(e);
481
               } catch (Exception ex) {
482
                   logger.warn("Error calling listener '"+listener.toString()+"'.", ex);
483
               }
484
           }
485
        }
486

    
487
        /**
488
         * Called by the method {@linkplain #setVisible(boolean)}. Notifies all listeners associated to this layer,
489
         *  that its visibility has changed.
490
         *
491
         * @param e a layer event with the name of the property that has changed
492
         *
493
         * @see #setVisible(boolean)
494
         */
495
        private void callVisibilityChanged(LayerEvent e) {
496
           for (LayerListener listener : this.layerListeners) {
497
               try {
498
                   listener.visibilityChanged(e);
499
               } catch (Exception ex) {
500
                   logger.warn("Error calling listener '"+listener.toString()+"'.", ex);
501
               }
502
           }
503
        }
504

    
505
        /**
506
         * Called by the method {@linkplain #setActive(boolean)}. Notifies all listeners associated to this layer,
507
         *  that its active state has changed.
508
         *
509
         * @param e a layer event with the name of the property that has changed
510
         *
511
         * @see #setActive(boolean)
512
         */
513
       private void callActivationChanged(LayerEvent e) {
514
           for (LayerListener listener : this.layerListeners) {
515
               try {
516
                   listener.activationChanged(e);
517
               } catch (Exception ex) {
518
                   logger.warn("Error calling listener '"+listener.toString()+"'.", ex);
519
               }
520
           }
521
       }
522

    
523
        /**
524
         * Returns the virtual layers associated to this layer.
525
         *
526
         * @return a node with the layers
527
         *
528
         * @see #setVirtualLayers(FLayers)
529
         */
530
        //        public FLayers getVirtualLayers() {
531
        //                return virtualLayers;
532
        //        }
533

    
534
        /**
535
         * Inserts virtual layers to this layer.
536
         *
537
         * @param virtualLayers a node with the layers
538
         *
539
         * @see #getVirtualLayers()
540
         */
541
        //        public void setVirtualLayers(FLayers virtualLayers) {
542
        //                this.virtualLayers = virtualLayers;
543
        //        }
544

    
545
        /**
546
         * Sets transformation coordinates for this layer.
547
         *
548
         * @param ct an object that implements the <code>ICoordTrans</code> interface, and with the transformation coordinates
549
         *
550
         * @see #getCoordTrans()
551
         */
552
        public void setCoordTrans(ICoordTrans ct) {
553
                if (this.ct == ct){
554
                        return;
555
                }
556
                if (this.ct != null && this.ct.equals(ct)){
557
                        return;
558
                }
559
                this.ct = ct;
560
                this.updateDrawVersion();
561
        }
562

    
563
        /**
564
         * Returns the transformation coordinates of this layer.
565
         *
566
         * @return an object that implements the <code>ICoordTrans</code> interface, and with the transformation coordinates
567
         *
568
         * @see #setCoordTrans(ICoordTrans)
569
         */
570
        public ICoordTrans getCoordTrans() {
571
                return ct;
572
        }
573

    
574
        /**
575
         * <p>Method called by {@link FLayers FLayers} to notify this layer that is going to be added.
576
         *  This previous notification is useful for the layers that need do something before being added. For
577
         *  example, the raster needs reopen a file that could have been closed recently.</p>
578
         */
579
        public void wakeUp() throws LoadLayerException {
580
        }
581
        /*
582
         * (non-Javadoc)
583
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMinScale()
584
         */
585
        public double getMinScale() {
586
                return minScale;
587
        }
588

    
589
        /*
590
         * (non-Javadoc)
591
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMaxScale()
592
         */
593
        public double getMaxScale() {
594
                return maxScale;
595
        }
596
        /*
597
         * (non-Javadoc)
598
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setMinScale(double)
599
         */
600
        public void setMinScale(double minScale) {
601
                if (this.minScale != minScale){
602
                        this.minScale = minScale;
603
                        this.updateDrawVersion();
604
                }
605
        }
606
        /*
607
         * (non-Javadoc)
608
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setMaxScale(double)
609
         */
610
        public void setMaxScale(double maxScale) {
611
                if (this.maxScale != maxScale){
612
                        this.maxScale = maxScale;
613
                        this.updateDrawVersion();
614
                }
615
        }
616
        /*
617
         * (non-Javadoc)
618
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isWithinScale(double)
619
         */
620
        public boolean isWithinScale(double scale) {
621

    
622
                boolean bVisible = true;
623
                if (getMinScale() != -1) {
624
                        if (scale < getMinScale()){
625
                                bVisible = false;
626
                        }
627
                }
628
                if (getMaxScale() != -1) {
629
                        if (scale > getMaxScale()) {
630
                                bVisible = false;
631
                        }
632
                }
633

    
634
                return bVisible;
635
        }
636
        /*
637
         * (non-Javadoc)
638
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setEditing(boolean)
639
         */
640
        public void setEditing(boolean b) throws StartEditionLayerException {
641
                status.editing = b;
642
        }
643
        /**
644
         * Called by some version of the method {@linkplain #setEditing(boolean)} overwritten. Notifies
645
         *  all listeners associated to this layer, that its edition state has changed.
646
         *
647
         * @param e a layer event with the name of the property that has changed
648
         *
649
         * @see #setEditing(boolean)
650
         */
651
        protected void callEditionChanged(LayerEvent e) {
652
           for (LayerListener listener : this.layerListeners) {
653
               try {
654
                   listener.editionChanged(e);
655
               } catch (Exception ex) {
656
                   logger.warn("Error calling listener '"+listener.toString()+"'.", ex);
657
               }
658
           }
659
        }
660

    
661
        /*
662
         * (non-Javadoc)
663
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isEditing()
664
         */
665
        public boolean isEditing() {
666
                return status.editing;
667
        }
668
        /*
669
         * (non-Javadoc)
670
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocImageIcon()
671
         */
672
        public String getTocImageIcon() {
673
                return "layer-icon";
674
        }
675

    
676
        /*
677
         * (non-Javadoc)
678
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isInTOC()
679
         */
680
        public boolean isInTOC() {
681
                return status.inTOC;
682
        }
683
        /*
684
         * (non-Javadoc)
685
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setInTOC(boolean)
686
         */
687
        public void setInTOC(boolean b) {
688
                status.inTOC=b;
689
        }
690
        /*
691
         * (non-Javadoc)
692
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isAvailable()
693
         */
694
        public boolean isAvailable() {
695
                return status.available;
696
        }
697
        /*
698
         * (non-Javadoc)
699
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setAvailable(boolean)
700
         */
701
        public void setAvailable(boolean available) {
702
                if (status.available != available){
703
                        status.available = available;
704
                        this.updateDrawVersion();
705
                }
706
        }
707
        /*
708
         * (non-Javadoc)
709
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#reload()
710
         */
711
        public void reload() throws ReloadLayerException {
712
            if(this.ct!=null){
713
                IProjection srcProj = this.ct.getPOrig();
714
                if(!this.getProjection().equals(srcProj)){
715
                    this.ct = this.getProjection().getCT(this.ct.getPDest());
716
                }
717
            }
718
                this.setAvailable(true);
719
        }
720

    
721
        /*
722
         * (non-Javadoc)
723
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFLayerStatus()
724
         */
725
        public FLayerStatus getFLayerStatus(){
726
                return status.cloneStatus();
727
        }
728
        /*
729
         * (non-Javadoc)
730
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setFLayerStatus(com.iver.cit.gvsig.fmap.layers.FLayerStatus)
731
         */
732
        public void setFLayerStatus(FLayerStatus status){
733
                if (!this.status.equals(status)){
734
                        this.status = status;
735
                        this.updateDrawVersion();
736
                }
737
        }
738

    
739
        /*
740
         * (non-Javadoc)
741
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isOk()
742
         */
743

    
744
        public boolean isOk(){
745
                return status.isOk();
746
        }
747
        /*
748
         * (non-Javadoc)
749
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getNumErrors()
750
         */
751
        public int getNumErrors(){
752
                return status.getNumErrors();
753
        }
754

    
755
        /*
756
         * (non-Javadoc)
757
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getError(int)
758
         */
759
        public BaseException getError(int i){
760
                return status.getError(i);
761
        }
762
        /*
763
         * (non-Javadoc)
764
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getErrors()
765
         */
766
        public List getErrors(){
767
                return status.getErrors();
768
        }
769

    
770
        /*
771
         * (non-Javadoc)
772
         *
773
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#addError(BaseException)
774
         */
775
        @Override
776
        public void addError(BaseException exception){
777
                status.addLayerError(exception);
778
        }
779

    
780
        @Override
781
        public void setError(Exception ex) {
782
            this.status.setLayerError(ex);
783
        }
784

    
785
        /*
786
         * (non-Javadoc)
787
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#visibleRequired()
788
         */
789
        public boolean visibleRequired() {
790
                return status.visible;
791
        }
792
        /*
793
         * (non-Javadoc)
794
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getInfoString()
795
         */
796
        public String getInfoString() {
797
                return null;
798
        }
799
        /*
800
         * (non-Javadoc)
801
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isWritable()
802
         */
803
        public boolean isWritable() {
804
                return status.writable;
805
        }
806

    
807
        /*
808
         * (non-Javadoc)
809
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
810
         */
811
        public FLayer cloneLayer() throws Exception {
812
                return this;
813
        }
814
        /*
815
         * (non-Javadoc)
816
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocStatusImage()
817
         */
818
        public Image getTocStatusImage() {
819
            return tocStatusImage;
820
        }
821

    
822
        /**
823
         * Inserts the image icon that will be shown in the TOC next to this layer, according its status.
824
         *
825
         * @param tocStatusImage the image
826
         *
827
         * @see #getTocStatusImage()
828
         */
829
        public void setTocStatusImage(Image tocStatusImage) {
830
                this.tocStatusImage = tocStatusImage;
831
                logger.debug("setTocStatusImage " + tocStatusImage + " sobre capa " + this.getName());
832
        }
833

    
834
        public ComposedLayer newComposedLayer() {
835
                return null;
836
        }
837

    
838
        public boolean allowLinks()
839
        {
840
                return false;
841
        }
842

    
843
        public AbstractLinkProperties getLinkProperties()
844
        {
845
                return null;
846
        }
847

    
848
        public URI[] getLink(Point2D point, double tolerance) throws ReadException{
849
                return null;
850
        }
851

    
852
        public void addLegendListener(LegendListener listener) {
853
            if( listener == null ) {
854
                return ;
855
            }
856
            layerChangeSupport.addLayerListener(listener);
857
        }
858

    
859
        /**
860
     * @param e
861
         * @see LayerChangeSupport#callLegendChanged(LegendChangedEvent)
862
         */
863
        protected void callLegendChanged(LegendChangedEvent e) {
864
                layerChangeSupport.callLegendChanged(e);
865
                if(parentLayer != null) {
866
                        parentLayer.callLegendChanged(e);
867
                }
868
        }
869

    
870
        /**
871
         * @see LayerChangeSupport#removeLayerListener(LegendListener)
872
         */
873
        public void removeLegendListener(LegendListener listener) {
874
                layerChangeSupport.removeLayerListener(listener);
875
        }
876
        public String getClassName() {
877
                return this.getClass().getName();
878
        }
879

    
880
        public void delegate(DynObject dynObject) {
881
                this.metadataContainer.delegate(dynObject);
882
        }
883

    
884
        public DynClass getDynClass() {
885
                return this.metadataContainer.getDynClass();
886
        }
887

    
888
        public Object getDynValue(String name) throws DynFieldNotFoundException {
889
                return this.metadataContainer.getDynValue(name);
890
        }
891

    
892
        public boolean hasDynValue(String name) {
893
                return this.metadataContainer.hasDynValue(name);
894
        }
895

    
896
        public void implement(DynClass dynClass) {
897
                this.metadataContainer.implement(dynClass);
898
        }
899

    
900
        public Object invokeDynMethod(int code, Object[] args)
901
        throws DynMethodException {
902
                return this.metadataContainer.invokeDynMethod(this, code, args);
903
        }
904

    
905
        public Object invokeDynMethod(String name, Object[] args)
906
        throws DynMethodException {
907
                return this.metadataContainer.invokeDynMethod(this, name, args);
908
        }
909

    
910
        public void setDynValue(String name, Object value)
911
        throws DynFieldNotFoundException {
912
                this.metadataContainer.setDynValue(name, value);
913
        }
914

    
915
        public long getDrawVersion() {
916
                return this.drawVersion;
917
        }
918

    
919
        protected void updateDrawVersion(){
920
                this.drawVersion++;
921
                this.callDrawValueChanged(LayerEvent.createDrawValuesChangedEvent(this, ""));
922
                if (this.parentLayer != null){
923
                        this.parentLayer.updateDrawVersion();
924
                }
925
        }
926

    
927
        public boolean hasChangedForDrawing(long value){
928
                return this.drawVersion > value;
929
        }
930

    
931
        public void activationChanged(LayerEvent e) {
932
        }
933

    
934
        public void drawValueChanged(LayerEvent e) {
935
                this.updateDrawVersion();
936
        }
937

    
938
        public void editionChanged(LayerEvent e) {
939

    
940
        }
941

    
942
        public void nameChanged(LayerEvent e) {
943

    
944
        }
945

    
946
        public void visibilityChanged(LayerEvent e) {
947

    
948
        }
949

    
950
        // ========================================================
951

    
952
        public void saveToState(PersistentState state) throws PersistenceException {
953
            try {
954
                state.set("parentLayer", (Persistent)parentLayer);
955
                state.set("status",status);
956
                state.set("minScale", minScale);
957
                state.set("maxScale", maxScale);
958
                state.set("transparency",transparency);
959
                state.set("coordTrans",ct);
960
                state.set("name", getName());
961
                state.set("crs", getProjection());
962
                state.set("properties",properties.getExtendedProperties());
963
            } catch(PersistenceException ex) {
964
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
965
                throw ex;
966
            } catch(RuntimeException ex) {
967
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
968
                throw ex;
969
            }
970
        }
971

    
972
        public void loadFromState(PersistentState state) throws PersistenceException {
973
            try {
974
                this.setDynValue(METADATA_NAME, state.getString("name"));
975
                this.setDynValue(METADATA_CRS, state.get("crs"));
976

    
977
                this.parentLayer = (FLayers) state.get("parentLayer");
978
                this.status = (FLayerStatus) state.get("status");
979
                this.minScale = state.getDouble("minScale");
980
                this.maxScale = state.getDouble("maxScale");
981
                this.transparency = state.getInt("transparency");
982
                this.ct = (ICoordTrans) state.get("coordTrans");
983

    
984
                this.properties.setExtendedProperties((Map)state.get("properties"));
985
            } catch(PersistenceException ex) {
986
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
987
                throw ex;
988
            } catch(RuntimeException ex) {
989
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
990
                throw ex;
991
            }
992

    
993
        }
994
    public static class RegisterPersistence implements Callable {
995

    
996
        public Object call() {
997
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
998
                DynStruct definition = manager.addDefinition(
999
                                FLyrDefault.class,
1000
                                "FLyrDefault",
1001
                                "FLyrDefault Persistence definition",
1002
                                null,
1003
                                null
1004
                );
1005
                definition.addDynFieldString("name").setMandatory(false);
1006
                definition.addDynFieldInt("transparency").setMandatory(true);
1007
                definition.addDynFieldDouble("minScale").setMandatory(true);
1008
                definition.addDynFieldDouble("maxScale").setMandatory(true);
1009
                definition.addDynFieldObject("crs").setClassOfValue(IProjection.class).setMandatory(false);
1010
                definition.addDynFieldObject("parentLayer").setClassOfValue(FLayers.class).setMandatory(false);
1011
                definition.addDynFieldObject("coordTrans").setClassOfValue(ICoordTrans.class).setMandatory(false);
1012
                definition.addDynFieldObject("status").setClassOfValue(FLayerStatus.class).setMandatory(true);
1013
                definition.addDynFieldMap("properties").setClassOfItems(Object.class)
1014
                                .setMandatory(true);
1015

    
1016
            return Boolean.TRUE;
1017
        }
1018
    }
1019

    
1020

    
1021
//        /**
1022
//         * Splits string into an array of strings
1023
//         * @param input input string
1024
//         * @param sep separator string
1025
//         * @return an array of strings
1026
//         */
1027
//        public static String[] splitString(String input, String sep) {
1028
//                return Pattern.compile(sep).split(input, 0);
1029
//        }
1030

    
1031
        public void clear() {
1032
                if (metadataContainer != null) {
1033
                        metadataContainer.clear();
1034
                }
1035
        }
1036

    
1037
    public String getMetadataName() throws MetadataException {
1038
        return FLayer.METADATA_DEFINITION_NAME;
1039
    }
1040

    
1041
        public static class RegisterMetadata implements Callable {
1042

    
1043
            public Object call() {
1044
                MetadataManager metadataManager = MetadataLocator.getMetadataManager();
1045

    
1046
                DynStruct metadataDefinition = metadataManager.getDefinition(FLayer.METADATA_DEFINITION_NAME);
1047
                if ( metadataDefinition == null ) {
1048
                    try {
1049
                        metadataDefinition = metadataManager.addDefinition(
1050
                                FLayer.METADATA_DEFINITION_NAME,
1051
                                FLayer.METADATA_DEFINITION_DESCRIPTION);
1052
                        metadataDefinition.addDynField(FLayer.METADATA_NAME)
1053
                                .setMandatory(true);
1054

    
1055
                        IProjection ipr
1056
                                = MapContextLocator.getMapContextManager().getDefaultCRS();
1057

    
1058
                        metadataDefinition.addDynFieldObject(FLayer.METADATA_CRS)
1059
                                .setType(DataTypes.CRS).setMandatory(true)
1060
                                .setDefaultFieldValue(ipr);
1061
                    } catch (MetadataException e) {
1062
                        logger.warn("Can't create metadata definition for 'Layer'", e);
1063
                    }
1064
                }
1065
                return Boolean.TRUE;
1066
            }
1067
        }
1068

    
1069
        public String toString() {
1070
                return this.getName();
1071
        }
1072

    
1073
        public boolean hidesThisArea(Envelope area) {
1074
                return false;
1075
        }
1076

    
1077
        public boolean isTemporary() {
1078
            return this.temporary;
1079
        }
1080

    
1081
        public void setTemporary(boolean temporary) {
1082
            this.temporary = temporary;
1083
        }
1084
}