Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrDefault.java @ 8745

History | View | Annotate | Download (14.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.awt.image.BufferedImage;
44
import java.util.ArrayList;
45
import java.util.Hashtable;
46
import java.util.Iterator;
47

    
48
import javax.swing.ImageIcon;
49

    
50
import org.apache.log4j.Logger;
51
import org.cresques.cts.ICoordTrans;
52
import org.cresques.cts.IProjection;
53

    
54
import com.iver.cit.gvsig.fmap.DriverException;
55
import com.iver.cit.gvsig.fmap.MapContext;
56
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
57
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
58
import com.iver.cit.gvsig.fmap.edition.EditionException;
59
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
60
import com.iver.utiles.XMLEntity;
61

    
62
/**
63
 * Implementaci?n de las caracter?sticas de alto nivel de las capas:
64
 * visibilidad, activaci?n, nombre, ...
65
 */
66
public abstract class FLyrDefault implements FLayer {
67
        // private PropertyChangeSupport lnkPropertyChangeSupport;
68
        private static Logger logger = Logger.getLogger(FLyrDefault.class);
69

    
70
        /** Path de la capa en el arbol de capas */
71
        private FLayers parentLayer = null;
72

    
73
        private FLayers virtualLayers = null;
74

    
75
        private FLyrText layerText = null;
76

    
77
        private String name;
78

    
79
        private IProjection projection;
80

    
81
        private boolean visible = true;
82

    
83
        private boolean active;
84

    
85
        private int transparency = 0;
86

    
87
        private ICoordTrans ct;
88

    
89
        private double minScale = -1; // -1 indica que no se usa
90

    
91
        private double maxScale = -1;
92

    
93
        private boolean isInTOC = true;
94

    
95
        protected ArrayList layerListeners = new ArrayList();
96

    
97
        private Strategy privateStrategy = null;
98

    
99
        private boolean isediting;
100

    
101
        private Hashtable properties = new Hashtable();
102

    
103
        private boolean bCacheDrawnLayers;
104
        
105
        private BufferedImage cacheImageDrawnLayers = null;
106
        
107
        private boolean bDirty;
108
        
109
        private boolean available = true;
110

    
111
        public Object getProperty(Object key) {
112
                return properties.get(key);
113
        }
114

    
115
        public void setProperty(Object key, Object val) {
116
                properties.put(key, val);
117
        }
118

    
119
        /**
120
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setActive(boolean)
121
         */
122
        public void setActive(boolean selected) {
123
                active = selected;
124
                callActivationChanged(LayerEvent.createActivationChangedEvent(this,
125
                                "active"));
126
        }
127

    
128
        /**
129
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isActive()
130
         */
131
        public boolean isActive() {
132
                return active;
133
        }
134

    
135
        /**
136
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setName(java.lang.String)
137
         */
138
        public void setName(String name) {
139
                this.name = name;
140
                callNameChanged(LayerEvent.createNameChangedEvent(this, "name"));
141
        }
142

    
143
        /**
144
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getName()
145
         */
146
        public String getName() {
147
                return name;
148
        }
149

    
150
        /*
151
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
152
         */
153
        public void load() throws DriverIOException {
154
        }
155

    
156
        /**
157
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setVisible(boolean)
158
         */
159
        public void setVisible(boolean visibility) {
160
                boolean changed = visible != visibility;
161
                visible = visibility;
162
                setDirty(true);
163
                if (changed){
164
                        if (this.getMapContext() != null){
165
                                this.getMapContext().clearAllCachingImageDrawnLayers();
166
                        }
167
                }
168
                callVisibilityChanged(LayerEvent.createVisibilityChangedEvent(this,
169
                                "visible"));
170
                        
171
                
172
        }
173

    
174
        /**
175
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isVisible()
176
         */
177
        public boolean isVisible() {
178
                return visible && this.available;
179
        }
180

    
181
        /**
182
         * Devuelve la capa padre de la actual.
183
         * 
184
         * @return FLayers padre.
185
         */
186
        public FLayers getParentLayer() {
187
                return parentLayer;
188
        }
189

    
190
        /**
191
         * Inserta la capa padre.
192
         * 
193
         * @param root
194
         *            capa padre.
195
         */
196
        public void setParentLayer(FLayers root) {
197
                this.parentLayer = root;
198
        }
199

    
200
        /**
201
         * Inserta una proyecci?n.
202
         * 
203
         * @param proj
204
         *            Proyecci?n.
205
         */
206
        public void setProjection(IProjection proj) {
207
                projection = proj;
208
                // Comprobar que la proyecci?n es la misma que la de FMap
209
                // Si no lo es, es una capa que est? reproyectada al vuelo
210
                if ((proj != null) && (getMapContext() != null))
211
                        if (proj != getMapContext().getProjection()) {
212
                                ICoordTrans ct = proj.getCT(getMapContext().getProjection());
213
                                setCoordTrans(ct);
214
                                logger.debug("Cambio proyecci?n: FMap con "
215
                                                + getMapContext().getProjection().getAbrev() + " y capa "
216
                                                + getName() + " con " + proj.getAbrev());
217
                        }
218
        }
219

    
220
        /**
221
         * @see org.cresques.geo.Projected#getProjection()
222
         */
223
        public IProjection getProjection() {
224
                return projection;
225
        }
226

    
227
        /**
228
         * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
229
         */
230
        public void reProject(ICoordTrans arg0) {
231
        }
232

    
233
        /**
234
         * Devuelve el nivel de transparencia de la capa.
235
         * 
236
         * @return Entero que representa el nivel de transparencia.
237
         */
238
        public int getTransparency() {
239
                return transparency;
240
        }
241

    
242
        /**
243
         * Inserta el nivel de transparencia de la capa.
244
         * 
245
         * @param trans
246
         *            Nivel de transparencia.
247
         */
248
        public void setTransparency(int trans) {
249
                transparency = trans;
250
                setDirty(true);
251
        }
252

    
253
        /**
254
         * Devuelve el XMLEntity a partir del objeto.
255
         * 
256
         * @return XMLEntity.
257
         * @throws XMLException
258
         */
259
        public XMLEntity getXMLEntity() throws XMLException {
260
                XMLEntity xml = new XMLEntity();
261
                xml.putProperty("className", this.getClass().getName());
262

    
263
                if (this instanceof FLayers) {
264
                }
265

    
266
                xml.putProperty("active", active);
267
                xml.putProperty("name", name);
268
                xml.putProperty("minScale", minScale);
269
                xml.putProperty("maxScale", maxScale);
270

    
271
                // TODO xml.addChild(parentLayer.getXMLEntity());
272
                xml.putProperty("visible", visible);
273
                if (projection != null) {
274
                        xml.putProperty("proj", projection.getAbrev());
275
                }
276
                xml.putProperty("transparency", transparency);
277
                xml.putProperty("isInTOC", isInTOC);
278
                return xml;
279
        }
280

    
281
        /*
282
         * Inserta los valores de los atributos del XMLEntity al objeto.
283
         * 
284
         * @param xml XMLEntity.
285
         * 
286
         * @throws XMLException @throws DriverException @throws DriverIOException
287
         * 
288
         * public void setXMLEntity03(XMLEntity xml) throws XMLException { active =
289
         * xml.getBooleanProperty("active"); name = xml.getStringProperty("name");
290
         * minScale=xml.getDoubleProperty("minScale");
291
         * maxScale=xml.getDoubleProperty("maxScale"); visible =
292
         * xml.getBooleanProperty("visible"); if (xml.contains("proj")) {
293
         * setProjection(ProjectionPool.get(xml.getStringProperty("proj"))); } if
294
         * (xml.contains("transparency")) transparency =
295
         * xml.getIntProperty("transparency"); }
296
         */
297

    
298
        /**
299
         * Inserta los valores de los atributos del XMLEntity al objeto.
300
         * 
301
         * @param xml
302
         *            XMLEntity.
303
         * 
304
         * @throws XMLException
305
         * @throws DriverException
306
         * @throws DriverIOException
307
         */
308
        public void setXMLEntity(XMLEntity xml) throws XMLException {
309
                active = xml.getBooleanProperty("active");
310
                name = xml.getStringProperty("name");
311
                minScale = xml.getDoubleProperty("minScale");
312
                maxScale = xml.getDoubleProperty("maxScale");
313
                visible = xml.getBooleanProperty("visible");
314
                if (xml.contains("proj")) {
315
                        setProjection(CRSFactory.getCRS(xml.getStringProperty("proj")));
316
                }
317
                if (xml.contains("transparency"))
318
                        transparency = xml.getIntProperty("transparency");
319
                if (xml.contains("isInTOC"))
320
                        isInTOC = xml.getBooleanProperty("isInTOC");
321
        }
322

    
323
        /**
324
         * Inserta los valores de los atributos del XMLEntity al objeto.
325
         * 
326
         * @param xml
327
         *            XMLEntity.
328
         * 
329
         * @throws XMLException
330
         * @throws DriverException
331
         * @throws DriverIOException
332
         */
333
        public void setXMLEntity03(XMLEntity xml) throws XMLException {
334
                active = xml.getBooleanProperty("active");
335
                name = xml.getStringProperty("name");
336
                minScale = xml.getDoubleProperty("minScale");
337
                maxScale = xml.getDoubleProperty("maxScale");
338
                visible = xml.getBooleanProperty("visible");
339
                if (xml.contains("proj")) {
340
                        setProjection(CRSFactory.getCRS(xml.getStringProperty("proj")));
341
                }
342
                if (xml.contains("transparency"))
343
                        transparency = xml.getIntProperty("transparency");
344
        }
345

    
346
        /**
347
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMapContext()
348
         */
349
        public MapContext getMapContext() {
350
                if (getParentLayer() != null) {
351
                        return getParentLayer().getMapContext();
352
                } else {
353
                        return null;
354
                }
355
        }
356

    
357
        /**
358
         * A?ade a la lista de listener un nuevo LayerListener.
359
         * 
360
         * @param o
361
         *            LayerListener.
362
         * 
363
         * @return boolean.
364
         */
365
        public boolean addLayerListener(LayerListener o) {
366
                if (layerListeners.contains(o))
367
                        return false;
368
                return layerListeners.add(o);
369
        }
370
        public LayerListener[] getLayerListeners() {
371
                return (LayerListener[])layerListeners.toArray(new LayerListener[0]);
372
        }
373
        /**
374
         * Borra de la lista de listeners el que se pasa como par?metro.
375
         * 
376
         * @param o
377
         *            LayerListener a borrar.
378
         * 
379
         * @return True si ha sido correcto el borrado del Listener.
380
         */
381
        public boolean removeLayerListener(LayerListener o) {
382
                return layerListeners.remove(o);
383
        }
384

    
385
        /**
386
         * Llamada al metodo nameChanged de los Listeners dados de alta.
387
         * 
388
         * @param e
389
         *            LayerEvent.
390
         */
391
        private void callNameChanged(LayerEvent e) {
392
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
393
                        LayerListener listener = (LayerListener) iter.next();
394

    
395
                        listener.nameChanged(e);
396
                }
397
        }
398

    
399
        /**
400
         * Llamada al m?todo visibilityChanged de los Listeners.
401
         * 
402
         * @param e
403
         *            LayerEvent.
404
         */
405
        private void callVisibilityChanged(LayerEvent e) {
406
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
407
                        LayerListener listener = (LayerListener) iter.next();
408

    
409
                        listener.visibilityChanged(e);
410
                }
411
        }
412

    
413
        /**
414
         * Llamada al m?todo activationChanged de los Listener.
415
         * 
416
         * @param e
417
         *            LayerEvent.
418
         */
419
        private void callActivationChanged(LayerEvent e) {
420
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
421
                        LayerListener listener = (LayerListener) iter.next();
422

    
423
                        listener.activationChanged(e);
424
                }
425
        }
426

    
427
        /**
428
         * Devuelve las capas virtuales.
429
         * 
430
         * @return FLayers.
431
         */
432
        public FLayers getVirtualLayers() {
433
                return virtualLayers;
434
        }
435

    
436
        /**
437
         * Inserta las capas virtuales.
438
         * 
439
         * @param virtualLayers
440
         *            FLayers.
441
         */
442
        public void setVirtualLayers(FLayers virtualLayers) {
443
                this.virtualLayers = virtualLayers;
444
        }
445

    
446
        /**
447
         * Devuelve la capa de texto.
448
         * 
449
         * @return capa de texto.
450
         */
451
        public FLyrText getLayerText() {
452
                return layerText;
453
        }
454

    
455
        /**
456
         * Inserta la capa de texto.
457
         * 
458
         * @param layerText
459
         *            Capa de texto.
460
         */
461
        public void setLayerText(FLyrText layerText) {
462
                this.layerText = layerText;
463
        }
464

    
465
        /**
466
         * Inserta la Transformaci?n de coordenadas.
467
         * 
468
         * @param ct
469
         *            Transformaci?n de coordenadas.
470
         */
471
        public void setCoordTrans(ICoordTrans ct) {
472
                this.ct = ct;
473
        }
474

    
475
        /**
476
         * Devuelve las transformaci?n de coordenadas.
477
         * 
478
         * @return ct.
479
         */
480
        public ICoordTrans getCoordTrans() {
481
                return ct;
482
        }
483

    
484
        /**
485
         * M?todo que es llamado por Flayers para notificar a la capa que va a ser
486
         * a?adida. Esta previa notificaci?n es util para las capas que necesitan
487
         * hacer algo antes de ser a?adida. Por ejemplo, el raster necesita volver a
488
         * abrir el fichero que ha podido ser cerrado con anterioridad. Si no se
489
         * redefine este m?todo no se har? nada ya que este es vacio.
490
         */
491
        public void wakeUp() {
492
        }
493

    
494
        public double getMinScale() {
495
                return minScale;
496
        }
497

    
498
        /*
499
         * (non-Javadoc)
500
         * 
501
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getMaxScale()
502
         */
503
        public double getMaxScale() {
504
                return maxScale;
505
        }
506

    
507
        public void setMinScale(double minScale) {
508
                this.minScale = minScale;
509
        }
510

    
511
        public void setMaxScale(double maxScale) {
512
                this.maxScale = maxScale;
513
        }
514

    
515
        public boolean isWithinScale(double scale) {
516

    
517
                boolean bVisible = true;
518
                if (getMinScale() != -1) {
519
                        if (scale < getMinScale())
520
                                bVisible = false;
521
                }
522
                if (getMaxScale() != -1) {
523
                        if (scale > getMaxScale())
524
                                bVisible = false;
525
                }
526

    
527
                return bVisible;
528
        }
529

    
530
        public Strategy getStrategy() {
531
                return privateStrategy;
532
        }
533

    
534
        public void setStrategy(Strategy s) {
535
                privateStrategy = s;
536
        }
537

    
538
        public void setEditing(boolean b) throws EditionException {
539
                isediting = b;
540
                setDirty(true);
541
                setCachingDrawnLayers(b);
542
        }
543

    
544
        protected void callEditionChanged(LayerEvent e) {
545
                for (Iterator iter = layerListeners.iterator(); iter.hasNext();) {
546
                        LayerListener listener = (LayerListener) iter.next();
547

    
548
                        listener.editionChanged(e);
549
                }
550
        }
551

    
552
        public boolean isEditing() {
553
                return isediting;
554
        }
555

    
556
        public ImageIcon getTocImageIcon() {
557
                return null;
558
        }
559

    
560
        public boolean isInTOC() {
561
                return isInTOC;
562
        }
563

    
564
        /* (non-Javadoc)
565
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#isCachingDrawnLayers()
566
         */
567
        public boolean isCachingDrawnLayers() {
568
                return bCacheDrawnLayers;
569
        }
570

    
571
        /* (non-Javadoc)
572
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#setCachingDrawnLayers(boolean)
573
         */
574
        public void setCachingDrawnLayers(boolean bCacheDrawnLayers) {
575
                this.bCacheDrawnLayers = bCacheDrawnLayers;
576
                if (bCacheDrawnLayers == false)
577
                        this.cacheImageDrawnLayers = null;
578
        }
579

    
580
        public BufferedImage getCacheImageDrawnLayers() {
581
                return cacheImageDrawnLayers;
582
        }
583

    
584
        public void setCacheImageDrawnLayers(BufferedImage cacheImageDrawnLayers) {
585
                this.cacheImageDrawnLayers = cacheImageDrawnLayers;
586
        }
587

    
588
        public boolean isDirty() {
589
                return bDirty;
590
        }
591

    
592
        public void setDirty(boolean dirty) {
593
                bDirty = dirty;
594
        }
595
        
596
        public boolean isAvailable() {
597
                return this.available;
598
        }
599
        
600
        public void setAvailable(boolean available) {
601
                this.available = available;
602
        }
603
        
604
        public void reload() throws DriverIOException {
605
                this.setAvailable(true);
606
        }
607
        
608
        public boolean visibleRequired() {
609
                return this.visible;
610
        }
611
        
612
        public String getInfoString() {
613
                return null;
614
        }
615

    
616
        public boolean isWritable() {
617
                // TODO Auto-generated method stub
618
                return false;
619
        }
620

    
621

    
622
}