aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/widgets/CertTableModel.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/ui/widgets/CertTableModel.java')
-rw-r--r--src/main/ui/widgets/CertTableModel.java129
1 files changed, 129 insertions, 0 deletions
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<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;
+ }
+ }
+}