001 /*
002 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.filechooser;
027
028 import java.io.File;
029 import java.util.Locale;
030
031 /**
032 * An implementation of {@code FileFilter} that filters using a
033 * specified set of extensions. The extension for a file is the
034 * portion of the file name after the last ".". Files whose name does
035 * not contain a "." have no file name extension. File name extension
036 * comparisons are case insensitive.
037 * <p>
038 * The following example creates a
039 * {@code FileNameExtensionFilter} that will show {@code jpg} files:
040 * <pre>
041 * FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
042 * JFileChooser fileChooser = ...;
043 * fileChooser.addChoosableFileFilter(filter);
044 * </pre>
045 *
046 * @see FileFilter
047 * @see javax.swing.JFileChooser#setFileFilter
048 * @see javax.swing.JFileChooser#addChoosableFileFilter
049 * @see javax.swing.JFileChooser#getFileFilter
050 *
051 * @version 1.7 05/05/07
052 * @since 1.6
053 */
054 public final class FileNameExtensionFilter extends FileFilter {
055 // Description of this filter.
056 private final String description;
057 // Known extensions.
058 private final String[] extensions;
059 // Cached ext
060 private final String[] lowerCaseExtensions;
061
062 /**
063 * Creates a {@code FileNameExtensionFilter} with the specified
064 * description and file name extensions. The returned {@code
065 * FileNameExtensionFilter} will accept all directories and any
066 * file with a file name extension contained in {@code extensions}.
067 *
068 * @param description textual description for the filter, may be
069 * {@code null}
070 * @param extensions the accepted file name extensions
071 * @throws IllegalArgumentException if extensions is {@code null}, empty,
072 * contains {@code null}, or contains an empty string
073 * @see #accept
074 */
075 public FileNameExtensionFilter(String description,
076 String... extensions) {
077 if (extensions == null || extensions.length == 0) {
078 throw new IllegalArgumentException(
079 "Extensions must be non-null and not empty");
080 }
081 this .description = description;
082 this .extensions = new String[extensions.length];
083 this .lowerCaseExtensions = new String[extensions.length];
084 for (int i = 0; i < extensions.length; i++) {
085 if (extensions[i] == null || extensions[i].length() == 0) {
086 throw new IllegalArgumentException(
087 "Each extension must be non-null and not empty");
088 }
089 this .extensions[i] = extensions[i];
090 lowerCaseExtensions[i] = extensions[i]
091 .toLowerCase(Locale.ENGLISH);
092 }
093 }
094
095 /**
096 * Tests the specified file, returning true if the file is
097 * accepted, false otherwise. True is returned if the extension
098 * matches one of the file name extensions of this {@code
099 * FileFilter}, or the file is a directory.
100 *
101 * @param f the {@code File} to test
102 * @return true if the file is to be accepted, false otherwise
103 */
104 public boolean accept(File f) {
105 if (f != null) {
106 if (f.isDirectory()) {
107 return true;
108 }
109 // NOTE: we tested implementations using Maps, binary search
110 // on a sorted list and this implementation. All implementations
111 // provided roughly the same speed, most likely because of
112 // overhead associated with java.io.File. Therefor we've stuck
113 // with the simple lightweight approach.
114 String fileName = f.getName();
115 int i = fileName.lastIndexOf('.');
116 if (i > 0 && i < fileName.length() - 1) {
117 String desiredExtension = fileName.substring(i + 1)
118 .toLowerCase(Locale.ENGLISH);
119 for (String extension : lowerCaseExtensions) {
120 if (desiredExtension.equals(extension)) {
121 return true;
122 }
123 }
124 }
125 }
126 return false;
127 }
128
129 /**
130 * The description of this filter. For example: "JPG and GIF Images."
131 *
132 * @return the description of this filter
133 */
134 public String getDescription() {
135 return description;
136 }
137
138 /**
139 * Returns the set of file name extensions files are tested against.
140 *
141 * @return the set of file name extensions files are tested against
142 */
143 public String[] getExtensions() {
144 String[] result = new String[extensions.length];
145 System.arraycopy(extensions, 0, result, 0, extensions.length);
146 return result;
147 }
148
149 /**
150 * Returns a string representation of the {@code FileNameExtensionFilter}.
151 * This method is intended to be used for debugging purposes,
152 * and the content and format of the returned string may vary
153 * between implementations.
154 *
155 * @return a string representation of this {@code FileNameExtensionFilter}
156 */
157 public String toString() {
158 return super .toString() + "[description=" + getDescription()
159 + " extensions="
160 + java.util.Arrays.asList(getExtensions()) + "]";
161 }
162 }
|