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 (int i=0; i<inSuffixes.length; i++)
36 String suffix = inSuffixes[i];
39 suffix = suffix.trim().toLowerCase();
40 if (suffix.length() == 3) {
41 _threeCharSuffixes[threeIndex++] = suffix;
43 else if (suffix.length() == 4) {
44 _fourCharSuffixes[fourIndex++] = suffix;
52 * Check whether to accept the specified file or not
53 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
55 public boolean accept(File inFile)
57 return inFile.isDirectory() || acceptFilename(inFile.getName());
61 * Check whether to accept the given filename
62 * @param inName name of file
63 * @return true if accepted, false otherwise
65 public boolean acceptFilename(String inName)
69 int nameLen = inName.length();
72 // Check for three character suffixes
73 char currChar = inName.charAt(nameLen - 4);
76 return acceptFilename(inName.substring(nameLen - 3).toLowerCase(), _threeCharSuffixes);
78 // check for four character suffixes
79 currChar = inName.charAt(nameLen - 5);
82 return acceptFilename(inName.substring(nameLen - 4).toLowerCase(), _fourCharSuffixes);
86 // Not matched so don't accept
91 * Check whether to accept the given filename
92 * @param inSuffixToCheck suffix to check
93 * @param inAllowedSuffixes array of allowed suffixes
94 * @return true if accepted, false otherwise
96 public boolean acceptFilename(String inSuffixToCheck, String[] inAllowedSuffixes)
98 if (inSuffixToCheck != null && inAllowedSuffixes != null)
100 // Loop over allowed suffixes
101 for (int i=0; i<inAllowedSuffixes.length; i++)
103 if (inAllowedSuffixes[i] != null && inSuffixToCheck.equals(inAllowedSuffixes[i]))
109 // Fallen through so not allowed
116 * @see javax.swing.filechooser.FileFilter#getDescription()
118 public String getDescription()