1   /*
2    *  ExtensionFileFilter.java
3    *
4    *  Copyright (c) 1998-2005, 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.6 2005/01/11 13:51:37 ian Exp $
14   */
15  package gate.util;
16  
17  import java.io.File;
18  import java.io.FileFilter;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  /**
23   * Implementation of a file filter
24   * This class is used by {@link javax.swing.JFileChooser} to filter the
25   * displayed files by their extension.
26   *
27   */
28  public class ExtensionFileFilter extends javax.swing.filechooser.FileFilter
29                                   implements FileFilter {
30  
31    /** Debug flag
32     */
33    private static final boolean DEBUG = false;
34  
35    /**
36     * Builds a new ExtensionFileFilter
37     */
38    public ExtensionFileFilter() {
39    }
40  
41    /**
42     * Checks a file for compliance with the requested extensions.
43     *
44     * @param f
45     */
46    public boolean accept(File f){
47      String name = f.getName();
48      if(f.isDirectory()) return true;
49  
50      for(int i = 0; i < acceptedExtensions.size(); i++){
51        if(name.endsWith((String)acceptedExtensions.get(i))) return true;
52      }
53      return false;
54    }
55  
56    /**
57     * Returns the user-frielndly description for the files, e.g. "Text files"
58     *
59     */
60    public String getDescription() {
61      return description;
62    }
63  
64    /**
65     * Adds a new extension to the list of accepted extensions.
66     *
67     * @param ext
68     */
69    public void addExtension(String ext) {
70      acceptedExtensions.add(ext);
71    }
72  
73    /**
74     * Sets the user friendly description for the accepted files.
75     *
76     * @param desc
77     */
78    public void setDescription(String desc) {
79      description = desc;
80    }
81  
82    /**
83     * The set of accepted extensions
84     *
85     */
86    private List acceptedExtensions = new ArrayList();
87  
88    /**
89     * The desciption of the accepted files.
90     *
91     */
92    private String description;
93  
94  } // ExtensionFileFilter
95