From 963f5dabe12e78a1464d5374779b71825c3cf470 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Sat, 19 Nov 2016 00:32:22 -0500 Subject: Implement basic focusing of planes --- .../planes/gui/AircraftMapComponent.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/com/benburwell/planes/gui/AircraftMapComponent.java b/src/com/benburwell/planes/gui/AircraftMapComponent.java index 780f1db..268b524 100644 --- a/src/com/benburwell/planes/gui/AircraftMapComponent.java +++ b/src/com/benburwell/planes/gui/AircraftMapComponent.java @@ -2,12 +2,14 @@ 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. @@ -15,6 +17,7 @@ import java.util.ArrayList; public class AircraftMapComponent implements ViewComponent { private AircraftStore store; private AircraftMap mapPanel; + private String focusedAircraftIdentifier = null; public AircraftMapComponent(AircraftStore store) { this.store = store; @@ -23,6 +26,23 @@ public class AircraftMapComponent implements ViewComponent { this.subscribeToChanges(); } + public void focusNextAircraft() { + List 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); @@ -42,11 +62,23 @@ public class AircraftMapComponent implements ViewComponent { 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 -- cgit v1.2.3