1   package util;
2   
3   import java.io.*;
4   
5   /**
6    * This class provides a simple javax.swing.filechooser.FileFilter
7    * implementation that accepts files based on their extension, or 
8    * if they are a directory (to allow traversal of the filesystem).
9    * multiple permissible extensions can be specified.
10   *
11   * @author Graeme Bell
12   */
13  public class FileChooserFilter extends javax.swing.filechooser.FileFilter {
14  
15      /**
16       * An array containing the extensions to match against - should 
17       * be strings in a String [] but some classes may produce strings in 
18       * an  Object[] arrays so accepts Object[] as well.
19       */
20      Object[] extensions;
21  
22      /**
23       * This is a description of what files are being filtered by the 
24       * set of extensions given, for example ("all molecules"). 
25       */
26      String description;
27  
28      /**
29       * This method instantiates a new FileChooser filter.
30       * 
31       * @param extensions a String[] of extensions to map against
32       * @param description a String describing what these extensions
33       *            represent.
34       */
35      public FileChooserFilter(String[] extensions,String description) {
36  
37          this.extensions=extensions;     
38          this.description=description;
39  
40      } // end method
41  
42      /**
43       * This method instantiates a new FileChooser filter.
44       * 
45       * @param extensions a Object[] of extensions to map against
46       * @param description a String describing what these extensions
47       *            represent.
48       */
49      public FileChooserFilter(Object[] extensions, String description) {
50  
51          this.extensions=extensions;     
52          this.description=description;
53  
54      } // end method
55  
56      /**
57       * This implements javax.swing.filechooser.FileFilter functionality.
58       * It accepts files based on their extension and also if they 
59       * are directories.
60       * 
61       * @param f the file to be checked for having a suitable extension
62       *      or for being a directory.
63       * @return true if file matches extension/is a directory. false otherwise.
64       */
65          public boolean accept(File f) {
66                  
67                  // Strip path information
68  
69          for (int i=0 ; i<extensions.length ; i++) {
70  
71              if (f.getName().endsWith((String)extensions[i]) || f.isDirectory()) {
72  
73                  return true;
74  
75              } // end if
76  
77          } // end for 
78      
79          // if we get here, we didn't match any extensions.
80  
81          return false; 
82  
83          } // end method
84  
85  
86      /**
87       * This is used to provide the JFileChooser with information
88       * on what the accept mapping represents.
89       *  
90       * @return a String describing this FileFilters mapping.
91       */
92      public String getDescription() {
93  
94          // this could be extended to provide information on the molecule.
95          return description;
96  
97      } // end method
98  
99  } // end class
100 
101