import javax.swing.table.*;

/**
 * A TableModel in which each cell is editable and can be an
 * object of any class.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class GenericTableModel extends DefaultTableModel 
{
  private static final long serialVersionUID = 1L;

  private boolean[]   editable;

  /**
   * Explicit Value Constructor.
   *
   * @param data        The data in the table
   * @param columnNames The names of the columns
   */
  public GenericTableModel(Object[][] data, Object[] columnNames)
  {
    super(data, columnNames);
    editable = new boolean[columnNames.length];
    for (int i=0; i<editable.length; i++) editable[i] = true;
  }

  /*
   * Get the class associated with a particular column.
   *
   * The JTable uses this method to determine the default 
   * renderer/editor for each cell. 
   *
   * @param column   The column index
   * @return         The class associated with the given column
   */
  public Class<?> getColumnClass(int column) 
  {
    return getValueAt(0, column).getClass();
  }

  /**
   * Is a particular cell editable?
   *
   * @param row    The row number of the cell
   * @param column The column number of the cell
   * @return       true if editable and false otherwise
   */
  public boolean isCellEditable(int row, int column) 
  {
    if ((column >= 0) && (column < editable.length)) {

      return editable[column];
    }
    return false;
  }


  /**
   * Make a column editable or not
   *
   * @param column   The column index
   * @param editable true to make it editable
   */
  public void setColumnEditable(int column, boolean editable)
  {
    if ((column >= 0) && (column < this.editable.length)) {

      this.editable[column] = editable;
    }
  }
}
