Back to index

wims  3.65+svn20090927
FileExtFilter.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import java.util.*;
00014 import java.text.*;
00015 import java.lang.*;
00016 import java.io.*;
00017 import javax.swing.filechooser.*;
00018 
00019 // Selecting files by extension (strangely absent from Java).
00020 
00021 public class FileExtFilter extends javax.swing.filechooser.FileFilter
00022 {
00023     String descr;
00024     ArrayList<String> exts;
00025     
00026     public FileExtFilter(String descroot,String suffixes)
00027     {
00028        exts=new ArrayList<String>();
00029        StringTokenizer tok=new StringTokenizer(suffixes,";");
00030        while (tok.hasMoreTokens()) exts.add(tok.nextToken());
00031        
00032        descr=descroot+" (";
00033        for (int n=0;n<exts.size();n++) descr=descr+(n>0 ? " " : "")+"*"+exts.get(n);
00034        descr=descr+")";
00035     }
00036     
00037     public String getDescription() {return descr;}
00038     
00039     public boolean accept(File f)
00040     {
00041        if (f.isDirectory()) return true;
00042        for (int n=0;n<exts.size();n++) if (f.getName().endsWith(exts.get(n))) return true;
00043        return false;
00044     }
00045 }