edu.northwestern.at.utils.swing
Class XTableColumnModel

java.lang.Object
  extended by javax.swing.table.DefaultTableColumnModel
      extended by edu.northwestern.at.utils.swing.XTableColumnModel
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.io.Serializable, java.util.EventListener, javax.swing.event.ListSelectionListener, javax.swing.table.TableColumnModel

public class XTableColumnModel
extends javax.swing.table.DefaultTableColumnModel

XTableColumnModel extends DefaultTableColumnModel with methods to hide and unhide columns.

Columns retain their relative positions when hidden and unhidden.

Hiding a column fires a columnRemoved event and unhiding a column fires a columnAdded event, and possibly a columnMoved event as well.

The following methods still deal with visible columns only: getColumnCount(), getColumns(), getColumnIndex(), getColumn() . Use the overloaded versions of these methods that take a parameter onlyVisible if you wish to account for hidden columns as well.

See Also:
Serialized Form

Field Summary
protected  java.util.Vector allTableColumns
          Holds all the column objects, regardless of visibility.
 
Fields inherited from class javax.swing.table.DefaultTableColumnModel
changeEvent, columnMargin, columnSelectionAllowed, listenerList, selectionModel, tableColumns, totalColumnWidth
 
Constructor Summary
XTableColumnModel()
          Create an extended table column model.
 
Method Summary
 void addColumn(javax.swing.table.TableColumn column)
          Append column to the right of exisiting columns.
 javax.swing.table.TableColumn getColumn(int columnIndex, boolean onlyVisible)
          Return the TableColumn object for the column at columnIndex.
 javax.swing.table.TableColumn getColumnByModelIndex(int modelColumnIndex)
          Maps the index of the column in the table model at modelColumnIndex to the TableColumn object.
 int getColumnCount(boolean onlyVisible)
          Returns the total number of columns in this model.
 int getColumnIndex(java.lang.Object identifier, boolean onlyVisible)
          Returns the position of first column matching specified identifier.
 java.util.Enumeration getColumns(boolean onlyVisible)
          Returns an enumeration of all the columns in the model.
 boolean isColumnVisible(int columnIndex)
          Checks whether a specified column is currently visible.
 boolean isColumnVisible(javax.swing.table.TableColumn column)
          Checks whether a specified column is currently visible.
 void moveColumn(int oldIndex, int newIndex)
          Moves column.
 void removeColumn(javax.swing.table.TableColumn column)
          Removes column from the column model.
 void setAllColumnsVisible()
          Make all columns in this model visible.
 void setColumnVisible(int columnIndex, boolean isVisible)
          Sets the visibility of the specified table column by index.
 void setColumnVisible(javax.swing.table.TableColumn column, boolean isVisible)
          Sets the visibility of the specified table column.
 
Methods inherited from class javax.swing.table.DefaultTableColumnModel
addColumnModelListener, createSelectionModel, fireColumnAdded, fireColumnMarginChanged, fireColumnMoved, fireColumnRemoved, fireColumnSelectionChanged, getColumn, getColumnCount, getColumnIndex, getColumnIndexAtX, getColumnMargin, getColumnModelListeners, getColumns, getColumnSelectionAllowed, getListeners, getSelectedColumnCount, getSelectedColumns, getSelectionModel, getTotalColumnWidth, propertyChange, recalcWidthCache, removeColumnModelListener, setColumnMargin, setColumnSelectionAllowed, setSelectionModel, valueChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

allTableColumns

protected java.util.Vector allTableColumns
Holds all the column objects, regardless of visibility.

Constructor Detail

XTableColumnModel

public XTableColumnModel()
Create an extended table column model.

Method Detail

setColumnVisible

public void setColumnVisible(javax.swing.table.TableColumn column,
                             boolean isVisible)
Sets the visibility of the specified table column.

Parameters:
column - The column to hide/unhide.
isVisible - True to unhide the column, false to hide.

setColumnVisible

public void setColumnVisible(int columnIndex,
                             boolean isVisible)
Sets the visibility of the specified table column by index.

Parameters:
columnIndex - The inde of the column to hide/unhide.
isVisible - True to unhide the column, false to hide.

setAllColumnsVisible

public void setAllColumnsVisible()
Make all columns in this model visible.


getColumnByModelIndex

public javax.swing.table.TableColumn getColumnByModelIndex(int modelColumnIndex)
Maps the index of the column in the table model at modelColumnIndex to the TableColumn object.

Parameters:
modelColumnIndex - Index of column in table model.
Returns:
Table column object or null if no such column exists in this column model.

There may be multiple TableColumn objects showing the same model column, though this is uncommon.

This method will always return the first visible or else the first invisible column with the specified index.


isColumnVisible

public boolean isColumnVisible(javax.swing.table.TableColumn column)
Checks whether a specified column is currently visible.

Parameters:
column - Column to check.
Returns:
True if specified column is visible, false otherwise.

isColumnVisible

public boolean isColumnVisible(int columnIndex)
Checks whether a specified column is currently visible.

Parameters:
columnIndex - Index of column to check.
Returns:
True if specified column is visible, false otherwise.

addColumn

public void addColumn(javax.swing.table.TableColumn column)
Append column to the right of exisiting columns.

Specified by:
addColumn in interface javax.swing.table.TableColumnModel
Overrides:
addColumn in class javax.swing.table.DefaultTableColumnModel
Parameters:
column - The column to add.
Throws:
java.lang.IllegalArgumentException - if column is null.

removeColumn

public void removeColumn(javax.swing.table.TableColumn column)
Removes column from the column model.

Specified by:
removeColumn in interface javax.swing.table.TableColumnModel
Overrides:
removeColumn in class javax.swing.table.DefaultTableColumnModel
Parameters:
column - Column to be removed.

moveColumn

public void moveColumn(int oldIndex,
                       int newIndex)
Moves column.

Specified by:
moveColumn in interface javax.swing.table.TableColumnModel
Overrides:
moveColumn in class javax.swing.table.DefaultTableColumnModel
Parameters:
oldIndex - Index of column to move.
newIndex - New index for column.
Throws:
java.lang.IllegalArgumentException - if either oldIndex or newIndex are not in [0, getColumnCount() - 1].

getColumnCount

public int getColumnCount(boolean onlyVisible)
Returns the total number of columns in this model.

Parameters:
onlyVisible - If true, only visible columns are counted.
Returns:
Number of columns.

getColumns

public java.util.Enumeration getColumns(boolean onlyVisible)
Returns an enumeration of all the columns in the model.

Parameters:
onlyVisible - If true, all invisible columns will be excluded from the enumeration.
Returns:
An enumeration of the columns in the model.

getColumnIndex

public int getColumnIndex(java.lang.Object identifier,
                          boolean onlyVisible)
Returns the position of first column matching specified identifier.

Parameters:
identifier - Identifier object for which to search.
onlyVisible - True to search only visible columns.
Returns:
Index of first column matching specified identifier.
Throws:
java.lang.IllegalArgumentException - if identifier is null or no column matches.

getColumn

public javax.swing.table.TableColumn getColumn(int columnIndex,
                                               boolean onlyVisible)
Return the TableColumn object for the column at columnIndex.

Parameters:
columnIndex - Index of the desired column.
onlyVisible - If true, columnIndex is meant to be relative to all visible columns only. If false, it is the index in all columns.
Returns:
TableColumn object for the column at columnIndex.