1 | /* |
---|
2 | * $Id: SimpleFileFilter.java,v 1.2 2003/08/22 23:02:40 dwd Exp $ |
---|
3 | */ |
---|
4 | package gov.noaa.pmel.util; |
---|
5 | |
---|
6 | import javax.swing.filechooser.*; |
---|
7 | import java.io.File; |
---|
8 | |
---|
9 | public class SimpleFileFilter extends FileFilter { |
---|
10 | private String[] extensions; |
---|
11 | private String description; |
---|
12 | |
---|
13 | public SimpleFileFilter(String ext) { |
---|
14 | this(new String[] {ext}, null); |
---|
15 | } |
---|
16 | public SimpleFileFilter(String[] exts, String descr) { |
---|
17 | extensions = new String[exts.length]; |
---|
18 | for(int i=exts.length -1; i >=0; i--) { |
---|
19 | extensions[i]= exts[i].toLowerCase(); |
---|
20 | } |
---|
21 | description = (descr ==null? exts[0] + " files" : descr); |
---|
22 | } |
---|
23 | |
---|
24 | public boolean accept(File f) { |
---|
25 | if(f.isDirectory()) {return true;} |
---|
26 | String name = f.getName().toLowerCase(); |
---|
27 | for(int i=extensions.length-1; i>=0; i--) { |
---|
28 | if(name.endsWith(extensions[i])) { |
---|
29 | return true; |
---|
30 | } |
---|
31 | } |
---|
32 | return false; |
---|
33 | } |
---|
34 | |
---|
35 | public boolean hasExtension(String ext) { |
---|
36 | for(int i=0; i < extensions.length; i++) { |
---|
37 | if(extensions[i].equals(ext)) return true; |
---|
38 | } |
---|
39 | return false; |
---|
40 | } |
---|
41 | |
---|
42 | public String getExtension() { |
---|
43 | return getExtension(0); |
---|
44 | } |
---|
45 | |
---|
46 | public String getExtension(int index) { |
---|
47 | int idx = index; |
---|
48 | if(idx < 0 || idx >= extensions.length) idx = 0; |
---|
49 | return extensions[idx]; |
---|
50 | } |
---|
51 | public String getDescription() { |
---|
52 | return description; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|