1   /*
2    *  ExtensionFileFilter.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Valentin Tablan, 22/May/2000
12   *
13   *  $Id: ExtensionFileFilter.java,v 1.3 2001/11/29 15:45:07 valyt Exp $
14   */
15  package gate.util;
16  
17  import java.io.*;
18  import java.util.*;
19  
20  /**
21   * Implementation of a file filter
22   * This class is used by {@link javax.swing.JFileChooser} to filter the
23   * displayed files by their extension.
24   *
25   */
26  public class ExtensionFileFilter extends javax.swing.filechooser.FileFilter
27                                   implements FileFilter {
28  
29    /** Debug flag
30     */
31    private static final boolean DEBUG = false;
32  
33    /**
34     * Builds a new ExtensionFileFilter
35     */
36    public ExtensionFileFilter() {
37    }
38  
39    /**
40     * Checks a file for compliance with the requested extensions.
41     *
42     * @param f
43     */
44    public boolean accept(File f){
45      String name = f.getName();
46      if(f.isDirectory()) return true;
47  
48      for(int i = 0; i < acceptedExtensions.size(); i++){
49        if(name.endsWith((String)acceptedExtensions.get(i))) return true;
50      }
51      return false;
52    }
53  
54    /**
55     * Returns the user-frielndly description for the files, e.g. "Text files"
56     *
57     */
58    public String getDescription() {
59      return description;
60    }
61  
62    /**
63     * Adds a new extension to the list of accepted extensions.
64     *
65     * @param ext
66     */
67    public void addExtension(String ext) {
68      acceptedExtensions.add(ext);
69    }
70  
71    /**
72     * Sets the user friendly description for the accepted files.
73     *
74     * @param desc
75     */
76    public void setDescription(String desc) {
77      description = desc;
78    }
79  
80    /**
81     * The set of accepted extensions
82     *
83     */
84    private List acceptedExtensions = new ArrayList();
85  
86    /**
87     * The desciption of the accepted files.
88     *
89     */
90    private String description;
91  
92  } // ExtensionFileFilter
93