DefaultTableColumnModel.java [plain text]
package javax.swing.table;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Vector;
import javax.swing.ListSelectionModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
public class DefaultTableColumnModel
implements TableColumnModel, PropertyChangeListener, ListSelectionListener,
Serializable
{
private static final long serialVersionUID = 6580012493508960512L;
protected Vector tableColumns;
protected ListSelectionModel selectionModel;
protected int columnMargin;
protected EventListenerList listenerList = new EventListenerList();
protected transient ChangeEvent changeEvent = new ChangeEvent(this);
protected boolean columnSelectionAllowed;
protected int totalColumnWidth;
public DefaultTableColumnModel()
{
tableColumns = new Vector();
setSelectionModel(new DefaultListSelectionModel());
columnMargin = 1;
columnSelectionAllowed = false;
}
public void addColumn(TableColumn col)
{
tableColumns.add(col);
invalidateWidthCache();
}
public void removeColumn(TableColumn col)
{
tableColumns.remove(col);
invalidateWidthCache();
}
public void moveColumn(int i, int j)
{
Object tmp = tableColumns.get(i);
tableColumns.set(i, tableColumns.get(j));
tableColumns.set(j, tmp);
}
public void setColumnMargin(int m)
{
columnMargin = m;
}
public int getColumnCount()
{
return tableColumns.size();
}
public Enumeration getColumns()
{
return tableColumns.elements();
}
public int getColumnIndex(Object obj)
{
return tableColumns.indexOf(obj, 0);
}
public TableColumn getColumn(int i)
{
return (TableColumn) tableColumns.get(i);
}
public int getColumnMargin()
{
return columnMargin;
}
public int getColumnIndexAtX(int x)
{
for (int i = 0; i < tableColumns.size(); ++i)
{
int w = ((TableColumn)tableColumns.get(i)).getWidth();
if (0 <= x && x < w)
return i;
else
x -= w;
}
return -1;
}
public int getTotalColumnWidth()
{
if (totalColumnWidth == -1)
recalcWidthCache();
return totalColumnWidth;
}
public void setSelectionModel(ListSelectionModel model)
{
if (model == null)
throw new IllegalArgumentException();
selectionModel = model;
selectionModel.addListSelectionListener(this);
}
public ListSelectionModel getSelectionModel()
{
return selectionModel;
}
public void setColumnSelectionAllowed(boolean a)
{
columnSelectionAllowed = a;
}
public boolean getColumnSelectionAllowed()
{
return columnSelectionAllowed;
}
public int[] getSelectedColumns()
{
return null; }
public int getSelectedColumnCount()
{
return 0; }
public void addColumnModelListener(TableColumnModelListener listener)
{
listenerList.add(TableColumnModelListener.class, listener);
}
public void removeColumnModelListener(TableColumnModelListener value0)
{
}
protected void fireColumnAdded(TableColumnModelEvent value0)
{
}
protected void fireColumnRemoved(TableColumnModelEvent value0)
{
}
protected void fireColumnMoved(TableColumnModelEvent value0)
{
}
protected void fireColumnSelectionChanged(ListSelectionEvent evt)
{
EventListener [] listeners = getListeners(TableColumnModelListener.class);
for (int i = 0; i < listeners.length; ++i)
((TableColumnModelListener)listeners[i]).columnSelectionChanged(evt);
}
protected void fireColumnMarginChanged()
{
}
public EventListener[] getListeners(Class klass)
{
return listenerList.getListeners(klass);
}
public void propertyChange(PropertyChangeEvent value0)
{
}
public void valueChanged(ListSelectionEvent value0)
{
fireColumnSelectionChanged(value0);
}
protected ListSelectionModel createSelectionModel()
{
return null; }
protected void recalcWidthCache()
{
if (totalColumnWidth == -1)
{
totalColumnWidth = 0;
for (int i = 0; i < tableColumns.size(); ++i)
{
totalColumnWidth += ((TableColumn)tableColumns.get(i)).getWidth();
}
}
}
private void invalidateWidthCache()
{
totalColumnWidth = -1;
}
}