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

History | View | Annotate | Download (9.53 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.io.File;
52
import java.lang.reflect.Constructor;
53
import java.lang.reflect.InvocationTargetException;
54
import java.net.URL;
55
import java.util.List;
56

    
57
import javax.swing.JComponent;
58
import javax.swing.JPanel;
59

    
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
import org.gvsig.fmap.mapcontext.MapContext;
64
import org.gvsig.view3d.swing.api.MapControl3D;
65
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE;
66

    
67
/**
68
 * @author llmarques
69
 *
70
 */
71
public class DefaultMapControl3D extends JPanel implements MapControl3D {
72

    
73
    private static final long serialVersionUID = 2024899922367896097L;
74

    
75
    private static final Logger logger = LoggerFactory
76
        .getLogger(DefaultMapControl3D.class);
77

    
78
    private MapContext mapContext;
79
    
80
    private WorldWindowGLJPanel wwd;
81

    
82
    private TYPE type;
83

    
84
    public DefaultMapControl3D(MapContext theMapContext, TYPE type) {
85
        
86
        super(new BorderLayout());
87
        
88
        this.mapContext = theMapContext;
89
        this.type = type;
90

    
91
        // Load configuration file
92
        URL configURL = this.getClass().getResource("/config/worldwind.xml");
93

    
94
        if (configURL != null) {
95
            File configFile = new File(configURL.getPath());
96
            Configuration.insertConfigurationDocument(configFile
97
                .getAbsolutePath());
98
        }
99

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

    
107
        this.add(getWwd(), BorderLayout.CENTER);
108

    
109
        this.add(getStatusBar(), BorderLayout.SOUTH);
110

    
111
        // add(this.mapContext.getLayers());
112

    
113
    }
114

    
115
    @SuppressWarnings("rawtypes")
116
    private void addLayer(Class layerClazz) throws InstantiationException,
117
        IllegalAccessException, IllegalArgumentException,
118
        InvocationTargetException {
119
        LayerList layerList = getWwd().getModel().getLayers();
120

    
121
        if (isAlreadyAdded(layerList, layerClazz)) {
122
            return;
123
        }
124

    
125
        Constructor[] constructors = layerClazz.getConstructors();
126
        for (int i = 0; i < constructors.length; i++) {
127
            if (constructors[i].getParameterTypes().length == 0) {
128
                layerList.add((Layer) constructors[i].newInstance());
129
            }
130
        }
131
    }
132
    
133
    private void addNavigation() {
134

    
135
        ViewControlsLayer controlsLayer = new ViewControlsLayer();
136

    
137
        Model model = getWwd().getModel();
138
        model.getLayers().add(controlsLayer);
139

    
140
        getWwd().addSelectListener(
141
            new ViewControlsSelectListener(wwd, controlsLayer));
142
    }
143

    
144
    public JComponent asJComponent() {
145
        return this;
146
    }
147

    
148
    public MapContext getMapContext() {
149
        return this.mapContext;
150
    }
151

    
152
    private StatusBar getStatusBar() {
153
        StatusBar statusBar = new StatusBar();
154
        this.add(statusBar, BorderLayout.PAGE_END);
155
        statusBar.setEventSource(wwd);
156
        return statusBar;
157
    }
158

    
159
    public TYPE getType() {
160
        return this.type;
161
    }
162

    
163
    public double getVerticalExaggeration() {
164
        return getWwd().getSceneController().getVerticalExaggeration();
165
    }
166

    
167
    protected WorldWindowGLJPanel getWwd() {
168
        if (this.wwd == null) {
169
            intializeWWPanel();
170
        }
171
        return this.wwd;
172
    }
173

    
174
    public void hideAtmosphere() {
175
        hideLayer(SkyGradientLayer.class);
176
    }
177

    
178
    @SuppressWarnings("rawtypes")
179
    private void hideLayer(Class layerClazz) {
180
        LayerList layers = getWwd().getModel().getLayers();
181
        List<Layer> layersByClass = layers.getLayersByClass(layerClazz);
182
        layers.removeAll(layersByClass);
183
    }
184

    
185
    public void hideMiniMap() {
186
        hideLayer(WorldMapLayer.class);
187
    }
188

    
189
    public void hideNorthIndicator() {
190
        hideLayer(CompassLayer.class);
191
    }
192

    
193
    public void hideScale() {
194
        hideLayer(ScalebarLayer.class);
195
    }
196

    
197
    public void hideStarBackground() {
198
        hideLayer(StarsLayer.class);
199
    }
200

    
201
    private void intializeWWPanel() {
202
        wwd = new WorldWindowGLJPanel();
203
        wwd.setPreferredSize(new Dimension(800, 600));
204

    
205
        // Create the default model as described in the current worldwind
206
        // properties.
207
        Model m =
208
            (Model) WorldWind
209
                .createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
210
        getWwd().setModel(m);
211

    
212
        // Add view control layer
213
        addNavigation();
214
    }
215

    
216
    private boolean isAlreadyAdded(LayerList layerList,
217
        @SuppressWarnings("rawtypes") Class clazz) {
218

    
219
        List<Layer> layersByClass = layerList.getLayersByClass(clazz);
220

    
221
        return layersByClass.size() > 0;
222
    }
223

    
224
    public void reloadLayers() {
225
        // TODO
226
        throw new UnsupportedOperationException();
227
    }
228

    
229
    private void removeAllLayers() {
230
        getWwd().getModel().getLayers().removeAll();
231
    }
232

    
233
    private void setFlatPanelConfiguration() {
234
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME,
235
            EarthFlat.class.getName());
236
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
237
            FlatOrbitView.class.getName());
238
    }
239

    
240
    public void setMapContext(MapContext theMapContext) {
241
        this.mapContext = theMapContext;
242
    }
243

    
244
    private void setSpherePanelConfiguration() {
245
        Configuration.setValue(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
246
        Configuration.setValue(AVKey.VIEW_CLASS_NAME,
247
            BasicOrbitView.class.getName());
248
    }
249

    
250
    public void setVerticalExaggeration(double verticalExaggeration) {
251
        getWwd().getSceneController().setVerticalExaggeration(
252
            verticalExaggeration);
253
    }
254

    
255
    public void showAtmosphere() {
256
        try {
257
            Globe globe = getWwd().getModel().getGlobe();
258
            if (globe instanceof Earth) {
259
                getWwd().getModel().getLayers().add(new SkyGradientLayer());
260
                addLayer(SkyGradientLayer.class);
261
            } else if (globe instanceof EarthFlat) {
262
                getWwd().getModel().getLayers().add(new SkyColorLayer());
263
                addLayer(SkyColorLayer.class);
264
            }
265
        } catch (Exception e) {
266
            logger.warn(
267
                "Can't add " + SkyGradientLayer.class.getCanonicalName()
268
                    + " or " + SkyColorLayer.class.getCanonicalName() + " to "
269
                    + getWwd().getModel().toString(), e);
270
        }
271
    }
272

    
273
    public void showMiniMap() {
274
        try {
275
            addLayer(WorldMapLayer.class);
276
        } catch (Exception e) {
277
            logger.warn("Can't add " + WorldMapLayer.class.getCanonicalName()
278
                + "to " + getWwd().getModel().toString(), e);
279
        }
280
    }
281

    
282
    public void showNortIndicator() {
283
        try {
284
            addLayer(CompassLayer.class);
285
        } catch (Exception e) {
286
            logger.warn("Can't add " + CompassLayer.class.getCanonicalName()
287
                + " to " + getWwd().getModel().toString(), e);
288
        }
289
    }
290

    
291
    public void showScale() {
292
        try {
293
            addLayer(ScalebarLayer.class);
294
        } catch (Exception e) {
295
            logger.warn("Can't add " + ScalebarLayer.class.getCanonicalName()
296
                + " to " + getWwd().getModel().toString(), e);
297
        }
298
    }
299

    
300
    public void showStarBackgroundLayer() {
301
        try {
302
            addLayer(StarsLayer.class);
303
        } catch (Exception e) {
304
            logger.warn("Can't add " + StarsLayer.class.getCanonicalName()
305
                + " to " + getWwd().getModel().toString(), e);
306
        }
307
    }
308

    
309
    public void synchronizeLayers() {
310
        // TODO
311
        throw new UnsupportedOperationException();
312
    }
313

    
314
    public void synchronizeViewPorts() {
315
        // TODO
316
        throw new UnsupportedOperationException();
317
    }
318

    
319
}