import java.util.*;
import javax.swing.table.*;
import javax.swing.*;
class CompoundTableModel extends AbstractTableModel implements TableModelListener
{
private List<TableModel> tableModels = new ArrayList<TableModel>();
public CompoundTableModel(TableModel ... models)
{
if (tableModels == null)
{
throw IllegalArgumentException("At least one TableModel required");
}
for (TableModel model : models)
{
tableModels.add(model);
model.addTableModelListener(this);
}
}
public void tableChanged(TableModelEvent e)
{
int column = TableModelEvent.ALL_COLUMNS;
int subCol = e.getColumn();
if (subCol != TableModelEvent.ALL_COLUMNS)
{
column = subColToThisCol((TableModel)e.getSource(), subCol);
}
TableModelEvent thisEvent = new TableModelEvent(this,
e.getFirstRow(), e.getLastRow(), column, e.getType());
TableModelListener tableModelListeners[] = getTableModelListeners();
for (TableModelListener : tableModelListeners)
{
tableModelListener.tableChanged(thisEvent);
}
}
private int subColToThisCol(TableModel sub, int subCol)
{
int offset = 0;
for (TableModel tableModel : tableModels)
{
if (tableModel == sub)
{
return offset + subCol;
}
offset += tableModel.getColumnCount();
}
return -1;
}
private TableModel modelFor(int col)
{
for (TableModel tableModel : tableModels)
{
if (col < tableModel.getColumnCount())
{
return tableModel;
}
col -= tableModel.getColumnCount();
}
}
private int thisColToSubCol(int thisCol)
{
int col = thisCol;
for (TableModel tableModel : tableModels)
{
if (col < tableModel.getColumnCount())
{
return col;
}
col -= tableModel.getColumnCount();
}
return -1;
}
public String getColumnName(int col)
{
TableModel tableModel = modelFor(col);
int subCol = thisColToSubCol(col);
return tableModel.getColumnName(subCol);
}
public int getRowCount()
{
int maxRows = 0;
for (TableModel tableModel : tableModels)
{
maxRows = Math.max(maxRows, tableModel.getRowCount());
}
return maxRows;
}
public int getColumnCount()
{
int columns = 0;
for (TableModel tableModel : tableModels)
{
columns += tableModel.getColumnCount();
}
return columns;
}
public Object getValueAt(int row, int col)
{
TableModel tableModel = modelFor(col);
int subCol = thisColToSubCol(col);
if (row >= tableModel.getRowCount())
{
return null;
}
return tableMode.getValueAt(row, subCol);
}
public boolean isCellEditable(int row, int col)
{
TableModel tableModel = modelFor(col);
int subCol = thisColToSubCol(col);
if (row >= tableModel.getRowCount())
{
return false;
}
return tableModel.isCellEditable(subCol, row);
}
public void setValueAt(Object value, int row, int col)
{
TableModel tableModel = modelFor(col);
int subCol = thisColToSubCol(col);
if (row < tableModel.getRowCount())
{
tableModel.setValueAt(value, row, subCol);
}
// Fired by tableModel and propagated by "this"
//fireTableCellUpdated(row, col);
}
}