From 65ea6c17a0c1348aa9ef4e158102ddf173936882 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 23 Nov 2023 08:09:01 +0800 Subject: Add GUI Signed-off-by: Yuuta Liang --- src/main/ui/widgets/CertTableModel.java | 129 ++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/main/ui/widgets/CertTableModel.java (limited to 'src/main/ui/widgets/CertTableModel.java') diff --git a/src/main/ui/widgets/CertTableModel.java b/src/main/ui/widgets/CertTableModel.java new file mode 100644 index 0000000..0259e1b --- /dev/null +++ b/src/main/ui/widgets/CertTableModel.java @@ -0,0 +1,129 @@ +package ui.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 ptrData; + + /** + * Pointer to {@link CertificationAuthority#getRevoked()} + */ + private List ptrRevokedData; + + /** + * EFFECTS: Set pointer to certs + * MODIFIES: this + */ + public void setPtrData(List ptrData) { + this.ptrData = ptrData; + } + + /** + * EFFECTS: Set pointer to revoked + * MODIFIES: this + */ + public void setPtrRevokedData(List 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; + } + } +} -- cgit v1.2.3