001/*
002 *   InstanceOfRowFilter  -- A RowFilter that filters based on the model data class type.
003 *   
004 *   Copyright (C) 2014, by Joseph A. Huwaldt.
005 *   All rights reserved.
006 *   
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *   
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 */
022package jahuwaldt.swing;
023
024import java.util.ArrayList;
025import java.util.List;
026import javax.swing.RowFilter;
027import javax.swing.table.TableModel;
028
029/**
030 * A RowFilter that filters based on the Class type of the model data with behavior
031 * similar to the <code>instanceof</code> operator. Since TableModel does not necessarily
032 * return the correct class type for a table entry, this class requires a list of the
033 * model data being displayed in the table  to be supplied in the constructor.
034 *
035 * <p> Modified by: Joseph A. Huwaldt </p>
036 *
037 * @author Joseph A. Huwaldt Date: January 31, 2014
038 * @version January 31, 2014
039 */
040public class InstanceOfRowFilter<M extends TableModel, I extends Integer> extends RowFilter<M, I> {
041
042    private List contents = new ArrayList();
043    private List<Class> filterClasses = new ArrayList();
044
045    /**
046     * Construct a RowFilter using the specified list of model data objects and the
047     * specified list of Class objects to filter on.
048     * 
049     * @param modelData  The model data to use for filtering.
050     * @param filterClasses The Class types to use for filtering.
051     */
052    public InstanceOfRowFilter(List modelData, List<Class> filterClasses) {
053        contents.addAll(modelData);
054        this.filterClasses.addAll(filterClasses);
055    }
056
057    /**
058     * Returns true if the specified entry should be shown; returns false if the entry
059     * should be hidden.
060     *
061     * @param entry a non-null object that wraps the underlying object from the model
062     * @return true if the entry should be shown
063     */
064    @Override
065    public boolean include(Entry<? extends M, ? extends I> entry) {
066        int modelRow = entry.getIdentifier();
067        Class target = contents.get(modelRow).getClass();
068        for (Class cls : filterClasses) {
069            if (cls.isAssignableFrom(target))
070                return true;
071        }
072        return false;
073    }
074}