001    package net.sourceforge.mp3dings.filter;
002    
003    /*
004     *  Mp3dings - manage mp3 meta-information
005     *  Copyright (C) 2003 Moritz Ringler
006     *  $Id: FilterCollection.java 69 2007-10-24 21:57:43Z ringler $
007     *
008     *  This program is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU General Public License
010     *  as published by the Free Software Foundation; either version 2
011     *  of the License, or (at your option) any later version.
012     *
013     *  This program is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     *  GNU General Public License for more details.
017     *
018     *  You should have received a copy of the GNU General Public License
019     *  along with this program; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
021     */
022    import java.util.Hashtable;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.prefs.BackingStoreException;
026    import java.util.prefs.Preferences;
027    import net.sourceforge.mp3dings.Field;
028    import net.sourceforge.mp3dings.preferences.FilteredField;
029    import net.sourceforge.mp3dings.utils.Bean;
030    import net.sourceforge.mp3dings.utils.BeanSet;
031    import java.util.logging.Logger;
032    
033    /**
034     * A collection that holds all the filters for all the fields in an mp3dings
035     * table.
036     *
037     * @author Moritz Ringler
038     * @version $Revision: 69 $ ($Date: 2007-10-24 23:57:43 +0200 (Mi, 24 Okt 2007) $)
039     */
040    public class FilterCollection {
041        private static final Logger logger = Logger.getLogger(FilterCollection.class.getPackage().getName());
042        private BeanSet filterset = new BeanSet();
043    
044        private Hashtable filteredFields = new Hashtable();
045    
046    
047        /**
048         * Loads a FilterCollection from a preference node.
049         *
050         * @param beanSetNode Description of the Parameter
051         */
052        public FilterCollection(Preferences beanSetNode) {
053            String[] keys = new String[0];
054            try {
055                keys = beanSetNode.keys();
056            } catch (BackingStoreException ex) {
057                logger.severe("Error loading filter collection from " + beanSetNode);
058                logger.severe(ex.toString());
059            }
060            for (int i = 0; i < keys.length; i++) {
061                try {
062                    Bean b = Bean.load(beanSetNode, keys[i]);
063                    filterset.add(b);
064                } catch (Exception ex) {
065                    logger.severe("Error loading filter " + keys[i] + " from " + beanSetNode);
066                    logger.severe(ex.toString());
067                }
068            }
069        }
070    
071    
072        /** Constructs a new FilterCollection. */
073        public FilterCollection() { }
074    
075    
076        /**
077         * Creates a new filter collection that contains all the filters in fc. The
078         * filters themselves are not cloned.
079         *
080         * @param fc The FilterCollection to copy
081         */
082        public FilterCollection(FilterCollection fc) {
083            filterset.addAll(fc.getFilterSet());
084            Iterator it = fc.filteredFields.keySet().iterator();
085            while (it.hasNext()) {
086                Object key = it.next();
087                filteredFields.put(key, fc.filteredFields.get(key));
088            }
089        }
090    
091    
092        /**
093         * Sets the filterSet attribute of the FilterCollection object
094         *
095         * @param filterset The new filterSet value
096         */
097        public void setFilterSet(BeanSet filterset) {
098            this.filterset = filterset;
099            filteredFields.clear();
100        }
101    
102    
103        /**
104         * Gets the filterSet attribute of the FilterCollection object
105         *
106         * @return The filterSet value
107         */
108        public BeanSet getFilterSet() {
109            return filterset;
110        }
111    
112        /**
113         * Adds the filters saved under the given node.
114         *
115         * @param filterNode Description of the Parameter
116         */
117        public void load(Preferences filterNode) {
118            String[] keys = null;
119            try {
120                keys = filterNode.keys();
121            } catch (BackingStoreException ex) {
122                logger.severe("Error loading filter collection from " + filterNode);
123                logger.severe(ex.toString());
124            }
125            FilteredField ff;
126            for (int i = 0; i < keys.length; i++) {
127                Field field = Field.forName(keys[i]);
128                if (field == null) {
129                    continue;
130                }
131                ff = FilteredField.load(filterNode, field, filterset);
132                if (ff.isFiltered()) {
133                    filteredFields.put(ff.getField(), ff);
134                }
135            }
136        }
137    
138    
139        /**
140         * Gets the filteredField attribute of the FilterCollection object
141         *
142         * @param field Returns the filters for the specified field in the form of a
143         *      FilteredField object.
144         * @return The filteredField value
145         */
146        public FilteredField getFilteredField(Field field) {
147            FilteredField ff = (FilteredField) filteredFields.get(field);
148            return (ff != null) ? ff : new FilteredField(field);
149        }
150    
151    
152        /**
153         * Sets the filteredField attribute of the FilterCollection object
154         *
155         * @param field The new filteredField value
156         * @param ff The new filteredField value
157         */
158        public void setFilteredField(Field field, FilteredField ff) {
159            if (ff.isFiltered()) {
160                filteredFields.put(field, ff);
161            } else {
162                filteredFields.remove(field);
163            }
164        }
165    
166    
167        /**
168         * Filters the Object value with all filters registered for the specified
169         * field. The returned object is guaranteed to be of the same class as
170         * value.
171         *
172         * @param field The field the value belongs to
173         * @param value The value to be filtered
174         * @return the filtered value, will be an instance of the class of value if
175         *      value is non-null
176         */
177        public Object filter(Field field, Object value) {
178            Object myObj = value;
179            FilteredField ff;
180            //get the filters registered for the id colID
181            ff = (FilteredField) filteredFields.get(field);
182            if (ff != null) {
183                myObj = filter(ff.getFilters(), value);
184            }
185            ff = (FilteredField) filteredFields.get(Field.ALL_FIELDS);
186            if (ff != null) {
187                //get the filters registered for all columns
188                myObj = filter(ff.getFilters(), value);
189            }
190            return myObj;
191        }
192    
193    
194        /**
195         * Description of the Method
196         *
197         * @param vFilter Description of the Parameter
198         * @param value Description of the Parameter
199         * @return Description of the Return Value
200         */
201        private Object filter(List vFilter, Object value) {
202            Object myObj = value;
203            if (vFilter != null && myObj != null) {
204                Iterator vIterator = vFilter.iterator();
205                while (vIterator.hasNext()) {
206                    if (myObj == null) {
207                        return null;
208                    }
209                    myObj = ((Filter) vIterator.next()).filter(myObj);
210                }
211            }
212            return myObj;
213        }
214    
215    
216        /**
217         * Returns whether any filters are currently registered for the specified
218         * field.
219         *
220         * @param field the field whose status you want to know
221         * @return the filter status of the field
222         */
223        public boolean isFiltered(Field field) {
224            return (filteredFields.get(field) != null ||
225                    filteredFields.get(field) != null);
226        }
227    }