-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathStatusBar.java
329 lines (286 loc) · 11.1 KB
/
StatusBar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2006-2009, 2017, 2020 United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA World Wind Java (WWJ) platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASA World Wind Java (WWJ) also contains the following 3rd party Open Source
* software:
*
* Jackson Parser – Licensed under Apache 2.0
* GDAL – Licensed under MIT
* JOGL – Licensed under Berkeley Software Distribution (BSD)
* Gluegen – Licensed under Berkeley Software Distribution (BSD)
*
* A complete listing of 3rd Party software notices and licenses included in
* NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party
* notices and licenses PDF found in code directory.
*/
package gov.nasa.worldwind.util;
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.geom.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author tag
* @version $Id: StatusBar.java 1945 2014-04-18 17:08:43Z tgaskins $
*/
public class StatusBar extends JPanel implements PositionListener, RenderingListener
{
// Units constants TODO: Replace with UnitsFormat
public final static String UNIT_METRIC = "gov.nasa.worldwind.StatusBar.Metric";
public final static String UNIT_IMPERIAL = "gov.nasa.worldwind.StatusBar.Imperial";
protected static final int MAX_ALPHA = 254;
private WorldWindow eventSource;
private String elevationUnit = UNIT_METRIC;
private String angleFormat = Angle.ANGLE_FORMAT_DD;
protected final JLabel latDisplay = new JLabel("");
protected final JLabel lonDisplay = new JLabel(Logging.getMessage("term.OffGlobe"));
protected final JLabel altDisplay = new JLabel("");
protected final JLabel eleDisplay = new JLabel("");
protected AtomicBoolean showNetworkStatus = new AtomicBoolean(true);
protected AtomicBoolean isNetworkAvailable = new AtomicBoolean(true);
protected Thread netCheckThread;
private Timer downloadTimer;
public StatusBar()
{
super(new GridLayout(1, 0));
final JLabel heartBeat = new JLabel(Logging.getMessage("term.Downloading"));
altDisplay.setHorizontalAlignment(SwingConstants.CENTER);
latDisplay.setHorizontalAlignment(SwingConstants.CENTER);
lonDisplay.setHorizontalAlignment(SwingConstants.CENTER);
eleDisplay.setHorizontalAlignment(SwingConstants.CENTER);
this.add(altDisplay);
this.add(latDisplay);
this.add(lonDisplay);
this.add(eleDisplay);
this.add(heartBeat);
heartBeat.setHorizontalAlignment(SwingConstants.CENTER);
heartBeat.setForeground(new java.awt.Color(255, 0, 0, 0));
downloadTimer = new Timer(100, new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent actionEvent)
{
if (!showNetworkStatus.get())
{
if (heartBeat.getText().length() > 0)
heartBeat.setText("");
return;
}
if (!isNetworkAvailable.get())
{
heartBeat.setText(Logging.getMessage("term.NoNetwork"));
heartBeat.setForeground(new Color(255, 0, 0, MAX_ALPHA));
return;
}
Color color = heartBeat.getForeground();
int alpha = color.getAlpha();
if (isNetworkAvailable.get() && WorldWind.getRetrievalService().hasActiveTasks())
{
heartBeat.setText(Logging.getMessage("term.Downloading"));
if (alpha >= MAX_ALPHA)
alpha = MAX_ALPHA;
else
alpha = alpha < 16 ? 16 : Math.min(MAX_ALPHA, alpha + 20);
}
else
{
alpha = Math.max(0, alpha - 20);
}
heartBeat.setForeground(new Color(255, 0, 0, alpha));
}
});
downloadTimer.start();
this.netCheckThread = this.startNetCheckThread();
WorldWind.getNetworkStatus().addPropertyChangeListener(NetworkStatus.HOST_UNAVAILABLE,
new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
Object nv = evt.getNewValue();
String message = Logging.getMessage("NetworkStatus.UnavailableHost",
nv != null && nv instanceof URL ? ((URL) nv).getHost() : "Unknown");
Logging.logger().info(message);
}
});
WorldWind.getNetworkStatus().addPropertyChangeListener(NetworkStatus.HOST_AVAILABLE,
new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
Object nv = evt.getNewValue();
String message = Logging.getMessage("NetworkStatus.HostNowAvailable",
nv != null && nv instanceof URL ? ((URL) nv).getHost() : "Unknown");
Logging.logger().info(message);
}
});
}
protected NetworkCheckThread startNetCheckThread()
{
NetworkCheckThread nct = new NetworkCheckThread(this.showNetworkStatus, this.isNetworkAvailable, null);
nct.setDaemon(true);
nct.start();
return nct;
}
public void setEventSource(WorldWindow newEventSource)
{
if (this.eventSource != null)
{
this.eventSource.removePositionListener(this);
this.eventSource.removeRenderingListener(this);
}
if (newEventSource != null)
{
newEventSource.addPositionListener(this);
newEventSource.addRenderingListener(this);
}
this.eventSource = newEventSource;
}
public boolean isShowNetworkStatus()
{
return showNetworkStatus.get();
}
public void setShowNetworkStatus(boolean showNetworkStatus)
{
this.showNetworkStatus.set(showNetworkStatus);
if (showNetworkStatus)
{
if (this.netCheckThread != null)
this.netCheckThread.interrupt();
this.netCheckThread = this.startNetCheckThread();
}
else
{
if (this.netCheckThread != null)
this.netCheckThread.interrupt();
this.netCheckThread = null;
}
}
public void moved(PositionEvent event)
{
this.handleCursorPositionChange(event);
}
public WorldWindow getEventSource()
{
return this.eventSource;
}
public String getElevationUnit()
{
return this.elevationUnit;
}
public void setElevationUnit(String unit)
{
if (unit == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.elevationUnit = unit;
}
public String getAngleFormat()
{
return this.angleFormat;
}
public void setAngleFormat(String format)
{
if (format == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.angleFormat = format;
}
protected String makeCursorElevationDescription(double metersElevation)
{
String s;
String elev = Logging.getMessage("term.Elev");
if (UNIT_IMPERIAL.equals(elevationUnit))
s = String.format(elev + " %,7d feet", (int) (WWMath.convertMetersToFeet(metersElevation)));
else // Default to metric units.
s = String.format(elev + " %,7d meters", (int) metersElevation);
return s;
}
protected String makeEyeAltitudeDescription(double metersAltitude)
{
String s;
String altitude = Logging.getMessage("term.Altitude");
if (UNIT_IMPERIAL.equals(elevationUnit))
{
double miles = WWMath.convertMetersToMiles(metersAltitude);
if (Math.abs(miles) >= 1)
s = String.format(altitude + " %,7d mi", (int) Math.round(miles));
else
s = String.format(altitude + " %,7d ft", (int) Math.round(WWMath.convertMetersToFeet(metersAltitude)));
}
else if (Math.abs(metersAltitude) >= 1000) // Default to metric units.
s = String.format(altitude + " %,7d km", (int) Math.round(metersAltitude / 1e3));
else
s = String.format(altitude + " %,7d m", (int) Math.round(metersAltitude));
return s;
}
protected String makeAngleDescription(String label, Angle angle)
{
String s;
if (Angle.ANGLE_FORMAT_DMS.equals(angleFormat))
s = String.format("%s %s", label, angle.toDMSString());
else
s = String.format("%s %7.4f\u00B0", label, angle.degrees);
return s;
}
protected void handleCursorPositionChange(PositionEvent event)
{
Position newPos = event.getPosition();
if (newPos != null)
{
String las = makeAngleDescription("Lat", newPos.getLatitude());
String los = makeAngleDescription("Lon", newPos.getLongitude());
String els = makeCursorElevationDescription(
eventSource.getModel().getGlobe().getElevation(newPos.getLatitude(), newPos.getLongitude()));
latDisplay.setText(las);
lonDisplay.setText(los);
eleDisplay.setText(els);
}
else
{
latDisplay.setText("");
lonDisplay.setText(Logging.getMessage("term.OffGlobe"));
eleDisplay.setText("");
}
}
public void stageChanged(RenderingEvent event)
{
if (!event.getStage().equals(RenderingEvent.BEFORE_BUFFER_SWAP))
return;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
if (eventSource.getView() != null && eventSource.getView().getEyePosition() != null)
altDisplay.setText(makeEyeAltitudeDescription(
eventSource.getView().getEyePosition().getElevation()));
else
altDisplay.setText(Logging.getMessage("term.Altitude"));
}
});
}
public void stopDownloadTimer()
{
downloadTimer.stop();
}
}