Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / DefaultMapControl3D.java @ 497

History | View | Annotate | Download (26.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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 2
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

    
25
package org.gvsig.view3d.swing.impl;
26

    
27
import gov.nasa.worldwind.Configuration;
28
import gov.nasa.worldwind.Model;
29
import gov.nasa.worldwind.WorldWind;
30
import gov.nasa.worldwind.avlist.AVKey;
31
import gov.nasa.worldwind.avlist.AVList;
32
import gov.nasa.worldwind.awt.WorldWindowGLJPanel;
33
import gov.nasa.worldwind.globes.Earth;
34
import gov.nasa.worldwind.globes.EarthFlat;
35
import gov.nasa.worldwind.globes.ElevationModel;
36
import gov.nasa.worldwind.layers.CompassLayer;
37
import gov.nasa.worldwind.layers.Layer;
38
import gov.nasa.worldwind.layers.LayerList;
39
import gov.nasa.worldwind.layers.ScalebarLayer;
40
import gov.nasa.worldwind.layers.SkyColorLayer;
41
import gov.nasa.worldwind.layers.SkyGradientLayer;
42
import gov.nasa.worldwind.layers.StarsLayer;
43
import gov.nasa.worldwind.layers.ViewControlsLayer;
44
import gov.nasa.worldwind.layers.ViewControlsSelectListener;
45
import gov.nasa.worldwind.layers.WorldMapLayer;
46
import gov.nasa.worldwind.terrain.CompoundElevationModel;
47
import gov.nasa.worldwind.util.StatusBar;
48
import gov.nasa.worldwind.view.orbit.BasicOrbitView;
49
import gov.nasa.worldwind.view.orbit.FlatOrbitView;
50

    
51
import java.awt.BorderLayout;
52
import java.awt.Dimension;
53
import java.beans.PropertyChangeEvent;
54
import java.beans.PropertyChangeListener;
55
import java.util.HashMap;
56
import java.util.List;
57
import java.util.Map;
58

    
59
import javax.swing.JComponent;
60
import javax.swing.JPanel;
61

    
62
import org.gvsig.fmap.dal.exception.DataException;
63
import org.gvsig.fmap.mapcontext.MapContext;
64
import org.gvsig.fmap.mapcontext.layers.CancelationException;
65
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesSupport;
66
import org.gvsig.fmap.mapcontext.layers.FLayer;
67
import org.gvsig.fmap.mapcontext.layers.FLayers;
68
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
69
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
70
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
71
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
72
import org.gvsig.fmap.mapcontext.layers.LayerListener;
73
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
74
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
75
import org.gvsig.tools.exception.BaseException;
76
import org.gvsig.tools.observer.BaseNotification;
77
import org.gvsig.tools.observer.Notification;
78
import org.gvsig.tools.observer.ObservableHelper;
79
import org.gvsig.tools.observer.Observer;
80
import org.gvsig.tools.task.Cancellable;
81
import org.gvsig.tools.visitor.VisitCanceledException;
82
import org.gvsig.view3d.lib.api.View3DLocator;
83
import org.gvsig.view3d.lib.api.View3DManager;
84
import org.gvsig.view3d.lib.api.properties.GeneralProperties3D;
85
import org.gvsig.view3d.lib.api.properties.LayerProperties3D;
86
import org.gvsig.view3d.lib.api.properties.MapControlProperties3D;
87
import org.gvsig.view3d.lib.api.properties.RasterLayerProperties3D;
88
import org.gvsig.view3d.lib.api.properties.VectorialLayerProperties3D;
89
import org.gvsig.view3d.swing.api.MapControl3D;
90
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE;
91
import org.gvsig.view3d.swing.impl.data.DefaultLayerConverter;
92
import org.gvsig.view3d.swing.impl.data.DefaultTiledImageLayer;
93
import org.gvsig.view3d.swing.impl.data.LayerConverter;
94
import org.slf4j.Logger;
95
import org.slf4j.LoggerFactory;
96

    
97
/**
98
 * Default implementation of {@link MapControl3D}.
99
 * 
100
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
101
 */
102
@SuppressWarnings("deprecation")
103
public class DefaultMapControl3D extends JPanel implements MapControl3D {
104

    
105
    private static final long serialVersionUID = 2024899922367896097L;
106

    
107
    private static final Logger LOG = LoggerFactory
108
        .getLogger(DefaultMapControl3D.class);
109

    
110
    private Map<FLayer, Long> drawLayerStatus;
111

    
112
    private ObservableHelper observableHelper;
113

    
114
    /**
115
     * Name of gvSIG MapContext property
116
     */
117
    public static final String GVSIG_MAPCONTROL3D =
118
        "org.gvsig.view3d.swing.api.MapControl3D";
119

    
120
    /**
121
     * Name of gvSIG Layer property
122
     */
123
    public static final String GVSIG_LAYER =
124
        "org.gvsig.fmap.mapcontext.layers.FLayer";
125

    
126
    private MapContext mapContext;
127

    
128
    private MapControlProperties3D properties;
129

    
130
    private ExtendedPropertiesSupport viewProperties;
131

    
132
    private WorldWindowGLJPanel wwd;
133

    
134
    private StatusBar statusBar;
135

    
136
    private TYPE type;
137

    
138
    private Cancellable cancellable;
139

    
140
    /**
141
     * Default constructor of {@link DefaultMapControl3D}. Initializes local
142
     * fileds, sets configuration depends on {@link TYPE} and adds
143
     * <code>MapContext</code> layers.
144
     * 
145
     * @param theMapContext
146
     *            associated <code>MapContext</code>
147
     * @param type
148
     *            Type of this {@link DefaultMapControl3D}. See {@link TYPE}.
149
     * @param viewProperties
150
     *            View properties to register this <code>MapControl3D</code>
151
     */
152
    public DefaultMapControl3D(MapContext theMapContext, TYPE type,
153
        ExtendedPropertiesSupport viewProperties) {
154

    
155
        super(new BorderLayout());
156

    
157
        this.mapContext = theMapContext;
158
        this.type = type;
159
        this.cancellable = getCancellable();
160
        this.observableHelper = new ObservableHelper();
161
        this.drawLayerStatus = new HashMap<FLayer, Long>();
162
        this.viewProperties = viewProperties;
163

    
164
        // Set mode before instantiation
165
        if (type == TYPE.SPHERE) {
166
            setSpherePanelConfiguration();
167
        } else if (type == TYPE.FLAT) {
168
            setFlatPanelConfiguration();
169
        }
170

    
171
        super.add(getWwd(), BorderLayout.CENTER);
172

    
173
        super.add(getStatusBar(), BorderLayout.SOUTH);
174

    
175
        try {
176
            addGvSIGLayers(mapContext.getLayers());
177
        } catch (BaseException e) {
178
            LOG.info("Can't add MapControl layers", e);
179
        }
180

    
181
        addLayerCollectionListener(mapContext.getLayers());
182

    
183
        setGeneral3DProperties();
184

    
185
        View3DManager manager = View3DLocator.getManager();
186
        this.properties = manager.getMapControl3DProperties(viewProperties);
187
        addChangePropertyListener(this.properties);
188
        setMapControl3DProperties();
189
    }
190

    
191
    private void setGeneral3DProperties() {
192

    
193
        View3DManager manager = View3DLocator.getManager();
194
        GeneralProperties3D general3DProperties =
195
            manager.getGeneral3DProperties();
196

    
197
        this.setAtmosphereVisibility(general3DProperties
198
            .getAtmosphereVisibility());
199
        this.setMiniMapVisibility(general3DProperties.getMinimapVisibility());
200
        this.setNorthIndicatorVisibility(general3DProperties
201
            .getNorthIndicatorVisibility());
202
        this.setStarsBackgroundVisibility(general3DProperties
203
            .getStarsBackgroundVisibility());
204
        this.setScaleVisibility(general3DProperties.getScaleVisibility());
205
    }
206

    
207
    private void addChangePropertyListener(MapControlProperties3D properties) {
208
        properties.addPropertyChangeListener(new PropertyChangeListener() {
209

    
210
            public void propertyChange(PropertyChangeEvent evt) {
211

    
212
                String propertyName = evt.getPropertyName();
213

    
214
                if (MapControlProperties3D.SPHERE_VERTICAL_EXAGGERATION_PROPERTY_NAME
215
                    .equals(propertyName) && getType() == TYPE.SPHERE) {
216
                    setVerticalExaggeration((Double) evt.getNewValue());
217

    
218
                } else if (MapControlProperties3D.FLAT_VERTICAL_EXAGGERATION_PROPERTY_NAME
219
                    .equals(propertyName) && getType() == TYPE.FLAT) {
220
                    setVerticalExaggeration((Double) evt.getNewValue());
221

    
222
                } else if (MapControlProperties3D.BLUE_MARBEL_VISIBILITY_PROPERTY_NAME
223
                    .equals(propertyName)) {
224
                    boolean visibility = (Boolean) evt.getNewValue();
225
                    setBlueMarbelLayerVisibility(visibility);
226

    
227
                } else if (MapControlProperties3D.NASA_LANDSAT_VISIBILITY_PROPERTY_NAME
228
                    .equals(propertyName)) {
229
                    boolean visibility = (Boolean) evt.getNewValue();
230
                    setNasaLandsatLayerVisibility(visibility);
231

    
232
                } else if (MapControlProperties3D.DEFAULT_ELEVATION_VISIBILITY_PROPERTY_NAME
233
                    .equals(propertyName)) {
234
                    boolean visibility = (Boolean) evt.getNewValue();
235
                    setDefaultElevationVisibility(visibility);
236
                }
237
            }
238
        });
239

    
240
    }
241

    
242
    private void addLayerCollectionListener(FLayers layers) {
243

    
244
        if (layers == null) {
245
            return;
246
        }
247

    
248
        layers.addLayerCollectionListener(new LayerCollectionListener() {
249

    
250
            public void visibilityChanged(LayerCollectionEvent e)
251
                throws CancelationException {
252
            }
253

    
254
            public void layerRemoving(LayerCollectionEvent e)
255
                throws CancelationException {
256
                FLayer layer = e.getAffectedLayer();
257
                removeGvSIGLayer(layer);
258
            }
259

    
260
            public void layerRemoved(LayerCollectionEvent e) {
261
            }
262

    
263
            public void layerMoving(LayerPositionEvent e)
264
                throws CancelationException {
265
            }
266

    
267
            public void layerMoved(LayerPositionEvent e) {
268
                int oldPos = e.getOldPos();
269
                int newPos = e.getNewPos();
270
                FLayer layer = e.getAffectedLayer();
271

    
272
                LayerList layers = getWwd().getModel().getLayers();
273
                Layer tileImageLayer = getTileImageLayer(layer);
274
                int tiledIndex = layers.indexOf(tileImageLayer);
275

    
276
                layers.remove(tiledIndex);
277
                layers.add(tiledIndex + (newPos - oldPos), tileImageLayer);
278
            }
279

    
280
            public void layerAdding(LayerCollectionEvent e)
281
                throws CancelationException {
282
            }
283

    
284
            public void layerAdded(LayerCollectionEvent e) {
285
                FLayer layer = e.getAffectedLayer();
286
                try {
287
                    addGvSIGLayer(layer);
288
                } catch (BaseException ex) {
289
                        LOG.info("Can't add layer {} from {} event",
290
                            new Object[] { layer.getName(), e }, ex);
291
                }
292
            }
293
        });
294
    }
295

    
296
    private void addLayerListener(FLayer layer) {
297

    
298
        if (layer == null) {
299
            return;
300
        }
301

    
302
        layer.addLayerListener(new LayerListener() {
303

    
304
            public void visibilityChanged(LayerEvent e) {
305
                FLayer layer = e.getSource();
306
                Layer tiledImageLayer = getTileImageLayer(layer);
307
                if (tiledImageLayer != null) {
308
                    tiledImageLayer.setEnabled(layer.isVisible());
309
                }
310
            }
311

    
312
            public void nameChanged(LayerEvent e) {
313
            }
314

    
315
            public void editionChanged(LayerEvent e) {
316
            }
317

    
318
            public void drawValueChanged(LayerEvent e) {
319
                MapControlProperties3D properties = getProperties();
320
                if (properties.getAutoLayerSynchronize()) {
321
                    FLayer layer = e.getSource();
322
                    if (layer.isAvailable()) {
323
                        synchronizeLayers();
324
                    }
325
                }
326
            }
327

    
328
            public void activationChanged(LayerEvent e) {
329
            }
330
        });
331
    }
332

    
333
    private void addGvSIGLayer(FLayer layer) throws DataException {
334
        addLayerListener(layer);
335
        LayerList layers = getWwd().getModel().getLayers();
336
        addGvSIGLayer(layer, layers.size());
337
    }
338

    
339
    private void addGvSIGLayer(FLayer layer, int index) throws DataException {
340
        addLayerListener(layer);
341
        LayerConverter converter = new DefaultLayerConverter();
342

    
343
        View3DManager manager = View3DLocator.getManager();
344
        LayerProperties3D layerProperties = manager.getLayerProperties(layer);
345

    
346
        if (layerProperties.getElevation() == false) {
347

    
348
            if ((layerProperties instanceof VectorialLayerProperties3D && ((VectorialLayerProperties3D) layerProperties)
349
                .getRasterized() == true)
350
                || layerProperties instanceof RasterLayerProperties3D) {
351

    
352
                Layer rasterTiledLayer = converter.convertToLayer(this, layer);
353
                getWwd().getModel().getLayers().add(index, rasterTiledLayer);
354
                drawLayerStatus.put(layer, (long) layer.getDrawVersion());
355

    
356
            } else {
357
                return; // TODO Convert to 3D vectorial layer
358
            }
359
        } else {
360
            ElevationModel elevationModel =
361
                converter.convertToElevationModel(this, layer);
362
            CompoundElevationModel cem =
363
                (CompoundElevationModel) getWwd().getModel().getGlobe()
364
                    .getElevationModel();
365
            cem.addElevationModel(elevationModel);
366
        }
367
    }
368

    
369
    private void addGvSIGLayers(FLayers layers) throws BaseException {
370
        if (layers == null) {
371
            return;
372
        }
373

    
374
        layers.accept(new LayersVisitor() {
375

    
376
            public void visit(Object obj) throws VisitCanceledException,
377
                BaseException {
378
                throw new UnsupportedOperationException();
379

    
380
            }
381

    
382
            public void visit(FLayer layer) throws BaseException {
383

    
384
                if (layer.isAvailable() && layer.isVisible()) {
385
                    addGvSIGLayer(layer);
386
                }
387
            }
388
        });
389

    
390
        getWwd().redraw();
391
    }
392

    
393
    protected void setBlueMarbelLayerVisibility(boolean visibility) {
394
        setWWLayerVisibility("Blue Marble May 2004", visibility);
395
    }
396

    
397
    protected void setDefaultElevationVisibility(boolean visibility) {
398

    
399
        ElevationModel elevationModel =
400
            getWwd().getModel().getGlobe().getElevationModel();
401
        if (elevationModel instanceof CompoundElevationModel) {
402
            CompoundElevationModel compoundElevationModel =
403
                (CompoundElevationModel) elevationModel;
404

    
405
            List<ElevationModel> elevationModels =
406
                compoundElevationModel.getElevationModels();
407

    
408
            for (ElevationModel eModel : elevationModels) {
409
                if (eModel.getName().equals("USA 10m, World 30m, Ocean 900m")) {
410
                    eModel.setEnabled(visibility);
411
                }
412
            }
413

    
414
        } else {
415
            elevationModel.setEnabled(visibility);
416
        }
417

    
418
    }
419

    
420
    protected void setNasaLandsatLayerVisibility(boolean visibility) {
421
        setWWLayerVisibility("i-cubed Landsat", visibility);
422
    }
423

    
424
    @SuppressWarnings("rawtypes")
425
    private void setWWLayerVisibility(Class layerClazz, boolean visiblity) {
426
        List<Layer> layersByClass =
427
            getWwd().getModel().getLayers().getLayersByClass(layerClazz);
428

    
429
        for (Layer layer : layersByClass) {
430
            layer.setEnabled(visiblity);
431
        }
432
    };
433

    
434
    private void setWWLayerVisibility(String layerName, boolean visibilty) {
435
        Layer layer = getWwd().getModel().getLayers().getLayerByName(layerName);
436
        if (layer != null) {
437
            layer.setEnabled(visibilty);
438
        }
439
    };
440

    
441
    private void addNavigation() {
442

    
443
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
444

    
445
        Model model = getWwd().getModel();
446
        model.getLayers().add(controlsLayer);
447

    
448
        getWwd().addSelectListener(
449
            new ViewControlsSelectListener(wwd, controlsLayer));
450
    }
451

    
452
    public JComponent asJComponent() {
453
        return this;
454
    }
455

    
456
    public void dispose() {
457

    
458
        Thread disposeThread = new Thread(new Runnable() {
459

    
460
            public void run() {
461
                Notification beforeDisposeNotification =
462
                    new BaseNotification(
463
                        MapControl3D.BEFORE_DISPOSE_MAPCONTEX3D_NOTIFICATION,
464
                        null);
465

    
466
                observableHelper.notifyObservers(DefaultMapControl3D.this,
467
                    beforeDisposeNotification);
468

    
469
                while (WorldWind.getRetrievalService().hasActiveTasks()) {
470
                    try {
471
                        Thread.sleep(100);
472
                    } catch (InterruptedException e) {
473
                        LOG.warn("This thread has been interrupted, dispose action can not be performed");
474
                        return;
475
                    }
476
                }
477

    
478
                disposeWwd();
479

    
480
                if (getType() == TYPE.SPHERE) {
481
                    viewProperties.getExtendedProperties().remove(
482
                        "spehereMapControl3D");
483
                } else if (getType() == TYPE.FLAT) {
484
                    viewProperties.getExtendedProperties().remove(
485
                        "flatMapControl3D");
486
                }
487

    
488
                Notification afterDisposeNotification =
489
                    new BaseNotification(
490
                        MapControl3D.AFTER_DISPOSE_MAPCONTEX3D_NOTIFICATION,
491
                        null);
492

    
493
                observableHelper.notifyObservers(DefaultMapControl3D.this,
494
                    afterDisposeNotification);
495
            }
496
        }, "Dispose World Wind thread");
497

    
498
        disposeThread.start();
499
    }
500

    
501
    public Cancellable getCancellable() {
502
        if (this.cancellable == null) {
503
            this.cancellable = new Cancellable() {
504

    
505
                private boolean cancel = false;
506

    
507
                public void setCanceled(boolean canceled) {
508
                    this.cancel = canceled;
509
                }
510

    
511
                public boolean isCanceled() {
512
                    return cancel;
513
                }
514
            };
515
        }
516

    
517
        return this.cancellable;
518
    }
519

    
520
    public MapContext getMapContext() {
521
        return this.mapContext;
522
    }
523

    
524
    private StatusBar getStatusBar() {
525
        if (statusBar == null) {
526
            statusBar = new StatusBar();
527
            this.add(statusBar, BorderLayout.PAGE_END);
528
            statusBar.setEventSource(wwd);
529
        }
530
        return statusBar;
531
    }
532

    
533
    private Layer getTileImageLayer(FLayer layer) {
534
        List<Layer> layers =
535
            getWwd().getModel().getLayers()
536
                .getLayersByClass(DefaultTiledImageLayer.class);
537
        for (Layer tiledLayer : layers) {
538
            AVList constructionParams =
539
                (AVList) tiledLayer.getValue(AVKey.CONSTRUCTION_PARAMETERS);
540
            if (constructionParams != null) {
541
                FLayer likedLayer =
542
                    (FLayer) constructionParams.getValue(GVSIG_LAYER);
543
                if (layer.equals(likedLayer)) {
544
                    return tiledLayer;
545
                }
546
            }
547
        }
548
        return null;
549
    }
550

    
551
    public TYPE getType() {
552
        return this.type;
553
    }
554

    
555
    public double getVerticalExaggeration() {
556
        return getWwd().getSceneController().getVerticalExaggeration();
557
    }
558

    
559
    protected WorldWindowGLJPanel getWwd() {
560
        if (this.wwd == null) {
561
            intializeWWPanel();
562
        }
563
        return this.wwd;
564
    }
565

    
566
    private void disposeWwd() {
567
        if (getWwd() != null) {
568
            this.wwd = null;
569
        }
570
    }
571

    
572
    public void setAtmosphereVisibility(boolean visibility) {
573
        if (getType() == TYPE.SPHERE) {
574
            setWWLayerVisibility(SkyGradientLayer.class, visibility);
575
        } else if (getType() == TYPE.FLAT) {
576
            setWWLayerVisibility(SkyColorLayer.class, visibility);
577
        }
578
    }
579

    
580
    public void setMiniMapVisibility(boolean visiblity) {
581
        setWWLayerVisibility(WorldMapLayer.class, visiblity);
582
    }
583

    
584
    public void setNorthIndicatorVisibility(boolean visibility) {
585
        setWWLayerVisibility(CompassLayer.class, visibility);
586
    }
587

    
588
    public void setScaleVisibility(boolean visibility) {
589
        setWWLayerVisibility(ScalebarLayer.class, visibility);
590
    }
591

    
592
    public void setStarsBackgroundVisibility(boolean visibility) {
593
        setWWLayerVisibility(StarsLayer.class, visibility);
594
    }
595

    
596
    private void intializeWWPanel() {
597
        wwd = new WorldWindowGLJPanel();
598

    
599
        View3DManager manager = View3DLocator.getManager();
600
        GeneralProperties3D general3dProperties =
601
            manager.getGeneral3DProperties();
602
        wwd.setPreferredSize(new Dimension(general3dProperties
603
            .getDefaultViewWidht(), general3dProperties.getDefaultViewHeight()));
604

    
605
        // Create the default model as described in the current worldwind
606
        // properties.
607
        Model m =
608
            (Model) WorldWind
609
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
610
        getWwd().setModel(m);
611

    
612
        // Add view control layer
613
        addNavigation();
614
    }
615

    
616
    public void reloadLayers() {
617
        getWwd().getModel().getLayers().removeAll();
618

    
619
        if (this.mapContext != null && this.mapContext.getLayers() != null) {
620
            try {
621
                addGvSIGLayers(mapContext.getLayers());
622
            } catch (BaseException e) {
623
                LOG.error("Can't add MapContext layers of {}",
624
                    mapContext.getLayers(), e);
625
            }
626
        }
627
    }
628

    
629
    private void setFlatPanelConfiguration() {
630
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
631
            EarthFlat.class.getName());
632
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
633
            FlatOrbitView.class.getName());
634
    }
635

    
636
    private void setMapControl3DProperties() {
637

    
638
        double verticalExaggeration = 0;
639
        if (getType() == TYPE.SPHERE) {
640
            verticalExaggeration =
641
                this.properties.getSphereVerticalExaggeration();
642
        } else if (getType() == TYPE.FLAT) {
643
            verticalExaggeration =
644
                this.properties.getFlatVerticalExaggeration();
645
        }
646
        setVerticalExaggeration(verticalExaggeration);
647

    
648
        boolean visibility = this.properties.getBlueMarbleLayerVisibility();
649
        setBlueMarbelLayerVisibility(visibility);
650

    
651
        visibility = this.properties.getNasaLandsatLayerVisibility();
652
        setNasaLandsatLayerVisibility(visibility);
653

    
654
        visibility = this.properties.getDefaultElevationVisibility();
655
        setDefaultElevationVisibility(visibility);
656
    }
657

    
658
    private void setSpherePanelConfiguration() {
659
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
660
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
661
            BasicOrbitView.class.getName());
662
    }
663

    
664
    public void setVerticalExaggeration(double verticalExaggeration) {
665
        getWwd().getSceneController().setVerticalExaggeration(
666
            verticalExaggeration);
667
    }
668

    
669
    public void synchronizeLayers() {
670

    
671
        for (FLayer layer : drawLayerStatus.keySet()) {
672
            if (layer instanceof FLyrDefault) {
673
                FLyrDefault defaultLayer = (FLyrDefault) layer;
674

    
675
                if (defaultLayer.hasChangedForDrawing(drawLayerStatus
676
                    .get(layer))) {
677
                    replaceGvSIGLayer(defaultLayer);
678
                }
679
            }
680
        }
681
    }
682

    
683
    private int removeGvSIGLayer(FLayer layer) {
684
        List<Layer> loadedLayers = getWwd().getModel().getLayers();
685
        Layer tiledImageLayer = getTileImageLayer(layer);
686

    
687
        if (tiledImageLayer == null) {
688
            return -1;
689
        }
690

    
691
        int layerIndex = loadedLayers.indexOf(tiledImageLayer);
692
        getWwd().getModel().getLayers().remove(tiledImageLayer);
693
        drawLayerStatus.remove(layer);
694
        return layerIndex;
695
    }
696

    
697
    private void replaceGvSIGLayer(FLayer layer) {
698
        // Remove layer without update drawLayerStatus map.
699
        List<Layer> loadedLayers = getWwd().getModel().getLayers();
700
        Layer tiledImageLayer = getTileImageLayer(layer);
701

    
702
        if (tiledImageLayer == null) {
703
            return;
704
        }
705

    
706
        int layerIndex = loadedLayers.indexOf(tiledImageLayer);
707
        getWwd().getModel().getLayers().remove(tiledImageLayer);
708

    
709
        // Add new layer at the same index
710
        if (layerIndex >= 0) {
711
            try {
712
                addGvSIGLayer(layer, layerIndex);
713
            } catch (DataException e) {
714
                LOG.error("Can't add {} at {}", new Object[] { layer.getName(),
715
                    layerIndex }, e);
716
            }
717
        }
718
    }
719

    
720
    public void synchronizeViewPorts() {
721
        // TODO
722
        throw new UnsupportedOperationException();
723
    }
724

    
725
    public void addObserver(Observer o) {
726
        if (observableHelper != null) {
727
            observableHelper.addObserver(o);
728
        }
729
    }
730

    
731
    public void deleteObserver(Observer o) {
732
        if (observableHelper != null) {
733
            observableHelper.deleteObserver(o);
734
        }
735

    
736
    }
737

    
738
    public void deleteObservers() {
739
        if (observableHelper != null) {
740
            observableHelper.deleteObservers();
741
        }
742
    }
743

    
744
    public boolean getAtmosphereVisibility() {
745
        if (isWWLayerVisible(SkyGradientLayer.class)
746
            || isWWLayerVisible(SkyColorLayer.class)) {
747
            return true;
748
        }
749
        return false;
750
    }
751

    
752
    public boolean getMinimapVisibility() {
753
        if (isWWLayerVisible(WorldMapLayer.class)) {
754
            return true;
755
        }
756
        return false;
757
    }
758

    
759
    public boolean getNorthIndicatorVisibility() {
760
        if (isWWLayerVisible(CompassLayer.class)) {
761
            return true;
762
        }
763
        return false;
764
    }
765

    
766
    public boolean getStarBackgroundVisibility() {
767
        if (isWWLayerVisible(StarsLayer.class)) {
768
            return true;
769
        }
770
        return false;
771
    }
772

    
773
    public boolean getScaleVisibility() {
774
        if (isWWLayerVisible(ScalebarLayer.class)) {
775
            return true;
776
        }
777
        return false;
778
    }
779

    
780
    @SuppressWarnings("rawtypes")
781
    private boolean isWWLayerVisible(Class clazz) {
782
        List<Layer> layersByClass =
783
            getWwd().getModel().getLayers().getLayersByClass(clazz);
784
        for (Layer layer : layersByClass) {
785
            return layer.isEnabled();
786
        }
787
        return false;
788
    }
789

    
790
    public MapControlProperties3D getProperties() {
791
        return properties;
792
    }
793
}