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

📄 tableexclusionfiltermodel.java.svn-base

📁 java 表格排序
💻 SVN-BASE
字号:
/*
 * TableExclusionFilterModel.java
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package au.id.dodson.FilterableSortableTable;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

public class TableExclusionFilterModel extends AbstractTableModel implements
        TableModelListener {
    
    protected ArrayList indexList = new ArrayList(0);
    
    protected String filter = "All";
    
    protected AbstractTableModel model;
    
    private int filterColumn = 0;
    private int noOfFilterableColumns = 0;
    private boolean filterAllColumns = false;
    
    /**
     *
     * @return
     */
    public AbstractTableModel getModel() {
        return model;
    }
    
    /**
     *
     * @param model
     */
    public void setModel(AbstractTableModel model) {
        this.model = model;
        fillIndexList(filter);
        model.addTableModelListener(this);
    }
    
    /**
     *
     * @param model
     * @param filterColumn
     */
    public TableExclusionFilterModel(AbstractTableModel model, int filterColumn) {
        setModel(model);
        this.filterColumn = filterColumn;
    }
    
    /**
     *
     * @param model
     * @param noOfFilterableColumns
     * @param filterAllColumns
     */
    public TableExclusionFilterModel(AbstractTableModel model,
            int noOfFilterableColumns, boolean filterAllColumns) {
        setModel(model);
        this.noOfFilterableColumns = noOfFilterableColumns;
        this.filterAllColumns = filterAllColumns;
    }
    
    /**
     *
     * @param model
     */
    public TableExclusionFilterModel(AbstractTableModel model) {
        setModel(model);
        this.noOfFilterableColumns = model.getColumnCount();
        this.filterAllColumns = true;
    }
    
    private boolean filterInString(String checkString) {
        return ((filter.equals("All")) || (checkString.toLowerCase().indexOf(
                filter.toLowerCase()) != -1));
    }
    
    private void fillIndexList(String filter) {
        this.filter = filter;
        int rowCount = model.getRowCount();
        indexList.clear();
        for (int z = 0; z < rowCount; z++) {
            int start;
            int end;
            if (filterAllColumns) {
                start = 0;
                end = noOfFilterableColumns;
                for (int i = 0; i < noOfFilterableColumns; i++) {
                    filterColumn = i;
                    if (model.getValueAt(z, filterColumn) != null) {
                        String myFilter = model.getValueAt(z, filterColumn).toString();
                        if (filterInString(myFilter)) {
                            indexList.add(new Integer(z));
                            break;
                        }
                    }
                }
            } else { // changed for single column filter
                if (model.getValueAt(z, filterColumn) != null) {
                    String myFilter = model.getValueAt(z, filterColumn).toString();
                    if (filterInString(myFilter)) {
                        indexList.add(new Integer(z));
                    }
                }
            }
        }
    }
    
    /**
     *
     * @param filter
     * @param doAll
     */
    public synchronized void setFilter(String filter, boolean doAll) {
        fillIndexList(filter);
        // Tell the component that the data has changed
        this.fireTableDataChanged();
    }
    
    /**
     *
     * @param filter
     */
    public synchronized void setFilter(String filter) {
        fillIndexList(filter);
        this.fireTableDataChanged();
    }
    
    /**
     *
     * @return
     */
    public String getFilter() {
        return filter;
    }
    
    /**
     *
     * @return
     */
    public int getRowCount() {
        return indexList.size();
    }
    
    /**
     *
     * @return
     */
    public int getColumnCount() {
        return model.getColumnCount();
    }
    
    /**
     *
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        int newIndex = ((Integer) indexList.get(rowIndex)).intValue();
        return model.getValueAt(newIndex, columnIndex);
    }
    
    /**
     *
     * @param aColumn
     * @return
     */
    public String getColumnName(int aColumn) {
        return model.getColumnName(aColumn);
    }
    
    /**
     *
     * @param aColumn
     * @return
     */
    public Class getColumnClass(int aColumn) {
        return model.getColumnClass(aColumn);
    }
    
    /**
     *
     * @param row
     * @param column
     * @return
     */
    public boolean isCellEditable(int row, int column) {
        return model.isCellEditable(row, column);
    }
    
    public synchronized void reallocateIndexes() {
        fillIndexList(filter);
    }
    
    /**
     *
     * @param e
     */
    public void tableChanged(TableModelEvent e) {
        reallocateIndexes();
        fireTableChanged(e);
    }
}

⌨️ 快捷键说明

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