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

History | View | Annotate | Download (11 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 LOG = 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
    /**
90
     * Name of gvSIG Layer property
91
     */
92
    public static final String GVSIG_LAYER = "org.gvsig.fmap.mapcontext.layers.FLayer";
93

    
94
    private MapContext mapContext;
95

    
96
    private WorldWindowGLJPanel wwd;
97

    
98
    private StatusBar statusBar;
99

    
100
    private TYPE type;
101

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

    
104
        super(new BorderLayout());
105

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

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

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

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

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

    
126
    }
127

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

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

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

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

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

    
145
            }
146

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

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

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

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

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

    
176
    private void addNavigation() {
177

    
178
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
179

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

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

    
187
    public JComponent asJComponent() {
188
        return this;
189
    }
190

    
191
    public MapContext getMapContext() {
192
        return this.mapContext;
193
    }
194

    
195
    private StatusBar getStatusBar() {
196
        if (statusBar == null) {
197
            statusBar = new StatusBar();
198
            this.add(statusBar, BorderLayout.PAGE_END);
199
            statusBar.setEventSource(wwd);
200
        }
201
        return statusBar;
202
    }
203

    
204
    public TYPE getType() {
205
        return this.type;
206
    }
207

    
208
    public double getVerticalExaggeration() {
209
        return getWwd().getSceneController().getVerticalExaggeration();
210
    }
211

    
212
    protected WorldWindowGLJPanel getWwd() {
213
        if (this.wwd == null) {
214
            intializeWWPanel();
215
        }
216
        return this.wwd;
217
    }
218

    
219
    public void hideAtmosphere() {
220
        hideLayer(SkyGradientLayer.class);
221
    }
222

    
223
    @SuppressWarnings("rawtypes")
224
    private void hideLayer(Class layerClazz) {
225
        LayerList layers = getWwd().getModel().getLayers();
226
        List<Layer> layersByClass = layers.getLayersByClass(layerClazz);
227
        layers.removeAll(layersByClass);
228
    }
229

    
230
    public void hideMiniMap() {
231
        hideLayer(WorldMapLayer.class);
232
    }
233

    
234
    public void hideNorthIndicator() {
235
        hideLayer(CompassLayer.class);
236
    }
237

    
238
    public void hideScale() {
239
        hideLayer(ScalebarLayer.class);
240
    }
241

    
242
    public void hideStarBackground() {
243
        hideLayer(StarsLayer.class);
244
    }
245

    
246
    private void intializeWWPanel() {
247
        wwd = new WorldWindowGLJPanel();
248
        wwd.setPreferredSize(new Dimension(800, 600));
249

    
250
        // Create the default model as described in the current worldwind
251
        // properties.
252
        Model m =
253
            (Model) WorldWind
254
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
255
        getWwd().setModel(m);
256

    
257
        // Add view control layer
258
        addNavigation();
259
    }
260

    
261
    private boolean isAlreadyAdded(LayerList layerList,
262
        @SuppressWarnings("rawtypes") Class clazz) {
263

    
264
        List<Layer> layersByClass = layerList.getLayersByClass(clazz);
265

    
266
        return layersByClass.size() > 0;
267
    }
268

    
269
    public void reloadLayers() {
270
        // TODO
271
        throw new UnsupportedOperationException();
272
    }
273

    
274
    private void removeAllLayers() {
275
        getWwd().getModel().getLayers().removeAll();
276
    }
277

    
278
    private void setFlatPanelConfiguration() {
279
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
280
            EarthFlat.class.getName());
281
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
282
            FlatOrbitView.class.getName());
283
    }
284

    
285
    public void setMapContext(MapContext theMapContext) {
286
        this.mapContext = theMapContext;
287
    }
288

    
289
    private void setSpherePanelConfiguration() {
290
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
291
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
292
            BasicOrbitView.class.getName());
293
    }
294

    
295
    public void setVerticalExaggeration(double verticalExaggeration) {
296
        getWwd().getSceneController().setVerticalExaggeration(
297
            verticalExaggeration);
298
    }
299

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

    
318
    public void showMiniMap() {
319
        try {
320
            addWWLayer(WorldMapLayer.class);
321
        } catch (Exception e) {
322
            LOG.warn("Can't add " + WorldMapLayer.class.getCanonicalName()
323
                + "to " + getWwd().getModel().toString(), e);
324
        }
325
    }
326

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

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

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

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

    
359
    public void synchronizeViewPorts() {
360
        // TODO
361
        throw new UnsupportedOperationException();
362
    }
363

    
364
}