committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / gui / main / SimpleFileFilter.java
1 package net.sf.openrocket.gui.main;
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).
10  * 
11  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12  */
13 public class SimpleFileFilter extends FileFilter {
14
15         private final String description;
16         private final String[] extensions;
17         
18         
19         /**
20          * Sole constructor.
21          * 
22          * @param description   the description of this file filter.
23          * @param extensions    an array of extensions that match this filter.
24          */
25         public SimpleFileFilter(String description, String ... extensions) {
26                 this.description = description;
27                 this.extensions = new String[extensions.length];
28                 for (int i=0; i<extensions.length; i++) {
29                         String ext = extensions[i].toLowerCase();
30                         if (ext.charAt(0) == '.') {
31                                 this.extensions[i] = ext;
32                         } else {
33                                 this.extensions[i] = '.' + ext;
34                         }
35                 }
36         }
37         
38         
39         
40         @Override
41         public boolean accept(File file) {
42                 if (file == null)
43                         return false;
44                 if (file.isDirectory())
45                         return true;
46                 
47                 String filename = file.getName();
48                 filename = filename.toLowerCase();
49                 for (String ext: extensions) {
50                         if (filename.endsWith(ext))
51                                 return true;
52                 }
53                 
54                 return false;
55         }
56
57         @Override
58         public String getDescription() {
59                 return description;
60         }
61
62 }