forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAirspaceBuilder.java
More file actions
1410 lines (1171 loc) · 53.7 KB
/
AirspaceBuilder.java
File metadata and controls
1410 lines (1171 loc) · 53.7 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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.*;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.pick.PickedObjectList;
import gov.nasa.worldwind.render.Material;
import gov.nasa.worldwind.render.airspaces.*;
import gov.nasa.worldwind.render.airspaces.Polygon;
import gov.nasa.worldwind.render.airspaces.editor.*;
import gov.nasa.worldwind.util.WWIO;
import gov.nasa.worldwind.view.orbit.BasicOrbitView;
import gov.nasa.worldwindx.examples.util.*;
import javax.swing.*;
import javax.swing.Box;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.zip.*;
/**
* Illustrates runtime construction of 3D extruded polygons and spheres using WorldWind <code>{@link Airspace}</code>
* shapes. This uses a <code>{@link PolygonEditor}</code> and a <code>{@link SphereAirspaceEditor}</code> to enable
* runtime editing of <code>{@link Polygon}</code> airspace and <code>{@link SphereAirspace}</code> shapes.
* <h2>Usage Instructions</h2>
* <p>
* <strong>Adding and Removing Shapes</strong> <br> Add a shape by selecting either <code>Polygon</code> or
* <code>Sphere</code> in the drop down box then clicking <code>New shape</code>. Delete a shape by left-clicking it
* then pressing the <code>delete</code> key.
* <p>
* <strong>Moving Shapes</strong> <br> Move a shape by left-clicking and dragging it.
* <p>
* <strong>Editing Spheres</strong> <br> <i>Note: a sphere must be selected before it can be edited. Select a sphere by
* left-clicking it.</i> <br> Adjust a sphere's height by holding the <code>Shift</code> key then left-click and drag
* the sphere. Resize a sphere by moving the cursor toward the sphere's edge until a blue control point appears, then
* left-click and drag the control point.
* <p>
* <strong>Editing Polygons</strong> <br> <i>Note: a polygon must be selected before it can be edited. Select a polygon
* by left-clicking it.</i> <br> Add a polygon vertex by holding the <code>Alt</code> key and left-clicking anywhere
* near the polygon. Remove a polygon control point by holding the <code>Control</code> key and left-clicking the blue
* sphere at the vertex. Move a polygon vertex by left-clicking and dragging it. Change a polygon's bottom or top height
* by holding the <code>Shift</code> key then left-click any blue sphere at a vertex and drag it.
* <h2>Demo Shapes</h2>
* <p>
* Select <code>File -> Load Demo Shapes</code> to display a set of polygon airspace shapes built with this editor.
* The data for these shapes is located in the WorldWind project under
* src/gov/nasa/worldwindx/examples/data/AirspaceBuilder-DemoShapes.zip.
*
* @author dcollins
* @version $Id: AirspaceBuilder.java 2231 2014-08-15 19:03:12Z dcollins $
*/
public class AirspaceBuilder extends ApplicationTemplate {
protected static final String AIRSPACE_LAYER_NAME = "Airspace Shapes";
protected static final String CLEAR_SELECTION = "AirspaceBuilder.ClearSelection";
protected static final String SIZE_NEW_SHAPES_TO_VIEWPORT = "AirspaceBuilder.SizeNewShapesToViewport";
protected static final String ENABLE_EDIT = "AirspaceBuilder.EnableEdit";
protected static final String OPEN = "AirspaceBuilder.Open";
protected static final String OPEN_URL = "AirspaceBuilder.OpenUrl";
protected static final String OPEN_DEMO_AIRSPACES = "AirspaceBuilder.OpenDemoAirspaces";
protected static final String NEW_AIRSPACE = "AirspaceBuilder.NewAirspace";
protected static final String REMOVE_SELECTED = "AirspaceBuilder.RemoveSelected";
protected static final String SAVE = "AirspaceBuilder.Save";
protected static final String SELECTION_CHANGED = "AirspaceBuilder.SelectionChanged";
protected static final String DEMO_AIRSPACES_PATH
= "gov/nasa/worldwindx/examples/data/AirspaceBuilder-DemoShapes.zip";
//**************************************************************//
//******************** Airspace Builder Model ****************//
//**************************************************************//
protected static class AirspaceEntry extends WWObjectImpl {
protected Airspace airspace;
protected AirspaceEditor editor;
protected AirspaceAttributes attributes;
protected boolean editing = false;
protected boolean selected = false;
protected boolean intersecting = false;
public AirspaceEntry(Airspace airspace, AirspaceEditor editor) {
this.airspace = airspace;
this.editor = editor;
this.attributes = this.airspace.getAttributes();
}
public boolean isEditing() {
return this.editing;
}
public void setEditing(boolean editing) {
this.editing = editing;
this.updateAttributes();
}
public boolean isSelected() {
return this.selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
this.updateAttributes();
}
public boolean isIntersecting() {
return this.intersecting;
}
public void setIntersecting(boolean intersecting) {
this.intersecting = intersecting;
this.updateAttributes();
}
public String getName() {
return this.getStringValue(AVKey.DISPLAY_NAME);
}
public void setName(String name) {
this.setValue(AVKey.DISPLAY_NAME, name);
}
public Airspace getAirspace() {
return airspace;
}
public AirspaceEditor getEditor() {
return editor;
}
public AirspaceAttributes getAttributes() {
return this.attributes;
}
@Override
public String toString() {
return this.getName();
}
@Override
public Object getValue(String key) {
Object value = super.getValue(key);
if (value == null) {
value = this.airspace.getValue(key);
}
return value;
}
@Override
public Object setValue(String key, Object value) {
if (AVKey.DISPLAY_NAME.equals(key)) {
return this.airspace.setValue(key, value);
} else {
return super.setValue(key, value);
}
}
protected void updateAttributes() {
if (this.isSelected() && this.isIntersecting()) {
this.airspace.setAttributes(getSelectionAndIntersectionAttributes());
} else if (this.isSelected()) {
this.airspace.setAttributes(getSelectionAttributes());
} else if (this.isIntersecting()) {
this.airspace.setAttributes(getIntersectionAttributes());
} else {
this.airspace.setAttributes(this.getAttributes());
}
}
}
protected static class AirspaceBuilderModel extends AbstractTableModel {
protected static String[] columnName = {"Name"};
protected static Class[] columnClass = {String.class};
protected static String[] columnAttribute = {AVKey.DISPLAY_NAME};
protected ArrayList<AirspaceEntry> entryList = new ArrayList<>();
public AirspaceBuilderModel() {
}
@Override
public String getColumnName(int columnIndex) {
return columnName[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnClass[columnIndex];
}
@Override
public int getRowCount() {
return this.entryList.size();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
AirspaceEntry entry = this.entryList.get(rowIndex);
return entry.getValue(columnAttribute[columnIndex]);
}
@Override
public void setValueAt(Object aObject, int rowIndex, int columnIndex) {
AirspaceEntry entry = this.entryList.get(rowIndex);
String key = columnAttribute[columnIndex];
entry.setValue(key, aObject);
}
public java.util.List<AirspaceEntry> getEntries() {
return Collections.unmodifiableList(this.entryList);
}
public void setEntries(Iterable<? extends AirspaceEntry> entries) {
this.entryList.clear();
if (entries != null) {
for (AirspaceEntry entry : entries) {
this.entryList.add(entry);
}
}
this.fireTableDataChanged();
}
public void addEntry(AirspaceEntry entry) {
this.entryList.add(entry);
int index = this.entryList.size() - 1;
this.fireTableRowsInserted(index, index);
}
public void removeEntry(AirspaceEntry entry) {
int index = this.entryList.indexOf(entry);
if (index != -1) {
this.entryList.remove(entry);
this.fireTableRowsDeleted(index, index);
}
}
public void removeAllEntries() {
this.entryList.clear();
this.fireTableDataChanged();
}
public AirspaceEntry getEntry(int index) {
return this.entryList.get(index);
}
public AirspaceEntry setEntry(int index, AirspaceEntry entry) {
return this.entryList.set(index, entry);
}
public int getIndexForEntry(AirspaceEntry entry) {
return this.entryList.indexOf(entry);
}
}
protected static AirspaceFactory[] defaultAirspaceFactories = new AirspaceFactory[]{
new PolygonAirspaceFactory(),
new SphereAirspaceFactory()
};
protected static final double DEFAULT_SHAPE_SIZE_METERS = 200000.0; // 200 km
protected interface AirspaceFactory {
Airspace createAirspace(WorldWindow wwd, boolean fitShapeToViewport);
AirspaceEditor createEditor(Airspace airspace);
}
protected static class PolygonAirspaceFactory implements AirspaceFactory {
public PolygonAirspaceFactory() {
}
@Override
public Airspace createAirspace(WorldWindow wwd, boolean fitShapeToViewport) {
Polygon poly = new Polygon();
poly.setAttributes(getDefaultAttributes());
poly.setValue(AVKey.DISPLAY_NAME, getNextName(toString()));
poly.setAltitudes(0.0, 0.0);
poly.setTerrainConforming(true, false);
this.initializePolygon(wwd, poly, fitShapeToViewport);
return poly;
}
@Override
public AirspaceEditor createEditor(Airspace airspace) {
PolygonEditor editor = new PolygonEditor();
editor.setPolygon((Polygon) airspace);
setEditorAttributes(editor);
return editor;
}
protected void initializePolygon(WorldWindow wwd, Polygon polygon, boolean fitShapeToViewport) {
// Creates a rectangle in the center of the viewport. Attempts to guess at a reasonable size and height.
Position position = ShapeUtils.getNewShapePosition(wwd);
Angle heading = ShapeUtils.getNewShapeHeading(wwd, true);
double sizeInMeters = fitShapeToViewport
? ShapeUtils.getViewportScaleFactor(wwd) : DEFAULT_SHAPE_SIZE_METERS;
java.util.List<LatLon> locations = ShapeUtils.createSquareInViewport(wwd, position, heading, sizeInMeters);
double maxElevation = -Double.MAX_VALUE;
Globe globe = wwd.getModel().getGlobe();
for (LatLon ll : locations) {
double e = globe.getElevation(ll.getLatitude(), ll.getLongitude());
if (e > maxElevation) {
maxElevation = e;
}
}
polygon.setAltitudes(0.0, maxElevation + sizeInMeters);
polygon.setTerrainConforming(true, false);
polygon.setLocations(locations);
}
@Override
public String toString() {
return "Polygon";
}
}
protected static class SphereAirspaceFactory implements AirspaceFactory {
public SphereAirspaceFactory() {
}
@Override
public Airspace createAirspace(WorldWindow wwd, boolean fitShapeToViewport) {
SphereAirspace sphere = new SphereAirspace();
sphere.setAttributes(getDefaultAttributes());
sphere.setValue(AVKey.DISPLAY_NAME, getNextName(toString()));
sphere.setAltitude(0.0);
sphere.setTerrainConforming(true);
this.initializeSphere(wwd, sphere, fitShapeToViewport);
return sphere;
}
@Override
public AirspaceEditor createEditor(Airspace airspace) {
SphereAirspaceEditor editor = new SphereAirspaceEditor();
editor.setSphere((SphereAirspace) airspace);
setEditorAttributes(editor);
return editor;
}
protected void initializeSphere(WorldWindow wwd, SphereAirspace sphere, boolean fitShapeToViewport) {
// Creates a sphere in the center of the viewport. Attempts to guess at a reasonable size and height.
Position position = ShapeUtils.getNewShapePosition(wwd);
double sizeInMeters = fitShapeToViewport
? ShapeUtils.getViewportScaleFactor(wwd) : DEFAULT_SHAPE_SIZE_METERS;
sphere.setLocation(new LatLon(position));
sphere.setRadius(sizeInMeters / 2.0);
}
@Override
public String toString() {
return "Sphere";
}
}
public static AirspaceAttributes getDefaultAttributes() {
AirspaceAttributes attributes = new BasicAirspaceAttributes();
attributes.setInteriorMaterial(new Material(Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, Color.BLACK, 0.0f));
attributes.setOutlineMaterial(Material.DARK_GRAY);
attributes.setDrawOutline(true);
attributes.setInteriorOpacity(0.95);
attributes.setOutlineOpacity(.95);
attributes.setOutlineWidth(2);
return attributes;
}
public static AirspaceAttributes getSelectionAttributes() {
AirspaceAttributes attributes = new BasicAirspaceAttributes();
attributes.setInteriorMaterial(Material.WHITE);
attributes.setOutlineMaterial(Material.BLACK);
attributes.setDrawOutline(true);
attributes.setInteriorOpacity(0.8);
attributes.setOutlineOpacity(0.8);
attributes.setOutlineWidth(2);
return attributes;
}
public static AirspaceAttributes getIntersectionAttributes() {
AirspaceAttributes attributes = new BasicAirspaceAttributes();
attributes.setInteriorMaterial(Material.RED);
attributes.setInteriorOpacity(0.95);
return attributes;
}
public static AirspaceAttributes getSelectionAndIntersectionAttributes() {
AirspaceAttributes attributes = new BasicAirspaceAttributes();
attributes.setInteriorMaterial(Material.ORANGE);
attributes.setInteriorOpacity(0.8);
return attributes;
}
public static void setEditorAttributes(AirspaceEditor editor) {
editor.setUseRubberBand(true);
editor.setKeepControlPointsAboveTerrain(true);
}
public static String getNextName(String base) {
StringBuilder sb = new StringBuilder();
sb.append(base);
sb.append(nextEntryNumber++);
return sb.toString();
}
protected static AirspaceEditor getEditorFor(Airspace airspace) {
if (airspace instanceof Polygon) {
PolygonEditor editor = new PolygonEditor();
editor.setPolygon((Polygon) airspace);
setEditorAttributes(editor);
return editor;
} else if (airspace instanceof SphereAirspace) {
SphereAirspaceEditor editor = new SphereAirspaceEditor();
editor.setSphere((SphereAirspace) airspace);
setEditorAttributes(editor);
return editor;
}
return null;
}
protected static long nextEntryNumber = 1;
//**************************************************************//
//******************** Airspace Builder Panel ****************//
//**************************************************************//
@SuppressWarnings("unchecked")
protected static class AirspaceBuilderPanel extends JPanel {
protected JComboBox factoryComboBox;
protected JTable entryTable;
protected boolean ignoreSelectEvents = false;
public AirspaceBuilderPanel(AirspaceBuilderModel model, AirspaceBuilderController controller) {
this.initComponents(model, controller);
}
public int[] getSelectedIndices() {
return this.entryTable.getSelectedRows();
}
public void setSelectedIndices(int[] indices) {
this.ignoreSelectEvents = true;
if (indices != null && indices.length != 0) {
for (int index : indices) {
this.entryTable.setRowSelectionInterval(index, index);
}
} else {
this.entryTable.clearSelection();
}
this.ignoreSelectEvents = false;
}
public AirspaceFactory getSelectedFactory() {
return (AirspaceFactory) this.factoryComboBox.getSelectedItem();
}
public void setSelectedFactory(AirspaceFactory factory) {
this.factoryComboBox.setSelectedItem(factory);
}
protected void initComponents(AirspaceBuilderModel model, final AirspaceBuilderController controller) {
final JCheckBox resizeNewShapesCheckBox;
final JCheckBox enableEditCheckBox;
JPanel newShapePanel = new JPanel();
{
JButton newShapeButton = new JButton("New shape");
newShapeButton.setActionCommand(NEW_AIRSPACE);
newShapeButton.addActionListener(controller);
newShapeButton.setToolTipText("Create a new shape centered in the viewport");
this.factoryComboBox = new JComboBox(defaultAirspaceFactories);
this.factoryComboBox.setEditable(false);
this.factoryComboBox.setToolTipText("Choose shape type to create");
resizeNewShapesCheckBox = new JCheckBox("Fit new shapes to viewport");
resizeNewShapesCheckBox.setActionCommand(SIZE_NEW_SHAPES_TO_VIEWPORT);
resizeNewShapesCheckBox.addActionListener(controller);
resizeNewShapesCheckBox.setSelected(controller.isResizeNewShapesToViewport());
resizeNewShapesCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
resizeNewShapesCheckBox.setToolTipText("New shapes are sized to fit the geographic viewport");
enableEditCheckBox = new JCheckBox("Enable shape editing");
enableEditCheckBox.setActionCommand(ENABLE_EDIT);
enableEditCheckBox.addActionListener(controller);
enableEditCheckBox.setSelected(controller.isEnableEdit());
enableEditCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
enableEditCheckBox.setToolTipText("Allow modifications to shapes");
Box newShapeBox = Box.createHorizontalBox();
newShapeBox.add(newShapeButton);
newShapeBox.add(Box.createHorizontalStrut(5));
newShapeBox.add(this.factoryComboBox);
newShapeBox.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel gridPanel = new JPanel(new GridLayout(0, 1, 0, 5)); // rows, cols, hgap, vgap
gridPanel.add(newShapeBox);
gridPanel.add(resizeNewShapesCheckBox);
gridPanel.add(enableEditCheckBox);
newShapePanel.setLayout(new BorderLayout());
newShapePanel.add(gridPanel, BorderLayout.NORTH);
}
JPanel entryPanel = new JPanel();
{
this.entryTable = new JTable(model);
this.entryTable.setColumnSelectionAllowed(false);
this.entryTable.setRowSelectionAllowed(true);
this.entryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.entryTable.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
if (!ignoreSelectEvents) {
controller.actionPerformed(new ActionEvent(e.getSource(), -1, SELECTION_CHANGED));
}
});
this.entryTable.setToolTipText("<html>Click to select<br>Double-Click to rename</html>");
JScrollPane tablePane = new JScrollPane(this.entryTable);
tablePane.setPreferredSize(new Dimension(200, 100));
entryPanel.setLayout(new BorderLayout(0, 0)); // hgap, vgap
entryPanel.add(tablePane, BorderLayout.CENTER);
}
JPanel selectionPanel = new JPanel();
{
JButton delselectButton = new JButton("Deselect");
delselectButton.setActionCommand(CLEAR_SELECTION);
delselectButton.addActionListener(controller);
delselectButton.setToolTipText("Clear the selection");
JButton deleteButton = new JButton("Delete Selected");
deleteButton.setActionCommand(REMOVE_SELECTED);
deleteButton.addActionListener(controller);
deleteButton.setToolTipText("Delete selected shapes");
JPanel gridPanel = new JPanel(new GridLayout(0, 1, 0, 5)); // rows, cols, hgap, vgap
gridPanel.add(delselectButton);
gridPanel.add(deleteButton);
selectionPanel.setLayout(new BorderLayout());
selectionPanel.add(gridPanel, BorderLayout.NORTH);
}
this.setLayout(new BorderLayout(30, 0)); // hgap, vgap
this.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // top, left, bottom, right
this.add(newShapePanel, BorderLayout.WEST);
this.add(entryPanel, BorderLayout.CENTER);
this.add(selectionPanel, BorderLayout.EAST);
controller.addPropertyChangeListener((PropertyChangeEvent e) -> {
if (SIZE_NEW_SHAPES_TO_VIEWPORT.equals(e.getPropertyName())) {
resizeNewShapesCheckBox.setSelected(controller.isResizeNewShapesToViewport());
} else if (ENABLE_EDIT.equals(e.getPropertyName())) {
enableEditCheckBox.setSelected(controller.isEnableEdit());
}
});
}
}
//**************************************************************//
//******************** Airspace Builder Controller ***********//
//**************************************************************//
protected static class AirspaceBuilderController extends WWObjectImpl implements ActionListener, MouseListener,
AirspaceEditListener {
protected AppFrame app;
protected AirspaceBuilderModel model;
protected AirspaceBuilderPanel view;
protected AirspaceEntry selectedEntry;
protected AirspaceEditorController editorController;
protected boolean enabled = true;
protected boolean enableEdit = true;
protected boolean resizeNewShapes;
// UI components.
protected JFileChooser fileChooser;
public AirspaceBuilderController(AppFrame app) {
this.app = app;
this.editorController = new AirspaceEditorController();
// The ordering is important here; we want first pass at mouse events.
this.editorController.setWorldWindow(this.app.getWwd());
this.app.getWwd().getInputHandler().addMouseListener(this);
}
public AppFrame getApp() {
return this.app;
}
public AirspaceBuilderModel getModel() {
return this.model;
}
public void setModel(AirspaceBuilderModel model) {
this.model = model;
}
public AirspaceBuilderPanel getView() {
return this.view;
}
public void setView(AirspaceBuilderPanel view) {
this.view = view;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
getView().setEnabled(enabled);
getApp().setEnabled(enabled);
}
public boolean isEnableEdit() {
return this.enableEdit;
}
public void setEnableEdit(boolean enable) {
this.enableEdit = enable;
this.handleEnableEdit(enable);
this.firePropertyChange(ENABLE_EDIT, null, enable);
}
public boolean isResizeNewShapesToViewport() {
return this.resizeNewShapes;
}
public void setResizeNewShapesToViewport(boolean resize) {
this.resizeNewShapes = resize;
this.firePropertyChange(SIZE_NEW_SHAPES_TO_VIEWPORT, null, resize);
}
@Override
public void actionPerformed(ActionEvent e) {
if (!this.isEnabled()) {
return;
}
if (null != e.getActionCommand()) {
switch (e.getActionCommand()) {
case NEW_AIRSPACE:
this.createNewEntry(this.getView().getSelectedFactory());
break;
case CLEAR_SELECTION:
this.selectEntry(null, true);
break;
case SIZE_NEW_SHAPES_TO_VIEWPORT:
if (e.getSource() instanceof AbstractButton) {
boolean selected = ((AbstractButton) e.getSource()).isSelected();
this.setResizeNewShapesToViewport(selected);
}
break;
case ENABLE_EDIT:
if (e.getSource() instanceof AbstractButton) {
boolean selected = ((AbstractButton) e.getSource()).isSelected();
this.setEnableEdit(selected);
}
break;
case OPEN:
this.openFromFile();
break;
case OPEN_URL:
this.openFromURL();
break;
case OPEN_DEMO_AIRSPACES:
this.openFromPath(DEMO_AIRSPACES_PATH);
this.zoomTo(LatLon.fromDegrees(47.6584074779224, -122.3059199579634),
Angle.fromDegrees(-152), Angle.fromDegrees(75), 750);
break;
case REMOVE_SELECTED:
this.removeEntries(Arrays.asList(this.getSelectedEntries()));
break;
case SAVE:
this.saveToFile();
break;
case SELECTION_CHANGED:
this.viewSelectionChanged();
break;
default:
break;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e == null || e.isConsumed()) {
return;
}
if (!this.isEnabled()) {
return;
}
if (e.getButton() == MouseEvent.BUTTON1) {
this.handleSelect();
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void airspaceMoved(AirspaceEditEvent e) {
this.updateShapeIntersection();
}
@Override
public void airspaceResized(AirspaceEditEvent e) {
this.updateShapeIntersection();
}
@Override
public void controlPointAdded(AirspaceEditEvent e) {
}
@Override
public void controlPointRemoved(AirspaceEditEvent e) {
}
@Override
public void controlPointChanged(AirspaceEditEvent e) {
}
protected void handleSelect() {
// If the picked object is null or something other than an airspace, then ignore the mouse click. If we
// deselect the current entry at this point, the user cannot easily navigate without loosing the selection.
PickedObjectList pickedObjects = this.getApp().getWwd().getObjectsAtCurrentPosition();
Object topObject = pickedObjects.getTopObject();
if (!(topObject instanceof Airspace)) {
return;
}
AirspaceEntry pickedEntry = this.getEntryFor((Airspace) topObject);
if (pickedEntry == null) {
return;
}
if (this.getSelectedEntry() != pickedEntry) {
this.selectEntry(pickedEntry, true);
}
}
protected void handleEnableEdit(boolean enable) {
if (this.getSelectedEntry() == null) {
return;
}
if (this.isSelectionEditing() != enable) {
this.setSelectionEditing(enable);
}
}
protected void updateShapeIntersection() {
AirspaceEntry selected = this.getSelectedEntry();
if (selected != null) {
boolean hasIntersection = false;
for (AirspaceEntry entry : this.getModel().getEntries()) {
if (entry != selected) {
boolean intersecting = this.areShapesIntersecting(entry.getAirspace(), selected.getAirspace());
if (intersecting) {
hasIntersection = true;
}
entry.setIntersecting(intersecting);
}
}
selected.setIntersecting(hasIntersection);
} else {
for (AirspaceEntry entry : this.getModel().getEntries()) {
entry.setIntersecting(false);
}
}
}
protected boolean areShapesIntersecting(Airspace a1, Airspace a2) {
if ((a1 instanceof SphereAirspace) && (a2 instanceof SphereAirspace)) {
SphereAirspace s1 = (SphereAirspace) a1;
SphereAirspace s2 = (SphereAirspace) a2;
LatLon location1 = s1.getLocation();
LatLon location2 = s2.getLocation();
double altitude1 = s1.getAltitudes()[0];
double altitude2 = s2.getAltitudes()[0];
boolean terrainConforming1 = s1.isTerrainConforming()[0];
boolean terrainConforming2 = s2.isTerrainConforming()[0];
// We have to compute the 3D coordinates of the sphere's center ourselves here.
Vec4 p1 = terrainConforming1 ? this.getSurfacePoint(location1, altitude1)
: this.getPoint(location1, altitude1);
Vec4 p2 = terrainConforming2 ? this.getSurfacePoint(location2, altitude2)
: this.getPoint(location2, altitude2);
double r1 = s1.getRadius();
double r2 = s2.getRadius();
double d = p1.distanceTo3(p2);
return d <= (r1 + r2);
}
return false;
}
protected Vec4 getSurfacePoint(LatLon latlon, double elevation) {
Vec4 point = null;
SceneController sc = this.getApp().getWwd().getSceneController();
Globe globe = this.getApp().getWwd().getModel().getGlobe();
if (sc.getTerrain() != null) {
point = sc.getTerrain().getSurfacePoint(
latlon.getLatitude(), latlon.getLongitude(), elevation * sc.getVerticalExaggeration());
}
if (point == null) {
double e = globe.getElevation(latlon.getLatitude(), latlon.getLongitude());
point = globe.computePointFromPosition(
latlon.getLatitude(), latlon.getLongitude(), (e + elevation) * sc.getVerticalExaggeration());
}
return point;
}
protected Vec4 getPoint(LatLon latlon, double elevation) {
SceneController sc = this.getApp().getWwd().getSceneController();
Globe globe = this.getApp().getWwd().getModel().getGlobe();
double e = globe.getElevation(latlon.getLatitude(), latlon.getLongitude());
return globe.computePointFromPosition(
latlon.getLatitude(), latlon.getLongitude(), (e + elevation) * sc.getVerticalExaggeration());
}
public void createNewEntry(AirspaceFactory factory) {
Airspace airspace = factory.createAirspace(this.getApp().getWwd(), this.isResizeNewShapesToViewport());
AirspaceEditor editor = factory.createEditor(airspace);
AirspaceEntry entry = new AirspaceEntry(airspace, editor);
this.addEntry(entry);
this.selectEntry(entry, true);
}
public void removeEntries(Iterable<? extends AirspaceEntry> entries) {
if (entries != null) {
for (AirspaceEntry entry : entries) {
this.removeEntry(entry);
}
}
}
public void addEntry(AirspaceEntry entry) {
entry.getEditor().addEditListener(this);
this.getModel().addEntry(entry);
this.updateShapeIntersection();
this.getApp().getAirspaceLayer().addRenderable(entry.getAirspace());
this.getApp().getWwd().redraw();
}
public void removeEntry(AirspaceEntry entry) {
entry.getEditor().removeEditListener(this);
if (this.getSelectedEntry() == entry) {
this.selectEntry(null, true);
}
this.getModel().removeEntry(entry);
this.updateShapeIntersection();
this.getApp().getAirspaceLayer().removeRenderable(entry.getAirspace());
this.getApp().getWwd().redraw();
}
public AirspaceEntry getSelectedEntry() {
return this.selectedEntry;
}
public void selectEntry(AirspaceEntry entry, boolean updateView) {
this.setSelectedEntry(entry);
if (updateView) {
if (entry != null) {
int index = this.getModel().getIndexForEntry(entry);
this.getView().setSelectedIndices(new int[]{index});
} else {
this.getView().setSelectedIndices(new int[0]);
}
}
if (this.isEnableEdit()) {
if (this.getSelectedEntry() != null && !this.isSelectionEditing()) {
this.setSelectionEditing(true);
}
}
this.updateShapeIntersection();
this.getApp().getWwd().redraw();
}
protected void setSelectedEntry(AirspaceEntry entry) {
if (this.selectedEntry != null) {
if (this.selectedEntry != entry && this.selectedEntry.isEditing()) {
this.setSelectionEditing(false);
}
this.selectedEntry.setSelected(false);
}
this.selectedEntry = entry;
if (this.selectedEntry != null) {
this.selectedEntry.setSelected(true);
}
}
protected boolean isSelectionEditing() {
return this.selectedEntry != null && this.selectedEntry.isEditing();
}
protected void setSelectionEditing(boolean editing) {
if (this.selectedEntry == null) {
throw new IllegalStateException();
}
if (this.selectedEntry.isEditing() == editing) {