KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ExampleFileView


1 /*
2  * @(#)ExampleFileView.java 1.11 05/11/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ExampleFileView.java 1.11 05/11/30
39  */

40
41 import javax.swing.*;
42 import javax.swing.filechooser.*;
43
44 import java.io.File JavaDoc;
45 import java.util.Hashtable JavaDoc;
46
47 /**
48  * A convenience implementation of the FileView interface that
49  * manages name, icon, traversable, and file type information.
50  *
51  * This this implemention will work well with file systems that use
52  * "dot" extensions to indicate file type. For example: "picture.gif"
53  * as a gif image.
54  *
55  * If the java.io.File ever contains some of this information, such as
56  * file type, icon, and hidden file inforation, this implementation may
57  * become obsolete. At minimum, it should be rewritten at that time to
58  * use any new type information provided by java.io.File
59  *
60  * Example:
61  * JFileChooser chooser = new JFileChooser();
62  * fileView = new ExampleFileView();
63  * fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
64  * fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
65  * chooser.setFileView(fileView);
66  *
67  * @version 1.11 11/30/05
68  * @author Jeff Dinkins
69  */

70 public class ExampleFileView extends FileView {
71     private Hashtable JavaDoc icons = new Hashtable JavaDoc(5);
72     private Hashtable JavaDoc fileDescriptions = new Hashtable JavaDoc(5);
73     private Hashtable JavaDoc typeDescriptions = new Hashtable JavaDoc(5);
74
75     /**
76      * The name of the file. Do nothing special here. Let
77      * the system file view handle this.
78      * @see FileView#getName
79      */

80     public String JavaDoc getName(File JavaDoc f) {
81     return null;
82     }
83
84     /**
85      * Adds a human readable description of the file.
86      */

87     public void putDescription(File JavaDoc f, String JavaDoc fileDescription) {
88     fileDescriptions.put(f, fileDescription);
89     }
90
91     /**
92      * A human readable description of the file.
93      *
94      * @see FileView#getDescription
95      */

96     public String JavaDoc getDescription(File JavaDoc f) {
97     return (String JavaDoc) 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 JavaDoc extension, String JavaDoc typeDescription) {
105     typeDescriptions.put(extension, typeDescription);
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 JavaDoc f, String JavaDoc 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 JavaDoc getTypeDescription(File JavaDoc f) {
123     return (String JavaDoc) typeDescriptions.get(getExtension(f));
124     }
125
126     /**
127      * Convenience method that returns the "dot" extension for the
128      * given file.
129      */

130     public String JavaDoc getExtension(File JavaDoc f) {
131     String JavaDoc 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 JavaDoc 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 JavaDoc f) {
158     Icon icon = null;
159     String JavaDoc extension = getExtension(f);
160     if(extension != null) {
161         icon = (Icon) icons.get(extension);
162     }
163     return icon;
164     }
165
166     /**
167      * Whether the directory is traversable or not. Generic implementation
168      * returns true for all directories and special folders.
169      *
170      * You might want to subtype ExampleFileView to do somethimg more interesting,
171      * such as recognize compound documents directories; in such a case you might
172      * return a special icon for the directory that makes it look like a regular
173      * document, and return false for isTraversable to not allow users to
174      * descend into the directory.
175      *
176      * @see FileView#isTraversable
177      */

178     public Boolean JavaDoc isTraversable(File JavaDoc f) {
179     // if (some_reason) {
180
// return Boolean.FALSE;
181
// }
182
return null; // Use default from FileSystemView
183
};
184
185 }
186
Popular Tags