FileDocCategorySizeDatePackage
SortFilterModel.javaAPI DocExample5962Mon Jul 23 13:26:34 BST 2007org.apache.struts2.components.table

SortFilterModel

public class SortFilterModel extends AbstractFilterModel implements TableModelListener, SortableTableModel

Fields Summary
private static final long
serialVersionUID
private ArrayList
rows
private String
_sortDirection
These are just here to implement the interface
private boolean
dirty
private int
sortColumn
Constructors Summary
public SortFilterModel(TableModel tm)



       
        super(tm);
        setModel(tm);
    
Methods Summary
public voidaddMouseListener(javax.swing.JTable table)

        table.getTableHeader().addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event) {
                // check for double click
                if (event.getClickCount() < 2) {
                    return;
                }

                // find column of click and
                int tableColumn = table.columnAtPoint(event.getPoint());

                // translate to table model index and sort
                int modelColumn = table.convertColumnIndexToModel(tableColumn);
                sort(modelColumn);
            }
        });
    
public intgetSortedColumnNumber()

        return sortColumn;
    
public java.lang.StringgetSortedDirection(int columnNumber)

        if (getSortedColumnNumber() < 0) {
            return NONE;
        }

        return _sortDirection;
    
public java.lang.ObjectgetValueAt(int r, int c)

        if ((rows.size() > 0) && (r < rows.size())) {
            return model.getValueAt(((Row) rows.get(r)).index, c);
        }

        return null;
    
public booleanisCellEditable(int r, int c)

        if ((rows.size() > 0) && (r < rows.size())) {
            return model.isCellEditable(((Row) rows.get(r)).index, c);
        }

        return false;
    
protected voidrefresh()

        rows.clear();

        for (int i = 0; i < model.getRowCount(); i++) {
            rows.add(new Row(i));
        }

        if (dirty && (sortColumn > -1)) {
            sort(sortColumn);
        }
    
public voidremoveRow(int rowNum)

        int mappedRow = ((Row) rows.get(rowNum)).index;
        super.removeRow(mappedRow);
    
public voidsetModel(javax.swing.table.TableModel tm)

        super.setModel(tm);
        rows.ensureCapacity(model.getRowCount());
        model.addTableModelListener(this);
        sortColumn = -1;
        dirty = true;
        refresh();
    
public voidsetValueAt(java.lang.Object aValue, int r, int c)

        if ((rows.size() > 0) && (r < rows.size())) {
            model.setValueAt(aValue, ((Row) rows.get(r)).index, c);
        }
    
public voidsort(int columnNumber, java.lang.String direction)

        _sortDirection = ASC;
        dirty = true;
        sort(columnNumber);

        if (DESC.equals(direction)) {
            sort(columnNumber);
            _sortDirection = DESC;
        }
    
protected voidsort(int c)

        boolean sorted = (sortColumn == c);
        sortColumn = c;

        if (dirty || !sorted) {
            Collections.sort(rows);
            dirty = false;
        } else {
            Collections.reverse(rows);
        }

        fireTableDataChanged();
    
public voidtableChanged(javax.swing.event.TableModelEvent e)
Implements the TableModelListener interface to receive notifications of * changes to the table model. SortFilterModel needs to receive events for adding and removing rows.

        dirty = true;
        refresh();
        fireTableChanged(e);