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

History | View | Annotate | Download (29.7 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.ArrayList;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Map;
33

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

    
67

    
68

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

    
96
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
97

    
98
        /**
99
         * Flag to set the layer as a temporary layer.
100
         */
101
        private boolean temporary;
102
        
103
        /**
104
         * Path to the upper layer which this layer belongs.
105
         *
106
         * @see #getParentLayer()
107
         * @see #setParentLayer(FLayers)
108
         */
109
        private FLayers parentLayer = null;
110

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

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

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

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

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

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

    
191
        protected MetadataContainer metadataContainer;
192

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

    
202
        
203
        private ExtendedPropertiesHelper properties = new ExtendedPropertiesHelper();
204
        
205
        public FLyrDefault(MetadataContainer metadataContainer) {
206
                this.metadataContainer = metadataContainer;
207
        }
208

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

    
216

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

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

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

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

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

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

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

    
311

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

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

    
328

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

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

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

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

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

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

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

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

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

    
478
                        listener.nameChanged(e);
479
                }
480
        }
481

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

    
494
                        listener.visibilityChanged(e);
495
                }
496
        }
497

    
498
        /**
499
         * Called by the method {@linkplain #setActive(boolean)}. Notifies all listeners associated to this layer,
500
         *  that its active state has changed.
501
         *
502
         * @param e a layer event with the name of the property that has changed
503
         *
504
         * @see #setActive(boolean)
505
         */
506
        private void callActivationChanged(LayerEvent e) {
507
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
508
                        LayerListener listener = (LayerListener) iter.next();
509

    
510
                        listener.activationChanged(e);
511
                }
512
        }
513

    
514
        /**
515
         * Returns the virtual layers associated to this layer.
516
         *
517
         * @return a node with the layers
518
         *
519
         * @see #setVirtualLayers(FLayers)
520
         */
521
        //        public FLayers getVirtualLayers() {
522
        //                return virtualLayers;
523
        //        }
524

    
525
        /**
526
         * Inserts virtual layers to this layer.
527
         *
528
         * @param virtualLayers a node with the layers
529
         *
530
         * @see #getVirtualLayers()
531
         */
532
        //        public void setVirtualLayers(FLayers virtualLayers) {
533
        //                this.virtualLayers = virtualLayers;
534
        //        }
535

    
536
        /**
537
         * Sets transformation coordinates for this layer.
538
         *
539
         * @param ct an object that implements the <code>ICoordTrans</code> interface, and with the transformation coordinates
540
         *
541
         * @see #getCoordTrans()
542
         */
543
        public void setCoordTrans(ICoordTrans ct) {
544
                if (this.ct == ct){
545
                        return;
546
                }
547
                if (this.ct != null && this.ct.equals(ct)){
548
                        return;
549
                }
550
                this.ct = ct;
551
                this.updateDrawVersion();
552
        }
553

    
554
        /**
555
         * Returns the transformation coordinates of this layer.
556
         *
557
         * @return an object that implements the <code>ICoordTrans</code> interface, and with the transformation coordinates
558
         *
559
         * @see #setCoordTrans(ICoordTrans)
560
         */
561
        public ICoordTrans getCoordTrans() {
562
                return ct;
563
        }
564

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

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

    
613
                boolean bVisible = true;
614
                if (getMinScale() != -1) {
615
                        if (scale < getMinScale()){
616
                                bVisible = false;
617
                        }
618
                }
619
                if (getMaxScale() != -1) {
620
                        if (scale > getMaxScale()) {
621
                                bVisible = false;
622
                        }
623
                }
624

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

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

    
703
        /*
704
         * (non-Javadoc)
705
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFLayerStatus()
706
         */
707
        public FLayerStatus getFLayerStatus(){
708
                return status.cloneStatus();
709
        }
710
        /*
711
         * (non-Javadoc)
712
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setFLayerStatus(com.iver.cit.gvsig.fmap.layers.FLayerStatus)
713
         */
714
        public void setFLayerStatus(FLayerStatus status){
715
                if (!this.status.equals(status)){
716
                        this.status = status;
717
                        this.updateDrawVersion();
718
                }
719
        }
720

    
721
        /*
722
         * (non-Javadoc)
723
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isOk()
724
         */
725

    
726
        public boolean isOk(){
727
                return status.isOk();
728
        }
729
        /*
730
         * (non-Javadoc)
731
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getNumErrors()
732
         */
733
        public int getNumErrors(){
734
                return status.getNumErrors();
735
        }
736

    
737
        /*
738
         * (non-Javadoc)
739
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getError(int)
740
         */
741
        public BaseException getError(int i){
742
                return status.getError(i);
743
        }
744
        /*
745
         * (non-Javadoc)
746
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getErrors()
747
         */
748
        public List getErrors(){
749
                return status.getErrors();
750
        }
751

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

    
782
        /*
783
         * (non-Javadoc)
784
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
785
         */
786
        public FLayer cloneLayer() throws Exception {
787
                return this;
788
        }
789
        /*
790
         * (non-Javadoc)
791
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getTocStatusImage()
792
         */
793
        public Image getTocStatusImage() {
794
            return tocStatusImage;
795
        }
796

    
797
        /**
798
         * Inserts the image icon that will be shown in the TOC next to this layer, according its status.
799
         *
800
         * @param tocStatusImage the image
801
         *
802
         * @see #getTocStatusImage()
803
         */
804
        public void setTocStatusImage(Image tocStatusImage) {
805
                this.tocStatusImage = tocStatusImage;
806
                logger.debug("setTocStatusImage " + tocStatusImage + " sobre capa " + this.getName());
807
        }
808

    
809
        public ComposedLayer newComposedLayer() {
810
                return null;
811
        }
812

    
813
        public boolean allowLinks()
814
        {
815
                return false;
816
        }
817

    
818
        public AbstractLinkProperties getLinkProperties()
819
        {
820
                return null;
821
        }
822

    
823
        public URI[] getLink(Point2D point, double tolerance) throws ReadException{
824
                return null;
825
        }
826

    
827
        public void addLegendListener(LegendListener listener) {
828
                layerChangeSupport.addLayerListener(listener);
829
        }
830

    
831
        /**
832
         * @see LayerChangeSupport#callLegendChanged(LegendChangedEvent)
833
         */
834
        protected void callLegendChanged(LegendChangedEvent e) {
835
                layerChangeSupport.callLegendChanged(e);
836
                if(parentLayer != null) {
837
                        parentLayer.callLegendChanged(e);
838
                }
839
        }
840

    
841
        /**
842
         * @see LayerChangeSupport#removeLayerListener(LegendListener)
843
         */
844
        public void removeLegendListener(LegendListener listener) {
845
                layerChangeSupport.removeLayerListener(listener);
846
        }
847
        public String getClassName() {
848
                return this.getClass().getName();
849
        }
850

    
851
        public void delegate(DynObject dynObject) {
852
                this.metadataContainer.delegate(dynObject);
853
        }
854

    
855
        public DynClass getDynClass() {
856
                return this.metadataContainer.getDynClass();
857
        }
858

    
859
        public Object getDynValue(String name) throws DynFieldNotFoundException {
860
                return this.metadataContainer.getDynValue(name);
861
        }
862

    
863
        public boolean hasDynValue(String name) {
864
                return this.metadataContainer.hasDynValue(name);
865
        }
866

    
867
        public void implement(DynClass dynClass) {
868
                this.metadataContainer.implement(dynClass);
869
        }
870

    
871
        public Object invokeDynMethod(int code, Object[] args)
872
        throws DynMethodException {
873
                return this.metadataContainer.invokeDynMethod(this, code, args);
874
        }
875

    
876
        public Object invokeDynMethod(String name, Object[] args)
877
        throws DynMethodException {
878
                return this.metadataContainer.invokeDynMethod(this, name, args);
879
        }
880

    
881
        public void setDynValue(String name, Object value)
882
        throws DynFieldNotFoundException {
883
                this.metadataContainer.setDynValue(name, value);
884
        }
885

    
886
        public long getDrawVersion() {
887
                return this.drawVersion;
888
        }
889

    
890
        protected void updateDrawVersion(){
891
                this.drawVersion++;
892
                this.callDrawValueChanged(LayerEvent.createDrawValuesChangedEvent(this, ""));
893
                if (this.parentLayer != null){
894
                        this.parentLayer.updateDrawVersion();
895
                }
896
        }
897

    
898
        public boolean hasChangedForDrawing(long value){
899
                return this.drawVersion > value;
900
        }
901

    
902
        public void activationChanged(LayerEvent e) {
903
        }
904

    
905
        public void drawValueChanged(LayerEvent e) {
906
                this.updateDrawVersion();
907
        }
908

    
909
        public void editionChanged(LayerEvent e) {
910

    
911
        }
912

    
913
        public void nameChanged(LayerEvent e) {
914

    
915
        }
916

    
917
        public void visibilityChanged(LayerEvent e) {
918

    
919
        }
920
        
921
        // ========================================================
922
        
923
        public void saveToState(PersistentState state) throws PersistenceException {
924
            try {
925
                state.set("parentLayer", (Persistent)parentLayer);
926
                state.set("status",status);
927
                state.set("minScale", minScale);
928
                state.set("maxScale", maxScale);
929
                state.set("transparency",transparency);
930
                state.set("coordTrans",ct);
931
                state.set("name", getName());
932
                state.set("crs", getProjection());
933
                state.set("properties",properties.getExtendedProperties());
934
            } catch(PersistenceException ex) {
935
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
936
                throw ex;
937
            } catch(RuntimeException ex) {
938
                logger.warn("Can't save to persistent state the layer '"+this.getName()+"'.");
939
                throw ex;
940
            }
941
        }
942

    
943
        public void loadFromState(PersistentState state) throws PersistenceException {
944
            try {
945
                this.setDynValue(METADATA_NAME, state.getString("name"));
946
                this.setDynValue(METADATA_CRS, state.get("crs"));
947
                
948
                this.parentLayer = (FLayers) state.get("parentLayer");
949
                this.status = (FLayerStatus) state.get("status");
950
                this.minScale = state.getDouble("minScale");
951
                this.maxScale = state.getDouble("maxScale");
952
                this.transparency = state.getInt("transparency");
953
                this.ct = (ICoordTrans) state.get("coordTrans");
954

    
955
                this.properties.setExtendedProperties((Map)state.get("properties"));
956
            } catch(PersistenceException ex) {
957
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
958
                throw ex;
959
            } catch(RuntimeException ex) {
960
                logger.warn("Can't load from persietent state the layer '"+this.getName()+"'.");
961
                throw ex;
962
            }
963

    
964
        }
965
    public static class RegisterPersistence implements Callable {
966

    
967
        public Object call() {
968
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
969
                DynStruct definition = manager.addDefinition(
970
                                FLyrDefault.class,
971
                                "FLyrDefault",
972
                                "FLyrDefault Persistence definition",
973
                                null, 
974
                                null
975
                );
976
                definition.addDynFieldString("name").setMandatory(false);
977
                definition.addDynFieldInt("transparency").setMandatory(true);
978
                definition.addDynFieldDouble("minScale").setMandatory(true);
979
                definition.addDynFieldDouble("maxScale").setMandatory(true);
980
                definition.addDynFieldObject("crs").setClassOfValue(IProjection.class).setMandatory(false);
981
                definition.addDynFieldObject("parentLayer").setClassOfValue(FLayers.class).setMandatory(false);
982
                definition.addDynFieldObject("coordTrans").setClassOfValue(ICoordTrans.class).setMandatory(false);
983
                definition.addDynFieldObject("status").setClassOfValue(FLayerStatus.class).setMandatory(true);
984
                definition.addDynFieldMap("properties").setClassOfItems(Object.class)
985
                                .setMandatory(true);
986
            
987
            return Boolean.TRUE;
988
        }
989
    }
990

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

    
1014
            public Object call() {
1015
                MetadataManager metadataManager = MetadataLocator.getMetadataManager();
1016

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

    
1026
                        IProjection ipr
1027
                                = MapContextLocator.getMapContextManager().getDefaultCRS();
1028

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

    
1040
        public String toString() {
1041
                return this.getName();
1042
        }
1043
        
1044
        public boolean hidesThisArea(Envelope area) {
1045
                return false;
1046
        }
1047
        
1048
        public boolean isTemporary() {
1049
            return this.temporary;
1050
        }
1051
        
1052
        public void setTemporary(boolean temporary) {
1053
            this.temporary = temporary;
1054
        }
1055
}