Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SuffixFileFilter |
|
| 0.0;0 |
1 | package us.daveread.basicquery.util; | |
2 | ||
3 | import java.io.File; | |
4 | ||
5 | import javax.swing.filechooser.FileFilter; | |
6 | ||
7 | /** | |
8 | * A FileFilter that limits files to those that end with one of the supplied | |
9 | * suffix values. A dot should be the first character of the suffix pattern if | |
10 | * the entire suffix must match the pattern. If only the ending characters must | |
11 | * match then the dot should be excluded. For example the suffix pattern ".txt" | |
12 | * will match files ending with ".txt" while a suffix pattern of "xt" will match | |
13 | * files ending in things like ".txt", ".pxt", ".ext", ".next" and so forth. | |
14 | * | |
15 | * @author David Read | |
16 | */ | |
17 | public class SuffixFileFilter extends FileFilter { | |
18 | /** | |
19 | * The description of the filter | |
20 | */ | |
21 | private String description; | |
22 | ||
23 | /** | |
24 | * The collection of suffixes that match the filter | |
25 | */ | |
26 | private String[] acceptedSuffixes; | |
27 | ||
28 | /** | |
29 | * Create a new filter with the supplied description and collection of | |
30 | * suffixes. Note that the suffixes are not case sensitive even on case | |
31 | * sensitive operating systems. | |
32 | * | |
33 | * @param pDescription | |
34 | * The description of the filter to be shown to the user | |
35 | * @param pAcceptedSuffixes | |
36 | * The set of file suffix patterns that match the filter | |
37 | */ | |
38 | public SuffixFileFilter(String pDescription, String[] pAcceptedSuffixes) { | |
39 | 0 | super(); |
40 | ||
41 | 0 | setDescription(pDescription); |
42 | 0 | setAcceptedSuffixes(pAcceptedSuffixes); |
43 | 0 | } |
44 | ||
45 | /** | |
46 | * Set the description for this filter | |
47 | * | |
48 | * @param pDescription | |
49 | * The filter description | |
50 | */ | |
51 | private void setDescription(String pDescription) { | |
52 | 0 | description = pDescription; |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Set the collection of suffix patterns to be accepted by this filter | |
57 | * | |
58 | * @param pAcceptedSuffixes | |
59 | * The accepted suffix patterns | |
60 | */ | |
61 | private void setAcceptedSuffixes(String[] pAcceptedSuffixes) { | |
62 | 0 | acceptedSuffixes = new String[pAcceptedSuffixes.length]; |
63 | ||
64 | 0 | for (int index = 0; index < pAcceptedSuffixes.length; ++index) { |
65 | 0 | acceptedSuffixes[index] = pAcceptedSuffixes[index].toLowerCase(); |
66 | } | |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * Check whether the supplied file's name agrees with this suffix file filter. | |
71 | * If not (e.g. the suffix is not a match) then append the first suffix in the | |
72 | * list for this filter to the filename and return it. Otherwise return the | |
73 | * original file name. | |
74 | * | |
75 | * @param file | |
76 | * The file whose name is to be check against this filter | |
77 | * | |
78 | * @return The file name adjust ed match this suffix filter if necessary | |
79 | */ | |
80 | public File makeWithPrimarySuffix(File file) { | |
81 | 0 | if (!accept(file)) { |
82 | 0 | String primarySuffix = acceptedSuffixes[0]; |
83 | 0 | if (!primarySuffix.startsWith(".")) { |
84 | 0 | primarySuffix = "." + primarySuffix; |
85 | } | |
86 | 0 | return new File(file.getAbsolutePath() + primarySuffix); |
87 | } else { | |
88 | 0 | return file; |
89 | } | |
90 | } | |
91 | ||
92 | @Override | |
93 | public boolean accept(File file) { | |
94 | 0 | if (file.isDirectory()) { |
95 | 0 | return true; |
96 | } | |
97 | ||
98 | 0 | final String fileNameLowerCase = file.getName().toLowerCase(); |
99 | ||
100 | 0 | for (String suffix : acceptedSuffixes) { |
101 | 0 | if (fileNameLowerCase.endsWith(suffix)) { |
102 | 0 | return true; |
103 | } | |
104 | } | |
105 | ||
106 | 0 | return false; |
107 | } | |
108 | ||
109 | @Override | |
110 | public String getDescription() { | |
111 | 0 | return description; |
112 | } | |
113 | } |