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

import model.ca.CertificationAuthority;
import model.pki.cert.Certificate;
import model.pki.crl.RevokedCertificate;

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

/**
 * Table model that displays issued certificates.
 */
public class CertTableModel extends AbstractTableModel {
    /**
     * Valid certificate icon.
     */
    private static final ImageIcon ICON_OK =
            new ImageIcon(CertTableModel.class.getResource("/verified.png"));

    /**
     * Revoked certificate icon (same as toolbar revoke icon).
     */
    private static final ImageIcon ICON_REVOKED =
            new ImageIcon(CertTableModel.class.getResource("/deletetest.png"));

    /**
     * Columns
     */
    private static final String[] COLS = new String[] {
            "", // Icon
            "Serial",
            "Subject",
            "Signed",
            "Expires"
    };

    /**
     * Pointer to {@link CertificationAuthority#getSigned()}
     */
    private List<Certificate> ptrData;

    /**
     * Pointer to {@link CertificationAuthority#getRevoked()}
     */
    private List<RevokedCertificate> ptrRevokedData;

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

    /**
     * EFFECTS: Set pointer to revoked
     * MODIFIES: this
     */
    public void setPtrRevokedData(List<RevokedCertificate> ptrRevokedData) {
        this.ptrRevokedData = ptrRevokedData;
    }

    /**
     * EFFECTS: Count rows.
     */
    @Override
    public int getRowCount() {
        return ptrData == null ? 0 : ptrData.size();
    }

    /**
     * EFFECTS: Count 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:
     *          ImageIcon (Valid / Revoked)
     *          String (Serial number)
     *          String (Subject)
     *          String (NotBefore)
     *          String (NotAfter)
     *          Throws {@link IllegalArgumentException} if columnIndex is not in 0 ~ 4
     * REQUIRES: rowIndex must in range.
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        final Certificate e = ptrData.get(rowIndex);
        switch (columnIndex) {
            case 0: return ptrRevokedData.stream().anyMatch(r ->
                    r.getSerialNumber().getLong() == e.getCertificate().getSerialNumber().getLong()
            ) ? ICON_REVOKED : ICON_OK;
            case 1: return e.getCertificate().getSerialNumber().getLong();
            case 2: return e.getCertificate().getSubject().toString();
            case 3:
                return e.getCertificate().getValidity().getNotBefore().getTimestamp()
                        .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            case 4:
                return e.getCertificate().getValidity().getNotAfter().getTimestamp()
                        .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            default: throw new IllegalArgumentException();
        }
    }

    /**
     * EFFECTS: Return ImageIcon for 0, String otherwise.
     */
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0: return ImageIcon.class;
            default: return String.class;
        }
    }
}