]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/load/GenericFileFilter.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / load / GenericFileFilter.java
1 package tim.prune.load;
2
3 import java.io.File;
4 import javax.swing.filechooser.FileFilter;
5
6 import tim.prune.I18nManager;
7
8 /**
9  * Class to act as a generic file filter based on file extension
10  */
11 public class GenericFileFilter extends FileFilter
12 {
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;
19
20
21         /**
22          * Constructor
23          * @param inDescKey key for filter description
24          * @param inSuffixes array of allowed 3- and 4-character file suffixes
25          */
26         public GenericFileFilter(String inDescKey, String[] inSuffixes)
27         {
28                 _filterDesc = I18nManager.getText(inDescKey);
29                 if (inSuffixes != null && inSuffixes.length > 0)
30                 {
31                         _threeCharSuffixes = new String[inSuffixes.length];
32                         _fourCharSuffixes = new String[inSuffixes.length];
33                         int threeIndex = 0, fourIndex = 0;
34                         for (String suffix : inSuffixes)
35                         {
36                                 if (suffix != null)
37                                 {
38                                         suffix = suffix.trim().toLowerCase();
39                                         if (suffix.length() == 3) {
40                                                 _threeCharSuffixes[threeIndex++] = suffix;
41                                         }
42                                         else if (suffix.length() == 4) {
43                                                 _fourCharSuffixes[fourIndex++] = suffix;
44                                         }
45                                 }
46                         }
47                 }
48         }
49
50         /**
51          * Check whether to accept the specified file or not
52          * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
53          */
54         public boolean accept(File inFile)
55         {
56                 return inFile.isDirectory() || acceptFilename(inFile.getName());
57         }
58
59         /**
60          * Check whether to accept the given filename
61          * @param inName name of file
62          * @return true if accepted, false otherwise
63          */
64         public boolean acceptFilename(String inName)
65         {
66                 if (inName != null)
67                 {
68                         final int nameLen = inName.length();
69                         if (nameLen > 4)
70                         {
71                                 // Check for three character suffixes
72                                 char currChar = inName.charAt(nameLen - 4);
73                                 if (currChar == '.')
74                                 {
75                                         return acceptFilename(inName.substring(nameLen - 3).toLowerCase(), _threeCharSuffixes);
76                                 }
77                                 // check for four character suffixes
78                                 currChar = inName.charAt(nameLen - 5);
79                                 if (currChar == '.')
80                                 {
81                                         return acceptFilename(inName.substring(nameLen - 4).toLowerCase(), _fourCharSuffixes);
82                                 }
83                         }
84                 }
85                 // Not matched so don't accept
86                 return false;
87         }
88
89         /**
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
94          */
95         private static boolean acceptFilename(String inSuffixToCheck, String[] inAllowedSuffixes)
96         {
97                 if (inSuffixToCheck != null && inAllowedSuffixes != null)
98                 {
99                         // Loop over allowed suffixes
100                         for (String allowed : inAllowedSuffixes)
101                         {
102                                 if (allowed != null && inSuffixToCheck.equals(allowed)) {
103                                         return true;
104                                 }
105                         }
106                 }
107                 // Fallen through so not allowed
108                 return false;
109         }
110
111
112         /**
113          * Get description
114          * @see javax.swing.filechooser.FileFilter#getDescription()
115          */
116         public String getDescription()
117         {
118                 return _filterDesc;
119         }
120 }