001    /* $Id: FileField.java 12 2007-07-27 09:48:53Z ringler $
002    * This class is part of the de.mospace.swing library.
003    * Copyright (C) 2005-2006 Moritz Ringler
004    *
005    * This program is free software; you can redistribute it and/or
006    * modify it under the terms of the GNU General Public License
007    * as published by the Free Software Foundation; either version 2
008    * of the License, or (at your option) any later version.
009    *
010    * This program is distributed in the hope that it will be useful,
011    * but WITHOUT ANY WARRANTY; without even the implied warranty of
012    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013    * GNU General Public License for more details.
014    *
015    * You should have received a copy of the GNU General Public License
016    * along with this program; if not, write to the Free Software
017    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018    */
019    package de.mospace.swing;
020    
021    import java.io.File;
022    import javax.swing.filechooser.FileFilter;
023    
024    public class ExtensionFileFilter extends FileFilter{
025        private String descr;
026        private String[] ext;
027    
028        public ExtensionFileFilter(String description, String[] extensions){
029            descr = description;
030            ext = extensions;
031            if(ext != null && ext.length == 0){
032                ext = null;
033                if(descr == null){
034                    descr = "*";
035                }
036            }
037            if(descr == null){
038                StringBuffer sb = new StringBuffer();
039                for(int i=0; i<ext.length; i++){
040                    sb.append('*').append(ext[i]).append(' ');
041                }
042                descr = sb.toString();
043            }
044        }
045    
046        public boolean accept(File f){
047            if(ext == null || f.isDirectory()){
048                return true;
049            }
050            String name = f.getName();
051            for(int i=0; i<ext.length; i++){
052                if(name.endsWith(ext[i])){
053                    return true;
054                }
055            }
056            return false;
057        }
058    
059        public String getDescription(){
060            return descr;
061        }
062    }