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

History | View | Annotate | Download (11.2 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.awt.WorldWindowGLJPanel;
32
import gov.nasa.worldwind.globes.Earth;
33
import gov.nasa.worldwind.globes.EarthFlat;
34
import gov.nasa.worldwind.globes.Globe;
35
import gov.nasa.worldwind.layers.CompassLayer;
36
import gov.nasa.worldwind.layers.Layer;
37
import gov.nasa.worldwind.layers.LayerList;
38
import gov.nasa.worldwind.layers.ScalebarLayer;
39
import gov.nasa.worldwind.layers.SkyColorLayer;
40
import gov.nasa.worldwind.layers.SkyGradientLayer;
41
import gov.nasa.worldwind.layers.StarsLayer;
42
import gov.nasa.worldwind.layers.ViewControlsLayer;
43
import gov.nasa.worldwind.layers.ViewControlsSelectListener;
44
import gov.nasa.worldwind.layers.WorldMapLayer;
45
import gov.nasa.worldwind.util.StatusBar;
46
import gov.nasa.worldwind.view.orbit.BasicOrbitView;
47
import gov.nasa.worldwind.view.orbit.FlatOrbitView;
48

    
49
import java.awt.BorderLayout;
50
import java.awt.Dimension;
51
import java.lang.reflect.Constructor;
52
import java.lang.reflect.InvocationTargetException;
53
import java.util.List;
54

    
55
import javax.swing.JComponent;
56
import javax.swing.JPanel;
57

    
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

    
61
import org.gvsig.fmap.dal.exception.DataException;
62
import org.gvsig.fmap.mapcontext.MapContext;
63
import org.gvsig.fmap.mapcontext.layers.FLayer;
64
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
65
import org.gvsig.tools.dispose.Disposable;
66
import org.gvsig.tools.exception.BaseException;
67
import org.gvsig.tools.visitor.VisitCanceledException;
68
import org.gvsig.view3d.swing.api.MapControl3D;
69
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE;
70
import org.gvsig.view3d.swing.impl.data.DefaultLayerConverter;
71
import org.gvsig.view3d.swing.impl.data.LayerConverter;
72

    
73
/**
74
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
75
 *
76
 */
77
public class DefaultMapControl3D extends JPanel implements MapControl3D, Disposable{
78

    
79
    private static final long serialVersionUID = 2024899922367896097L;
80

    
81
    private static final Logger LOG = LoggerFactory
82
        .getLogger(DefaultMapControl3D.class);
83
    
84
    /**
85
     * Name of gvSIG MapContext property
86
     */
87
    public static final String GVSIG_MAPCONTEXT =
88
        "org.gvsig.fmap.mapcontext.MapContext";
89
    
90
    /**
91
     * Name of gvSIG Layer property
92
     */
93
    public static final String GVSIG_LAYER = "org.gvsig.fmap.mapcontext.layers.FLayer";
94

    
95
    private MapContext mapContext;
96

    
97
    private WorldWindowGLJPanel wwd;
98

    
99
    private StatusBar statusBar;
100

    
101
    private TYPE type;
102

    
103
    public DefaultMapControl3D(MapContext theMapContext, TYPE type) {
104

    
105
        super(new BorderLayout());
106

    
107
        this.mapContext = theMapContext;
108
        this.type = type;
109

    
110
        // Set mode before instantiation
111
        if (type == TYPE.SPHERE) {
112
            setSpherePanelConfiguration();
113
        } else if (type == TYPE.FLAT) {
114
            setFlatPanelConfiguration();
115
        }
116

    
117
        super.add(getWwd(), BorderLayout.CENTER);
118

    
119
        super.add(getStatusBar(), BorderLayout.SOUTH);
120

    
121
        try {
122
            addLayers();
123
        } catch (BaseException e) {
124
            LOG.info("Can't add MapControl layers", e);
125
        }
126

    
127
    }
128

    
129
    private void addGvSIGLayer(FLayer layer) throws DataException {
130

    
131
        LayerConverter converter = new DefaultLayerConverter();
132
        
133
        // TODO Check load mode to create WW Layer
134
        Layer rasterTiledLayer = converter.convertToLayer(mapContext, layer);
135

    
136
        getWwd().getModel().getLayers().add(rasterTiledLayer);
137
    }
138

    
139
    private void addLayers() throws BaseException {
140
        mapContext.getLayers().accept(new LayersVisitor() {
141

    
142
            public void visit(Object obj) throws VisitCanceledException,
143
                BaseException {
144
                throw new UnsupportedOperationException();
145

    
146
            }
147

    
148
            public void visit(FLayer layer) throws BaseException {
149
                // TODO Make new threads to add layers
150
                if (layer.isAvailable() && layer.isVisible()) {
151
                    addGvSIGLayer(layer);
152
                }
153
            }
154
        });
155

    
156
        getWwd().redraw();
157
    }
158

    
159
    @SuppressWarnings("rawtypes")
160
    private void addWWLayer(Class layerClazz) throws InstantiationException,
161
        IllegalAccessException, IllegalArgumentException,
162
        InvocationTargetException {
163
        LayerList layerList = getWwd().getModel().getLayers();
164

    
165
        if (isAlreadyAdded(layerList, layerClazz)) {
166
            return;
167
        }
168

    
169
        Constructor[] constructors = layerClazz.getConstructors();
170
        for (int i = 0; i < constructors.length; i++) {
171
            if (constructors[i].getParameterTypes().length == 0) {
172
                layerList.add((Layer) constructors[i].newInstance());
173
            }
174
        }
175
    }
176

    
177
    private void addNavigation() {
178

    
179
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
180

    
181
        Model model = getWwd().getModel();
182
        model.getLayers().add(controlsLayer);
183

    
184
        getWwd().addSelectListener(
185
            new ViewControlsSelectListener(wwd, controlsLayer));
186
    }
187

    
188
    public JComponent asJComponent() {
189
        return this;
190
    }
191
    
192
    public void dispose() {
193
        
194
        if(this.wwd != null){
195
            this.wwd = null;
196
        }
197
        
198
        WorldWind.shutDown();
199
    }
200

    
201
    public MapContext getMapContext() {
202
        return this.mapContext;
203
    }
204

    
205
    private StatusBar getStatusBar() {
206
        if (statusBar == null) {
207
            statusBar = new StatusBar();
208
            this.add(statusBar, BorderLayout.PAGE_END);
209
            statusBar.setEventSource(wwd);
210
        }
211
        return statusBar;
212
    }
213

    
214
    public TYPE getType() {
215
        return this.type;
216
    }
217

    
218
    public double getVerticalExaggeration() {
219
        return getWwd().getSceneController().getVerticalExaggeration();
220
    }
221

    
222
    protected WorldWindowGLJPanel getWwd() {
223
        if (this.wwd == null) {
224
            intializeWWPanel();
225
        }
226
        return this.wwd;
227
    }
228

    
229
    public void hideAtmosphere() {
230
        hideLayer(SkyGradientLayer.class);
231
    }
232

    
233
    @SuppressWarnings("rawtypes")
234
    private void hideLayer(Class layerClazz) {
235
        LayerList layers = getWwd().getModel().getLayers();
236
        List<Layer> layersByClass = layers.getLayersByClass(layerClazz);
237
        layers.removeAll(layersByClass);
238
    }
239

    
240
    public void hideMiniMap() {
241
        hideLayer(WorldMapLayer.class);
242
    }
243

    
244
    public void hideNorthIndicator() {
245
        hideLayer(CompassLayer.class);
246
    }
247

    
248
    public void hideScale() {
249
        hideLayer(ScalebarLayer.class);
250
    }
251

    
252
    public void hideStarBackground() {
253
        hideLayer(StarsLayer.class);
254
    }
255

    
256
    private void intializeWWPanel() {
257
        wwd = new WorldWindowGLJPanel();
258
        wwd.setPreferredSize(new Dimension(800, 600));
259

    
260
        // Create the default model as described in the current worldwind
261
        // properties.
262
        Model m =
263
            (Model) WorldWind
264
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
265
        getWwd().setModel(m);
266

    
267
        // Add view control layer
268
        addNavigation();
269
    }
270

    
271
    private boolean isAlreadyAdded(LayerList layerList,
272
        @SuppressWarnings("rawtypes") Class clazz) {
273

    
274
        List<Layer> layersByClass = layerList.getLayersByClass(clazz);
275

    
276
        return layersByClass.size() > 0;
277
    }
278

    
279
    public void reloadLayers() {
280
        // TODO
281
        throw new UnsupportedOperationException();
282
    }
283

    
284
    private void removeAllLayers() {
285
        getWwd().getModel().getLayers().removeAll();
286
    }
287

    
288
    private void setFlatPanelConfiguration() {
289
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
290
            EarthFlat.class.getName());
291
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
292
            FlatOrbitView.class.getName());
293
    }
294

    
295
    public void setMapContext(MapContext theMapContext) {
296
        this.mapContext = theMapContext;
297
    }
298

    
299
    private void setSpherePanelConfiguration() {
300
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
301
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
302
            BasicOrbitView.class.getName());
303
    }
304

    
305
    public void setVerticalExaggeration(double verticalExaggeration) {
306
        getWwd().getSceneController().setVerticalExaggeration(
307
            verticalExaggeration);
308
    }
309

    
310
    public void showAtmosphere() {
311
        try {
312
            Globe globe = getWwd().getModel().getGlobe();
313
            if (globe instanceof Earth) {
314
                getWwd().getModel().getLayers().add(new SkyGradientLayer());
315
                addWWLayer(SkyGradientLayer.class);
316
            } else if (globe instanceof EarthFlat) {
317
                getWwd().getModel().getLayers().add(new SkyColorLayer());
318
                addWWLayer(SkyColorLayer.class);
319
            }
320
        } catch (Exception e) {
321
            LOG.warn(
322
                "Can't add " + SkyGradientLayer.class.getCanonicalName()
323
                    + " or " + SkyColorLayer.class.getCanonicalName() + " to "
324
                    + getWwd().getModel().toString(), e);
325
        }
326
    }
327

    
328
    public void showMiniMap() {
329
        try {
330
            addWWLayer(WorldMapLayer.class);
331
        } catch (Exception e) {
332
            LOG.warn("Can't add " + WorldMapLayer.class.getCanonicalName()
333
                + "to " + getWwd().getModel().toString(), e);
334
        }
335
    }
336

    
337
    public void showNortIndicator() {
338
        try {
339
            addWWLayer(CompassLayer.class);
340
        } catch (Exception e) {
341
            LOG.warn("Can't add " + CompassLayer.class.getCanonicalName()
342
                + " to " + getWwd().getModel().toString(), e);
343
        }
344
    }
345

    
346
    public void showScale() {
347
        try {
348
            addWWLayer(ScalebarLayer.class);
349
        } catch (Exception e) {
350
            LOG.warn("Can't add " + ScalebarLayer.class.getCanonicalName()
351
                + " to " + getWwd().getModel().toString(), e);
352
        }
353
    }
354

    
355
    public void showStarBackgroundLayer() {
356
        try {
357
            addWWLayer(StarsLayer.class);
358
        } catch (Exception e) {
359
            LOG.warn("Can't add " + StarsLayer.class.getCanonicalName()
360
                + " to " + getWwd().getModel().toString(), e);
361
        }
362
    }
363

    
364
    public void synchronizeLayers() {
365
        // TODO
366
        throw new UnsupportedOperationException();
367
    }
368

    
369
    public void synchronizeViewPorts() {
370
        // TODO
371
        throw new UnsupportedOperationException();
372
    }
373
}