Back to index

wims  3.65+svn20090927
GenericFileFilter.java
Go to the documentation of this file.
00001 /*
00002 $Id: GenericFileFilter.java,v 1.2 2003/02/18 11:48:48 sander Exp $
00003 */
00004 
00005 
00006 package fr.ove.utils;
00007 
00008 import javax.swing.filechooser.FileFilter;
00009 import java.io.File;
00010 
00014 public class GenericFileFilter extends FileFilter {
00018     private String type;
00019     
00023     private String extension;
00024     
00028     public GenericFileFilter() {
00029         this("Text", "txt");
00030     }
00031     
00037     public GenericFileFilter(String type, String extension) {
00038         this.type = type;
00039         this.extension = extension;
00040     }
00041     
00046     public void setType(String type) {
00047         this.type = type;
00048     }
00049     
00053     public String getType() {
00054         return type;
00055     }
00056     
00061     public void setExtension(String extension) {
00062         this.extension = extension;
00063     }
00064     
00068     public String getExtension() {
00069         return extension;
00070     }
00071     
00076     public boolean accept(File file) {
00077         boolean accept = file.isDirectory();
00078         
00079         if (!accept) {
00080             String suffix = getSuffix(file);
00081             if (suffix != null)
00082                 accept = suffix.equals(extension);
00083         }
00084         
00085         return accept;
00086     }
00087     
00091     public String getDescription() {
00092         return type + " Files (*." + extension + ")";
00093     }
00094     
00099     private String getSuffix(File file) {
00100         String path = file.getPath();
00101         String suffix = null;
00102         int index = path.lastIndexOf('.');
00103         if ((index > 0) && (index < (path.length() - 1)))
00104             suffix = path.substring(index + 1).toLowerCase();
00105             
00106         return suffix;
00107     }
00108 }