1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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