KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > filechooser > FileNameExtensionFilter


1 /*
2  * @(#)FileNameExtensionFilter.java 1.1 06/02/02
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.filechooser;
9
10 import java.io.File JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 /**
14  * An implementation of {@code FileFilter} that filters using a
15  * specified set of extensions. The extension for a file is the
16  * portion of the file name after the last ".". Files whose name does
17  * not contain a "." have no file name extension. File name extension
18  * comparisons are case insensitive.
19  * <p>
20  * The following example creates a
21  * {@code FileNameExtensionFilter} that will show {@code jpg} files:
22  * <pre>
23  * FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
24  * JFileChooser fileChooser = ...;
25  * fileChooser.addChoosableFileFilter(filter);
26  * </pre>
27  *
28  * @see FileFilter
29  * @see javax.swing.JFileChooser#setFileFilter
30  * @see javax.swing.JFileChooser#addChoosableFileFilter
31  * @see javax.swing.JFileChooser#getFileFilter
32  *
33  * @version 1.1 02/02/06
34  * @since 1.6
35  */

36 public final class FileNameExtensionFilter extends FileFilter JavaDoc {
37     // Description of this filter.
38
private final String JavaDoc description;
39     // Known extensions.
40
private final String JavaDoc[] extensions;
41     // Cached ext
42
private final String JavaDoc[] lowerCaseExtensions;
43
44     /**
45      * Creates a {@code FileNameExtensionFilter} with the specified
46      * description and file name extensions. The returned {@code
47      * FileNameExtensionFilter} will accept all directories and any
48      * file with a file name extension contained in {@code extensions}.
49      *
50      * @param description textual description for the filter, may be
51      * {@code null}
52      * @param extensions the accepted file name extensions
53      * @throws IllegalArgumentException if extensions is {@code null}, empty,
54      * contains {@code null}, or contains an empty string
55      * @see #accept
56      */

57     public FileNameExtensionFilter(String JavaDoc description, String JavaDoc... extensions) {
58         if (extensions == null || extensions.length == 0) {
59             throw new IllegalArgumentException JavaDoc(
60                     "Extensions must be non-null and not empty");
61         }
62         this.description = description;
63         this.extensions = new String JavaDoc[extensions.length];
64         this.lowerCaseExtensions = new String JavaDoc[extensions.length];
65         for (int i = 0; i < extensions.length; i++) {
66             if (extensions[i] == null || extensions[i].length() == 0) {
67                 throw new IllegalArgumentException JavaDoc(
68                     "Each extension must be non-null and not empty");
69             }
70             this.extensions[i] = extensions[i];
71             lowerCaseExtensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
72         }
73     }
74
75     /**
76      * Tests the specified file, returning true if the file is
77      * accepted, false otherwise. True is returned if the extension
78      * matches one of the file name extensions of this {@code
79      * FileFilter}, or the file is a directory.
80      *
81      * @param f the {@code File} to test
82      * @return true if the file is to be accepted, false otherwise
83      */

84     public boolean accept(File JavaDoc f) {
85         if (f != null) {
86             if (f.isDirectory()) {
87                 return true;
88             }
89             // NOTE: we tested implementations using Maps, binary search
90
// on a sorted list and this implementation. All implementations
91
// provided roughly the same speed, most likely because of
92
// overhead associated with java.io.File. Therefor we've stuck
93
// with the simple lightweight approach.
94
String JavaDoc fileName = f.getName();
95         int i = fileName.lastIndexOf('.');
96         if (i > 0 && i < fileName.length() - 1) {
97                 String JavaDoc desiredExtension = fileName.substring(i+1).
98                         toLowerCase(Locale.ENGLISH);
99                 for (String JavaDoc extension : lowerCaseExtensions) {
100                     if (desiredExtension.equals(extension)) {
101                         return true;
102                     }
103                 }
104         }
105         }
106         return false;
107     }
108
109     /**
110      * The description of this filter. For example: "JPG and GIF Images."
111      *
112      * @return the description of this filter
113      */

114     public String JavaDoc getDescription() {
115         return description;
116     }
117
118     /**
119      * Returns the set of file name extensions files are tested against.
120      *
121      * @return the set of file name extensions files are tested against
122      */

123     public String JavaDoc[] getExtensions() {
124         String JavaDoc[] result = new String JavaDoc[extensions.length];
125         System.arraycopy(extensions, 0, result, 0, extensions.length);
126         return result;
127     }
128
129     /**
130      * Returns a string representation of the {@code FileNameExtensionFilter}.
131      * This method is intended to be used for debugging purposes,
132      * and the content and format of the returned string may vary
133      * between implementations.
134      *
135      * @return a string representation of this {@code FileNameExtensionFilter}
136      */

137     public String JavaDoc toString() {
138         return super.toString() + "[description=" + getDescription() +
139             " extensions=" + java.util.Arrays.asList(getExtensions()) + "]";
140     }
141 }
142
Popular Tags