001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.aspects.gui;
019    
020    import java.util.Collection;
021    import org.objectweb.jac.core.rtti.CollectionItem;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    
024    public class FilterCriteria {
025        public FilterCriteria(int column, FieldItem field) {
026            this.column = column;
027            this.field = field;
028        }
029    
030        /**
031         * Tells wether a row of table model matches the filter
032         * @param model the table model
033         * @param row row index in the table model
034         */
035        public boolean match(ExtendedTableModel model, int row) {
036            Object modelValue = model.getObject(row,column);
037            if (field instanceof CollectionItem) {
038                return !active || 
039                    (modelValue!=null && ((Collection)modelValue).contains(value));
040            } else {
041                return !active || 
042                    (value==null && modelValue==null) ||
043                    (value!=null && value.equals(modelValue));
044            }
045        }
046    
047        /** The column to filter */
048        int column;
049        public int getColumn() {
050            return column;
051        }
052    
053        FieldItem field;
054        public FieldItem getField() {
055            return field;
056        }
057    
058        /** If false, the filer is inactive */
059        boolean active = false;
060        public boolean isActive() {
061            return active;
062        }
063        public void setActive(boolean active) {
064            this.active = active;
065        }
066    
067        Object value;
068        public void setValue(Object value) {
069            this.value = value;
070        }
071        public Object getValue() {
072            return value;
073        }
074    
075        public String toString() {
076            return column+(active?("=="+value):"(off)");
077        }
078    
079        public boolean equals(Object o) {
080            if (!(o instanceof FilterCriteria))
081                return false;
082            FilterCriteria criteria = (FilterCriteria)o;
083            return criteria.column==column && criteria.active==active;
084        }
085    
086        public int hashCode() {
087            return column ^ (active ? 0 : 2^31);
088        }
089    }