create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / SimpleFileFilter.java
1 package net.sf.openrocket.gui.util;
2
3 import java.io.File;
4 import java.util.Locale;
5
6 import javax.swing.filechooser.FileFilter;
7
8 /**
9  * A FileFilter similar to FileNameExtensionFilter except that
10  * it allows multipart extensions (.ork.gz), and also implements
11  * the java.io.FileFilter interface.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class SimpleFileFilter extends FileFilter implements java.io.FileFilter {
16         
17         private final String description;
18         private final boolean acceptDir;
19         private final String[] extensions;
20         
21         
22         /**
23          * Create filter that accepts files with the provided extensions that
24          * accepts directories as well.
25          * 
26          * @param description   the description of this file filter.
27          * @param extensions    an array of extensions that match this filter.
28          */
29         public SimpleFileFilter(String description, String... extensions) {
30                 this(description, true, extensions);
31         }
32         
33         
34         /**
35          * Create filter that accepts files with the provided extensions.
36          * 
37          * @param description   the description of this file filter.
38          * @param acceptDir             whether to accept directories
39          * @param extensions    an array of extensions that match this filter.
40          */
41         public SimpleFileFilter(String description, boolean acceptDir, String... extensions) {
42                 this.description = description;
43                 this.acceptDir = acceptDir;
44                 this.extensions = new String[extensions.length];
45                 for (int i = 0; i < extensions.length; i++) {
46                         String ext = extensions[i].toLowerCase(Locale.ENGLISH);
47                         if (ext.charAt(0) == '.') {
48                                 this.extensions[i] = ext;
49                         } else {
50                                 this.extensions[i] = '.' + ext;
51                         }
52                 }
53         }
54         
55         
56         @Override
57         public boolean accept(File file) {
58                 if (file == null)
59                         return false;
60                 if (file.isDirectory())
61                         return acceptDir;
62                 
63                 String filename = file.getName();
64                 filename = filename.toLowerCase(Locale.ENGLISH);
65                 for (String ext : extensions) {
66                         if (filename.endsWith(ext))
67                                 return true;
68                 }
69                 
70                 return false;
71         }
72         
73         @Override
74         public String getDescription() {
75                 return description;
76         }
77         
78 }