Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GenericFileFilter.java

Go to the documentation of this file.
00001 /*
00002  * GenericFileFilter.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.gui;
00024 
00025 import javax.swing.filechooser.FileFilter;
00026 import java.io.File;
00027 
00033 public class GenericFileFilter extends FileFilter implements java.io.FileFilter {
00034 
00038     private String[] extensions;
00039 
00043     private String description;
00044 
00051     public GenericFileFilter(String description, String... extensions) {
00052         if (extensions == null) {
00053             extensions = new String[0];
00054         }
00055         if (description == null) {
00056             description = "";
00057         }
00058         for (int i = 0; i < extensions.length; i++) {
00059             while (extensions[i].startsWith(".")) {
00060                 extensions[i] = extensions[i].substring(1);
00061             }
00062         }
00063         if (extensions.length > 0) {
00064             description += " (*." + extensions[0];
00065             for (int i = 1; i < extensions.length; i++) {
00066                 description += ", *." + extensions[i];
00067             }
00068             description += ")";
00069 
00070 
00071         }
00072 
00073         this.extensions = extensions;
00074         this.description = description;
00075     }
00076 
00083     public boolean accept(File pathname) {
00084         if (pathname.isDirectory()) {
00085             return true;
00086         }
00087 
00088         String extension = getExtension(pathname);
00089         if (extension != null) {
00090             for (int i = 0; i < this.extensions.length; i++) {
00091                 if (extension.equals(this.extensions[i])) {
00092                     return true;
00093                 }
00094             }
00095         }
00096         return false;
00097     }
00098 
00105     private static String getExtension(File f) {
00106         String ext = null;
00107         String s = f.getName();
00108         int i = s.lastIndexOf('.');
00109         if (i > 0 && i < s.length() - 1) {
00110             ext = s.substring(i + 1).toLowerCase();
00111         }
00112         return ext;
00113     }
00114 
00118     public String getDescription() {
00119         return this.description;
00120     }
00121 }

Generated on Fri May 6 16:00:31 2005 for Squid by  doxygen 1.4.1