]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/GenericFileFilter.java
Version 6, October 2008
[GpsPrune.git] / 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 (int i=0; i<inSuffixes.length; i++)
35                         {
36                                 String suffix = inSuffixes[i];
37                                 if (suffix != null)
38                                 {
39                                         suffix = suffix.trim().toLowerCase();
40                                         if (suffix.length() == 3) {
41                                                 _threeCharSuffixes[threeIndex++] = suffix;
42                                         }
43                                         else if (suffix.length() == 4) {
44                                                 _fourCharSuffixes[fourIndex++] = suffix;
45                                         }
46                                 }
47                         }
48                 }
49         }
50
51         /**
52          * Check whether to accept the specified file or not
53          * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
54          */
55         public boolean accept(File inFile)
56         {
57                 return inFile.isDirectory() || acceptFilename(inFile.getName());
58         }
59
60         /**
61          * Check whether to accept the given filename
62          * @param inName name of file
63          * @return true if accepted, false otherwise
64          */
65         public boolean acceptFilename(String inName)
66         {
67                 if (inName != null)
68                 {
69                         int nameLen = inName.length();
70                         if (nameLen > 4)
71                         {
72                                 // Check for three character suffixes
73                                 char currChar = inName.charAt(nameLen - 4);
74                                 if (currChar == '.')
75                                 {
76                                         return acceptFilename(inName.substring(nameLen - 3).toLowerCase(), _threeCharSuffixes);
77                                 }
78                                 // check for four character suffixes
79                                 currChar = inName.charAt(nameLen - 5);
80                                 if (currChar == '.')
81                                 {
82                                         return acceptFilename(inName.substring(nameLen - 4).toLowerCase(), _fourCharSuffixes);
83                                 }
84                         }
85                 }
86                 // Not matched so don't accept
87                 return false;
88         }
89
90         /**
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
95          */
96         public boolean acceptFilename(String inSuffixToCheck, String[] inAllowedSuffixes)
97         {
98                 if (inSuffixToCheck != null && inAllowedSuffixes != null)
99                 {
100                         // Loop over allowed suffixes
101                         for (int i=0; i<inAllowedSuffixes.length; i++)
102                         {
103                                 if (inAllowedSuffixes[i] != null && inSuffixToCheck.equals(inAllowedSuffixes[i]))
104                                 {
105                                         return true;
106                                 }
107                         }
108                 }
109                 // Fallen through so not allowed
110                 return false;
111         }
112
113
114         /**
115          * Get description
116          * @see javax.swing.filechooser.FileFilter#getDescription()
117          */
118         public String getDescription()
119         {
120                 return _filterDesc;
121         }
122
123 }