From 1073af21305360bd33903c533cdac57e9f936294 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Tue, 28 Nov 2023 18:19:39 -0800 Subject: Move TUI and GUI into separate packages Signed-off-by: Yuuta Liang --- src/main/ui/gui/widgets/LogTableModel.java | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/ui/gui/widgets/LogTableModel.java (limited to 'src/main/ui/gui/widgets/LogTableModel.java') diff --git a/src/main/ui/gui/widgets/LogTableModel.java b/src/main/ui/gui/widgets/LogTableModel.java new file mode 100644 index 0000000..a7b52ac --- /dev/null +++ b/src/main/ui/gui/widgets/LogTableModel.java @@ -0,0 +1,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 ptrData; + + /** + * EFFECTS: Set the pointer to templates + * MODIFIES: this + */ + public void setPtrData(List 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(); + } + } +} -- cgit v1.2.3