source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/ExtensionFileFilter.java @ 1

Revision 1, 2.9 KB checked in by bmcguffin, 9 years ago (diff)

2017/07/18: Uploaded entire prototype to SVN repo.

Line 
1package scriptbuilder.gui;
2
3import java.io.File;
4
5import javax.swing.filechooser.FileFilter;
6
7/**
8 * Filters files based on extension. Multiple extensions can be designated as
9 * valid.
10 *
11 * @author Greg Eddington
12 * @author Bryan McGuffin
13 */
14public class ExtensionFileFilter extends FileFilter
15{
16
17    /**
18     * The description of the valid extensions.
19     */
20    String description;
21
22    /**
23     * All valid file extensions.
24     */
25    String extensions[];
26
27    /**
28     * Constructor for a single file extension. Don't include the period in the
29     * entry; that is, if the valid extension is .jpg, the extension parameter
30     * should be "jpg".
31     *
32     * @param description The extension description
33     * @param extension the valid file extension
34     */
35    public ExtensionFileFilter(String description, String extension)
36    {
37        this(description, new String[]
38        {
39            extension
40        });
41    }
42
43    /**
44     * Constructor for an array of one or more file extensions. Don't include
45     * the period in each entry; that is, if a valid extension is .jpg, the
46     * corresponding entry in the array should be "jpg".
47     *
48     * @param description The extension description
49     * @param extensions an array of valid file extensions
50     */
51    public ExtensionFileFilter(String description, String extensions[])
52    {
53        if (description == null)
54        {
55            this.description = extensions[0];
56        }
57        else
58        {
59            this.description = description;
60        }
61        this.extensions = (String[]) extensions.clone();
62        toLower(this.extensions);
63    }
64
65    /**
66     * Transform all extensions to lowercase.
67     *
68     * @param array the array of extension strings
69     */
70    private void toLower(String array[])
71    {
72        for (int i = 0, n = array.length; i < n; i++)
73        {
74            array[i] = array[i].toLowerCase();
75        }
76    }
77
78    /**
79     * Getter for the description of the extension
80     *
81     * @return
82     */
83    @Override
84    public String getDescription()
85    {
86        return description;
87    }
88
89    /**
90     * Determine whether the given file is valid based on its extension.
91     *
92     * @param file The file to be checked
93     * @return true if the extension on the file is in this filter's list of
94     * valid extensions, false otherwise
95     */
96    @Override
97    public boolean accept(File file)
98    {
99        if (file.isDirectory())
100        {
101            return true;
102        }
103        else
104        {
105            String path = file.getAbsolutePath().toLowerCase();
106            for (int i = 0, n = extensions.length; i < n; i++)
107            {
108                String extension = extensions[i];
109                if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.'))
110                {
111                    return true;
112                }
113            }
114        }
115        return false;
116    }
117}
Note: See TracBrowser for help on using the repository browser.