/*
 * Created on 30-03-2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package net.sf.hibern8ide;

import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * @author max
 *
 */
public class BeanTableModel extends AbstractTableModel {

	List data = Collections.EMPTY_LIST;
	List columnNames = Collections.EMPTY_LIST;

	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int column) {
		return columnNames.get(column).toString();
	}

	public void setData(List data) {
		this.data = data;
		fireTableDataChanged();
	}

	public int getRowCount() {
		if (data != null) {
			return data.size();
		} else {
			return 0;
		}
	}

	public int getColumnCount() {
		return columnNames.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Object o = data.get(rowIndex);
		if (o == null) {
			return "<null>";
		}
		if (o.getClass().isArray()) {
			Object[] arr = (Object[]) o;
			if (columnIndex > arr.length - 1) {
				return "<Unknown value>";
			}
			return arr[columnIndex];
		} else {
			return o;
		}

	}

	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	public Class getColumnClass(int columnIndex) {
		Class c = super.getColumnClass(columnIndex);

		if (c == Object.class) {
			return String.class;
		} else {
			return c;
		}
	}

	/**
	 * @param list
	 */
	public void setColumns(List list) {
		columnNames = list;
		fireTableStructureChanged();

	}

}
