summaryrefslogtreecommitdiff
path: root/src/com/benburwell/planes/gui/Main1090.java
blob: d7fc830902db91303e10c0502775d31a13d5a200 (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
/**
 * Created by ben on 11/15/16.
 */

package com.benburwell.planes.gui;

import com.benburwell.planes.sbs.*;
import com.benburwell.planes.data.*;
import com.benburwell.planes.gui.aircraftmap.*;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;

public class Main1090 extends JFrame {
    private AggregateDataSource sbsDataSource = new AggregateDataSource();
    private AircraftStore aircraft = new AircraftStore();
    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();

        this.createTabs();
    }

    private void createTabs() {
        AircraftTableComponent aircraftData = new AircraftTableComponent(this.aircraft);
        this.tabbedPane.addTab("Aircraft Data", aircraftData.getComponent());

        AircraftMapComponent aircraftMap = new AircraftMapComponent(this.aircraft);
        this.tabbedPane.addTab("Live Map", aircraftMap.getComponent());

        this.add(this.tabbedPane);
        this.tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        this.tabbedPane.setSelectedIndex(1);
    }

    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) {
        EventQueue.invokeLater(() -> {
            Main1090 app = new Main1090();
            app.setVisible(true);
        });
    }
}