diff options
Diffstat (limited to 'src/main/ui/gui/widgets/LogTableModel.java')
-rw-r--r-- | src/main/ui/gui/widgets/LogTableModel.java | 79 |
1 files changed, 79 insertions, 0 deletions
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<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(); + } + } +} |