View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.io;
21  
22  import java.io.File;
23  import java.io.FileFilter;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /***
29   * Provides a <code>FileFilter</code> that accepts a certain file
30   * extension only.
31   * 
32   * @author Steffen Pingel
33   */
34  public class FileExtensionFilter extends javax.swing.filechooser.FileFilter
35  	implements FileFilter
36  {
37  
38      private List<String> extensions = new ArrayList<String>();
39  	private String description;
40  
41      public FileExtensionFilter(String description, String extension)
42      {
43  		this.description = description;
44  		this.extensions.add(extension);
45      }
46  
47      public FileExtensionFilter(String description, String[] extensions)
48      {
49  		this.description = description;
50  		this.extensions.addAll(Arrays.asList(extensions));
51      }
52  
53      public boolean accept(File file)
54      {
55  		if (file.isDirectory()) {
56  			return true;
57  		}
58  		
59  		String name = file.getName();
60  		for (String extension : extensions) {
61  			if (name.endsWith(extension)) {
62  				return true;
63  			}
64  		}
65  		return false;
66      }
67  	
68  	public String getDescription()
69  	{
70  		return description;
71  	}
72  
73  }
74