1 package tim.prune.load;
4 import javax.swing.filechooser.FileFilter;
6 import tim.prune.I18nManager;
9 * Class to act as a generic file filter based on file extension
11 public class GenericFileFilter extends FileFilter
13 /** Filter description for display */
14 private String _filterDesc = null;
15 /** Array of allowed three-character suffixes */
16 private String[] _threeCharSuffixes = null;
17 /** Array of allowed four-character suffixes */
18 private String[] _fourCharSuffixes = null;
23 * @param inDescKey key for filter description
24 * @param inSuffixes array of allowed 3- and 4-character file suffixes
26 public GenericFileFilter(String inDescKey, String[] inSuffixes)
28 _filterDesc = I18nManager.getText(inDescKey);
29 if (inSuffixes != null && inSuffixes.length > 0)
31 _threeCharSuffixes = new String[inSuffixes.length];
32 _fourCharSuffixes = new String[inSuffixes.length];
33 int threeIndex = 0, fourIndex = 0;
34 for (String suffix : inSuffixes)
38 suffix = suffix.trim().toLowerCase();
39 if (suffix.length() == 3) {
40 _threeCharSuffixes[threeIndex++] = suffix;
42 else if (suffix.length() == 4) {
43 _fourCharSuffixes[fourIndex++] = suffix;
51 * Check whether to accept the specified file or not
52 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
54 public boolean accept(File inFile)
56 return inFile.isDirectory() || acceptFilename(inFile.getName());
60 * Check whether to accept the given filename
61 * @param inName name of file
62 * @return true if accepted, false otherwise
64 public boolean acceptFilename(String inName)
68 final int nameLen = inName.length();
71 // Check for three character suffixes
72 char currChar = inName.charAt(nameLen - 4);
75 return acceptFilename(inName.substring(nameLen - 3).toLowerCase(), _threeCharSuffixes);
77 // check for four character suffixes
78 currChar = inName.charAt(nameLen - 5);
81 return acceptFilename(inName.substring(nameLen - 4).toLowerCase(), _fourCharSuffixes);
85 // Not matched so don't accept
90 * Check whether to accept the given filename
91 * @param inSuffixToCheck suffix to check
92 * @param inAllowedSuffixes array of allowed suffixes
93 * @return true if accepted, false otherwise
95 private static boolean acceptFilename(String inSuffixToCheck, String[] inAllowedSuffixes)
97 if (inSuffixToCheck != null && inAllowedSuffixes != null)
99 // Loop over allowed suffixes
100 for (String allowed : inAllowedSuffixes)
102 if (allowed != null && inSuffixToCheck.equals(allowed)) {
107 // Fallen through so not allowed
114 * @see javax.swing.filechooser.FileFilter#getDescription()
116 public String getDescription()