⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortfiltermodel.java.svn-base

📁 梦界家园程序开发基底框架
💻 SVN-BASE
字号:
package jm.framework.gui.module.jmtable.model ;

import java.util.Arrays;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class SortFilterModel
    extends AbstractTableModel
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -8827297612982261046L;
	private TableModel model ;
    private int sortColumn ;
    private Row[] rows ;
//    private JM2DArray arowdata = null;
    public SortFilterModel ( TableModel m )
    {
        model = m ;
        rows = new Row[model.getRowCount () ] ;
        for ( int i = 0 ; i < rows.length ; i++ )
        {
            rows[ i ] = new Row () ;
            rows[ i ].index = i ;
        }
    }

    /**
      Sorts the rows.
      @param c the column that should become sorted
     */
    public void sort ( int c )
    {
        sortColumn = c ;
        Arrays.sort ( rows ) ;
        fireTableDataChanged () ;
    }

    // Compute the moved row for the three methods that access
    // model elements

    public Object getValueAt ( int r , int c )
    {
        return model.getValueAt ( rows[ r ].index , c ) ;
    }

    public boolean isCellEditable ( int r , int c )
    {
        return model.isCellEditable ( rows[ r ].index , c ) ;
    }

    public void setValueAt ( Object aValue , int r , int c )
    {
        model.setValueAt ( aValue , rows[ r ].index , c ) ;
    }

    // delegate all remaining methods to the model

    public int getRowCount ()
    {
        return model.getRowCount () ;
    }

    public int getColumnCount ()
    {
        return model.getColumnCount () ;
    }

    public String getColumnName ( int c )
    {
        return model.getColumnName ( c ) ;
    }

    @SuppressWarnings("unchecked")
	public Class getColumnClass ( int c )
    {
        return model.getColumnClass ( c ) ;
    }

    /**
       This inner class holds the index of the model row
       Rows are compared by looking at the model row entries
       in the sort column.
     */
    private class Row
        implements Comparable
    {
        public int index ;
        @SuppressWarnings("unchecked")
		public int compareTo ( Object other )
        {
            Row otherRow = ( Row ) other ;
            Object a = model.getValueAt ( index , sortColumn ) ;
            Object b = model.getValueAt ( otherRow.index , sortColumn ) ;
            if ( a instanceof Comparable )
                return ( ( Comparable ) a ).compareTo ( b ) ;
            else
                return a.toString ().compareTo ( b.toString () ) ;

            //            return index - otherRow.index;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -