Statistics
| Revision:

root / branches / dal_time_support / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / FLyrDefault.java @ 37204

History | View | Annotate | Download (27.9 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 org.gvsig.fmap.mapcontext.layers;
42

    
43
import java.awt.Image;
44
import java.awt.geom.Point2D;
45
import java.net.URI;
46
import java.util.ArrayList;
47
import java.util.Hashtable;
48
import java.util.Iterator;
49
import java.util.List;
50
import java.util.Map;
51

    
52
import org.cresques.cts.ICoordTrans;
53
import org.cresques.cts.IProjection;
54
import org.gvsig.fmap.dal.DataTypes;
55
import org.gvsig.fmap.dal.exception.ReadException;
56
import org.gvsig.fmap.mapcontext.MapContext;
57
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
58
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException;
59
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
60
import org.gvsig.fmap.mapcontext.layers.operations.ComposedLayer;
61
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
62
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
63
import org.gvsig.metadata.MetadataContainer;
64
import org.gvsig.metadata.MetadataLocator;
65
import org.gvsig.metadata.MetadataManager;
66
import org.gvsig.metadata.exceptions.MetadataException;
67
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dispose.impl.AbstractDisposable;
69
import org.gvsig.tools.dynobject.DynClass;
70
import org.gvsig.tools.dynobject.DynObject;
71
import org.gvsig.tools.dynobject.DynStruct;
72
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
73
import org.gvsig.tools.dynobject.exception.DynMethodException;
74
import org.gvsig.tools.exception.BaseException;
75
import org.gvsig.tools.persistence.PersistenceManager;
76
import org.gvsig.tools.persistence.PersistentState;
77
import org.gvsig.tools.persistence.exception.PersistenceException;
78
import org.slf4j.LoggerFactory;
79

    
80

    
81

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

    
109
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
110

    
111
        /**
112
         * Path to the upper layer which this layer belongs.
113
         *
114
         * @see #getParentLayer()
115
         * @see #setParentLayer(FLayers)
116
         */
117
        private FLayers parentLayer = null;
118

    
119
        /**
120
         * Transparency level of this layer in the range 0-255. By default 255.
121
         * 0   --> Transparent
122
         * 255 --> Opaque
123
         *
124
         * @see #getTransparency()
125
         * @see #setTransparency(int)
126
         */
127
        private int transparency = 255;
128

    
129
        /**
130
         * Coordinate transformation.
131
         *
132
         * @see #getCoordTrans()
133
         * @see #setCoordTrans(ICoordTrans)
134
         */
135
        private ICoordTrans ct;
136

    
137
        /**
138
         * Minimum scale, >= 0 or -1 if not defined. By default -1.
139
         *
140
         * @see #getMinScale()
141
         * @see #setMinScale(double)
142
         */
143
        private double minScale = -1; // -1 indica que no se usa
144

    
145
        /**
146
         * Maximum scale, >= 0 or -1 if not defined. By default -1.
147
         *
148
         * @see #getMaxScale()
149
         * @see #setMaxScale(double)
150
         */
151
        private double maxScale = -1;
152
        //        private boolean isInTOC = true;
153

    
154
        /**
155
         * Array list with all listeners registered to this layer.
156
         *
157
         * @see #getLayerListeners()
158
         * @see #removeLayerListener(LayerListener)
159
         * @see #callEditionChanged(LayerEvent)
160
         */
161
        protected ArrayList layerListeners = new ArrayList();
162

    
163

    
164
        /**
165
         * Hash table with the extended properties of this layer.
166
         *
167
         * @see #getProperty(Object)
168
         * @see #setProperty(Object, Object)
169
         * @see #getExtendedProperties()
170
         */
171
        private Map properties = new Hashtable();
172

    
173
        //by default, all is active, visible and avalaible
174
        /**
175
         * Status of this layer.
176
         *
177
         * @see #getFLayerStatus()
178
         * @see #setFLayerStatus(FLayerStatus)
179
         * @see #isActive()
180
         * @see #setActive(boolean)
181
         * @see #isVisible()
182
         * @see #setVisible(boolean)
183
         * @see #visibleRequired()
184
         * @see #isEditing()
185
         * @see #setEditing(boolean)
186
         * @see #isInTOC()
187
         * @see #isCachingDrawnLayers()
188
         * @see #setCachingDrawnLayers(boolean)
189
         * @see #isDirty()
190
         * @see #setDirty(boolean)
191
         * @see #isAvailable()
192
         * @see #setAvailable(boolean)
193
         * @see #isOk()
194
         * @see #isWritable()
195
         * @see #getNumErrors()
196
         * @see #getError(int)
197
         * @see #getErrors()
198
         * @see #addError(BaseException)
199
         */
200
        private FLayerStatus status = new FLayerStatus();
201
        /**
202
         * Image drawn shown in the TOC according the status of this layer.
203
         *
204
         * @see #getTocStatusImage()
205
         * @see #setTocStatusImage(Image)
206
         */
207
        private Image tocStatusImage;
208

    
209
        protected MetadataContainer metadataContainer;
210

    
211
        /**
212
         * Draw version of the context. It's used for know when de componend has
213
         * changed any visualization property
214
         *
215
         *  @see getDrawVersion
216
         *  @see updateDrawVersion
217
         */
218
        private long drawVersion= 0L;
219

    
220
        public FLyrDefault(MetadataContainer metadataContainer) {
221
                this.metadataContainer = metadataContainer;
222
        }
223

    
224
        public FLyrDefault() {
225
                this(MetadataLocator
226
                        .getMetadataManager()
227
                                .createMetadataContainer(FLayer.METADATA_DEFINITION_NAME)
228
                );
229
        }
230

    
231

    
232
        /*
233
         * (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperty(java.lang.Object)
235
         */
236
        public Object getProperty(Object key) {
237
                return properties.get(key);
238
        }
239
        /*
240
         * (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setProperty(java.lang.Object, java.lang.Object)
242
         */
243
        public void setProperty(Object key, Object val) {
244
                properties.put(key, val);
245
        }
246
        /*
247
         * (non-Javadoc)
248
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getExtendedProperties()
249
         */
250
        public Map getExtendedProperties() {
251
                return properties;
252
        }
253
        /*
254
         * (non-Javadoc)
255
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setActive(boolean)
256
         */
257
        public void setActive(boolean selected) {
258
                status.active = selected;
259
                callActivationChanged(LayerEvent.createActivationChangedEvent(this,
260
                "active"));
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isActive()
266
         */
267
        public boolean isActive() {
268
                return status.active;
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setName(java.lang.String)
274
         */
275
        public void setName(String name) {
276
                this.metadataContainer.setDynValue(METADATA_NAME, name);
277
                callNameChanged(LayerEvent.createNameChangedEvent(this, "name"));
278
        }
279

    
280
        /*
281
         * (non-Javadoc)
282
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getName()
283
         */
284
        public String getName() {
285
                return (String) this.metadataContainer.getDynValue(METADATA_NAME);
286
        }
287

    
288
        /*
289
         * (non-Javadoc)
290
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
291
         */
292
        public void load() throws LoadLayerException {
293
            MetadataManager manager = MetadataLocator.getMetadataManager();
294
            try {
295
            manager.loadMetadata(this);
296
        } catch (MetadataException e) {
297
            throw new LoadLayerException("Can't load metadata." , e );
298
        }
299
        }
300

    
301
        /*
302
         * (non-Javadoc)
303
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setVisible(boolean)
304
         */
305
        public void setVisible(boolean visibility) {
306
                if (status.visible != visibility){
307
                        status.visible = visibility;
308
                        this.updateDrawVersion();
309

    
310
                        //                        if (this.getMapContext() != null){
311
                        //                                this.getMapContext().clearAllCachingImageDrawnLayers();
312
                        //                        }
313
                        callVisibilityChanged(LayerEvent.createVisibilityChangedEvent(this,
314
                        "visible"));
315
                }
316
        }
317

    
318

    
319
        /*
320
         * (non-Javadoc)
321
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isVisible()
322
         */
323
        public boolean isVisible() {
324
                return status.visible && status.available;
325
        }
326

    
327
        /*
328
         * (non-Javadoc)
329
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getParentLayer()
330
         */
331
        public FLayers getParentLayer() {
332
                return parentLayer;
333
        }
334

    
335

    
336
        /*
337
         * (non-Javadoc)
338
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setParentLayer(com.iver.cit.gvsig.fmap.layers.FLayers)
339
         */
340
        public void setParentLayer(FLayers root) {
341
                if (this.parentLayer != root){
342
                        this.parentLayer = root;
343
                        this.updateDrawVersion();
344
                }
345
        }
346

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

    
378
        /*
379
         * (non-Javadoc)
380
         * @see org.cresques.geo.Projected#getProjection()
381
         */
382
        public IProjection getProjection() {
383
                return (IProjection) this.metadataContainer.getDynValue(METADATA_CRS);
384
        }
385

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

    
401
        /**
402
         * Returns the transparency level of this layer, in the range 0-255 .
403
         *
404
         * @return the transparency level
405
         *
406
         * @see #setTransparency(int)
407
         */
408
        public int getTransparency() {
409
                return transparency;
410
        }
411

    
412
        /**
413
         * Inserts the transparency level for this layer, the range allowed is 0-255 .
414
         *
415
         * @param trans the transparency level
416
         *
417
         * @see #getTransparency()
418
         */
419
        public void setTransparency(int trans) {
420
                if (this.transparency != trans){
421
                        transparency = trans;
422
                        this.updateDrawVersion();
423
                }
424
        }
425
        
426

    
427
        /*
428
         * (non-Javadoc)
429
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMapContext()
430
         */
431
        public MapContext getMapContext() {
432
                if (getParentLayer() != null) {
433
                        return getParentLayer().getMapContext();
434
                } else {
435
                        return null;
436
                }
437
        }
438

    
439
        /*
440
         * (non-Javadoc)
441
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#addLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
442
         */
443
        public boolean addLayerListener(LayerListener o) {
444
                if (layerListeners.contains(o)) {
445
                        return false;
446
                }
447
                return layerListeners.add(o);
448
        }
449
        /*
450
         * (non-Javadoc)
451
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getLayerListeners()
452
         */
453
        public LayerListener[] getLayerListeners() {
454
                return (LayerListener[])layerListeners.toArray(new LayerListener[0]);
455
        }
456
        /*
457
         * (non-Javadoc)
458
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#removeLayerListener(com.iver.cit.gvsig.fmap.layers.LayerListener)
459
         */
460
        public boolean removeLayerListener(LayerListener o) {
461
                return layerListeners.remove(o);
462
        }
463
        /**
464
         *
465
         */
466
        private void callDrawValueChanged(LayerEvent e) {
467
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
468
                        LayerListener listener = (LayerListener) iter.next();
469

    
470
                        listener.drawValueChanged(e);
471
                }
472
        }
473
        /**
474
         * Called by the method {@linkplain #setName(String)}. Notifies all listeners associated to this layer,
475
         *  that its name has changed.
476
         *
477
         * @param e a layer event with the name of the property that has changed
478
         *
479
         * @see #setName(String)
480
         */
481
        private void callNameChanged(LayerEvent e) {
482
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
483
                        LayerListener listener = (LayerListener) iter.next();
484

    
485
                        listener.nameChanged(e);
486
                }
487
        }
488

    
489
        /**
490
         * Called by the method {@linkplain #setVisible(boolean)}. Notifies all listeners associated to this layer,
491
         *  that its visibility has changed.
492
         *
493
         * @param e a layer event with the name of the property that has changed
494
         *
495
         * @see #setVisible(boolean)
496
         */
497
        private void callVisibilityChanged(LayerEvent e) {
498
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
499
                        LayerListener listener = (LayerListener) iter.next();
500

    
501
                        listener.visibilityChanged(e);
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 (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
515
                        LayerListener listener = (LayerListener) iter.next();
516

    
517
                        listener.activationChanged(e);
518
                }
519
        }
520

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

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

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

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

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

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

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

    
632
                return bVisible;
633
        }
634
        /*
635
         * (non-Javadoc)
636
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setEditing(boolean)
637
         */
638
        public void setEditing(boolean b) throws StartEditionLayerException {
639
                status.editing = b;
640
        }
641
        /**
642
         * Called by some version of the method {@linkplain #setEditing(boolean)} overwritten. Notifies
643
         *  all listeners associated to this layer, that its edition state has changed.
644
         *
645
         * @param e a layer event with the name of the property that has changed
646
         *
647
         * @see #setEditing(boolean)
648
         */
649
        protected void callEditionChanged(LayerEvent e) {
650
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
651
                        LayerListener listener = (LayerListener) iter.next();
652

    
653
                        listener.editionChanged(e);
654
                }
655
        }
656
        /*
657
         * (non-Javadoc)
658
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isEditing()
659
         */
660
        public boolean isEditing() {
661
                return status.editing;
662
        }
663
        /*
664
         * (non-Javadoc)
665
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocImageIcon()
666
         */
667
        public String getTocImageIcon() {
668
                return "toc-layer-group";
669
        }
670
        /*
671
         * (non-Javadoc)
672
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isInTOC()
673
         */
674
        public boolean isInTOC() {
675
                return status.inTOC;
676
        }
677
        /*
678
         * (non-Javadoc)
679
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setInTOC(boolean)
680
         */
681
        public void setInTOC(boolean b) {
682
                status.inTOC=b;
683
        }
684
        /*
685
         * (non-Javadoc)
686
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isAvailable()
687
         */
688
        public boolean isAvailable() {
689
                return status.available;
690
        }
691
        /*
692
         * (non-Javadoc)
693
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setAvailable(boolean)
694
         */
695
        public void setAvailable(boolean available) {
696
                if (status.available != available){
697
                        status.available = available;
698
                        this.updateDrawVersion();
699
                }
700
        }
701
        /*
702
         * (non-Javadoc)
703
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#reload()
704
         */
705
        public void reload() throws ReloadLayerException {
706
                this.setAvailable(true);
707
        }
708

    
709
        /*
710
         * (non-Javadoc)
711
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFLayerStatus()
712
         */
713
        public FLayerStatus getFLayerStatus(){
714
                return status.cloneStatus();
715
        }
716
        /*
717
         * (non-Javadoc)
718
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setFLayerStatus(com.iver.cit.gvsig.fmap.layers.FLayerStatus)
719
         */
720
        public void setFLayerStatus(FLayerStatus status){
721
                if (!this.status.equals(status)){
722
                        this.status = status;
723
                        this.updateDrawVersion();
724
                }
725
        }
726

    
727
        /*
728
         * (non-Javadoc)
729
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isOk()
730
         */
731

    
732
        public boolean isOk(){
733
                return status.isOk();
734
        }
735
        /*
736
         * (non-Javadoc)
737
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getNumErrors()
738
         */
739
        public int getNumErrors(){
740
                return status.getNumErrors();
741
        }
742

    
743
        /*
744
         * (non-Javadoc)
745
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getError(int)
746
         */
747
        public BaseException getError(int i){
748
                return status.getError(i);
749
        }
750
        /*
751
         * (non-Javadoc)
752
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getErrors()
753
         */
754
        public List getErrors(){
755
                return status.getErrors();
756
        }
757

    
758
        /*
759
         * (non-Javadoc)
760
         *
761
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#addError(BaseException)
762
         */
763
        public void addError(BaseException exception){
764
                status.addLayerError(exception);
765
        }
766
        /*
767
         * (non-Javadoc)
768
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#visibleRequired()
769
         */
770
        public boolean visibleRequired() {
771
                return status.visible;
772
        }
773
        /*
774
         * (non-Javadoc)
775
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getInfoString()
776
         */
777
        public String getInfoString() {
778
                return null;
779
        }
780
        /*
781
         * (non-Javadoc)
782
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isWritable()
783
         */
784
        public boolean isWritable() {
785
                return status.writable;
786
        }
787

    
788
        /*
789
         * (non-Javadoc)
790
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
791
         */
792
        public FLayer cloneLayer() throws Exception {
793
                return this;
794
        }
795
        /*
796
         * (non-Javadoc)
797
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocStatusImage()
798
         */
799
        public Image getTocStatusImage() {
800
                return tocStatusImage;
801
        }
802

    
803
        /**
804
         * Inserts the image icon that will be shown in the TOC next to this layer, according its status.
805
         *
806
         * @param tocStatusImage the image
807
         *
808
         * @see #getTocStatusImage()
809
         */
810
        public void setTocStatusImage(Image tocStatusImage) {
811
                this.tocStatusImage = tocStatusImage;
812
                logger.debug("setTocStatusImage " + tocStatusImage + " sobre capa " + this.getName());
813
        }
814
        /*
815
         * (non-Javadoc)
816
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#newComposedLayer()
817
         */
818
        public ComposedLayer newComposedLayer() {
819
                return null;
820
        }
821

    
822
        /*
823
         * (non-Javadoc)
824
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#allowLinks()
825
         */
826
        public boolean allowLinks()
827
        {
828
                return false;
829
        }
830

    
831
        /*
832
         * (non-Javadoc)
833
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getLinkProperties()
834
         */
835
        public AbstractLinkProperties getLinkProperties()
836
        {
837
                return null;
838
        }
839

    
840
        /*
841
         * (non-Javadoc)
842
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getLink(java.awt.geom.Point2D, double)
843
         */
844
        public URI[] getLink(Point2D point, double tolerance) throws ReadException{
845
                return null;
846
        }
847

    
848
        /**
849
         * @see LayerChangeSupport#addLayerListener(LegendListener)
850
         */
851
        public void addLegendListener(LegendListener listener) {
852
                layerChangeSupport.addLayerListener(listener);
853
        }
854

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

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

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

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

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

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

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

    
895
        public Object invokeDynMethod(int code, DynObject context)
896
        throws DynMethodException {
897
                return this.metadataContainer.invokeDynMethod(this, code, context);
898
        }
899

    
900
        public Object invokeDynMethod(String name, DynObject context)
901
        throws DynMethodException {
902
                return this.metadataContainer.invokeDynMethod(this, name, context);
903
        }
904

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

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

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

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

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

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

    
933
        public void editionChanged(LayerEvent e) {
934

    
935
        }
936

    
937
        public void nameChanged(LayerEvent e) {
938

    
939
        }
940

    
941
        public void visibilityChanged(LayerEvent e) {
942

    
943
        }
944
        
945
        // ========================================================
946
        
947
        public void saveToState(PersistentState state) throws PersistenceException {
948
                state.set("parentLayer", 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);
957
        }
958

    
959
        public void loadFromState(PersistentState state) throws PersistenceException {
960
                this.parentLayer = (FLayers) state.get("parentLayer");
961
                this.status = (FLayerStatus) state.get("status");
962
                this.minScale = state.getDouble("minScale");
963
                this.maxScale = state.getDouble("maxScale");
964
                this.transparency = state.getInt("transparency");
965
                this.ct = (ICoordTrans) state.get("coordTrans");
966

    
967
                this.setDynValue(METADATA_NAME, state.getString("name"));
968
                this.setDynValue(METADATA_CRS, state.get("crs"));
969
                
970
                this.properties = new Hashtable((Map)state.get("properties"));
971
        }
972

    
973
        public static void registerPersistent() {
974
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
975
                DynStruct definition = manager.addDefinition(
976
                                FLyrDefault.class,
977
                                "FLyrDefault",
978
                                "FLyrDefault Persistence definition",
979
                                null, 
980
                                null
981
                );
982
                definition.addDynFieldString("name").setMandatory(false);
983
                definition.addDynFieldInt("transparency").setMandatory(true);
984
                definition.addDynFieldDouble("minScale").setMandatory(true);
985
                definition.addDynFieldDouble("maxScale").setMandatory(true);
986
                definition.addDynFieldObject("crs").setClassOfValue(IProjection.class).setMandatory(false);
987
                definition.addDynFieldObject("parentLayer").setClassOfValue(FLayers.class).setMandatory(false);
988
                definition.addDynFieldObject("coordTrans").setClassOfValue(ICoordTrans.class).setMandatory(false);
989
                definition.addDynFieldObject("status").setClassOfValue(FLayerStatus.class).setMandatory(true);
990
                definition.addDynFieldMap("properties").setClassOfItems(Object.class)
991
                                .setMandatory(true);
992
        }
993

    
994
        
995
//        /**
996
//         * Splits string into an array of strings
997
//         * @param input input string
998
//         * @param sep separator string
999
//         * @return an array of strings
1000
//         */
1001
//        public static String[] splitString(String input, String sep) {
1002
//                return Pattern.compile(sep).split(input, 0);
1003
//        }
1004
        
1005
        public void clear() {
1006
                if (metadataContainer != null) {
1007
                        metadataContainer.clear();
1008
                }
1009
        }
1010
        
1011
    public String getMetadataName() throws MetadataException {
1012
        return FLayer.METADATA_DEFINITION_NAME;
1013
    }
1014
    
1015
        public static void registerMetadata() {
1016
                MetadataManager metadataManager = MetadataLocator.getMetadataManager();
1017

    
1018
                
1019
                DynStruct metadataDefinition = metadataManager.getDefinition(FLayer.METADATA_DEFINITION_NAME);
1020
                if (metadataDefinition == null) {
1021
                        try {
1022
                                metadataDefinition = metadataManager.addDefinition(
1023
                                                FLayer.METADATA_DEFINITION_NAME,
1024
                                                FLayer.METADATA_DEFINITION_DESCRIPTION);
1025
                                metadataDefinition.addDynField(FLayer.METADATA_NAME)
1026
                                                .setMandatory(true);
1027

    
1028
                                metadataDefinition.addDynFieldObject(FLayer.METADATA_CRS)
1029
                                                .setType(DataTypes.CRS).setMandatory(true);
1030
                        } catch (MetadataException e) {
1031
                                logger.warn("Can't create metadata definition for 'Layer'", e);
1032
                        }
1033
                }
1034
        }
1035
        
1036

    
1037
}