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