jdk/src/share/demo/jfc/FileChooserDemo/ExampleFileView.java
author tbell
Fri, 27 Feb 2009 10:54:11 -0800
changeset 2091 7faffd237305
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
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 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A convenience implementation of the FileView interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * manages name, icon, traversable, and file type information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This implementation will work well with file systems that use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * "dot" extensions to indicate file type. For example: "picture.gif"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * as a gif image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If the java.io.File ever contains some of this information, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * file type, icon, and hidden file inforation, this implementation may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * become obsolete. At minimum, it should be rewritten at that time to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * use any new type information provided by java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    JFileChooser chooser = new JFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    fileView = new ExampleFileView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    chooser.setFileView(fileView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class ExampleFileView extends FileView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final Hashtable<String, Icon> icons = new Hashtable<String, Icon>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final Hashtable<File, String> fileDescriptions = new Hashtable<File, String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final Hashtable<String, String> typeDescriptions = new Hashtable<String, String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The name of the file.  Do nothing special here. Let
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the system file view handle this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see FileView#getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public String getName(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Adds a human readable description of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void putDescription(File f, String fileDescription) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        fileDescriptions.put(f, fileDescription);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * A human readable description of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see FileView#getDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public String getDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return fileDescriptions.get(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Adds a human readable type description for files. Based on "dot"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * extension strings, e.g: ".gif". Case is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void putTypeDescription(String extension, String typeDescription) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        typeDescriptions.put(extension, typeDescription);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Adds a human readable type description for files of the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the passed in file. Based on "dot" extension strings, e.g: ".gif".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Case is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void putTypeDescription(File f, String typeDescription) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        putTypeDescription(getExtension(f), typeDescription);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * A human readable description of the type of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see FileView#getTypeDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public String getTypeDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return typeDescriptions.get(getExtension(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Convenience method that returns the "dot" extension for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * given file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private String getExtension(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if(name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int extensionIndex = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if(extensionIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return name.substring(extensionIndex+1).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return null;
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
     * Adds an icon based on the file type "dot" extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * string, e.g: ".gif". Case is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void putIcon(String extension, Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        icons.put(extension, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Icon that reperesents this file. Default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * null. You might want to override this to return something more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * interesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see FileView#getIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public Icon getIcon(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Icon icon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        String extension = getExtension(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if(extension != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            icon = icons.get(extension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Whether the directory is traversable or not. Generic implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * returns true for all directories and special folders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * You might want to subtype ExampleFileView to do somethimg more interesting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * such as recognize compound documents directories; in such a case you might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * return a special icon for the directory that makes it look like a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * document, and return false for isTraversable to not allow users to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * descend into the directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see FileView#isTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Boolean isTraversable(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // if (some_reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //    return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return null;    // Use default from FileSystemView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}