FileDocCategorySizeDatePackage
SortableTableModel.javaAPI DocExample6577Mon Jan 09 11:01:58 GMT 2006None

SortableTableModel

public class SortableTableModel extends Object implements TableModelListener, TableModel

Fields Summary
EventListenerList
listenerList
TableModel
delegatedModel
int[]
sortedIndicies
int
sortColumn
Comparator
comparator
Comparator[]
comparators
Constructors Summary
public SortableTableModel(TableModel tm)


        
        delegatedModel = tm;
        delegatedModel.addTableModelListener (this);
        comparators = new Comparator [tm.getColumnCount()];
        sortedIndicies = new int [0];
        setSortColumn (0);
    
Methods Summary
public voidaddTableModelListener(javax.swing.event.TableModelListener l)

        listenerList.add (TableModelListener.class, l);
    
protected voidfireAllChanged()

        TableModelEvent e = new TableModelEvent (this);
        fireTableModelEvent (e);
    
public voidfireTableModelEvent(javax.swing.event.TableModelEvent e)

        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i] == TableModelListener.class) {
                ((TableModelListener) listeners[i+1]).tableChanged(e);
            }
        }
    
public java.lang.ClassgetColumnClass(int columnIndex)

        if (delegatedModel.getRowCount() > 0)
            return delegatedModel.getValueAt(0, columnIndex).getClass();
        else
            return Object.class;
    
public intgetColumnCount()

        return delegatedModel.getColumnCount();
    
public java.lang.StringgetColumnName(int index)

 
        return delegatedModel.getColumnName (index);
    
private intgetDelegatedRow(int row)

        return sortedIndicies [row];
    
public intgetRowCount()

        return delegatedModel.getRowCount();
    
public intgetSortColumn()

        return sortColumn;
    
public java.lang.ObjectgetValueAt(int rowIndex, int columnIndex)

        return delegatedModel.getValueAt (getDelegatedRow(rowIndex),
                                          columnIndex);
    
public booleanisCellEditable(int rowIndex, int columnIndex)

        return delegatedModel.isCellEditable (rowIndex, columnIndex);
    
public voidremoveTableModelListener(javax.swing.event.TableModelListener l)

        listenerList.remove (TableModelListener.class, l);
    
protected voidresort()
called to rebuild the delegate-to-sortable mapping

        // does sortedIndicies need to grow or shrink?
        if (sortedIndicies.length != delegatedModel.getRowCount()) {
            sortedIndicies = new int [delegatedModel.getRowCount()];
        }
        // build up a list of SortingDelegates
        ArrayList sortMe = new ArrayList();
        for (int i=0; i<delegatedModel.getRowCount(); i++) {
            SortingDelegate sd =
                new SortingDelegate (delegatedModel.getValueAt(i, getSortColumn()),
                                     i);
            sortMe.add (sd);
        }
        // now sort him with the SortingDelegateComparator
        SortingDelegateComparator sdc =
            new SortingDelegateComparator (comparator);
        Collections.sort (sortMe, sdc);

        // fill sortedIndicies array
        // index -> value represents mapping from original
        // row to sorted row
        for (int i=0; i<sortMe.size(); i++) {
            sortedIndicies[i] =
                ((SortingDelegate) sortMe.get(i)).row;
        }

        // fire change event
        fireAllChanged();
    
public voidsetComparatorForColumn(java.util.Comparator c, int i)

        // range check
        if (i > comparators.length) {
            Comparator[] newComparators = new Comparator[i+1];
            System.arraycopy (comparators, 0,
                              newComparators, 0,
                              comparators.length);
            comparators = newComparators;
        }
        // add the comparator
        comparators[i] = c;
    
public voidsetSortColumn(int i)

        sortColumn = i;
        
        // reset current comparator, possibly to null, which 
        // will make us use "natural ordering" for those values
        comparator = null;
        if ((comparators != null) &&
            (comparators.length > 0))
            // is there one in the list of comparators?
            comparator = comparators[sortColumn];
        
        // now do the sort
        resort();
    
public voidsetValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)

        delegatedModel.setValueAt (aValue, rowIndex, columnIndex);
    
public voidtableChanged(javax.swing.event.TableModelEvent e)

                            switch (e.getType()) {
                            case TableModelEvent.DELETE: {
                                resort();
                                fireAllChanged();
                                break;
                            }
                            case TableModelEvent.INSERT: {
                                resort();
                                fireAllChanged();
                                break;
                            }
                            case TableModelEvent.UPDATE: {
                                resort();
                                fireAllChanged();
                                break;
                            }

                            }