1   /*
2    * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    * 
8    * -Redistributions of source code must retain the above copyright
9    *  notice, this list of conditions and the following disclaimer.
10   * 
11   * -Redistribution in binary form must reproduct the above copyright
12   *  notice, this list of conditions and the following disclaimer in
13   *  the documentation and/or other materials provided with the distribution.
14   * 
15   * Neither the name of Sun Microsystems, Inc. or the names of contributors
16   * may be used to endorse or promote products derived from this software
17   * without specific prior written permission.
18   * 
19   * This software is provided "AS IS," without a warranty of any kind. ALL
20   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23   * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24   * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29   * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30   * 
31   * You acknowledge that Software is not designed, licensed or intended for
32   * use in the design, construction, operation or maintenance of any nuclear
33   * facility.
34   */
35  
36  /*
37   * @(#)ExampleFileView.java 1.6 03/01/23
38   */
39  
40  import javax.swing.*;
41  import javax.swing.filechooser.*;
42  
43  import java.io.File;
44  import java.util.Hashtable;
45  
46  /**
47   * A convenience implementation of the FileView interface that
48   * manages name, icon, traversable, and file type information.
49   *
50   * This this implemention will work well with file systems that use
51   * "dot" extensions to indicate file type. For example: "picture.gif"
52   * as a gif image.
53   *
54   * If the java.io.File ever contains some of this information, such as
55   * file type, icon, and hidden file inforation, this implementation may
56   * become obsolete. At minimum, it should be rewritten at that time to
57   * use any new type information provided by java.io.File
58   *
59   * Example:
60   *    JFileChooser chooser = new JFileChooser();
61   *    fileView = new ExampleFileView();
62   *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
63   *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
64   *    chooser.setFileView(fileView);
65   *
66   * @version 1.6 01/23/03
67   * @author Jeff Dinkins
68   */
69  public class ExampleFileView extends FileView {
70      private Hashtable icons = new Hashtable(5);
71      private Hashtable fileDescriptions = new Hashtable(5);
72      private Hashtable typeDescriptions = new Hashtable(5);
73  
74      /**
75       * The name of the file.  Do nothing special here. Let
76       * the system file view handle this.
77       * @see #setName
78       * @see FileView#getName
79       */
80      public String getName(File f) {
81      return null;
82      }
83  
84      /**
85       * Adds a human readable description of the file.
86       */
87      public void putDescription(File f, String fileDescription) {
88      fileDescriptions.put(fileDescription, f);
89      }
90  
91      /**
92       * A human readable description of the file.
93       *
94       * @see FileView#getDescription
95       */
96      public String getDescription(File f) {
97      return (String) fileDescriptions.get(f);
98      };
99  
100     /**
101      * Adds a human readable type description for files. Based on "dot"
102      * extension strings, e.g: ".gif". Case is ignored.
103      */
104     public void putTypeDescription(String extension, String typeDescription) {
105     typeDescriptions.put(typeDescription, extension);
106     }
107 
108     /**
109      * Adds a human readable type description for files of the type of
110      * the passed in file. Based on "dot" extension strings, e.g: ".gif".
111      * Case is ignored.
112      */
113     public void putTypeDescription(File f, String typeDescription) {
114     putTypeDescription(getExtension(f), typeDescription);
115     }
116 
117     /**
118      * A human readable description of the type of the file.
119      *
120      * @see FileView#getTypeDescription
121      */
122     public String getTypeDescription(File f) {
123     return (String) typeDescriptions.get(getExtension(f));
124     }
125 
126     /**
127      * Conveinience method that returnsa the "dot" extension for the
128      * given file.
129      */
130     public String getExtension(File f) {
131     String name = f.getName();
132     if(name != null) {
133         int extensionIndex = name.lastIndexOf('.');
134         if(extensionIndex < 0) {
135         return null;
136         }
137         return name.substring(extensionIndex+1).toLowerCase();
138     }
139     return null;
140     }
141 
142     /**
143      * Adds an icon based on the file type "dot" extension
144      * string, e.g: ".gif". Case is ignored.
145      */
146     public void putIcon(String extension, Icon icon) {
147     icons.put(extension, icon);
148     }
149 
150     /**
151      * Icon that reperesents this file. Default implementation returns
152      * null. You might want to override this to return something more
153      * interesting.
154      *
155      * @see FileView#getIcon
156      */
157     public Icon getIcon(File f) {
158     Icon icon = null;
159     String extension = getExtension(f);
160     if(extension != null) {
161         icon = (Icon) icons.get(extension);
162     }
163     return icon;
164     }
165 
166     /**
167      * Whether the file is hidden or not. This implementation returns
168      * true if the filename starts with a "."
169      *
170      * @see FileView#isHidden
171      */
172     public Boolean isHidden(File f) {
173     String name = f.getName();
174     if(name != null && !name.equals("") && name.charAt(0) == '.') {
175         return Boolean.TRUE;
176     } else {
177         return Boolean.FALSE;
178     }
179     };
180 
181     /**
182      * Whether the directory is traversable or not. Generic implementation
183      * returns true for all directories and special folders.
184      *
185      * You might want to subtype ExampleFileView to do somethimg more interesting,
186      * such as recognize compound documents directories; in such a case you might
187      * return a special icon for the diretory that makes it look like a regular
188      * document, and return false for isTraversable to not allow users to
189      * descend into the directory.
190      *
191      * @see FileView#isTraversable
192      */
193     public Boolean isTraversable(File f) {
194     // if (some_reason) {
195     //    return Boolean.FALSE;
196     // }
197     return null;    // Use default from FileSystemView
198     };
199 
200 }
201