2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
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
;
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 2979 2007-09-22 01:05:19Z tgaskins $
25 public class ApplicationTemplate
27 public static class AppPanel
extends JPanel
29 protected WorldWindowGLCanvas wwd
;
30 protected StatusBar statusBar
;
32 public AppPanel(Dimension canvasSize
, boolean includeStatusBar
)
34 super(new BorderLayout());
36 this.wwd
= new WorldWindowGLCanvas();
37 this.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
);
43 this.add(this.wwd
, BorderLayout
.CENTER
);
46 this.statusBar
= new StatusBar();
47 this.add(statusBar
, BorderLayout
.PAGE_END
);
48 this.statusBar
.setEventSource(wwd
);
53 protected static class AppFrame
extends JFrame
55 private Dimension canvasSize
= new Dimension(800, 600);
57 protected AppPanel wwjPanel
;
58 protected WorldWindowGLCanvas wwd
;
59 protected StatusBar statusBar
;
60 protected LayerPanel layerPanel
;
61 protected StatisticsPanel statsPanel
;
65 this.initialize(true, true, false);
68 public AppFrame(boolean includeStatusBar
, boolean includeLayerPanel
, boolean includeStatsPanel
)
70 this.initialize(includeStatusBar
, includeLayerPanel
, includeStatsPanel
);
73 private void initialize(boolean includeStatusBar
, boolean includeLayerPanel
, boolean includeStatsPanel
)
75 // Create the WorldWindow.
76 this.wwjPanel
= new AppPanel(this.canvasSize
, includeStatusBar
);
77 this.wwjPanel
.setPreferredSize(canvasSize
);
78 this.wwd
= this.wwjPanel
.wwd
;
79 this.statusBar
= this.wwjPanel
.statusBar
;
81 // Put the pieces together.
82 this.getContentPane().add(wwjPanel
, BorderLayout
.CENTER
);
83 if (includeLayerPanel
)
85 this.layerPanel
= new LayerPanel(wwd
, new Dimension(250, canvasSize
.height
));
86 this.getContentPane().add(this.layerPanel
, BorderLayout
.WEST
);
88 if (includeStatsPanel
)
90 this.statsPanel
= new StatisticsPanel(wwd
, new Dimension(250, canvasSize
.height
));
91 this.getContentPane().add(this.statsPanel
, BorderLayout
.EAST
);
92 this.wwd
.addRenderingListener(new RenderingListener()
94 public void stageChanged(RenderingEvent event
)
96 if (event
.getSource() instanceof WorldWindow
)
98 statsPanel
.update(wwd
);
105 // Center the application on the screen.
106 Dimension prefSize
= this.getPreferredSize();
107 Dimension parentSize
;
108 java
.awt
.Point parentLocation
= new java
.awt
.Point(0, 0);
109 parentSize
= Toolkit
.getDefaultToolkit().getScreenSize();
110 int x
= parentLocation
.x
+ (parentSize
.width
- prefSize
.width
) / 2;
111 int y
= parentLocation
.y
+ (parentSize
.height
- prefSize
.height
) / 2;
112 this.setLocation(x
, y
);
113 this.setResizable(true);
115 // // Forward events to the status bar to display the cursor position.
116 // if (includeStatusBar)
117 // this.statusBar.setEventSource(wwd);
121 public static void insertBeforeCompass(WorldWindow wwd
, Layer layer
)
123 // Insert the layer into the layer list just before the compass.
124 int compassPosition
= 0;
125 LayerList layers
= wwd
.getModel().getLayers();
126 for (Layer l
: layers
)
128 if (l
instanceof CompassLayer
)
129 compassPosition
= layers
.indexOf(l
);
131 layers
.add(compassPosition
, layer
);
134 public static void insertBeforePlacenames(WorldWindow wwd
, Layer layer
)
136 // Insert the layer into the layer list just before the placenames.
137 int compassPosition
= 0;
138 LayerList layers
= wwd
.getModel().getLayers();
139 for (Layer l
: layers
)
141 if (l
instanceof PlaceNameLayer
)
142 compassPosition
= layers
.indexOf(l
);
144 layers
.add(compassPosition
, layer
);
147 public static void insertBeforeLayerName(WorldWindow wwd
, Layer layer
, String targetName
)
149 // Insert the layer into the layer list just before the target layer.
150 int targetPosition
= 0;
151 LayerList layers
= wwd
.getModel().getLayers();
152 for (Layer l
: layers
)
154 if (l
.getName().indexOf(targetName
) != -1)
156 targetPosition
= layers
.indexOf(l
);
160 layers
.add(targetPosition
, layer
);
165 if (Configuration
.isMacOS())
167 System
.setProperty("apple.laf.useScreenMenuBar", "true");
168 System
.setProperty("com.apple.mrj.application.apple.menu.about.name", "World Wind Application");
169 System
.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
173 public static void start(String appName
, Class appFrameClass
)
175 if (Configuration
.isMacOS() && appName
!= null)
177 System
.setProperty("com.apple.mrj.application.apple.menu.about.name", appName
);
182 AppFrame frame
= (AppFrame
) appFrameClass
.newInstance();
183 frame
.setTitle(appName
);
184 frame
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
185 frame
.setVisible(true);
193 public static void main(String
[] args
)
195 // Call the static start method like this from the main method of your derived class.
196 // Substitute your application's name for the first argument.
197 ApplicationTemplate
.start("World Wind Application Template", AppFrame
.class);