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