summaryrefslogtreecommitdiff
path: root/src/main/java/com/benburwell/planes/gui/Main1090.java
blob: fb2bdc2c00309058cde3d02f95dad5f054669193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * @author ben
 */

package com.benburwell.planes.gui;

import com.benburwell.planes.data.*;
import com.benburwell.planes.graph.RouteGraph;
import com.benburwell.planes.gui.aircraftmap.AircraftMapComponent;
import com.benburwell.planes.gui.aircrafttable.AircraftTableComponent;
import com.benburwell.planes.gui.airportstable.AirportsComponent;
import com.benburwell.planes.gui.navigationaids.NavigationAidComponent;
import com.benburwell.planes.sbs.AggregateDataSource;
import com.benburwell.planes.sbs.SBSPacket;
import com.benburwell.planes.sbs.TCPDataSource;

import java.awt.EventQueue;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class Main1090 extends JFrame {
    private AggregateDataSource sbsDataSource = new AggregateDataSource();
    private AircraftStore aircraft = new AircraftStore();
    private CSVObjectStore<NavigationAid> navaids = new CSVObjectStore<>();
    private CSVObjectStore<Airport> airports = new CSVObjectStore<>();
    private CSVObjectStore<Runway> runways = new CSVObjectStore<>();
    private RouteGraph routeGraph = new RouteGraph();
    private int currentTcpConnection = 0;
    private JTabbedPane tabbedPane = new JTabbedPane();

    public Main1090() {
        this.initUI();
    }

    private void initUI() {
        this.createMenuBar();

        this.setTitle("1090");
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.openDataSource();

        try {
            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.readFromResource("/airports.csv", Airport.class);
        } catch (IOException | InstantiationException | IllegalAccessException e) {
            System.out.println("Could not read airport file: " + e.getMessage());
        }

        try {
            this.runways.readFromResource("/runways.csv", Runway.class);
        } catch (IOException | InstantiationException | IllegalAccessException e) {
            System.out.println("Could not read runway file: " + e.getMessage());
        }

        try {
            CSVObjectStore<Intersection> intersections = new CSVObjectStore<>();
            intersections.readFromResource("/airways_db.csv", Intersection.class);
            intersections.getObjects().forEach(intersection -> this.routeGraph.addIntersection(intersection));
        } catch (IOException | InstantiationException | IllegalAccessException e) {
            System.out.println("Could not read route intersection file: " + e.getMessage());
        }

        this.createTabs();
    }

    private void createTabs() {
        List<Tabbable> tabs = new ArrayList<>();
        tabs.add(new AircraftMapComponent(this.aircraft, this.navaids, this.airports, this.routeGraph, this.runways));
        tabs.add(new AircraftTableComponent(this.aircraft));
        tabs.add(new NavigationAidComponent(this.navaids));
        tabs.add(new AirportsComponent(this.airports));
        tabs.forEach(tab -> this.tabbedPane.addTab(tab.getName(), tab.getComponent()));
        this.add(this.tabbedPane);
        this.tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    private void createMenuBar() {
        MenuBarProvider provider = new MenuBarProvider();
        this.setJMenuBar(provider.getMenuBar());

        provider.getDataConnectItem().addActionListener((ActionEvent event) -> {
            if (this.currentTcpConnection == 0) {
                TCPConnectionOptionDialog dialog = new TCPConnectionOptionDialog();
                JOptionPane.showMessageDialog(this, dialog.getComponent(), "New Network Data Source", JOptionPane.PLAIN_MESSAGE);
                this.currentTcpConnection = this.addTcpSource(dialog.getHost(), dialog.getPort());
                provider.getDataConnectItem().setEnabled(false);
                provider.getDataDisconnectItem().setEnabled(true);
            }
        });
        provider.getDataDisconnectItem().addActionListener((ActionEvent event) -> {
            if (this.currentTcpConnection != 0) {
                this.sbsDataSource.closeSource(this.currentTcpConnection);
                provider.getDataConnectItem().setEnabled(true);
                provider.getDataDisconnectItem().setEnabled(false);
                this.currentTcpConnection = 0;
            }
        });
    }

    private void openDataSource() {
        this.sbsDataSource.subscribe((SBSPacket packet) -> {
            this.aircraft.addPacket(packet);
        });
        this.sbsDataSource.open();
    }

    private int addTcpSource(String host, int port) {
        TCPDataSource source = new TCPDataSource(host, port);
        source.open();
        return this.sbsDataSource.addSource(source);
    }

    public static void main(String[] args) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        EventQueue.invokeLater(() -> {
            Main1090 app = new Main1090();
            app.setVisible(true);
        });
    }
}