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