forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAnnotationControls.java
More file actions
682 lines (578 loc) · 22.1 KB
/
AnnotationControls.java
File metadata and controls
682 lines (578 loc) · 22.1 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
* 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.worldwindx.examples;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.pick.PickedObject;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.util.*;
import gov.nasa.worldwindx.examples.util.*;
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.InputStream;
/**
* Illustrates how to use a WorldWind <code>{@link Annotation}</code> with an <code>{@link
* AnnotationLayoutManager}</code> to display an Annotation with a simple embedded user interface. The custom Annotation
* layouts illustrated here can be found in the following example classes: <ul> <li><code>{@link
* AudioPlayerAnnotation}</code></li> <li><code>{@link SlideShowAnnotation}</code></li> </ul>
*
* @author dcollins
* @version $Id: AnnotationControls.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class AnnotationControls extends ApplicationTemplate
{
protected final static String AUDIO = "Audio";
protected final static String IMAGES = "Images";
protected final static String ICON_AUDIO = "gov/nasa/worldwindx/examples/images/audioicon-64.png";
protected final static String ICON_IMAGES = "gov/nasa/worldwindx/examples/images/imageicon-64.png";
protected final static String AUDIO_PATH_MUSIC = "gov/nasa/worldwindx/examples/data/spacemusic.au";
protected final static String IMAGE_PATH_MT_ST_HELENS = "gov/nasa/worldwindx/examples/images/MountStHelens.jpg";
protected final static String IMAGE_PATH_THE_NUT = "gov/nasa/worldwindx/examples/images/the_nut.jpg";
protected final static String IMAGE_PATH_IRELAND = "gov/nasa/worldwindx/examples/images/ireland.jpg";
protected final static String IMAGE_PATH_NEW_ZEALAND = "gov/nasa/worldwindx/examples/images/new_zealand.gif";
public static class AppFrame extends ApplicationTemplate.AppFrame implements SelectListener
{
protected IconLayer iconLayer;
protected WWIcon highlit;
protected RenderableLayer contentLayer;
protected BasicDragger dragger;
public AppFrame()
{
this.iconLayer = createIconLayer();
this.contentLayer = new RenderableLayer();
insertBeforePlacenames(this.getWwd(), this.iconLayer);
insertBeforePlacenames(this.getWwd(), this.contentLayer);
this.getWwd().addSelectListener(this);
this.dragger = new BasicDragger(this.getWwd());
}
public IconLayer getIconLayer()
{
return this.iconLayer;
}
public RenderableLayer getContentLayer()
{
return this.contentLayer;
}
@SuppressWarnings( {"StringEquality"})
public void selected(SelectEvent e)
{
if (e == null)
return;
PickedObject topPickedObject = e.getTopPickedObject();
if (e.getEventAction() == SelectEvent.LEFT_PRESS)
{
if (topPickedObject != null && topPickedObject.getObject() instanceof WWIcon)
{
WWIcon selected = (WWIcon) topPickedObject.getObject();
this.highlight(selected);
}
else
{
this.highlight(null);
}
}
else if (e.getEventAction() == SelectEvent.LEFT_DOUBLE_CLICK)
{
if (topPickedObject != null && topPickedObject.getObject() instanceof WWIcon)
{
WWIcon selected = (WWIcon) topPickedObject.getObject();
this.highlight(selected);
this.openResource(selected);
}
}
else if (e.getEventAction() == SelectEvent.DRAG || e.getEventAction() == SelectEvent.DRAG_END)
{
this.dragger.selected(e);
}
}
public void highlight(WWIcon icon)
{
if (this.highlit == icon)
return;
if (this.highlit != null)
{
this.highlit.setHighlighted(false);
this.highlit = null;
}
if (icon != null)
{
this.highlit = icon;
this.highlit.setHighlighted(true);
}
this.getWwd().redraw();
}
protected void closeResource(ContentAnnotation content)
{
if (content == null)
return;
content.detach();
}
protected void openResource(WWIcon icon)
{
if (icon == null)
return;
ContentAnnotation content = this.createContent(icon.getPosition(), icon);
if (content != null)
{
content.attach();
}
}
protected ContentAnnotation createContent(Position position, AVList params)
{
return createContentAnnotation(this, position, params);
}
}
public static class ContentAnnotation implements ActionListener
{
protected AppFrame appFrame;
protected DialogAnnotation annnotation;
protected DialogAnnotationController controller;
public ContentAnnotation(AppFrame appFrame, DialogAnnotation annnotation, DialogAnnotationController controller)
{
this.appFrame = appFrame;
this.annnotation = annnotation;
this.annnotation.addActionListener(this);
this.controller = controller;
}
public AppFrame getAppFrame()
{
return this.appFrame;
}
public DialogAnnotation getAnnotation()
{
return this.annnotation;
}
public DialogAnnotationController getController()
{
return this.controller;
}
@SuppressWarnings( {"StringEquality"})
public void actionPerformed(ActionEvent e)
{
if (e == null)
return;
if (e.getActionCommand() == AVKey.CLOSE)
{
this.getAppFrame().closeResource(this);
}
}
public void detach()
{
this.getController().setEnabled(false);
RenderableLayer layer = this.getAppFrame().getContentLayer();
layer.removeRenderable(this.getAnnotation());
}
public void attach()
{
this.getController().setEnabled(true);
RenderableLayer layer = this.appFrame.getContentLayer();
layer.removeRenderable(this.getAnnotation());
layer.addRenderable(this.getAnnotation());
}
}
public static class AudioContentAnnotation extends ContentAnnotation
{
protected Clip clip;
protected Object source;
protected Thread readThread;
public AudioContentAnnotation(AppFrame appFrame, AudioPlayerAnnotation annnotation,
AudioPlayerAnnotationController controller, Object source)
{
super(appFrame, annnotation, controller);
this.source = source;
this.retrieveAndSetClip(source);
}
public Object getSource()
{
return this.source;
}
public void detach()
{
super.detach();
// Stop any threads or timers the controller may be actively running.
AudioPlayerAnnotationController controller = (AudioPlayerAnnotationController) this.getController();
if (controller != null)
{
this.stopController(controller);
}
// Stop any threads that may be reading the audio source.
this.stopClipRetrieval();
}
@SuppressWarnings( {"StringEquality"})
protected void stopController(AudioPlayerAnnotationController controller)
{
String status = controller.getClipStatus();
if (status == AVKey.PLAY)
{
controller.stopClip();
}
}
protected void retrieveAndSetClip(Object source)
{
this.startClipRetrieval(source);
}
protected void doRetrieveAndSetClip(final Object source)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
getAnnotation().setBusy(true);
appFrame.getWwd().redraw();
}
});
final Clip clip = this.readClip(source);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
AudioPlayerAnnotationController controller = (AudioPlayerAnnotationController) getController();
if (controller != null)
{
controller.setClip(clip);
}
AudioPlayerAnnotation annotation = (AudioPlayerAnnotation) getAnnotation();
if (annotation != null)
{
if (clip == null)
{
annotation.getTitleLabel().setText(createErrorTitle(source.toString()));
}
}
getAnnotation().setBusy(false);
appFrame.getWwd().redraw();
}
});
}
protected Clip readClip(Object source)
{
InputStream stream = null;
try
{
stream = WWIO.openStream(source);
return openAudioStream(stream);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
WWIO.closeStream(stream, source.toString());
}
return null;
}
protected void startClipRetrieval(final Object source)
{
this.readThread = new Thread(new Runnable()
{
public void run()
{
doRetrieveAndSetClip(source);
}
});
this.readThread.start();
}
protected void stopClipRetrieval()
{
if (this.readThread != null)
{
if (this.readThread.isAlive())
{
this.readThread.interrupt();
}
}
this.readThread = null;
}
}
public static class ImageContentAnnotation extends ContentAnnotation
{
public ImageContentAnnotation(AppFrame appFrame, SlideShowAnnotation annnotation,
SlideShowAnnotationController controller)
{
super(appFrame, annnotation, controller);
}
public void detach()
{
super.detach();
// Stop any threads or timers the controller may be actively running.
SlideShowAnnotationController controller = (SlideShowAnnotationController) this.getController();
if (controller != null)
{
this.stopController(controller);
}
}
@SuppressWarnings( {"StringEquality"})
protected void stopController(SlideShowAnnotationController controller)
{
String state = controller.getState();
if (state == AVKey.PLAY)
{
controller.stopSlideShow();
}
controller.stopRetrievalTasks();
}
}
public static IconLayer createIconLayer()
{
IconLayer layer = new IconLayer();
layer.setPickEnabled(true);
WWIcon icon = createIcon(AUDIO, Position.fromDegrees(28.533513, -81.375789, 0),
"Music from the Java Sound demo", AUDIO_PATH_MUSIC);
layer.addIcon(icon);
icon = createIcon(IMAGES, Position.fromDegrees(46.1912, -122.1944, 0), "",
java.util.Arrays.asList(IMAGE_PATH_MT_ST_HELENS));
layer.addIcon(icon);
icon = createIcon(IMAGES, Position.fromDegrees(-12, -70, 0), "",
java.util.Arrays.asList(IMAGE_PATH_IRELAND, IMAGE_PATH_NEW_ZEALAND, IMAGE_PATH_THE_NUT));
layer.addIcon(icon);
return layer;
}
public static WWIcon createIcon(Object type, Position position, String title, Object data)
{
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (title == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (data == null)
{
String message = Logging.getMessage("nullValue.DataSetIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
String iconPath = (type == AUDIO) ? ICON_AUDIO : ICON_IMAGES;
UserFacingIcon icon = new UserFacingIcon(iconPath, position);
icon.setSize(new java.awt.Dimension(64, 64));
icon.setValue(AVKey.DATA_TYPE, type);
icon.setValue(AVKey.TITLE, title);
icon.setValue(AVKey.URL, data);
return icon;
}
@SuppressWarnings( {"StringEquality"})
public static ContentAnnotation createContentAnnotation(AppFrame appFrame, Position position, AVList params)
{
if (appFrame == null)
{
String message = "AppFrameIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (params == null)
{
String message = Logging.getMessage("nullValue.ParamsIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
String type = params.getStringValue(AVKey.DATA_TYPE);
String title = params.getStringValue(AVKey.TITLE);
Object source = params.getValue(AVKey.URL);
if (type == AUDIO)
{
return createAudioAnnotation(appFrame, position, title, source);
}
else if (type == IMAGES)
{
return createImageAnnotation(appFrame, position, title, (Iterable) source);
}
return null;
}
public static ContentAnnotation createAudioAnnotation(AppFrame appFrame, Position position, String title,
Object source)
{
if (appFrame == null)
{
String message = "AppFrameIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (title == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (source == null)
{
String message = Logging.getMessage("nullValue.SourceIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
AudioPlayerAnnotation annotation = new AudioPlayerAnnotation(position);
annotation.setAlwaysOnTop(true);
annotation.getTitleLabel().setText(title);
AudioPlayerAnnotationController controller = new AudioPlayerAnnotationController(appFrame.getWwd(), annotation);
return new AudioContentAnnotation(appFrame, annotation, controller, source);
}
@SuppressWarnings( {"TypeParameterExplicitlyExtendsObject"})
public static ContentAnnotation createImageAnnotation(AppFrame appFrame, Position position, String title,
Iterable sources)
{
if (appFrame == null)
{
String message = "AppFrameIsNull";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (position == null)
{
String message = Logging.getMessage("nullValue.PositionIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (title == null)
{
String message = Logging.getMessage("nullValue.StringIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (sources == null)
{
String message = Logging.getMessage("nullValue.IterableIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
SlideShowAnnotation annotation = new SlideShowAnnotation(position);
annotation.setAlwaysOnTop(true);
annotation.getTitleLabel().setText(title);
SlideShowAnnotationController controller = new SlideShowAnnotationController(appFrame.getWwd(), annotation,
sources);
return new ImageContentAnnotation(appFrame, annotation, controller);
}
public static Clip openAudioStream(InputStream stream) throws Exception
{
if (stream == null)
{
String message = Logging.getMessage("nullValue.InputStreamIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
Clip clip = null;
AudioInputStream ais = null;
try
{
// AudioSystem.getAudioInputStream requires that InputStreams for audio clip resources support the
// mark/reset functionality. Streams opened to class-path resources do not support this functionality, so
// we provide this functionality by wrapping the specified InputStream in a BufferedInputStream, which
// always supports mark/reset.
ais = AudioSystem.getAudioInputStream(WWIO.getBufferedInputStream(stream));
AudioFormat format = ais.getFormat();
// Code taken from Java Sound demo at
// http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo
//
// We can't yet open the device for ALAW/ULAW playback, convert ALAW/ULAW to PCM.
if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
(format.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmp = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2,
format.getChannels(),
format.getFrameSize() * 2,
format.getFrameRate(),
true);
ais = AudioSystem.getAudioInputStream(tmp, ais);
format = tmp;
}
DataLine.Info info = new DataLine.Info(
Clip.class,
ais.getFormat(),
((int) ais.getFrameLength() * format.getFrameSize()));
clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
}
finally
{
if (ais != null)
{
ais.close();
}
}
return clip;
}
public static String createErrorTitle(String path)
{
if (path == null)
{
String message = Logging.getMessage("nullValue.PathIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
StringBuilder sb = new StringBuilder();
sb.append("Cannot open the resource at <i>").append(path).append("</i>");
return sb.toString();
}
public static String createTitle(Iterable sources)
{
if (sources == null)
{
String message = Logging.getMessage("nullValue.IterableIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
StringBuilder sb = new StringBuilder();
java.util.Iterator iter = sources.iterator();
while (iter.hasNext())
{
Object o = iter.next();
sb.append(o);
if (iter.hasNext())
{
sb.append(", ");
}
}
return sb.toString();
}
public static void main(String[] args)
{
ApplicationTemplate.start("WorldWind Annotation Controls", AppFrame.class);
}
}