From a448320cc4661f9304a8323d6c0e05f1f0fee2b3 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Sat, 19 Nov 2016 22:55:10 -0500 Subject: Use resources for data (for now) At some point, it would be nice for this to be more configurable --- .../gui/aircrafttable/AircraftTableComponent.java | 27 +++++++ .../gui/aircrafttable/AircraftTableModel.java | 85 ++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java create mode 100644 src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java (limited to 'src/main/java/com/benburwell/planes/gui/aircrafttable') diff --git a/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java new file mode 100644 index 0000000..8f1e71d --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java @@ -0,0 +1,27 @@ +package com.benburwell.planes.gui.aircrafttable; + +import com.benburwell.planes.data.AircraftStore; +import com.benburwell.planes.gui.ViewComponent; + +import javax.swing.*; + +/** + * Created by ben on 11/17/16. + */ +public class AircraftTableComponent implements ViewComponent { + private JTable table; + private AircraftTableModel tableModel; + private JScrollPane scrollPane; + + public AircraftTableComponent(AircraftStore store) { + this.tableModel = new AircraftTableModel(store); + this.table = new JTable(this.tableModel); + this.table.setFillsViewportHeight(true); + this.scrollPane = new JScrollPane(table); + } + + @Override + public JComponent getComponent() { + return this.scrollPane; + } +} diff --git a/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java new file mode 100644 index 0000000..66dc798 --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java @@ -0,0 +1,85 @@ +package com.benburwell.planes.gui.aircrafttable; + +import com.benburwell.planes.data.Aircraft; +import com.benburwell.planes.data.AircraftStore; +import com.benburwell.planes.data.AircraftStoreListener; + +import javax.swing.table.AbstractTableModel; +import java.util.Map; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +/** + * Created by ben on 11/15/16. + */ +public class AircraftTableModel extends AbstractTableModel { + private Map aircraftMap; + private String[] columnNames = { "Hex", "Callsign", "Squawk", "Latitude", "Longitude", "Altitude", "Vertical Rate", "Track", "Ground Speed", "Packets" }; + + public AircraftTableModel(AircraftStore store) { + this.aircraftMap = store.getAircraft(); + store.subscribe(new AircraftStoreListener() { + @Override + public void aircraftStoreChanged() { + AircraftTableModel.super.fireTableDataChanged(); + } + + @Override + public boolean respondTo(String aircraftId) { + // listen for all changes + return true; + } + }); + } + + @Override + public int getRowCount() { + return this.aircraftMap.keySet().size(); + } + + @Override + public int getColumnCount() { + return this.columnNames.length; + } + + @Override + public String getColumnName(int col) { + return this.columnNames[col]; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + List aircraftList = this.getAircraftList(); + Aircraft aircraft = aircraftList.get(rowIndex); + switch (columnIndex) { + case 0: + return aircraft.getHexIdent(); + case 1: + return aircraft.getCallsign(); + case 2: + return aircraft.getSquawk(); + case 3: + return aircraft.getCurrentPosition().getLatitude(); + case 4: + return aircraft.getCurrentPosition().getLongitude(); + case 5: + return aircraft.getCurrentPosition().getAltitude(); + case 6: + return aircraft.getVerticalRate(); + case 7: + return aircraft.getTrack(); + case 8: + return aircraft.getGroundSpeed(); + case 9: + return aircraft.getPacketCount(); + } + return ""; + } + + private List getAircraftList() { + List aircraftList = new ArrayList<>(this.aircraftMap.values()); + Collections.sort(aircraftList); + return aircraftList; + } +} -- cgit v1.2.3