Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / awt / WorldWindowGLCanvas.java
blob5fe41614629ed206bd1a2727a53639cbdb0525c9
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.awt;
9 import gov.nasa.worldwind.*;
11 import javax.media.opengl.*;
12 import java.awt.*;
14 /**
15 * <code>WorldWindowGLCanvas</code> is an AWT component for displaying World Wind {@link Model}s (globe and layers).
16 * This implementation is a heavyweight AWT component. It derives from {@link GLCanvas} and therefore also provides a
17 * general {@link GLDrawable} for OpenGL rendering.
19 * @author Tom Gaskins
20 * @version $Id: WorldWindowGLCanvas.java 1793 2007-05-08 21:28:57Z tgaskins $
22 public class WorldWindowGLCanvas extends GLCanvas implements WorldWindow
24 private static final GLCapabilities caps = new GLCapabilities();
26 static
28 caps.setAlphaBits(8);
29 caps.setRedBits(8);
30 caps.setGreenBits(8);
31 caps.setBlueBits(8);
32 // caps.setStencilBits(8);
33 caps.setDepthBits(24);
36 private final WorldWindowGLAutoDrawable wwd; // everything delegates to wwd
37 private InputHandler inputHandler;
39 /**
40 * Constructs a new <code>WorldWindowGLCanvas</code> window on the default graphics device.
42 public WorldWindowGLCanvas()
44 super(caps);
45 try
47 this.wwd = new WorldWindowGLAutoDrawable(this); // TODO: Make class configurable
48 this.createView();
49 this.createDefaultInputHandler();
51 catch (Exception e)
53 String message = WorldWind.retrieveErrMsg("awt.WorldWindowGLSurface.UnabletoCreateWindow");
54 WorldWind.logger().log(java.util.logging.Level.FINE, message);
55 throw new WWRuntimeException(message, e);
59 /**
60 * Constructs a new <code>WorldWindowGLCanvas</code> window on the default graphics device that will share graphics
61 * resources with another <code>WorldWindowGLCanvas</code> window. The other window, <code>sharewith</code>, may not
62 * be null
64 * @param shareWith a <code>WorldWindowGLCanvas</code> with which to share graphics resources.
65 * @throws NullPointerException if shareWith is null
66 * @see GLCanvas#GLCanvas(GLCapabilities,GLCapabilitiesChooser,GLContext,GraphicsDevice)
68 public WorldWindowGLCanvas(WorldWindowGLCanvas shareWith)
70 super(caps, null, shareWith.getContext(), null);
71 try
73 this.wwd = new WorldWindowGLAutoDrawable(this);
74 this.createView();
75 this.createDefaultInputHandler();
77 catch (Exception e)
79 String message = WorldWind.retrieveErrMsg("awt.WorldWindowGLSurface.UnabletoCreateWindow");
80 WorldWind.logger().log(java.util.logging.Level.FINE, message);
81 throw new WWRuntimeException(message, e);
85 /**
86 * Constructs a new <code>WorldWindowGLCanvas</code> window that will share graphics resources with another
87 * <code>WorldWindowGLCanvas</code> window. The new window is created on the specified graphics device. Neither
88 * <code> shareWith</code> or <code>device</code> may be null.
90 * @param shareWith a <code>WorldWindowGLCanvas</code> with which to share graphics resources.
91 * @param device the <code>GraphicsDevice</code> on which to create the window.
92 * @throws NullPointerException if <code>shareWith</code> is null
93 * @throws IllegalArgumentException if <code>deevice</code> is null
94 * @see GLCanvas#GLCanvas(GLCapabilities,GLCapabilitiesChooser,GLContext,GraphicsDevice)
96 public WorldWindowGLCanvas(WorldWindowGLCanvas shareWith, java.awt.GraphicsDevice device)
98 super(caps, null, shareWith.getContext(), device);
100 if (device == null)
102 String msg = WorldWind.retrieveErrMsg("nullValue.DeviceIsNull");
103 WorldWind.logger().log(java.util.logging.Level.FINE, msg);
104 throw new IllegalArgumentException(msg);
108 this.wwd = new WorldWindowGLAutoDrawable(this);
109 this.createView();
110 this.createDefaultInputHandler();
112 catch (Exception e)
114 String message = WorldWind.retrieveErrMsg("awt.WorldWindowGLSurface.UnabletoCreateWindow");
115 WorldWind.logger().log(java.util.logging.Level.FINE, message);
116 throw new WWRuntimeException(message, e);
120 private void createView()
122 this.setView((View) WorldWind.createConfigurationComponent(
123 gov.nasa.worldwind.AVKey.VIEW_CLASS_NAME));
126 private void createDefaultInputHandler()
128 // this.inputHandler = new gov.nasa.worldwind.awt.AWTInputHandler();
129 this.inputHandler = (InputHandler) WorldWind.createConfigurationComponent(AVKey.INPUT_HANDLER_CLASS_NAME);
130 this.inputHandler.setEventSource(this);
133 public InputHandler getInputHandler()
135 return inputHandler;
138 public void setInputHandler(InputHandler eventSource)
140 if (this.inputHandler != null)
141 this.inputHandler.setEventSource(null); // remove this window as a source of events
143 this.inputHandler = eventSource;
144 if (this.inputHandler != null)
145 this.inputHandler.setEventSource(this);
148 public SceneController getSceneController()
150 return this.wwd.getSceneController();
153 public PickedObjectList pick(java.awt.Point pickPoint)
155 return this.wwd.pick(pickPoint);
158 public void setModel(Model model)
160 // null models are permissible
161 this.wwd.setModel(model);
164 public Model getModel()
166 return this.wwd.getModel();
169 public void setView(View view)
171 // null views are permissible
172 if (view != null)
173 this.wwd.setView(view);
176 public View getView()
178 return this.wwd.getView();
181 public void setModelAndView(Model model, View view)
182 { // null models/views are permissible
183 this.setModel(model);
184 this.setView(view);
187 public void addRenderingListener(RenderingListener listener)
189 this.wwd.addRenderingListener(listener);
192 public void removeRenderingListener(RenderingListener listener)
194 this.wwd.removeRenderingListener(listener);
197 public void addSelectListener(SelectListener listener)
199 this.inputHandler.addSelectListener(listener);
202 public void removeSelectListener(SelectListener listener)
204 this.inputHandler.removeSelectListener(listener);
207 public void addPositionListener(PositionListener listener)
209 this.inputHandler.addPositionListener(listener);
212 public void removePositionListener(PositionListener listener)
214 this.inputHandler.removePositionListener(listener);