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 | |
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')
12 files changed, 87 insertions, 92 deletions
diff --git a/src/main/java/com/benburwell/planes/data/AbstractCSVReader.java b/src/main/java/com/benburwell/planes/data/AbstractCSVReader.java new file mode 100644 index 0000000..66084ed --- /dev/null +++ b/src/main/java/com/benburwell/planes/data/AbstractCSVReader.java @@ -0,0 +1,10 @@ +package com.benburwell.planes.data; + +import org.apache.commons.csv.CSVRecord; + +/** + * Created by ben on 11/19/16. + */ +public abstract class AbstractCSVReader { + public abstract void readRecord(CSVRecord record); +} diff --git a/src/main/java/com/benburwell/planes/data/Airport.java b/src/main/java/com/benburwell/planes/data/Airport.java index 92e43bc..ab16248 100644 --- a/src/main/java/com/benburwell/planes/data/Airport.java +++ b/src/main/java/com/benburwell/planes/data/Airport.java @@ -5,7 +5,7 @@ import org.apache.commons.csv.CSVRecord; /** * Created by ben on 11/19/16. */ -public class Airport { +public class Airport extends AbstractCSVReader { private int id; private String ident; private String type; @@ -25,7 +25,7 @@ public class Airport { private String wikipediaLink; private String keywords; - public Airport(CSVRecord record) { + public void readRecord(CSVRecord record) { this.setId(Integer.valueOf(record.get("id"))); this.setIdent(record.get("ident")); this.setType(record.get("type")); diff --git a/src/main/java/com/benburwell/planes/data/AirportStore.java b/src/main/java/com/benburwell/planes/data/AirportStore.java deleted file mode 100644 index 93f1ccc..0000000 --- a/src/main/java/com/benburwell/planes/data/AirportStore.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.benburwell.planes.data; - -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVParser; -import org.apache.commons.csv.CSVRecord; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by ben on 11/19/16. - */ -public class AirportStore { - private List<Airport> airports = new ArrayList<>(); - - public void readFromFile(String filename) throws IOException { - File csvData = new File(filename); - CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180.withFirstRecordAsHeader()); - for (CSVRecord record : parser) { - this.airports.add(new Airport(record)); - } - } - - public List<Airport> getAirports() { - return this.airports; - } -} diff --git a/src/main/java/com/benburwell/planes/data/CSVObjectStore.java b/src/main/java/com/benburwell/planes/data/CSVObjectStore.java new file mode 100644 index 0000000..a92e0a2 --- /dev/null +++ b/src/main/java/com/benburwell/planes/data/CSVObjectStore.java @@ -0,0 +1,42 @@ +package com.benburwell.planes.data; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; + +import java.io.*; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by ben on 11/19/16. + */ +public class CSVObjectStore<T extends AbstractCSVReader> { + private List<T> objects = new ArrayList<>(); + + public void readFromFile(String fileName, Class<T> klass) throws IOException, IllegalAccessException, InstantiationException { + File csvData = new File(fileName); + CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180.withFirstRecordAsHeader()); + for (CSVRecord record : parser) { + T obj = klass.newInstance(); + obj.readRecord(record); + this.objects.add(obj); + } + } + + public void readFromResource(String resourceName, Class<T> klass) throws IOException, IllegalAccessException, InstantiationException { + InputStream stream = this.getClass().getResourceAsStream(resourceName); + Reader csvData = new BufferedReader(new InputStreamReader(stream)); + CSVParser parser = new CSVParser(csvData, CSVFormat.RFC4180.withFirstRecordAsHeader()); + for (CSVRecord record : parser) { + T obj = klass.newInstance(); + obj.readRecord(record); + this.objects.add(obj); + } + } + + public List<T> getObjects() { + return this.objects; + } +} diff --git a/src/main/java/com/benburwell/planes/data/NavigationAid.java b/src/main/java/com/benburwell/planes/data/NavigationAid.java index 85219ba..d730e7e 100644 --- a/src/main/java/com/benburwell/planes/data/NavigationAid.java +++ b/src/main/java/com/benburwell/planes/data/NavigationAid.java @@ -7,7 +7,7 @@ import org.apache.commons.csv.CSVRecord; * * Created by ben on 11/19/16. */ -public class NavigationAid { +public class NavigationAid extends AbstractCSVReader { private int id; private String filename; private String ident; @@ -29,7 +29,7 @@ public class NavigationAid { private String power; private String associatedAirport; - public NavigationAid(CSVRecord record) { + public void readRecord(CSVRecord record) { this.setId(Integer.valueOf(record.get("id"))); this.setFilename(record.get("filename")); this.setIdent(record.get("ident")); diff --git a/src/main/java/com/benburwell/planes/data/NavigationAidStore.java b/src/main/java/com/benburwell/planes/data/NavigationAidStore.java deleted file mode 100644 index 6f0ce9c..0000000 --- a/src/main/java/com/benburwell/planes/data/NavigationAidStore.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.benburwell.planes.data; - -import org.apache.commons.csv.CSVFormat; -import org.apache.commons.csv.CSVParser; -import org.apache.commons.csv.CSVRecord; - -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.io.File; - -/** - * Created by ben on 11/19/16. - */ -public class NavigationAidStore { - private List<NavigationAid> aids = new ArrayList<>(); - - public void readFromFile(String fileName) throws IOException { - File csvData = new File(fileName); - CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180.withFirstRecordAsHeader()); - for (CSVRecord record : parser) { - this.aids.add(new NavigationAid(record)); - } - } - - public List<NavigationAid> getNavigationAids() { - return this.aids; - } -} diff --git a/src/main/java/com/benburwell/planes/gui/Main1090.java b/src/main/java/com/benburwell/planes/gui/Main1090.java index 962e428..b0847bd 100644 --- a/src/main/java/com/benburwell/planes/gui/Main1090.java +++ b/src/main/java/com/benburwell/planes/gui/Main1090.java @@ -4,6 +4,8 @@ package com.benburwell.planes.gui; +import com.benburwell.planes.gui.aircrafttable.AircraftTableComponent; +import com.benburwell.planes.gui.navigationaids.NavigationAidComponent; import com.benburwell.planes.sbs.*; import com.benburwell.planes.data.*; import com.benburwell.planes.gui.aircraftmap.*; @@ -16,8 +18,8 @@ import java.io.IOException; public class Main1090 extends JFrame { private AggregateDataSource sbsDataSource = new AggregateDataSource(); private AircraftStore aircraft = new AircraftStore(); - private NavigationAidStore navaids = new NavigationAidStore(); - private AirportStore airports = new AirportStore(); + private CSVObjectStore<NavigationAid> navaids = new CSVObjectStore<>(); + private CSVObjectStore<Airport> airports = new CSVObjectStore<>(); private int currentTcpConnection = 0; private JTabbedPane tabbedPane = new JTabbedPane(); @@ -36,14 +38,14 @@ public class Main1090 extends JFrame { this.openDataSource(); try { - this.navaids.readFromFile("/home/ben/.airdata/navaids.csv"); - } catch (IOException e) { + this.navaids.readFromResource("/navaids.csv", NavigationAid.class); + } catch (IOException | InstantiationException | IllegalAccessException e) { System.out.println("Could not read navaid file: " + e.getMessage()); } try { - this.airports.readFromFile("/home/ben/.airdata/airports.csv"); - } catch (IOException e) { + this.airports.readFromResource("/airports.csv", Airport.class); + } catch (IOException | InstantiationException | IllegalAccessException e) { System.out.println("Could not read airport file: " + e.getMessage()); } @@ -51,19 +53,17 @@ public class Main1090 extends JFrame { } private void createTabs() { - AircraftTableComponent aircraftData = new AircraftTableComponent(this.aircraft); - this.tabbedPane.addTab("Aircraft Data", aircraftData.getComponent()); - AircraftMapComponent aircraftMap = new AircraftMapComponent(this.aircraft, this.navaids, this.airports); this.tabbedPane.addTab("Live Map", aircraftMap.getComponent()); + AircraftTableComponent aircraftData = new AircraftTableComponent(this.aircraft); + this.tabbedPane.addTab("Aircraft Data", aircraftData.getComponent()); + NavigationAidComponent navaids = new NavigationAidComponent(this.navaids); this.tabbedPane.addTab("Navigation Aids", navaids.getComponent()); this.add(this.tabbedPane); this.tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); - - this.tabbedPane.setSelectedIndex(1); } private void createMenuBar() { diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java index 24dfc94..b4bb7e2 100644 --- a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java @@ -15,12 +15,12 @@ import java.util.ArrayList; */ public class AircraftMapComponent implements ViewComponent { private AircraftStore store; - private NavigationAidStore navaids; - private AirportStore airportStore; + private CSVObjectStore<NavigationAid> navaids; + private CSVObjectStore<Airport> airportStore; private AircraftMap mapPanel; private String focusedAircraftIdentifier = null; - public AircraftMapComponent(AircraftStore store, NavigationAidStore navaids, AirportStore airportStore) { + public AircraftMapComponent(AircraftStore store, CSVObjectStore<NavigationAid> navaids, CSVObjectStore<Airport> airportStore) { this.store = store; this.navaids = navaids; this.airportStore = airportStore; @@ -28,8 +28,8 @@ public class AircraftMapComponent implements ViewComponent { this.setupMap(); this.bindKeys(); this.subscribeToChanges(); - this.mapPanel.addNavAids(this.navaids.getNavigationAids()); - this.mapPanel.addAirports(this.airportStore.getAirports()); + this.mapPanel.addNavAids(this.navaids.getObjects()); + this.mapPanel.addAirports(this.airportStore.getObjects()); } public void focusNextAircraft() { diff --git a/src/main/java/com/benburwell/planes/gui/AircraftTableComponent.java b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java index 221bdda..8f1e71d 100644 --- a/src/main/java/com/benburwell/planes/gui/AircraftTableComponent.java +++ b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableComponent.java @@ -1,13 +1,14 @@ -package com.benburwell.planes.gui; +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{ +public class AircraftTableComponent implements ViewComponent { private JTable table; private AircraftTableModel tableModel; private JScrollPane scrollPane; diff --git a/src/main/java/com/benburwell/planes/gui/AircraftTableModel.java b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java index 3931893..66dc798 100644 --- a/src/main/java/com/benburwell/planes/gui/AircraftTableModel.java +++ b/src/main/java/com/benburwell/planes/gui/aircrafttable/AircraftTableModel.java @@ -1,4 +1,4 @@ -package com.benburwell.planes.gui; +package com.benburwell.planes.gui.aircrafttable; import com.benburwell.planes.data.Aircraft; import com.benburwell.planes.data.AircraftStore; diff --git a/src/main/java/com/benburwell/planes/gui/NavigationAidComponent.java b/src/main/java/com/benburwell/planes/gui/navigationaids/NavigationAidComponent.java index a1f97cf..1ad7e3e 100644 --- a/src/main/java/com/benburwell/planes/gui/NavigationAidComponent.java +++ b/src/main/java/com/benburwell/planes/gui/navigationaids/NavigationAidComponent.java @@ -1,6 +1,8 @@ -package com.benburwell.planes.gui; +package com.benburwell.planes.gui.navigationaids; -import com.benburwell.planes.data.NavigationAidStore; +import com.benburwell.planes.data.CSVObjectStore; +import com.benburwell.planes.data.NavigationAid; +import com.benburwell.planes.gui.ViewComponent; import javax.swing.*; @@ -12,7 +14,7 @@ public class NavigationAidComponent implements ViewComponent { private NavigationAidsTableModel tableModel; private JScrollPane scrollPane; - public NavigationAidComponent(NavigationAidStore store) { + public NavigationAidComponent(CSVObjectStore<NavigationAid> store) { this.tableModel = new NavigationAidsTableModel(store); this.table = new JTable(this.tableModel); this.table.setFillsViewportHeight(true); diff --git a/src/main/java/com/benburwell/planes/gui/NavigationAidsTableModel.java b/src/main/java/com/benburwell/planes/gui/navigationaids/NavigationAidsTableModel.java index 064ca25..90b05eb 100644 --- a/src/main/java/com/benburwell/planes/gui/NavigationAidsTableModel.java +++ b/src/main/java/com/benburwell/planes/gui/navigationaids/NavigationAidsTableModel.java @@ -1,7 +1,7 @@ -package com.benburwell.planes.gui; +package com.benburwell.planes.gui.navigationaids; +import com.benburwell.planes.data.CSVObjectStore; import com.benburwell.planes.data.NavigationAid; -import com.benburwell.planes.data.NavigationAidStore; import javax.swing.table.AbstractTableModel; @@ -10,15 +10,15 @@ import javax.swing.table.AbstractTableModel; */ public class NavigationAidsTableModel extends AbstractTableModel { private final String[] COLUMN_NAMES = { "Ident", "Type", "Frequency", "DME Frequency", "DME Channel", "Usage Type", "Power", "Airport" }; - private NavigationAidStore store; + private CSVObjectStore<NavigationAid> store; - public NavigationAidsTableModel(NavigationAidStore store) { + public NavigationAidsTableModel(CSVObjectStore<NavigationAid> store) { this.store = store; } @Override public int getRowCount() { - return this.store.getNavigationAids().size(); + return this.store.getObjects().size(); } @Override @@ -33,7 +33,7 @@ public class NavigationAidsTableModel extends AbstractTableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - NavigationAid aid = this.store.getNavigationAids().get(rowIndex); + NavigationAid aid = this.store.getObjects().get(rowIndex); switch (columnIndex) { case 0: return aid.getIdent(); |