View Javadoc

1   /*
2    * @(#)$Id: ExtensionFileFilter.java 736 2009-08-14 09:20:17Z kurmannu $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:   General FileFilter based on file extension
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 15, 2007 	crocimi		Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.geco.util;
27  
28  
29  import java.io.File;
30  
31  import javax.swing.filechooser.FileFilter;
32  
33  
34  
35  /**
36   * General FileFilter based on file extension
37   * 
38   * @version 0.9, Nov 15, 2007
39   * @author Michele Croci, mcroci@gmail.com
40   */
41  public class ExtensionFileFilter extends FileFilter {
42     String description;
43  
44     String extensions[];
45  
46     /**
47      * Constructor
48      * 
49      * @param description a description of the file filter
50      * @param extension, the file extension not to filter
51      */
52     public ExtensionFileFilter(String description, String extension) {
53       this(description, new String[] { extension });
54     }
55  
56     /**
57      * Constructor
58      * 
59      * @param description a description of the file filter
60      * @param extensions an array of file extension not to filter
61      */
62     public ExtensionFileFilter(String description, String extensions[]) {
63       if (description == null) {
64         this.description = extensions[0];
65       } else {
66         this.description = description;
67       }
68       this.extensions = extensions.clone();
69       toLower(this.extensions);
70     }
71  
72     private void toLower(String array[]) {
73       for (int i = 0, n = array.length; i < n; i++) {
74         array[i] = array[i].toLowerCase();
75       }
76     }//toLower
77  
78     /**
79      * Returns the description
80      * 
81      * @return the description
82      */
83     public String getDescription() {
84       return description;
85     }//getDescription
86  
87     
88     public boolean accept(File file) {
89       if (file.isDirectory()) {
90         return true;
91       } else {
92         String path = file.getAbsolutePath().toLowerCase();
93         for (int i = 0, n = extensions.length; i < n; i++) {
94           String extension = extensions[i];
95           if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
96             return true;
97           }
98         }
99       }
100      return false;
101    }//accept
102  }