diff options
author | Ben Burwell <ben.burwell@trifecta.com> | 2016-11-19 22:55:10 -0500 |
---|---|---|
committer | Ben Burwell <ben.burwell@trifecta.com> | 2016-11-19 22:55:10 -0500 |
commit | a448320cc4661f9304a8323d6c0e05f1f0fee2b3 (patch) | |
tree | 274310efc18316c48e0911a428a0f0bce7899033 /src/main/java/com/benburwell/planes/gui/aircrafttable | |
parent | 70d15df43ea0d57c3fe237211098645733048c45 (diff) |
Use resources for data (for now)
At some point, it would be nice for this to be more configurable
Diffstat (limited to 'src/main/java/com/benburwell/planes/gui/aircrafttable')
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java | 27 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java | 85 |
2 files changed, 112 insertions, 0 deletions
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<String,Aircraft> 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<Aircraft> 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<Aircraft> getAircraftList() { + List<Aircraft> aircraftList = new ArrayList<>(this.aircraftMap.values()); + Collections.sort(aircraftList); + return aircraftList; + } +} |