summaryrefslogtreecommitdiff
path: root/src/com/benburwell/planes/gui/AircraftMapComponent.java
blob: 268b5247fbbc8354afa6515c01e0feea482b96f7 (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
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
package com.benburwell.planes.gui;

import com.benburwell.planes.data.AircraftStore;
import com.benburwell.planes.data.AircraftStoreListener;
import com.benburwell.planes.data.Position;

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

/**
 * Created by ben on 11/18/16.
 */
public class AircraftMapComponent implements ViewComponent {
    private AircraftStore store;
    private AircraftMap mapPanel;
    private String focusedAircraftIdentifier = null;

    public AircraftMapComponent(AircraftStore store) {
        this.store = store;
        this.setupMap();
        this.bindKeys();
        this.subscribeToChanges();
    }

    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);
            }
            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 -> {
                    String name = !aircraft.getCallsign().isEmpty() ? aircraft.getCallsign() : aircraft.getHexIdent();
                    double lat = aircraft.getCurrentPosition().getLatitude();
                    double lon = aircraft.getCurrentPosition().getLongitude();
                    planes.add(new Plane(name, lat, lon));
                });
                mapPanel.setPlanes(planes);
                mapPanel.validate();
                mapPanel.repaint();
            }

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

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

    private class AircraftMap extends JPanel {
        private final float FONT_SIZE = 12;
        private final int TEXT_PADDING = 5;
        private final int ZOOM_INTERVAL = 100;
        private final double PAN_INTERVAL = 1.0 / 60.0;
        private final double MAX_LATITUDE = 90.0;
        private final double MIN_LATITUDE = -90.0;
        private final double MAX_LONGITUDE = 180.0;
        private final double MIN_LONGITUDE = -180.0;
        private final int RING_SPACING = 10;
        private List<Drawable> planes = new ArrayList<>();
        private double centerLatitude;
        private double centerLongitude;
        private int pixelsPerDegree = 600;

        public AircraftMap() {
            super();
            this.setBackground(GraphicsTheme.Colors.BASE_1);
            this.setBorder(BorderFactory.createEmptyBorder());
            this.setCenter(0, 0);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            this.planes.forEach(item -> item.drawOn(g, this));
            this.drawPositionAndScale(g);
            this.drawRange(g);
        }

        public void drawPositionAndScale(Graphics g) {
            Font currentFont = g.getFont();
            Font newFont = currentFont.deriveFont(FONT_SIZE);
            g.setFont(newFont);
            g.setColor(GraphicsTheme.Colors.BLUE);
            g.drawString(String.format("%08.5f N", this.centerLatitude), TEXT_PADDING, (int)FONT_SIZE + TEXT_PADDING);
            g.drawString(String.format("%08.5f E", this.centerLongitude), TEXT_PADDING, (int)FONT_SIZE * 2 + TEXT_PADDING);
        }

        public void drawRange(Graphics g) {
            int centerX = this.getWidth() / 2;
            int centerY = this.getHeight() / 2;
            g.setColor(GraphicsTheme.Colors.BASE_3);
            int diameter = (int) this.getPixelsPerNauticalMile() * RING_SPACING;
            int ringNumber = 1;
            while (diameter < this.getWidth() || diameter < this.getHeight()) {
                g.drawOval(centerX - (diameter / 2), centerY - (diameter / 2), diameter, diameter);
                g.drawString(String.format("%d", ringNumber * RING_SPACING), centerX + (diameter / 2) + TEXT_PADDING, (int) (centerY + FONT_SIZE + TEXT_PADDING));
                g.drawString(String.format("%d", ringNumber * RING_SPACING), centerX + TEXT_PADDING, centerY - (int) ((diameter / 2) + FONT_SIZE));
                diameter += this.getPixelsPerNauticalMile() * 10;
                ringNumber++;
            }
            g.drawLine(centerX, 0, centerX, this.getHeight());
            g.drawLine(0, centerY, this.getWidth(), centerY);
        }

        public void setPlanes(List<Drawable> planes) {
            this.planes = planes;
            this.invalidate();
        }

        public void setCenter(double latitude, double longitude) {
            this.centerLatitude = latitude;
            this.centerLongitude = longitude;
        }

        public double getCenterLatitude() {
            return this.centerLatitude;
        }

        public double getCenterLongitude() {
            return this.centerLongitude;
        }

        public double getPixelsPerDegree() {
            return this.pixelsPerDegree;
        }

        public double getPixelsPerNauticalMile() {
            return this.pixelsPerDegree / 60.0;
        }

        public void zoomIn() {
            this.pixelsPerDegree += ZOOM_INTERVAL;
            this.invalidate();
            this.validate();
            this.repaint();
        }

        public void zoomOut() {
            this.pixelsPerDegree -= ZOOM_INTERVAL;
            this.invalidate();
            this.validate();
            this.repaint();
        }

        public void moveEast() {
            this.centerLongitude = Math.min(this.centerLongitude + PAN_INTERVAL, MAX_LONGITUDE);
            this.invalidate();
            this.validate();
            this.repaint();
        }

        public void moveWest() {
            this.centerLongitude = Math.max(this.centerLongitude - PAN_INTERVAL, MIN_LONGITUDE);
            this.invalidate();
            this.validate();
            this.repaint();
        }

        public void moveNorth() {
            this.centerLatitude = Math.min(this.centerLatitude + PAN_INTERVAL, MAX_LATITUDE);
            this.invalidate();
            this.validate();
            this.repaint();
        }

        public void moveSouth() {
            this.centerLatitude = Math.max(this.centerLatitude - PAN_INTERVAL, MIN_LATITUDE);
            this.invalidate();
            this.validate();
            this.repaint();
        }
    }

    private interface Drawable {
        void drawOn(Graphics graphicsContext, AircraftMap map);
    }

    private class GeoPoint {
        private double latitude;
        private double longitude;

        public GeoPoint(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public int getX(AircraftMap map) {
            double degreesFromCenter = map.getCenterLongitude() - this.longitude;
            double pixelsFromCenter = degreesFromCenter * map.getPixelsPerDegree();
            double centerPixels = map.getSize().getWidth() / 2;
            int xPosition = (int)(centerPixels - pixelsFromCenter);
            return xPosition;
        }

        public int getY(AircraftMap map) {
            double degreesFromCenter = map.getCenterLatitude() - this.latitude;
            double pixelsFromCenter = degreesFromCenter * map.getPixelsPerDegree();
            double centerPixels = map.getSize().getHeight() / 2;
            int yPosition = (int)(centerPixels + pixelsFromCenter);
            return yPosition;
        }
    }

    private class Plane extends GeoPoint implements Drawable {
        private String name;
        private final int DOT_DIMENSION = 10;
        private final int TEXT_OFFSET_X = 10;
        private final int TEXT_OFFSET_Y = 15;

        public Plane(String name, double latitude, double longitude) {
            super(latitude, longitude);
            this.name = name;
        }

        public void drawOn(Graphics g, AircraftMap map) {
            int x = this.getX(map);
            int y = this.getY(map);

            // draw the plane dot
            g.setColor(GraphicsTheme.Colors.ORANGE);
            g.fillOval(x - (DOT_DIMENSION / 2), y - (DOT_DIMENSION / 2), DOT_DIMENSION, DOT_DIMENSION);

            // draw the name of the plane
            g.setColor(GraphicsTheme.Colors.BLUE);
            g.drawString(this.name, this.getX(map) + TEXT_OFFSET_X, this.getY(map) + TEXT_OFFSET_Y);
        }
    }
}