From b632e590f94dbef1a4415100456528772f7014f8 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 17 Nov 2016 21:36:21 -0500 Subject: Create tabbed view --- src/com/benburwell/planes/data/AircraftStore.java | 34 ++++++++++++ .../planes/data/AircraftStoreListener.java | 9 ++++ .../planes/gui/AircraftTableComponent.java | 26 +++++++++ .../benburwell/planes/gui/AircraftTableModel.java | 18 ++++++- src/com/benburwell/planes/gui/Main1090.java | 61 +++++++--------------- src/com/benburwell/planes/gui/MenuBarProvider.java | 41 +++++++++++++++ src/com/benburwell/planes/gui/ViewComponent.java | 10 ++++ 7 files changed, 155 insertions(+), 44 deletions(-) create mode 100644 src/com/benburwell/planes/data/AircraftStore.java create mode 100644 src/com/benburwell/planes/data/AircraftStoreListener.java create mode 100644 src/com/benburwell/planes/gui/AircraftTableComponent.java create mode 100644 src/com/benburwell/planes/gui/MenuBarProvider.java create mode 100644 src/com/benburwell/planes/gui/ViewComponent.java diff --git a/src/com/benburwell/planes/data/AircraftStore.java b/src/com/benburwell/planes/data/AircraftStore.java new file mode 100644 index 0000000..076701d --- /dev/null +++ b/src/com/benburwell/planes/data/AircraftStore.java @@ -0,0 +1,34 @@ +package com.benburwell.planes.data; + +import com.benburwell.planes.sbs.SBSPacket; + +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.ArrayList; + +/** + * Created by ben on 11/17/16. + */ +public class AircraftStore { + private Map aircraftMap = new HashMap<>(); + private List aircraftListeners = new ArrayList<>(); + + public Map getAircraft() { + return this.aircraftMap; + } + + public void addPacket(SBSPacket packet) { + if (!this.aircraftMap.containsKey(packet.getHexIdent())) { + this.aircraftMap.put(packet.getHexIdent(), new Aircraft(packet.getHexIdent())); + } + this.aircraftMap.get(packet.getHexIdent()).handleUpdate(packet); + this.aircraftListeners.stream() + .filter(listener -> listener.respondTo(packet.getHexIdent())) + .forEach(AircraftStoreListener::aircraftStoreChanged); + } + + public void subscribe(AircraftStoreListener listener) { + this.aircraftListeners.add(listener); + } +} diff --git a/src/com/benburwell/planes/data/AircraftStoreListener.java b/src/com/benburwell/planes/data/AircraftStoreListener.java new file mode 100644 index 0000000..2ef635f --- /dev/null +++ b/src/com/benburwell/planes/data/AircraftStoreListener.java @@ -0,0 +1,9 @@ +package com.benburwell.planes.data; + +/** + * Created by ben on 11/17/16. + */ +public interface AircraftStoreListener { + void aircraftStoreChanged(); + boolean respondTo(String aircraftId); +} diff --git a/src/com/benburwell/planes/gui/AircraftTableComponent.java b/src/com/benburwell/planes/gui/AircraftTableComponent.java new file mode 100644 index 0000000..221bdda --- /dev/null +++ b/src/com/benburwell/planes/gui/AircraftTableComponent.java @@ -0,0 +1,26 @@ +package com.benburwell.planes.gui; + +import com.benburwell.planes.data.AircraftStore; + +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/com/benburwell/planes/gui/AircraftTableModel.java b/src/com/benburwell/planes/gui/AircraftTableModel.java index 72be372..a2b40de 100644 --- a/src/com/benburwell/planes/gui/AircraftTableModel.java +++ b/src/com/benburwell/planes/gui/AircraftTableModel.java @@ -1,6 +1,8 @@ package com.benburwell.planes.gui; 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; @@ -15,8 +17,20 @@ public class AircraftTableModel extends AbstractTableModel { private Map aircraftMap; private String[] columnNames = { "Hex", "Callsign", "Squawk", "Latitude", "Longitude", "Altitude", "Packets" }; - public AircraftTableModel(Map aircraftMap) { - this.aircraftMap = aircraftMap; + 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 diff --git a/src/com/benburwell/planes/gui/Main1090.java b/src/com/benburwell/planes/gui/Main1090.java index 02ef2bf..5b446c6 100644 --- a/src/com/benburwell/planes/gui/Main1090.java +++ b/src/com/benburwell/planes/gui/Main1090.java @@ -10,14 +10,12 @@ import com.benburwell.planes.data.*; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; -import java.util.Map; -import java.util.HashMap; public class Main1090 extends JFrame { private AggregateDataSource sbsDataSource = new AggregateDataSource(); - private Map aircraftMap = new HashMap<>(); + private AircraftStore aircraft = new AircraftStore(); private int currentTcpConnection = 0; - private AircraftTableModel aircraftTableModel = new AircraftTableModel(this.aircraftMap); + private JTabbedPane tabbedPane = new JTabbedPane(); public Main1090() { this.initUI(); @@ -26,69 +24,48 @@ public class Main1090 extends JFrame { private void initUI() { this.createMenuBar(); - this.createTable(); - this.setTitle("1090"); this.setSize(100, 100); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.openDataSource(); + + this.createTabs(); } - private void createTable() { - JTable table = new JTable(this.aircraftTableModel); - table.setFillsViewportHeight(true); - JScrollPane scrollPane = new JScrollPane(table); - this.add(scrollPane); + private void createTabs() { + AircraftTableComponent aircraftData = new AircraftTableComponent(this.aircraft); + this.tabbedPane.addTab("Aircraft Data", aircraftData.getComponent()); + + this.add(this.tabbedPane); + this.tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } private void createMenuBar() { - JMenuBar menubar = new JMenuBar(); - JMenu file = new JMenu("1090"); - JMenuItem eMenuItem = new JMenuItem("Quit"); - JMenu data = new JMenu("Data"); - JMenuItem dataConnectItem = new JMenuItem("Connect..."); - JMenuItem dataDisconnectItem = new JMenuItem("Disconnect"); - - eMenuItem.addActionListener((ActionEvent event) -> { - System.exit(0); - }); - file.add(eMenuItem); - menubar.add(file); + MenuBarProvider provider = new MenuBarProvider(); + this.setJMenuBar(provider.getMenuBar()); - dataConnectItem.addActionListener((ActionEvent event) -> { + provider.getDataConnectItem().addActionListener((ActionEvent event) -> { if (this.currentTcpConnection == 0) { this.currentTcpConnection = this.addTcpSource("10.0.0.111", 30003); - dataConnectItem.setEnabled(false); - dataDisconnectItem.setEnabled(true); + provider.getDataConnectItem().setEnabled(false); + provider.getDataDisconnectItem().setEnabled(true); } }); - dataDisconnectItem.addActionListener((ActionEvent event) -> { + provider.getDataDisconnectItem().addActionListener((ActionEvent event) -> { if (this.currentTcpConnection != 0) { this.sbsDataSource.closeSource(this.currentTcpConnection); - dataConnectItem.setEnabled(true); - dataDisconnectItem.setEnabled(false); + provider.getDataConnectItem().setEnabled(true); + provider.getDataDisconnectItem().setEnabled(false); this.currentTcpConnection = 0; } }); - dataDisconnectItem.setEnabled(false); - data.add(dataConnectItem); - data.add(dataDisconnectItem); - menubar.add(data); - - this.setJMenuBar(menubar); } private void openDataSource() { this.sbsDataSource.subscribe((SBSPacket packet) -> { - if (packet.getHexIdent() != null) { - if (!this.aircraftMap.containsKey(packet.getHexIdent())) { - this.aircraftMap.put(packet.getHexIdent(), new Aircraft(packet.getHexIdent())); - } - this.aircraftMap.get(packet.getHexIdent()).handleUpdate(packet); - this.aircraftTableModel.fireTableDataChanged(); - } + this.aircraft.addPacket(packet); }); this.sbsDataSource.open(); } diff --git a/src/com/benburwell/planes/gui/MenuBarProvider.java b/src/com/benburwell/planes/gui/MenuBarProvider.java new file mode 100644 index 0000000..add77ce --- /dev/null +++ b/src/com/benburwell/planes/gui/MenuBarProvider.java @@ -0,0 +1,41 @@ +package com.benburwell.planes.gui; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +/** + * Created by ben on 11/17/16. + */ +public class MenuBarProvider { + private JMenuBar menubar = new JMenuBar(); + private JMenu file = new JMenu("1090"); + private JMenuItem fileQuitMenuItem = new JMenuItem("Quit"); + private JMenu data = new JMenu("Data Source"); + private JMenuItem dataConnectItem = new JMenuItem("Connect to Remote..."); + private JMenuItem dataDisconnectItem = new JMenuItem("Disconnect"); + + public MenuBarProvider() { + fileQuitMenuItem.addActionListener((ActionEvent event) -> { + System.exit(0); + }); + file.add(fileQuitMenuItem); + menubar.add(file); + + dataDisconnectItem.setEnabled(false); + data.add(dataConnectItem); + data.add(dataDisconnectItem); + menubar.add(data); + } + + public JMenuBar getMenuBar() { + return this.menubar; + } + + public JMenuItem getDataConnectItem() { + return this.dataConnectItem; + } + + public JMenuItem getDataDisconnectItem() { + return this.dataDisconnectItem; + } +} diff --git a/src/com/benburwell/planes/gui/ViewComponent.java b/src/com/benburwell/planes/gui/ViewComponent.java new file mode 100644 index 0000000..91c21cd --- /dev/null +++ b/src/com/benburwell/planes/gui/ViewComponent.java @@ -0,0 +1,10 @@ +package com.benburwell.planes.gui; + +import javax.swing.*; + +/** + * Created by ben on 11/17/16. + */ +public interface ViewComponent { + JComponent getComponent(); +} -- cgit v1.2.3