summaryrefslogtreecommitdiff
path: root/src/com/benburwell/planes/gui/AircraftTableModel.java
blob: a2b40dece23a90e80ce79c26f039ff63ed48ca22 (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
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;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by ben on 11/15/16.
 */
public class AircraftTableModel extends AbstractTableModel {
    private Map<String,Aircraft> aircraftMap;
    private String[] columnNames = { "Hex", "Callsign", "Squawk", "Latitude", "Longitude", "Altitude", "Packets" };

    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
    public int getRowCount() {
        return this.aircraftMap.keySet().size();
    }

    @Override
    public int getColumnCount() {
        return this.columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return this.columnNames[col];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<Aircraft> aircraftList = this.getAircraftList();
        Aircraft aircraft = aircraftList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return aircraft.getHexIdent();
            case 1:
                return aircraft.getCallsign();
            case 2:
                return aircraft.getSquawk();
            case 3:
                return aircraft.getCurrentPosition().getLatitude();
            case 4:
                return aircraft.getCurrentPosition().getLongitude();
            case 5:
                return aircraft.getCurrentPosition().getAltitude();
            case 6:
                return aircraft.getPacketCount();
        }
        return "";
    }

    private List<Aircraft> getAircraftList() {
        List<Aircraft> aircraftList = new ArrayList<>(this.aircraftMap.values());
        Collections.sort(aircraftList);
        return aircraftList;
    }
}