View Javadoc

1   package com.melloware.jukes.file.filter;
2   
3   import java.io.File;
4   
5   import javax.swing.filechooser.FileFilter;
6   
7   import org.apache.commons.io.FilenameUtils;
8   import org.apache.commons.io.filefilter.IOFileFilter;
9   import org.apache.commons.io.filefilter.SuffixFileFilter;
10  import org.apache.commons.io.filefilter.TrueFileFilter;
11  import org.apache.commons.io.filefilter.FileFilterUtils;
12  
13  /**
14   * Provides FilterFactory for file and directory choosers based on Jakarta
15   * Commons-IO helper package.
16   * <p>
17   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
18   * @author Emil A. Lefkof III <info@melloware.com>
19   * @version 4.0
20   */
21  public final class FilterFactory {
22  	
23  	public static final IOFileFilter MUSIC_FILTER = musicIOFilter();
24  	public static final IOFileFilter IMAGE_FILTER = imageIOFilter();
25  	public static final IOFileFilter TRUE_FILTER = TrueFileFilter.INSTANCE;
26  
27      /**
28       * Default constructor.
29       */
30      private FilterFactory() {
31          super();
32      }
33  
34      /**
35       * Return a CsvFilter for a JFileChooser.
36       * <p>
37       * @return the FileFilter to use.
38       */
39      public static FileFilter csvFileFilter() {
40          return new CsvFilter();
41      }
42  
43      /**
44       * Return a csvIOFilter to filter out csv files of a directory.
45       * <p>
46       * @return the IOFilter
47       */
48      public static IOFileFilter csvIOFilter() {
49          return new SuffixFileFilter(CsvFilter.EXTENSIONS);
50      }
51  
52      /**
53       * Static method to force a certain extenion on a file.
54       * <p>
55       * @param aFile the file to check extension for
56       * @return the new file or the original if no changes made
57       */
58      public static File forceCsvExtension(final File aFile) {
59          File file = aFile;
60          if (!FilenameUtils.isExtension(file.getAbsolutePath(), CsvFilter.EXTENSIONS)) {
61              file = new File(file.getAbsolutePath() + "." + CsvFilter.CSV);
62          }
63          return file;
64      }
65  
66      /**
67       * Static method to force a certain extenion on a file.
68       * <p>
69       * @param aFile the file to check extension for
70       * @return the new file or the original if no changes made
71       */
72      public static File forceM3uExtension(final File aFile) {
73          File file = aFile;
74          if (!FilenameUtils.isExtension(file.getAbsolutePath(), M3uFilter.EXTENSIONS)) {
75              file = new File(file.getAbsolutePath() + "." + M3uFilter.M3U);
76          }
77          return file;
78      }
79  
80      /**
81       * Static method to force a certain extenion on a file.
82       * <p>
83       * @param aFile the file to check extension for
84       * @return the new file or the original if no changes made
85       */
86      public static File forcePdfExtension(final File aFile) {
87          File file = aFile;
88          if (!FilenameUtils.isExtension(file.getAbsolutePath(), PdfFilter.EXTENSIONS)) {
89              file = new File(file.getAbsolutePath() + "." + PdfFilter.PDF);
90          }
91          return file;
92      }
93  
94      /**
95       * Static method to force a certain extenion on a file.
96       * <p>
97       * @param aFile the file to check extension for
98       * @return the new file or the original if no changes made
99       */
100     public static File forcePlaylistExtension(final File aFile) {
101         File file = aFile;
102         if (!FilenameUtils.isExtension(file.getAbsolutePath(), PlaylistFilter.EXTENSIONS)) {
103             file = new File(file.getAbsolutePath() + "." + PlaylistFilter.M3U);
104         }
105         return file;
106     }
107 
108     /**
109      * Static method to force a certain extenion on a file.
110      * <p>
111      * @param aFile the file to check extension for
112      * @return the new file or the original if no changes made
113      */
114     public static File forceTextExtension(final File aFile) {
115         File file = aFile;
116         if (!FilenameUtils.isExtension(file.getAbsolutePath(), TextFilter.EXTENSIONS)) {
117             file = new File(file.getAbsolutePath() + "." + TextFilter.TXT);
118         }
119         return file;
120     }
121 
122     /**
123      * Static method to force a certain extenion on a file.
124      * <p>
125      * @param aFile the file to check extension for
126      * @return the new file or the original if no changes made
127      */
128     public static File forceXmlExtension(final File aFile) {
129         File file = aFile;
130         if (!FilenameUtils.isExtension(file.getAbsolutePath(), XmlFilter.EXTENSIONS)) {
131             file = new File(file.getAbsolutePath() + "." + XmlFilter.XML);
132         }
133         return file;
134     }
135 
136     /**
137      * Static method to force a certain extenion on a file.
138      * <p>
139      * @param aFile the file to check extension for
140      * @return the new file or the original if no changes made
141      */
142     public static File forceXspfExtension(final File aFile) {
143         File file = aFile;
144         if (!FilenameUtils.isExtension(file.getAbsolutePath(), XspfFilter.EXTENSIONS)) {
145             file = new File(file.getAbsolutePath() + "." + XspfFilter.XSPF);
146         }
147         return file;
148     }
149 
150     /**
151      * Return a ImageFileFilter for a JFileChooser.
152      * <p>
153      * @return the FileFilter to use.
154      */
155     public static FileFilter imageFileFilter() {
156         return new ImageFilter();
157     }
158 
159     /**
160      * Return a imageIOFilter to filter out images of a directory.
161      * <p>
162      * @return the IOFilter
163      */
164     public static IOFileFilter imageIOFilter() {
165         return new SuffixFileFilter(ImageFilter.EXTENSIONS);
166     }
167 
168     /**
169      * Return a m3uFileFilter for a JFileChooser.
170      * <p>
171      * @return the FileFilter to use.
172      */
173     public static FileFilter m3uFileFilter() {
174         return new M3uFilter();
175     }
176 
177     /**
178      * Return a m3uIOFilter to filter out m3u files of a directory.
179      * <p>
180      * @return the IOFilter
181      */
182     public static IOFileFilter m3uIOFilter() {
183         return new SuffixFileFilter(M3uFilter.EXTENSIONS);
184     }
185 
186     /**
187      * Return a MusicFileFilter for a JFileChooser.
188      * <p>
189      * @return the FileFilter to use.
190      */
191     public static FileFilter musicFileFilter() {
192         return new MusicFilter();
193     }
194 
195     /**
196      * Return a MusicIOFilter to filter out MP3's of a directory.
197      * <p>
198      * @return the IOFilter
199      */
200     public static IOFileFilter musicIOFilter() {
201         return new SuffixFileFilter(MusicFilter.EXTENSIONS);
202     }
203 
204     /**
205      * Return a PdfFilter for a JFileChooser.
206      * <p>
207      * @return the FileFilter to use.
208      */
209     public static FileFilter pdfFileFilter() {
210         return new PdfFilter();
211     }
212 
213     /**
214      * Return a pdfIOFilter to filter out pdf files of a directory.
215      * <p>
216      * @return the IOFilter
217      */
218     public static IOFileFilter pdfIOFilter() {
219         return new SuffixFileFilter(PdfFilter.EXTENSIONS);
220     }
221 
222     /**
223      * Return a PlaylistFileFilter for a JFileChooser.
224      * <p>
225      * @return the FileFilter to use.
226      */
227     public static FileFilter playlistFileFilter() {
228         return new PlaylistFilter();
229     }
230 
231     /**
232      * Return a playlistIOFilter to filter out playlists of a directory.
233      * <p>
234      * @return the IOFilter
235      */
236     public static IOFileFilter playlistIOFilter() {
237         return new SuffixFileFilter(PlaylistFilter.EXTENSIONS);
238     }
239 
240     /**
241      * Return a TextFileFilter for a JFileChooser.
242      * <p>
243      * @return the FileFilter to use.
244      */
245     public static FileFilter textFileFilter() {
246         return new TextFilter();
247     }
248 
249     /**
250      * Return a textIOFilter to filter out text files of a directory.
251      * <p>
252      * @return the IOFilter
253      */
254     public static IOFileFilter textIOFilter() {
255         return new SuffixFileFilter(TextFilter.EXTENSIONS);
256     }
257 
258     /**
259      * Return a XmlFilter for a JFileChooser.
260      * <p>
261      * @return the FileFilter to use.
262      */
263     public static FileFilter xmlFileFilter() {
264         return new XmlFilter();
265     }
266 
267     /**
268      * Return a textIOFilter to filter out xml files of a directory.
269      * <p>
270      * @return the IOFilter
271      */
272     public static IOFileFilter xmltIOFilter() {
273         return new SuffixFileFilter(XmlFilter.EXTENSIONS);
274     }
275 
276     /**
277      * Return a xspfFileFilter for a JFileChooser.
278      * <p>
279      * @return the FileFilter to use.
280      */
281     public static FileFilter xspfFileFilter() {
282         return new XspfFilter();
283     }
284 
285     /**
286      * Return a xspfIOFilter to filter out xspf files of a directory.
287      * <p>
288      * @return the IOFilter
289      */
290     public static IOFileFilter xspfIOFilter() {
291         return new SuffixFileFilter(XspfFilter.EXTENSIONS);
292     }
293     
294     //AZ Development
295     /**
296      * Static method to force a certain extenion on a file.
297      * <p>
298      * @param aFile the file to check extension for
299      * @return the new file or the original if no changes made
300      */
301     public static File forceLstExtension(final File aFile) {
302         File file = aFile;
303         if (!FilenameUtils.isExtension(file.getAbsolutePath(), lstFilter.EXTENSIONS)) {
304             file = new File(file.getAbsolutePath() + "." + lstFilter.lst);
305         }
306         return file;
307     }
308 
309     /**
310      * Return a DisclistFileFilter for a JFileChooser.
311      * <p>
312      * @return the FileFilter to use.
313      */
314     public static FileFilter disclistFileFilter() {
315         return new DisclistFilter();
316     }
317  
318     /**AZ:
319      * Return a dirFilter to filter out sub-directories of a directory.
320      * <p>
321      * @return the FileFilter
322      */
323     public static FileFilter dirFilter() {
324         return new dirFilter();
325     }
326     
327     /**AZ:
328      * Return a dirFilter to filter out sub-directories of a directory.
329      * <p>
330      * @return the IOFileFilter
331      */
332     public static IOFileFilter dirIOFilter() {
333     	return FileFilterUtils.directoryFileFilter();
334     }
335     
336 }