summaryrefslogtreecommitdiff
path: root/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java
blob: 305df99cb30e4709de289c19624be135426d09ce (plain)
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
package com.benburwell.planes.gui.aircraftmap;

import com.benburwell.planes.data.*;
import com.benburwell.planes.gui.Tabbable;
import com.benburwell.planes.gui.aircraftmap.symbols.PlaneSymbol;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by ben on 11/18/16.
 */
public class AircraftMapComponent implements Tabbable {
    private AircraftStore store;
    private CSVObjectStore<NavigationAid> navaids;
    private CSVObjectStore<Airport> airportStore;
    private AircraftMap mapPanel;
    private String focusedAircraftIdentifier = null;

    public AircraftMapComponent(AircraftStore store, CSVObjectStore<NavigationAid> navaids, CSVObjectStore<Airport> airportStore) {
        this.store = store;
        this.navaids = navaids;
        this.airportStore = airportStore;

        this.setupMap();
        this.bindKeys();
        this.subscribeToChanges();
        this.mapPanel.addNavAids(this.navaids.getObjects());
        this.mapPanel.addAirports(this.airportStore.getObjects());
    }

    public void focusNextAircraft() {
    //    List<String> aircraftIdentifiers = new ArrayList<>(this.store.getAircraft().keySet());
    //    Collections.sort(aircraftIdentifiers);
    //    if (this.focusedAircraftIdentifier == null && aircraftIdentifiers.size() > 0) {
    //        this.focusedAircraftIdentifier = aircraftIdentifiers.get(0);
    //    } else {
    //        int idx = aircraftIdentifiers.indexOf(this.focusedAircraftIdentifier);
    //        if (idx > 0 && idx < aircraftIdentifiers.size() - 1) {
    //            this.focusedAircraftIdentifier = aircraftIdentifiers.get(idx++);
    //        } else if (aircraftIdentifiers.size() > 0) {
    //            this.focusedAircraftIdentifier = aircraftIdentifiers.get(0);
    //        } else {
    //            this.focusedAircraftIdentifier = null;
    //        }
    //    }
    }

    private void setupMap() {
        this.mapPanel = new AircraftMap();
        this.mapPanel.setCenter(40.6188942, -75.4947205);
    }

    private void bindKeys() {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
            if (e.getKeyCode() == KeyEvent.VK_EQUALS && e.isShiftDown() && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.zoomIn();
            } else if (e.getKeyCode() == KeyEvent.VK_MINUS && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.zoomOut();
            } else if (e.getKeyCode() == KeyEvent.VK_L && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.moveEast();
            } else if (e.getKeyCode() == KeyEvent.VK_H && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.moveWest();
            } else if (e.getKeyCode() == KeyEvent.VK_J && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.moveSouth();
            } else if (e.getKeyCode() == KeyEvent.VK_K && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.moveNorth();
            } else if (e.getKeyCode() == KeyEvent.VK_0 && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.setCenter(40.6188942, -75.4947205);
            } else if (e.getKeyCode() == KeyEvent.VK_TAB && e.getID() == KeyEvent.KEY_PRESSED) {
                this.focusNextAircraft();
                this.centerMapOnPlane(this.focusedAircraftIdentifier);
            } else if (e.getKeyCode() == KeyEvent.VK_N && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.toggleNavAids();
            } else if (e.getKeyCode() == KeyEvent.VK_A && e.getID() == KeyEvent.KEY_PRESSED) {
                this.mapPanel.toggleAirports();
            }
            return false;
        });
    }

    private void centerMapOnPlane(String identifier) {
        if (identifier != null) {
            Position pos = this.store.getAircraft().get(identifier).getCurrentPosition();
            this.mapPanel.setCenter(pos.getLatitude(), pos.getLongitude());
        }
    }

    private void subscribeToChanges() {
        this.store.subscribe(new AircraftStoreListener() {
            @Override
            public void aircraftStoreChanged() {
                List<Drawable> planes = new ArrayList<>();
                store.getAircraft().values().forEach(aircraft -> planes.add(new PlaneSymbol(aircraft)));
                mapPanel.setPlanes(planes);
                mapPanel.validate();
                mapPanel.repaint();
            }

            @Override
            public boolean respondTo(String aircraftId) {
                return true;
            }
        });
    }

    @Override
    public JComponent getComponent() {
        return this.mapPanel;
    }

    @Override
    public String getName() {
        return "Map";
    }
}