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

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
                this.setAvailable(true);
713
        }
714

    
715
        /*
716
         * (non-Javadoc)
717
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFLayerStatus()
718
         */
719
        public FLayerStatus getFLayerStatus(){
720
                return status.cloneStatus();
721
        }
722
        /*
723
         * (non-Javadoc)
724
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setFLayerStatus(com.iver.cit.gvsig.fmap.layers.FLayerStatus)
725
         */
726
        public void setFLayerStatus(FLayerStatus status){
727
                if (!this.status.equals(status)){
728
                        this.status = status;
729
                        this.updateDrawVersion();
730
                }
731
        }
732

    
733
        /*
734
         * (non-Javadoc)
735
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isOk()
736
         */
737

    
738
        public boolean isOk(){
739
                return status.isOk();
740
        }
741
        /*
742
         * (non-Javadoc)
743
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getNumErrors()
744
         */
745
        public int getNumErrors(){
746
                return status.getNumErrors();
747
        }
748

    
749
        /*
750
         * (non-Javadoc)
751
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getError(int)
752
         */
753
        public BaseException getError(int i){
754
                return status.getError(i);
755
        }
756
        /*
757
         * (non-Javadoc)
758
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getErrors()
759
         */
760
        public List getErrors(){
761
                return status.getErrors();
762
        }
763

    
764
        /*
765
         * (non-Javadoc)
766
         *
767
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#addError(BaseException)
768
         */
769
        @Override
770
        public void addError(BaseException exception){
771
                status.addLayerError(exception);
772
        }
773
        
774
        @Override
775
        public void setError(Exception ex) {
776
            this.status.setLayerError(ex);
777
        }
778
        
779
        /*
780
         * (non-Javadoc)
781
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#visibleRequired()
782
         */
783
        public boolean visibleRequired() {
784
                return status.visible;
785
        }
786
        /*
787
         * (non-Javadoc)
788
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getInfoString()
789
         */
790
        public String getInfoString() {
791
                return null;
792
        }
793
        /*
794
         * (non-Javadoc)
795
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isWritable()
796
         */
797
        public boolean isWritable() {
798
                return status.writable;
799
        }
800

    
801
        /*
802
         * (non-Javadoc)
803
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
804
         */
805
        public FLayer cloneLayer() throws Exception {
806
                return this;
807
        }
808
        /*
809
         * (non-Javadoc)
810
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocStatusImage()
811
         */
812
        public Image getTocStatusImage() {
813
            return tocStatusImage;
814
        }
815

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

    
828
        public ComposedLayer newComposedLayer() {
829
                return null;
830
        }
831

    
832
        public boolean allowLinks()
833
        {
834
                return false;
835
        }
836

    
837
        public AbstractLinkProperties getLinkProperties()
838
        {
839
                return null;
840
        }
841

    
842
        public URI[] getLink(Point2D point, double tolerance) throws ReadException{
843
                return null;
844
        }
845

    
846
        public void addLegendListener(LegendListener listener) {
847
            if( listener == null ) {
848
                return ;
849
            }
850
            layerChangeSupport.addLayerListener(listener);
851
        }
852

    
853
        /**
854
     * @param e
855
         * @see LayerChangeSupport#callLegendChanged(LegendChangedEvent)
856
         */
857
        protected void callLegendChanged(LegendChangedEvent e) {
858
                layerChangeSupport.callLegendChanged(e);
859
                if(parentLayer != null) {
860
                        parentLayer.callLegendChanged(e);
861
                }
862
        }
863

    
864
        /**
865
         * @see LayerChangeSupport#removeLayerListener(LegendListener)
866
         */
867
        public void removeLegendListener(LegendListener listener) {
868
                layerChangeSupport.removeLayerListener(listener);
869
        }
870
        public String getClassName() {
871
                return this.getClass().getName();
872
        }
873

    
874
        public void delegate(DynObject dynObject) {
875
                this.metadataContainer.delegate(dynObject);
876
        }
877

    
878
        public DynClass getDynClass() {
879
                return this.metadataContainer.getDynClass();
880
        }
881

    
882
        public Object getDynValue(String name) throws DynFieldNotFoundException {
883
                return this.metadataContainer.getDynValue(name);
884
        }
885

    
886
        public boolean hasDynValue(String name) {
887
                return this.metadataContainer.hasDynValue(name);
888
        }
889

    
890
        public void implement(DynClass dynClass) {
891
                this.metadataContainer.implement(dynClass);
892
        }
893

    
894
        public Object invokeDynMethod(int code, Object[] args)
895
        throws DynMethodException {
896
                return this.metadataContainer.invokeDynMethod(this, code, args);
897
        }
898

    
899
        public Object invokeDynMethod(String name, Object[] args)
900
        throws DynMethodException {
901
                return this.metadataContainer.invokeDynMethod(this, name, args);
902
        }
903

    
904
        public void setDynValue(String name, Object value)
905
        throws DynFieldNotFoundException {
906
                this.metadataContainer.setDynValue(name, value);
907
        }
908

    
909
        public long getDrawVersion() {
910
                return this.drawVersion;
911
        }
912

    
913
        protected void updateDrawVersion(){
914
                this.drawVersion++;
915
                this.callDrawValueChanged(LayerEvent.createDrawValuesChangedEvent(this, ""));
916
                if (this.parentLayer != null){
917
                        this.parentLayer.updateDrawVersion();
918
                }
919
        }
920

    
921
        public boolean hasChangedForDrawing(long value){
922
                return this.drawVersion > value;
923
        }
924

    
925
        public void activationChanged(LayerEvent e) {
926
        }
927

    
928
        public void drawValueChanged(LayerEvent e) {
929
                this.updateDrawVersion();
930
        }
931

    
932
        public void editionChanged(LayerEvent e) {
933

    
934
        }
935

    
936
        public void nameChanged(LayerEvent e) {
937

    
938
        }
939

    
940
        public void visibilityChanged(LayerEvent e) {
941

    
942
        }
943
        
944
        // ========================================================
945
        
946
        public void saveToState(PersistentState state) throws PersistenceException {
947
            try {
948
                state.set("parentLayer", (Persistent)parentLayer);
949
                state.set("status",status);
950
                state.set("minScale", minScale);
951
                state.set("maxScale", maxScale);
952
                state.set("transparency",transparency);
953
                state.set("coordTrans",ct);
954
                state.set("name", getName());
955
                state.set("crs", getProjection());
956
                state.set("properties",properties.getExtendedProperties());
957
            } catch(PersistenceException ex) {
958
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
959
                throw ex;
960
            } catch(RuntimeException ex) {
961
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
962
                throw ex;
963
            }
964
        }
965

    
966
        public void loadFromState(PersistentState state) throws PersistenceException {
967
            try {
968
                this.setDynValue(METADATA_NAME, state.getString("name"));
969
                this.setDynValue(METADATA_CRS, state.get("crs"));
970
                
971
                this.parentLayer = (FLayers) state.get("parentLayer");
972
                this.status = (FLayerStatus) state.get("status");
973
                this.minScale = state.getDouble("minScale");
974
                this.maxScale = state.getDouble("maxScale");
975
                this.transparency = state.getInt("transparency");
976
                this.ct = (ICoordTrans) state.get("coordTrans");
977

    
978
                this.properties.setExtendedProperties((Map)state.get("properties"));
979
            } catch(PersistenceException ex) {
980
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
981
                throw ex;
982
            } catch(RuntimeException ex) {
983
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
984
                throw ex;
985
            }
986

    
987
        }
988
    public static class RegisterPersistence implements Callable {
989

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

    
1014
        
1015
//        /**
1016
//         * Splits string into an array of strings
1017
//         * @param input input string
1018
//         * @param sep separator string
1019
//         * @return an array of strings
1020
//         */
1021
//        public static String[] splitString(String input, String sep) {
1022
//                return Pattern.compile(sep).split(input, 0);
1023
//        }
1024
        
1025
        public void clear() {
1026
                if (metadataContainer != null) {
1027
                        metadataContainer.clear();
1028
                }
1029
        }
1030
        
1031
    public String getMetadataName() throws MetadataException {
1032
        return FLayer.METADATA_DEFINITION_NAME;
1033
    }
1034
    
1035
        public static class RegisterMetadata implements Callable {
1036

    
1037
            public Object call() {
1038
                MetadataManager metadataManager = MetadataLocator.getMetadataManager();
1039

    
1040
                DynStruct metadataDefinition = metadataManager.getDefinition(FLayer.METADATA_DEFINITION_NAME);
1041
                if ( metadataDefinition == null ) {
1042
                    try {
1043
                        metadataDefinition = metadataManager.addDefinition(
1044
                                FLayer.METADATA_DEFINITION_NAME,
1045
                                FLayer.METADATA_DEFINITION_DESCRIPTION);
1046
                        metadataDefinition.addDynField(FLayer.METADATA_NAME)
1047
                                .setMandatory(true);
1048

    
1049
                        IProjection ipr
1050
                                = MapContextLocator.getMapContextManager().getDefaultCRS();
1051

    
1052
                        metadataDefinition.addDynFieldObject(FLayer.METADATA_CRS)
1053
                                .setType(DataTypes.CRS).setMandatory(true)
1054
                                .setDefaultFieldValue(ipr);
1055
                    } catch (MetadataException e) {
1056
                        logger.warn("Can't create metadata definition for 'Layer'", e);
1057
                    }
1058
                }
1059
                return Boolean.TRUE;
1060
            }
1061
        }
1062

    
1063
        public String toString() {
1064
                return this.getName();
1065
        }
1066
        
1067
        public boolean hidesThisArea(Envelope area) {
1068
                return false;
1069
        }
1070
        
1071
        public boolean isTemporary() {
1072
            return this.temporary;
1073
        }
1074
        
1075
        public void setTemporary(boolean temporary) {
1076
            this.temporary = temporary;
1077
        }
1078
}