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

package com.benburwell.planes.gui;

import com.benburwell.planes.sbs.*;
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<String,Aircraft> aircraftMap = new HashMap<>();
    private int currentTcpConnection = 0;
    private AircraftTableModel aircraftTableModel = new AircraftTableModel(this.aircraftMap);

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

    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();
    }

    private void createTable() {
        JTable table = new JTable(this.aircraftTableModel);
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }

    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);

        dataConnectItem.addActionListener((ActionEvent event) -> {
            if (this.currentTcpConnection == 0) {
                this.currentTcpConnection = this.addTcpSource("10.0.0.111", 30003);
                dataConnectItem.setEnabled(false);
                dataDisconnectItem.setEnabled(true);
            }
        });
        dataDisconnectItem.addActionListener((ActionEvent event) -> {
            if (this.currentTcpConnection != 0) {
                this.sbsDataSource.closeSource(this.currentTcpConnection);
                dataConnectItem.setEnabled(true);
                dataDisconnectItem.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.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);
        });
    }
}