Worldwind public release 0.2.1
[worldwind-tracker.git] / gov / nasa / worldwind / examples / ApplicationTemplate.java
blob25b9a3be8d70d05bf5bcba04f6d388a8f5899475
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.layers.*;
11 import gov.nasa.worldwind.awt.*;
12 import worldwinddemo.*;
14 import javax.swing.*;
15 import java.awt.*;
17 /**
18 * Provides a base application framework for simple WorldWind examples. Although this class will run stand-alone, it is
19 * not meant to be used that way. But it has a main method to show how a derived class would call it.
21 * @version $Id: ApplicationTemplate.java 2067 2007-06-16 06:25:23Z tgaskins $
23 public class ApplicationTemplate
25 protected static class AppFrame extends JFrame
27 private Dimension canvasSize = new Dimension(800, 600);
29 protected WorldWindowGLCanvas wwd;
30 protected StatusBar statusBar;
31 protected LayerPanel layerPanel;
33 public AppFrame()
35 // Create the WorldWindow.
36 wwd = new WorldWindowGLCanvas();
37 wwd.setPreferredSize(canvasSize);
39 // Create the default model as described in the current worldwind properties.
40 Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
41 wwd.setModel(m);
43 // Create a status bar to display the cursor position.
44 this.statusBar = new StatusBar();
46 // Create a panel to manage the layers.
47 this.layerPanel = new LayerPanel(wwd, new Dimension(250, canvasSize.height));
49 // Create a container to hold the world window with the status bar centered below it.
50 JPanel mainPanel = new JPanel(new BorderLayout());
51 mainPanel.add(wwd, BorderLayout.CENTER);
52 mainPanel.add(statusBar, BorderLayout.PAGE_END);
54 // Put the pieces together.
55 this.getContentPane().add(mainPanel, BorderLayout.CENTER);
56 this.getContentPane().add(this.layerPanel, BorderLayout.WEST);
57 this.pack();
59 // Center the application on the screen.
60 Dimension prefSize = this.getPreferredSize();
61 Dimension parentSize;
62 java.awt.Point parentLocation = new java.awt.Point(0, 0);
63 parentSize = Toolkit.getDefaultToolkit().getScreenSize();
64 int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
65 int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
66 this.setLocation(x, y);
67 this.setResizable(true);
69 // Forward events to the status bar to display the cursor position.
70 this.statusBar.setEventSource(wwd);
74 public static void insertBeforeCompass(WorldWindow wwd, Layer layer)
76 // Insert the layer into the layer list just before the compass.
77 int compassPosition = 0;
78 LayerList layers = wwd.getModel().getLayers();
79 for (Layer l : layers)
81 if (l instanceof CompassLayer)
82 compassPosition = layers.indexOf(l);
84 layers.add(compassPosition, layer);
87 public static void insertBeforePlacenames(WorldWindow wwd, Layer layer)
89 // Insert the layer into the layer list just before the placenames.
90 int compassPosition = 0;
91 LayerList layers = wwd.getModel().getLayers();
92 for (Layer l : layers)
94 if (l instanceof PlaceNameLayer)
95 compassPosition = layers.indexOf(l);
97 layers.add(compassPosition, layer);
100 static
102 if (Configuration.isMacOS())
104 System.setProperty("apple.laf.useScreenMenuBar", "true");
105 System.setProperty("com.apple.mrj.application.apple.menu.about.name", "World Wind Application");
106 System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
110 public static void start(String appName, Class appFrameClass)
112 if (Configuration.isMacOS() && appName != null)
114 System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName);
119 AppFrame frame = (AppFrame) appFrameClass.newInstance();
120 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121 frame.setVisible(true);
123 catch (Exception e)
125 e.printStackTrace();
129 public static void main(String[] args)
131 // Call the static start method like this from the main method of your derived class.
132 // Substitute your application's name for the first argument.
133 ApplicationTemplate.start("World Wind Application Template", AppFrame.class);