home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / FileChooserDemo / ExampleFileView.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  5.0 KB  |  177 lines

  1. /*
  2.  * @(#)ExampleFileView.java    1.7 98/08/26
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.filechooser.*;
  17.  
  18. import java.io.File;
  19. import java.util.Hashtable;
  20.  
  21. /**
  22.  * A convenience implementation of the FileView interface that
  23.  * manages name, icon, traversable, and file type information.
  24.  *
  25.  * This this implemention will work well with file systems that use
  26.  * "dot" extensions to indicate file type. For example: "picture.gif"
  27.  * as a gif image.
  28.  *
  29.  * If the java.io.File ever contains some of this information, such as
  30.  * file type, icon, and hidden file inforation, this implementation may
  31.  * become obsolete. At minimum, it should be rewritten at that time to
  32.  * use any new type information provided by java.io.File
  33.  *
  34.  * Example:
  35.  *    JFileChooser chooser = new JFileChooser();
  36.  *    fileView = new ExampleFileView();
  37.  *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
  38.  *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
  39.  *    chooser.setFileView(fileView);
  40.  *
  41.  * @version 1.7 08/26/98
  42.  * @author Jeff Dinkins
  43.  */
  44. public class ExampleFileView extends FileView {
  45.     private Hashtable icons = new Hashtable(5);
  46.     private Hashtable fileDescriptions = new Hashtable(5);
  47.     private Hashtable typeDescriptions = new Hashtable(5);
  48.  
  49.     /**
  50.      * The name of the file.  Do nothing special here. Let
  51.      * the system file view handle this.
  52.      * @see #setName
  53.      * @see FileView#getName
  54.      */
  55.     public String getName(File f) {
  56.     return null;
  57.     }
  58.  
  59.     /**
  60.      * Adds a human readable description of the file.
  61.      */
  62.     public void putDescription(File f, String fileDescription) {
  63.     fileDescriptions.put(fileDescription, f);
  64.     }
  65.  
  66.     /**
  67.      * A human readable description of the file.
  68.      *
  69.      * @see FileView#getDescription
  70.      */
  71.     public String getDescription(File f) {
  72.     return (String) fileDescriptions.get(f);
  73.     };
  74.  
  75.     /**
  76.      * Adds a human readable type description for files. Based on "dot"
  77.      * extension strings, e.g: ".gif". Case is ignored.
  78.      */
  79.     public void putTypeDescription(String extension, String typeDescription) {
  80.     typeDescriptions.put(typeDescription, extension);
  81.     }
  82.  
  83.     /**
  84.      * Adds a human readable type description for files of the type of
  85.      * the passed in file. Based on "dot" extension strings, e.g: ".gif".
  86.      * Case is ignored.
  87.      */
  88.     public void putTypeDescription(File f, String typeDescription) {
  89.     putTypeDescription(getExtension(f), typeDescription);
  90.     }
  91.  
  92.     /**
  93.      * A human readable description of the type of the file.
  94.      *
  95.      * @see FileView#getTypeDescription
  96.      */
  97.     public String getTypeDescription(File f) {
  98.     return (String) typeDescriptions.get(getExtension(f));
  99.     }
  100.  
  101.     /**
  102.      * Conveinience method that returnsa the "dot" extension for the
  103.      * given file.
  104.      */
  105.     public String getExtension(File f) {
  106.     String name = f.getName();
  107.     if(name != null) {
  108.         int extensionIndex = name.lastIndexOf('.');
  109.         if(extensionIndex < 0) {
  110.         return null;
  111.         }
  112.         return name.substring(extensionIndex+1).toLowerCase();
  113.     }
  114.     return null;
  115.     }
  116.  
  117.     /**
  118.      * Adds an icon based on the file type "dot" extension
  119.      * string, e.g: ".gif". Case is ignored.
  120.      */
  121.     public void putIcon(String extension, Icon icon) {
  122.     icons.put(extension, icon);
  123.     }
  124.  
  125.     /**
  126.      * Icon that reperesents this file. Default implementation returns
  127.      * null. You might want to override this to return something more
  128.      * interesting.
  129.      *
  130.      * @see FileView#getIcon
  131.      */
  132.     public Icon getIcon(File f) {
  133.     Icon icon = null;
  134.     String extension = getExtension(f);
  135.     if(extension != null) {
  136.         icon = (Icon) icons.get(extension);
  137.     }
  138.     return icon;
  139.     }
  140.  
  141.     /**
  142.      * Whether the file is hidden or not. This implementation returns
  143.      * true if the filename starts with a "."
  144.      *
  145.      * @see FileView#isHidden
  146.      */
  147.     public Boolean isHidden(File f) {
  148.     String name = f.getName();
  149.     if(name != null && !name.equals("") && name.charAt(0) == '.') {
  150.         return Boolean.TRUE;
  151.     } else {
  152.         return Boolean.FALSE;
  153.     }
  154.     };
  155.  
  156.     /**
  157.      * Whether the directory is traversable or not. Generic implementation
  158.      * returns true for all directories.
  159.      *
  160.      * You might want to subtype ExampleFileView to do somethimg more interesting,
  161.      * such as recognize compound documents directories; in such a case you might
  162.      * return a special icon for the diretory that makes it look like a regular
  163.      * document, and return false for isTraversable to not allow users to
  164.      * descend into the directory.
  165.      *
  166.      * @see FileView#isTraversable
  167.      */
  168.     public Boolean isTraversable(File f) {
  169.     if(f.isDirectory()) {
  170.         return Boolean.TRUE;
  171.     } else {
  172.         return Boolean.FALSE;
  173.     }
  174.     };
  175.  
  176. }
  177.