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

History | View | Annotate | Download (10.7 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
    private MapContext mapContext;
84

    
85
    private WorldWindowGLJPanel wwd;
86

    
87
    private StatusBar statusBar;
88

    
89
    private TYPE type;
90

    
91
    public DefaultMapControl3D(MapContext theMapContext, TYPE type) {
92

    
93
        super(new BorderLayout());
94

    
95
        this.mapContext = theMapContext;
96
        this.type = type;
97

    
98
        // Set mode before instantiation
99
        if (type == TYPE.SPHERE) {
100
            setSpherePanelConfiguration();
101
        } else if (type == TYPE.FLAT) {
102
            setFlatPanelConfiguration();
103
        }
104

    
105
        super.add(getWwd(), BorderLayout.CENTER);
106

    
107
        super.add(getStatusBar(), BorderLayout.SOUTH);
108

    
109
        try {
110
            addLayers();
111
        } catch (BaseException e) {
112
            logger.info("Can't add MapControl layers", e);
113
        }
114

    
115
    }
116

    
117
    private void addGvSIGLayer(FLayer layer) throws DataException {
118

    
119
        LayerConverter converter = new DefaultLayerConverter();
120
        
121
        // TODO Check load mode to create WW Layer
122
        Layer rasterTiledLayer = converter.convertToLayer(mapContext, layer);
123

    
124
        getWwd().getModel().getLayers().add(rasterTiledLayer);
125
    }
126

    
127
    private void addLayers() throws BaseException {
128
        mapContext.getLayers().accept(new LayersVisitor() {
129

    
130
            public void visit(Object obj) throws VisitCanceledException,
131
                BaseException {
132
                throw new UnsupportedOperationException();
133

    
134
            }
135

    
136
            public void visit(FLayer layer) throws BaseException {
137
                // TODO Make new threads to add layers
138
                if (layer.isAvailable() && layer.isVisible()) {
139
                    addGvSIGLayer(layer);
140
                }
141
            }
142
        });
143

    
144
        getWwd().redraw();
145
    }
146

    
147
    @SuppressWarnings("rawtypes")
148
    private void addWWLayer(Class layerClazz) throws InstantiationException,
149
        IllegalAccessException, IllegalArgumentException,
150
        InvocationTargetException {
151
        LayerList layerList = getWwd().getModel().getLayers();
152

    
153
        if (isAlreadyAdded(layerList, layerClazz)) {
154
            return;
155
        }
156

    
157
        Constructor[] constructors = layerClazz.getConstructors();
158
        for (int i = 0; i < constructors.length; i++) {
159
            if (constructors[i].getParameterTypes().length == 0) {
160
                layerList.add((Layer) constructors[i].newInstance());
161
            }
162
        }
163
    }
164

    
165
    private void addNavigation() {
166

    
167
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
168

    
169
        Model model = getWwd().getModel();
170
        model.getLayers().add(controlsLayer);
171

    
172
        getWwd().addSelectListener(
173
            new ViewControlsSelectListener(wwd, controlsLayer));
174
    }
175

    
176
    public JComponent asJComponent() {
177
        return this;
178
    }
179

    
180
    public MapContext getMapContext() {
181
        return this.mapContext;
182
    }
183

    
184
    private StatusBar getStatusBar() {
185
        if (statusBar == null) {
186
            statusBar = new StatusBar();
187
            this.add(statusBar, BorderLayout.PAGE_END);
188
            statusBar.setEventSource(wwd);
189
        }
190
        return statusBar;
191
    }
192

    
193
    public TYPE getType() {
194
        return this.type;
195
    }
196

    
197
    public double getVerticalExaggeration() {
198
        return getWwd().getSceneController().getVerticalExaggeration();
199
    }
200

    
201
    protected WorldWindowGLJPanel getWwd() {
202
        if (this.wwd == null) {
203
            intializeWWPanel();
204
        }
205
        return this.wwd;
206
    }
207

    
208
    public void hideAtmosphere() {
209
        hideLayer(SkyGradientLayer.class);
210
    }
211

    
212
    @SuppressWarnings("rawtypes")
213
    private void hideLayer(Class layerClazz) {
214
        LayerList layers = getWwd().getModel().getLayers();
215
        List<Layer> layersByClass = layers.getLayersByClass(layerClazz);
216
        layers.removeAll(layersByClass);
217
    }
218

    
219
    public void hideMiniMap() {
220
        hideLayer(WorldMapLayer.class);
221
    }
222

    
223
    public void hideNorthIndicator() {
224
        hideLayer(CompassLayer.class);
225
    }
226

    
227
    public void hideScale() {
228
        hideLayer(ScalebarLayer.class);
229
    }
230

    
231
    public void hideStarBackground() {
232
        hideLayer(StarsLayer.class);
233
    }
234

    
235
    private void intializeWWPanel() {
236
        wwd = new WorldWindowGLJPanel();
237
        wwd.setPreferredSize(new Dimension(800, 600));
238

    
239
        // Create the default model as described in the current worldwind
240
        // properties.
241
        Model m =
242
            (Model) WorldWind
243
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
244
        getWwd().setModel(m);
245

    
246
        // Add view control layer
247
        addNavigation();
248
    }
249

    
250
    private boolean isAlreadyAdded(LayerList layerList,
251
        @SuppressWarnings("rawtypes") Class clazz) {
252

    
253
        List<Layer> layersByClass = layerList.getLayersByClass(clazz);
254

    
255
        return layersByClass.size() > 0;
256
    }
257

    
258
    public void reloadLayers() {
259
        // TODO
260
        throw new UnsupportedOperationException();
261
    }
262

    
263
    private void removeAllLayers() {
264
        getWwd().getModel().getLayers().removeAll();
265
    }
266

    
267
    private void setFlatPanelConfiguration() {
268
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
269
            EarthFlat.class.getName());
270
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
271
            FlatOrbitView.class.getName());
272
    }
273

    
274
    public void setMapContext(MapContext theMapContext) {
275
        this.mapContext = theMapContext;
276
    }
277

    
278
    private void setSpherePanelConfiguration() {
279
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
280
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
281
            BasicOrbitView.class.getName());
282
    }
283

    
284
    public void setVerticalExaggeration(double verticalExaggeration) {
285
        getWwd().getSceneController().setVerticalExaggeration(
286
            verticalExaggeration);
287
    }
288

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

    
307
    public void showMiniMap() {
308
        try {
309
            addWWLayer(WorldMapLayer.class);
310
        } catch (Exception e) {
311
            logger.warn("Can't add " + WorldMapLayer.class.getCanonicalName()
312
                + "to " + getWwd().getModel().toString(), e);
313
        }
314
    }
315

    
316
    public void showNortIndicator() {
317
        try {
318
            addWWLayer(CompassLayer.class);
319
        } catch (Exception e) {
320
            logger.warn("Can't add " + CompassLayer.class.getCanonicalName()
321
                + " to " + getWwd().getModel().toString(), e);
322
        }
323
    }
324

    
325
    public void showScale() {
326
        try {
327
            addWWLayer(ScalebarLayer.class);
328
        } catch (Exception e) {
329
            logger.warn("Can't add " + ScalebarLayer.class.getCanonicalName()
330
                + " to " + getWwd().getModel().toString(), e);
331
        }
332
    }
333

    
334
    public void showStarBackgroundLayer() {
335
        try {
336
            addWWLayer(StarsLayer.class);
337
        } catch (Exception e) {
338
            logger.warn("Can't add " + StarsLayer.class.getCanonicalName()
339
                + " to " + getWwd().getModel().toString(), e);
340
        }
341
    }
342

    
343
    public void synchronizeLayers() {
344
        // TODO
345
        throw new UnsupportedOperationException();
346
    }
347

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

    
353
}