aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/gui/widgets/LogTableModel.java
blob: a7b52acbe847ac1cdb1ece528f49759f9fc57d44 (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 ui.gui.widgets;

import model.ca.AuditLogEntry;
import model.ca.CertificationAuthority;

import javax.swing.table.AbstractTableModel;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Table model that displays audit logs.
 */
public class LogTableModel extends AbstractTableModel {
    /**
     * Columns
     */
    private static final String[] COLS = new String[] {
            "Time",
            "Operator",
            "Action"
    };

    /**
     * Pointer to the {@link CertificationAuthority#getLogs()}.
     */
    private List<AuditLogEntry> ptrData;

    /**
     * EFFECTS: Set the pointer to templates
     * MODIFIES: this
     */
    public void setPtrData(List<AuditLogEntry> ptrData) {
        this.ptrData = ptrData;
    }

    /**
     * EFFECT: Return number of rows.
     */
    @Override
    public int getRowCount() {
        return ptrData == null ? 0 : ptrData.size();
    }

    /**
     * EFFECT: Return number of columns.
     */
    @Override
    public int getColumnCount() {
        return COLS.length;
    }

    /**
     * EFFECTS: Get column name.
     * REQUIRES: column in [9, getColumnCount())
     */
    @Override
    public String getColumnName(int column) {
        return COLS[column];
    }

    /**
     * EFFECTS: Return the value for a cell:
     *          String (Time)
     *          String (Operator)
     *          String (Action)
     *          Throws {@link IllegalArgumentException} if columnIndex is not in 0 ~ 2
     * REQUIRES: rowIndex must in range.
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        final AuditLogEntry e = ptrData.get(rowIndex);
        switch (columnIndex) {
            case 0: return e.getTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            case 1: return e.getUser();
            case 2: return e.getAction();
            default: throw new IllegalArgumentException();
        }
    }
}