Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / examples / ApplicationTemplate.java
blobc8ad41b8bb762fb5c779ebdd3601a162dd0ed3ad
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.examples;
9 import gov.nasa.worldwind.*;
10 import gov.nasa.worldwind.event.*;
11 import gov.nasa.worldwind.avlist.AVKey;
12 import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
13 import gov.nasa.worldwind.layers.*;
14 import gov.nasa.worldwind.layers.placename.PlaceNameLayer;
16 import javax.swing.*;
17 import java.awt.*;
19 /**
20 * Provides a base application framework for simple WorldWind examples. Although this class will run stand-alone, it is
21 * not meant to be used that way. But it has a main method to show how a derived class would call it.
23 * @version $Id: ApplicationTemplate.java 2532 2007-08-13 05:08:11Z tgaskins $
25 public class ApplicationTemplate
27 protected static class AppFrame extends JFrame
29 private Dimension canvasSize = new Dimension(800, 600);
31 protected WorldWindowGLCanvas wwd;
32 protected StatusBar statusBar;
33 protected LayerPanel layerPanel;
34 protected StatisticsPanel statsPanel;
36 public AppFrame()
38 this.initialize(true, true, false);
41 public AppFrame(boolean includeStatusBar, boolean includeLayerPanel, boolean includeStatsPanel)
43 this.initialize(includeStatusBar, includeLayerPanel, includeStatsPanel);
46 private void initialize(boolean includeStatusBar, boolean includeLayerPanel, boolean includeStatsPanel)
48 // Create the WorldWindow.
49 wwd = new WorldWindowGLCanvas();
50 wwd.setPreferredSize(canvasSize);
52 // Create the default model as described in the current worldwind properties.
53 Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
54 wwd.setModel(m);
56 // Create a container to hold the world window with the status bar centered below it.
57 JPanel mainPanel = new JPanel(new BorderLayout());
58 mainPanel.add(wwd, BorderLayout.CENTER);
59 if (includeStatusBar)
61 this.statusBar = new StatusBar();
62 mainPanel.add(statusBar, BorderLayout.PAGE_END);
65 // Put the pieces together.
66 this.getContentPane().add(mainPanel, BorderLayout.CENTER);
67 if (includeLayerPanel)
69 this.layerPanel = new LayerPanel(wwd, new Dimension(250, canvasSize.height));
70 this.getContentPane().add(this.layerPanel, BorderLayout.WEST);
72 if (includeStatsPanel)
74 this.statsPanel = new StatisticsPanel(wwd, new Dimension(250, canvasSize.height));
75 this.getContentPane().add(this.statsPanel, BorderLayout.EAST);
76 this.wwd.addRenderingListener(new RenderingListener()
78 public void stageChanged(RenderingEvent event)
80 if (event.getSource() instanceof WorldWindow)
82 statsPanel.update(wwd);
85 });
87 this.pack();
89 // Center the application on the screen.
90 Dimension prefSize = this.getPreferredSize();
91 Dimension parentSize;
92 java.awt.Point parentLocation = new java.awt.Point(0, 0);
93 parentSize = Toolkit.getDefaultToolkit().getScreenSize();
94 int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
95 int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
96 this.setLocation(x, y);
97 this.setResizable(true);
99 // Forward events to the status bar to display the cursor position.
100 if (includeStatusBar)
101 this.statusBar.setEventSource(wwd);
105 public static void insertBeforeCompass(WorldWindow wwd, Layer layer)
107 // Insert the layer into the layer list just before the compass.
108 int compassPosition = 0;
109 LayerList layers = wwd.getModel().getLayers();
110 for (Layer l : layers)
112 if (l instanceof CompassLayer)
113 compassPosition = layers.indexOf(l);
115 layers.add(compassPosition, layer);
118 public static void insertBeforePlacenames(WorldWindow wwd, Layer layer)
120 // Insert the layer into the layer list just before the placenames.
121 int compassPosition = 0;
122 LayerList layers = wwd.getModel().getLayers();
123 for (Layer l : layers)
125 if (l instanceof PlaceNameLayer)
126 compassPosition = layers.indexOf(l);
128 layers.add(compassPosition, layer);
131 static
133 if (Configuration.isMacOS())
135 System.setProperty("apple.laf.useScreenMenuBar", "true");
136 System.setProperty("com.apple.mrj.application.apple.menu.about.name", "World Wind Application");
137 System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
141 public static void start(String appName, Class appFrameClass)
143 if (Configuration.isMacOS() && appName != null)
145 System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName);
150 AppFrame frame = (AppFrame) appFrameClass.newInstance();
151 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
152 frame.setVisible(true);
154 catch (Exception e)
156 e.printStackTrace();
160 public static void main(String[] args)
162 // Call the static start method like this from the main method of your derived class.
163 // Substitute your application's name for the first argument.
164 ApplicationTemplate.start("World Wind Application Template", AppFrame.class);