001 package net.sourceforge.mp3dings.preferences;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003-2006 Moritz Ringler
005 * $Id: FilteredField.java,v 1.7 2006/09/25 17:34:42 ringler Exp $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program 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
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU 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 */
021
022 import java.util.Collection;
023 import java.util.Iterator;
024 import java.util.List;
025 import java.util.ArrayList;
026 import java.util.logging.Logger;
027 import java.util.prefs.Preferences;
028 import javax.swing.event.ChangeEvent;
029 import javax.swing.event.ChangeListener;
030 import net.sourceforge.mp3dings.Field;
031 import net.sourceforge.mp3dings.utils.Bean;
032 import net.sourceforge.mp3dings.utils.BeanSet;
033 import de.mospace.swing.JDoubleList;
034
035 /** A single item representing a field in the FieldSelectorPanel JDoubleList. **/
036 public class FilteredField extends JDoubleList.ListElement implements ChangeListener{
037 private static final Logger logger = Logger.getLogger(FilteredField.class.getPackage().getName());
038 Field field;
039 private List filters;
040
041 /** Creates a new instance of this class.
042 * @param field the field represented by this item
043 * @param fieldName the name of the field represented by this item
044 * @param mov whether this item can be moved from left to right and vice versa
045 **/
046 public FilteredField(Field field, String fieldName, boolean mov){
047 super(fieldName, mov);
048 this.field = field;
049 filters = new ArrayList();
050 }
051
052 public FilteredField(Field field){
053 this(field, field.longname(), true);
054 }
055
056 /** Returns a string representation of a FilteredField object. **/
057 public String toString(){
058 return (String) getObject();
059 }
060
061 /** Returns the field attribute of this FilteredField. **/
062 public Field getField(){
063 return field;
064 }
065
066 public void stateChanged(ChangeEvent e){
067 if(e.getSource() instanceof Collection){
068 filters.retainAll((Collection) e.getSource());
069 }
070 }
071
072 public boolean isFiltered(){
073 return (filters.size() != 0);
074 }
075
076 public List getFilters(){
077 return filters;
078 }
079
080 public void setFilters(List filterlist){
081 if(filters == null){
082 throw new IllegalArgumentException("filterlist may not be null");
083 }
084 filters = filterlist;
085 }
086
087 public void save(Preferences p){
088 String key = String.valueOf(getField());
089 Iterator it = filters.iterator();
090 StringBuffer sb = new StringBuffer(filters.size() * 10);
091 while(it.hasNext()){
092 sb.append(Long.toString(((Bean) it.next()).getId(), 16));
093 sb.append(";");
094 }
095 p.put(key, sb.toString());
096 }
097
098 public static FilteredField load(Preferences pref, Field field, BeanSet availableFilters){
099 FilteredField result = new FilteredField(field);
100
101 String prefvalue = pref.get(field.toString(), null);
102 if(!(prefvalue == null || prefvalue.equals(""))){
103 String[] sfilterid = prefvalue.split(";");
104 Bean filter;
105 long filterid = Bean.NO_ID;
106 for(int i=0; i < sfilterid.length; i++){
107 try{
108 filterid = Long.parseLong(sfilterid[i], 16);
109 } catch (NumberFormatException ex){
110 logger.warning("Cannot load filter " + sfilterid[i]);
111 logger.warning(ex.toString());
112 continue;
113 }
114 filter = availableFilters.getBeanForID(filterid);
115 if(filter != null){
116 result.getFilters().add(filter);
117 }
118 }
119 }
120 return result;
121 }
122
123 }