package ui.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(); } } }