import javax.swing.table.AbstractTableModel;

/**
 * This model class holds the table data for a simple mailbox,
 * and also contains a set of UIDs for uniquely identifying messages.
 *
 * @author      Simon Pollard
 */

public class MailModel extends AbstractTableModel {
  private String columns[] = {"From", "Subject"};
  private String fromTexts[], subjectTexts[], UIDs[];

  private int numColumns = columns.length;
  private int numRows = 0;

  /**
   * Initialises the arrays holding the message data to the given size
   *
   * @param     numMessages the number of messages to hold in the table
   */
  public MailModel(int numMessages) {
    fromTexts = new String[numMessages];
    subjectTexts = new String[numMessages];
    UIDs = new String[numMessages];

    numRows = numMessages;
  }

  /**
   * Returns the number of columns in the table.  This will
   * normally be 2 - the 'From' column and the 'Subject' column.
   *
   * @return    the number of table columns
   * @see       javax.swing.table.TableModel#getColumnCount()
   */
  public int getColumnCount() { return numColumns; }

  /**
   * Returns the number of rows (ie, messages) in the table
   *
   * @return    the number of table rows
   * @see       javax.swing.table.TableModel#getRowCount()
   */
  public int getRowCount() { return numRows; }

  /**
   * Returns the name of a given column
   *
   * @param     columnIndex the index of the column
   * @return    the name of the column
   * @see       javax.swing.table.TableModel#getColumnName(int)
   */
  public String getColumnName (int columnIndex) {
    return columns[columnIndex];
  }

  /**
   * Returns the value for the cell at (row, column)
   *
   * @param     row the row (message) being queried
   * @param     column the column (0=From, 1=Subject) being queried
   * @return    the (String) value of the given cell
   * @see       javax.swing.table.TableModel#getValueAt(int, int)
   */
  public Object getValueAt(int row, int column) {
    switch(column) {
      case 0: return fromTexts[row];
      case 1: return subjectTexts[row];
    }
    return null;
  }

  /**
   * Sets the value of the cell at (row, column)
   *
   * @param     aValue the String value to set
   * @param     row the row (message) being set
   * @param     column the column (0=From, 1=Subject) being set
   * @see       javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
   */
  public void setValueAt(Object aValue, int row, int column) {
    switch(column) {
      case 0: fromTexts[row] = (String) aValue; break;
      case 1: subjectTexts[row] = (String) aValue; break;
    }
  }

  /**
   * Returns whether the cell at (row, column) is editable - always false
   *
   * @param     row the row (message) being queried
   * @param     column the column (0=From, 1=Subject) being queried
   * @return    whether the cell is editable
   * @see       javax.swing.table.TableModel#isCellEditable(int, int)
   */
  public boolean isCellEditable(int row, int column)
    { return false; }

  /**
   * Sets the UID field for the given row (message)
   *
   * @param     aValue the UID to set the row to
   * @param     row the row being set
   */
  public void setUID(String aValue, int row) { UIDs[row] = aValue; }

  /**
   * Queries the UID field for the given row (message)
   *
   * @param     row the row being queried
   */
  public String getUID(int row) { return UIDs[row]; }
}
