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