Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / examples / StatusBar.java
blob1f61f39fca027995e9bb391a4d1e7d15a1229e09
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.geom.Position;
13 import javax.swing.*;
14 import java.awt.event.*;
15 import java.awt.*;
17 /**
18 * @author tag
19 * @version $Id: StatusBar.java 3735 2007-12-06 02:20:43Z tgaskins $
21 public class StatusBar extends JPanel implements PositionListener, RenderingListener
23 private WorldWindow eventSource;
24 private final JLabel latDisplay = new JLabel("");
25 private final JLabel lonDisplay = new JLabel("Off globe");
26 private final JLabel altDisplay = new JLabel("");
27 private final JLabel eleDisplay = new JLabel("");
28 private boolean showNetworkStatus = true;
30 public StatusBar()
32 super(new GridLayout(1, 0));
34 final JLabel heartBeat = new JLabel("Downloading");
36 altDisplay.setHorizontalAlignment(SwingConstants.CENTER);
37 latDisplay.setHorizontalAlignment(SwingConstants.CENTER);
38 lonDisplay.setHorizontalAlignment(SwingConstants.CENTER);
39 eleDisplay.setHorizontalAlignment(SwingConstants.CENTER);
41 // this.add(new JLabel("")); // dummy label to visually balance with heartbeat
42 this.add(altDisplay);
43 this.add(latDisplay);
44 this.add(lonDisplay);
45 this.add(eleDisplay);
46 this.add(heartBeat);
48 heartBeat.setHorizontalAlignment(SwingConstants.CENTER);
49 heartBeat.setForeground(new java.awt.Color(255, 0, 0, 0));
51 Timer downloadTimer = new Timer(100, new ActionListener()
53 public void actionPerformed(java.awt.event.ActionEvent actionEvent)
55 if (!showNetworkStatus)
57 if (heartBeat.getText().length() > 0)
58 heartBeat.setText("");
59 return;
62 if (WorldWind.getNetworkStatus().isNetworkUnavailable())
64 heartBeat.setText("No Network");
65 heartBeat.setForeground(new java.awt.Color(255, 0, 0, 255));
66 return;
69 java.awt.Color color = heartBeat.getForeground();
70 int alpha = color.getAlpha();
71 if (WorldWind.getRetrievalService().hasActiveTasks())
73 heartBeat.setText("Downloading");
74 if (alpha == 255)
75 alpha = 255;
76 else
77 alpha = alpha < 16 ? 16 : Math.min(255, alpha + 20);
79 else
81 alpha = Math.max(0, alpha - 20);
83 heartBeat.setForeground(new java.awt.Color(255, 0, 0, alpha));
85 });
86 downloadTimer.start();
89 public void setEventSource(WorldWindow newEventSource)
91 if (this.eventSource != null)
93 this.eventSource.removePositionListener(this);
94 this.eventSource.removeRenderingListener(this);
97 if (newEventSource != null)
99 newEventSource.addPositionListener(this);
100 newEventSource.addRenderingListener(this);
103 this.eventSource = newEventSource;
106 public boolean isShowNetworkStatus()
108 return showNetworkStatus;
111 public void setShowNetworkStatus(boolean showNetworkStatus)
113 this.showNetworkStatus = showNetworkStatus;
116 public void moved(PositionEvent event)
118 this.handleCursorPositionChange(event);
121 public WorldWindow getEventSource()
123 return this.eventSource;
126 private void handleCursorPositionChange(PositionEvent event)
128 Position newPos = event.getPosition();
129 if (newPos != null)
131 String las = String.format("Lat %7.4f\u00B0", newPos.getLatitude().getDegrees());
132 String los = String.format("Lon %7.4f\u00B0", newPos.getLongitude().getDegrees());
133 String els = String.format("Elev %,7d meters", (int)
134 (eventSource.getModel().getGlobe().getElevation(newPos.getLatitude(), newPos.getLongitude())));
135 latDisplay.setText(las);
136 lonDisplay.setText(los);
137 eleDisplay.setText(els);
139 else
141 latDisplay.setText("");
142 lonDisplay.setText("Off globe");
143 eleDisplay.setText("");
147 public void stageChanged(RenderingEvent event)
149 if (eventSource.getView() != null && eventSource.getView().getEyePosition() != null)
150 altDisplay.setText(String.format("Altitude %,7d km",
151 (int) Math.round(eventSource.getView().getEyePosition().getElevation() / 1e3)));
152 else
153 altDisplay.setText("Altitude");