1 /*
2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.filechooser;
27 
28 import java.io.File;
29 import java.util.Locale;
30 
31 /**
32  * An implementation of {@code FileFilter} that filters using a
33  * specified set of extensions. The extension for a file is the
34  * portion of the file name after the last ".". Files whose name does
35  * not contain a "." have no file name extension. File name extension
36  * comparisons are case insensitive.
37  * <p>
38  * The following example creates a
39  * {@code FileNameExtensionFilter} that will show {@code jpg} files:
40  * <pre>
41  * FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
42  * JFileChooser fileChooser = ...;
43  * fileChooser.addChoosableFileFilter(filter);
44  * </pre>
45  *
46  * @see FileFilter
47  * @see javax.swing.JFileChooser#setFileFilter
48  * @see javax.swing.JFileChooser#addChoosableFileFilter
49  * @see javax.swing.JFileChooser#getFileFilter
50  *
51  * @since 1.6
52  */
53 public final class FileNameExtensionFilter extends FileFilter {
54     // Description of this filter.
55     private final String description;
56     // Known extensions.
57     private final String[] extensions;
58     // Cached ext
59     private final String[] lowerCaseExtensions;
60 
61     /**
62      * Creates a {@code FileNameExtensionFilter} with the specified
63      * description and file name extensions. The returned {@code
64      * FileNameExtensionFilter} will accept all directories and any
65      * file with a file name extension contained in {@code extensions}.
66      *
67      * @param description textual description for the filter, may be
68      *                    {@code null}
69      * @param extensions the accepted file name extensions
70      * @throws IllegalArgumentException if extensions is {@code null}, empty,
71      *         contains {@code null}, or contains an empty string
72      * @see #accept
73      */
FileNameExtensionFilter(String description, String... extensions)74     public FileNameExtensionFilter(String description, String... extensions) {
75         if (extensions == null || extensions.length == 0) {
76             throw new IllegalArgumentException(
77                     "Extensions must be non-null and not empty");
78         }
79         this.description = description;
80         this.extensions = new String[extensions.length];
81         this.lowerCaseExtensions = new String[extensions.length];
82         for (int i = 0; i < extensions.length; i++) {
83             if (extensions[i] == null || extensions[i].length() == 0) {
84                 throw new IllegalArgumentException(
85                     "Each extension must be non-null and not empty");
86             }
87             this.extensions[i] = extensions[i];
88             lowerCaseExtensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
89         }
90     }
91 
92     /**
93      * Tests the specified file, returning true if the file is
94      * accepted, false otherwise. True is returned if the extension
95      * matches one of the file name extensions of this {@code
96      * FileFilter}, or the file is a directory.
97      *
98      * @param f the {@code File} to test
99      * @return true if the file is to be accepted, false otherwise
100      */
accept(File f)101     public boolean accept(File f) {
102         if (f != null) {
103             if (f.isDirectory()) {
104                 return true;
105             }
106             // NOTE: we tested implementations using Maps, binary search
107             // on a sorted list and this implementation. All implementations
108             // provided roughly the same speed, most likely because of
109             // overhead associated with java.io.File. Therefor we've stuck
110             // with the simple lightweight approach.
111             String fileName = f.getName();
112             int i = fileName.lastIndexOf('.');
113             if (i > 0 && i < fileName.length() - 1) {
114                 String desiredExtension = fileName.substring(i+1).
115                         toLowerCase(Locale.ENGLISH);
116                 for (String extension : lowerCaseExtensions) {
117                     if (desiredExtension.equals(extension)) {
118                         return true;
119                     }
120                 }
121             }
122         }
123         return false;
124     }
125 
126     /**
127      * The description of this filter. For example: "JPG and GIF Images."
128      *
129      * @return the description of this filter
130      */
getDescription()131     public String getDescription() {
132         return description;
133     }
134 
135     /**
136      * Returns the set of file name extensions files are tested against.
137      *
138      * @return the set of file name extensions files are tested against
139      */
getExtensions()140     public String[] getExtensions() {
141         String[] result = new String[extensions.length];
142         System.arraycopy(extensions, 0, result, 0, extensions.length);
143         return result;
144     }
145 
146     /**
147      * Returns a string representation of the {@code FileNameExtensionFilter}.
148      * This method is intended to be used for debugging purposes,
149      * and the content and format of the returned string may vary
150      * between implementations.
151      *
152      * @return a string representation of this {@code FileNameExtensionFilter}
153      */
toString()154     public String toString() {
155         return super.toString() + "[description=" + getDescription() +
156             " extensions=" + java.util.Arrays.asList(getExtensions()) + "]";
157     }
158 }
159