/*
   *  Adjust the widths of all the columns in the table
   */
  public void adjustColumns() {
    TableColumnModel tcm = table.getColumnModel();

    for (int i = 0; i < tcm.getColumnCount(); i++) {
      adjustColumn(i);
    }
  }
  //
  //  Implement the TableModelListener
  //
  public void tableChanged(TableModelEvent e) {
    if (!isColumnDataIncluded) return;

    //  A cell has been updated

    if (e.getType() == TableModelEvent.UPDATE) {
      int column = table.convertColumnIndexToView(e.getColumn());

      //  Only need to worry about an increase in width for this cell

      if (isOnlyAdjustLarger) {
        int row = e.getFirstRow();
        TableColumn tableColumn = table.getColumnModel().getColumn(column);

        if (tableColumn.getResizable()) {
          int width = getCellDataWidth(row, column);
          updateTableColumn(column, width);
        }
      }

      //	Could be an increase of decrease so check all rows

      else {
        adjustColumn(column);
      }
    }

    //  The update affected more than one column so adjust all columns

    else {
      adjustColumns();
    }
  }
  /*
   *  Restore the width of the specified column to its previous width
   */
  private void restoreColumn(int column) {
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    Integer width = columnSizes.get(tableColumn);

    if (width != null) {
      table.getTableHeader().setResizingColumn(tableColumn);
      tableColumn.setWidth(width.intValue());
    }
  }
  /*
   *  Adjust the width of the specified column in the table
   */
  public void adjustColumn(final int column) {
    TableColumn tableColumn = table.getColumnModel().getColumn(column);

    if (!tableColumn.getResizable()) return;

    int columnHeaderWidth = getColumnHeaderWidth(column);
    int columnDataWidth = getColumnDataWidth(column);
    int preferredWidth = Math.max(columnHeaderWidth, columnDataWidth);

    updateTableColumn(column, preferredWidth);
  }
  /*
   *  Calculated the width based on the column name
   */
  private int getColumnHeaderWidth(int column) {
    if (!isColumnHeaderIncluded) return 0;

    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    Object value = tableColumn.getHeaderValue();
    TableCellRenderer renderer = tableColumn.getHeaderRenderer();

    if (renderer == null) {
      renderer = table.getTableHeader().getDefaultRenderer();
    }

    Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column);
    return c.getPreferredSize().width;
  }
  /*
   *  Calculate the width based on the widest cell renderer for the
   *  given column.
   */
  private int getColumnDataWidth(int column) {
    if (!isColumnDataIncluded) return 0;

    int preferredWidth = 0;
    int maxWidth = table.getColumnModel().getColumn(column).getMaxWidth();

    for (int row = 0; row < table.getRowCount(); row++) {
      preferredWidth = Math.max(preferredWidth, getCellDataWidth(row, column));

      //  We've exceeded the maximum width, no need to check other rows

      if (preferredWidth >= maxWidth) break;
    }

    return preferredWidth;
  }
  /*
   *  Update the TableColumn with the newly calculated width
   */
  private void updateTableColumn(int column, int width) {
    final TableColumn tableColumn = table.getColumnModel().getColumn(column);

    if (!tableColumn.getResizable()) return;

    width += spacing;

    //  Don't shrink the column width

    if (isOnlyAdjustLarger) {
      width = Math.max(width, tableColumn.getPreferredWidth());
    }

    columnSizes.put(tableColumn, new Integer(tableColumn.getWidth()));
    table.getTableHeader().setResizingColumn(tableColumn);
    tableColumn.setWidth(width);
  }