KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > ExtensionsFileFilter


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jaggenerator;
19
20 import javax.swing.filechooser.*;
21 import java.io.File JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * A FileFilter that accepts/rejects files depending on their extension (part of filename following the last 'dot').
28  *
29  * @author Michael O'Connor - Finalist IT Group
30  */

31 public class ExtensionsFileFilter extends FileFilter {
32
33    private static final char DOT = '.';
34    private final ArrayList JavaDoc acceptFilter = new ArrayList JavaDoc();
35    private String JavaDoc description;
36
37    /**
38     * Creates a filter with one extension.
39     * @param acceptableExtension
40     */

41    public ExtensionsFileFilter(String JavaDoc acceptableExtension) {
42       acceptFilter.add(acceptableExtension);
43       description = "*." + acceptableExtension;
44    }
45
46
47    /**
48     * Creates a filter with possibly more than one extension.
49     * @param acceptableExtensions
50     */

51    public ExtensionsFileFilter(String JavaDoc[] acceptableExtensions) {
52       acceptFilter.addAll(Arrays.asList(acceptableExtensions));
53
54       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55       Iterator JavaDoc i = acceptFilter.iterator();
56       while (i.hasNext()) {
57          String JavaDoc extension = (String JavaDoc) i.next();
58          sb.append("*.");
59          sb.append(extension);
60          if (i.hasNext()) {
61             sb.append(", ");
62          }
63       }
64       description = sb.toString();
65    }
66
67    /** @see {@link FileFilter#accept}. */
68    public boolean accept(File JavaDoc file) {
69       if (file.isDirectory()) return true;
70       String JavaDoc filename = file.toString().toLowerCase();
71       int lastDotPos = filename.lastIndexOf(DOT) + 1;
72       if (lastDotPos != 0) {
73          String JavaDoc extension = filename.substring(lastDotPos);
74          return acceptFilter.contains(extension);
75       }
76
77       return false; //maybe this should be true..? if a file has no extension, we don't know what it is...
78
}
79
80    /** @see {@link FileFilter#getDescription}. */
81    public String JavaDoc getDescription() {
82       return description;
83    }
84 }
Popular Tags