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

History | View | Annotate | Download (10.8 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.exception.BaseException;
66
import org.gvsig.tools.visitor.VisitCanceledException;
67
import org.gvsig.view3d.swing.api.MapControl3D;
68
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE;
69
import org.gvsig.view3d.swing.impl.data.DefaultLayerConverter;
70
import org.gvsig.view3d.swing.impl.data.LayerConverter;
71

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

    
78
    private static final long serialVersionUID = 2024899922367896097L;
79

    
80
    private static final Logger logger = LoggerFactory
81
        .getLogger(DefaultMapControl3D.class);
82
    
83
    /**
84
     * Name of gvSIG MapContext property
85
     */
86
    public static final String GVSIG_MAPCONTEXT =
87
        "org.gvsig.fmap.mapcontext.MapContext";
88

    
89
    private MapContext mapContext;
90

    
91
    private WorldWindowGLJPanel wwd;
92

    
93
    private StatusBar statusBar;
94

    
95
    private TYPE type;
96

    
97
    public DefaultMapControl3D(MapContext theMapContext, TYPE type) {
98

    
99
        super(new BorderLayout());
100

    
101
        this.mapContext = theMapContext;
102
        this.type = type;
103

    
104
        // Set mode before instantiation
105
        if (type == TYPE.SPHERE) {
106
            setSpherePanelConfiguration();
107
        } else if (type == TYPE.FLAT) {
108
            setFlatPanelConfiguration();
109
        }
110

    
111
        super.add(getWwd(), BorderLayout.CENTER);
112

    
113
        super.add(getStatusBar(), BorderLayout.SOUTH);
114

    
115
        try {
116
            addLayers();
117
        } catch (BaseException e) {
118
            logger.info("Can't add MapControl layers", e);
119
        }
120

    
121
    }
122

    
123
    private void addGvSIGLayer(FLayer layer) throws DataException {
124

    
125
        LayerConverter converter = new DefaultLayerConverter();
126
        
127
        // TODO Check load mode to create WW Layer
128
        Layer rasterTiledLayer = converter.convertToLayer(mapContext, layer);
129

    
130
        getWwd().getModel().getLayers().add(rasterTiledLayer);
131
    }
132

    
133
    private void addLayers() throws BaseException {
134
        mapContext.getLayers().accept(new LayersVisitor() {
135

    
136
            public void visit(Object obj) throws VisitCanceledException,
137
                BaseException {
138
                throw new UnsupportedOperationException();
139

    
140
            }
141

    
142
            public void visit(FLayer layer) throws BaseException {
143
                // TODO Make new threads to add layers
144
                if (layer.isAvailable() && layer.isVisible()) {
145
                    addGvSIGLayer(layer);
146
                }
147
            }
148
        });
149

    
150
        getWwd().redraw();
151
    }
152

    
153
    @SuppressWarnings("rawtypes")
154
    private void addWWLayer(Class layerClazz) throws InstantiationException,
155
        IllegalAccessException, IllegalArgumentException,
156
        InvocationTargetException {
157
        LayerList layerList = getWwd().getModel().getLayers();
158

    
159
        if (isAlreadyAdded(layerList, layerClazz)) {
160
            return;
161
        }
162

    
163
        Constructor[] constructors = layerClazz.getConstructors();
164
        for (int i = 0; i < constructors.length; i++) {
165
            if (constructors[i].getParameterTypes().length == 0) {
166
                layerList.add((Layer) constructors[i].newInstance());
167
            }
168
        }
169
    }
170

    
171
    private void addNavigation() {
172

    
173
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
174

    
175
        Model model = getWwd().getModel();
176
        model.getLayers().add(controlsLayer);
177

    
178
        getWwd().addSelectListener(
179
            new ViewControlsSelectListener(wwd, controlsLayer));
180
    }
181

    
182
    public JComponent asJComponent() {
183
        return this;
184
    }
185

    
186
    public MapContext getMapContext() {
187
        return this.mapContext;
188
    }
189

    
190
    private StatusBar getStatusBar() {
191
        if (statusBar == null) {
192
            statusBar = new StatusBar();
193
            this.add(statusBar, BorderLayout.PAGE_END);
194
            statusBar.setEventSource(wwd);
195
        }
196
        return statusBar;
197
    }
198

    
199
    public TYPE getType() {
200
        return this.type;
201
    }
202

    
203
    public double getVerticalExaggeration() {
204
        return getWwd().getSceneController().getVerticalExaggeration();
205
    }
206

    
207
    protected WorldWindowGLJPanel getWwd() {
208
        if (this.wwd == null) {
209
            intializeWWPanel();
210
        }
211
        return this.wwd;
212
    }
213

    
214
    public void hideAtmosphere() {
215
        hideLayer(SkyGradientLayer.class);
216
    }
217

    
218
    @SuppressWarnings("rawtypes")
219
    private void hideLayer(Class layerClazz) {
220
        LayerList layers = getWwd().getModel().getLayers();
221
        List<Layer> layersByClass = layers.getLayersByClass(layerClazz);
222
        layers.removeAll(layersByClass);
223
    }
224

    
225
    public void hideMiniMap() {
226
        hideLayer(WorldMapLayer.class);
227
    }
228

    
229
    public void hideNorthIndicator() {
230
        hideLayer(CompassLayer.class);
231
    }
232

    
233
    public void hideScale() {
234
        hideLayer(ScalebarLayer.class);
235
    }
236

    
237
    public void hideStarBackground() {
238
        hideLayer(StarsLayer.class);
239
    }
240

    
241
    private void intializeWWPanel() {
242
        wwd = new WorldWindowGLJPanel();
243
        wwd.setPreferredSize(new Dimension(800, 600));
244

    
245
        // Create the default model as described in the current worldwind
246
        // properties.
247
        Model m =
248
            (Model) WorldWind
249
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
250
        getWwd().setModel(m);
251

    
252
        // Add view control layer
253
        addNavigation();
254
    }
255

    
256
    private boolean isAlreadyAdded(LayerList layerList,
257
        @SuppressWarnings("rawtypes") Class clazz) {
258

    
259
        List<Layer> layersByClass = layerList.getLayersByClass(clazz);
260

    
261
        return layersByClass.size() > 0;
262
    }
263

    
264
    public void reloadLayers() {
265
        // TODO
266
        throw new UnsupportedOperationException();
267
    }
268

    
269
    private void removeAllLayers() {
270
        getWwd().getModel().getLayers().removeAll();
271
    }
272

    
273
    private void setFlatPanelConfiguration() {
274
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
275
            EarthFlat.class.getName());
276
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
277
            FlatOrbitView.class.getName());
278
    }
279

    
280
    public void setMapContext(MapContext theMapContext) {
281
        this.mapContext = theMapContext;
282
    }
283

    
284
    private void setSpherePanelConfiguration() {
285
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
286
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
287
            BasicOrbitView.class.getName());
288
    }
289

    
290
    public void setVerticalExaggeration(double verticalExaggeration) {
291
        getWwd().getSceneController().setVerticalExaggeration(
292
            verticalExaggeration);
293
    }
294

    
295
    public void showAtmosphere() {
296
        try {
297
            Globe globe = getWwd().getModel().getGlobe();
298
            if (globe instanceof Earth) {
299
                getWwd().getModel().getLayers().add(new SkyGradientLayer());
300
                addWWLayer(SkyGradientLayer.class);
301
            } else if (globe instanceof EarthFlat) {
302
                getWwd().getModel().getLayers().add(new SkyColorLayer());
303
                addWWLayer(SkyColorLayer.class);
304
            }
305
        } catch (Exception e) {
306
            logger.warn(
307
                "Can't add " + SkyGradientLayer.class.getCanonicalName()
308
                    + " or " + SkyColorLayer.class.getCanonicalName() + " to "
309
                    + getWwd().getModel().toString(), e);
310
        }
311
    }
312

    
313
    public void showMiniMap() {
314
        try {
315
            addWWLayer(WorldMapLayer.class);
316
        } catch (Exception e) {
317
            logger.warn("Can't add " + WorldMapLayer.class.getCanonicalName()
318
                + "to " + getWwd().getModel().toString(), e);
319
        }
320
    }
321

    
322
    public void showNortIndicator() {
323
        try {
324
            addWWLayer(CompassLayer.class);
325
        } catch (Exception e) {
326
            logger.warn("Can't add " + CompassLayer.class.getCanonicalName()
327
                + " to " + getWwd().getModel().toString(), e);
328
        }
329
    }
330

    
331
    public void showScale() {
332
        try {
333
            addWWLayer(ScalebarLayer.class);
334
        } catch (Exception e) {
335
            logger.warn("Can't add " + ScalebarLayer.class.getCanonicalName()
336
                + " to " + getWwd().getModel().toString(), e);
337
        }
338
    }
339

    
340
    public void showStarBackgroundLayer() {
341
        try {
342
            addWWLayer(StarsLayer.class);
343
        } catch (Exception e) {
344
            logger.warn("Can't add " + StarsLayer.class.getCanonicalName()
345
                + " to " + getWwd().getModel().toString(), e);
346
        }
347
    }
348

    
349
    public void synchronizeLayers() {
350
        // TODO
351
        throw new UnsupportedOperationException();
352
    }
353

    
354
    public void synchronizeViewPorts() {
355
        // TODO
356
        throw new UnsupportedOperationException();
357
    }
358

    
359
}