| ExampleFileView.java |
1 /*
2 * @(#)ExampleFileView.java 1.8 04/07/26
3 *
4 * Copyright (c) 2004 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.8 04/07/26
39 */
40
41 import javax.swing.*;
42 import javax.swing.filechooser.*;
43
44 import java.io.File;
45 import java.util.Hashtable;
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.8 07/26/04
68 * @author Jeff Dinkins
69 */
70 public class ExampleFileView extends FileView {
71 private Hashtable icons = new Hashtable(5);
72 private Hashtable fileDescriptions = new Hashtable(5);
73 private Hashtable typeDescriptions = new Hashtable(5);
74
75 /**
76 * The name of the file. Do nothing special here. Let
77 * the system file view handle this.
78 * @see #setName
79 * @see FileView#getName
80 */
81 public String getName(File f) {
82 return null;
83 }
84
85 /**
86 * Adds a human readable description of the file.
87 */
88 public void putDescription(File f, String fileDescription) {
89 fileDescriptions.put(fileDescription, f);
90 }
91
92 /**
93 * A human readable description of the file.
94 *
95 * @see FileView#getDescription
96 */
97 public String getDescription(File f) {
98 return (String) fileDescriptions.get(f);
99 };
100
101 /**
102 * Adds a human readable type description for files. Based on "dot"
103 * extension strings, e.g: ".gif". Case is ignored.
104 */
105 public void putTypeDescription(String extension, String typeDescription) {
106 typeDescriptions.put(typeDescription, extension);
107 }
108
109 /**
110 * Adds a human readable type description for files of the type of
111 * the passed in file. Based on "dot" extension strings, e.g: ".gif".
112 * Case is ignored.
113 */
114 public void putTypeDescription(File f, String typeDescription) {
115 putTypeDescription(getExtension(f), typeDescription);
116 }
117
118 /**
119 * A human readable description of the type of the file.
120 *
121 * @see FileView#getTypeDescription
122 */
123 public String getTypeDescription(File f) {
124 return (String) typeDescriptions.get(getExtension(f));
125 }
126
127 /**
128 * Conveinience method that returnsa the "dot" extension for the
129 * given file.
130 */
131 public String getExtension(File f) {
132 String name = f.getName();
133 if(name != null) {
134 int extensionIndex = name.lastIndexOf('.');
135 if(extensionIndex < 0) {
136 return null;
137 }
138 return name.substring(extensionIndex+1).toLowerCase();
139 }
140 return null;
141 }
142
143 /**
144 * Adds an icon based on the file type "dot" extension
145 * string, e.g: ".gif". Case is ignored.
146 */
147 public void putIcon(String extension, Icon icon) {
148 icons.put(extension, icon);
149 }
150
151 /**
152 * Icon that reperesents this file. Default implementation returns
153 * null. You might want to override this to return something more
154 * interesting.
155 *
156 * @see FileView#getIcon
157 */
158 public Icon getIcon(File f) {
159 Icon icon = null;
160 String extension = getExtension(f);
161 if(extension != null) {
162 icon = (Icon) icons.get(extension);
163 }
164 return icon;
165 }
166
167 /**
168 * Whether the file is hidden or not. This implementation returns
169 * true if the filename starts with a "."
170 *
171 * @see FileView#isHidden
172 */
173 public Boolean isHidden(File f) {
174 String name = f.getName();
175 if(name != null && !name.equals("") && name.charAt(0) == '.') {
176 return Boolean.TRUE;
177 } else {
178 return Boolean.FALSE;
179 }
180 };
181
182 /**
183 * Whether the directory is traversable or not. Generic implementation
184 * returns true for all directories and special folders.
185 *
186 * You might want to subtype ExampleFileView to do somethimg more interesting,
187 * such as recognize compound documents directories; in such a case you might
188 * return a special icon for the diretory that makes it look like a regular
189 * document, and return false for isTraversable to not allow users to
190 * descend into the directory.
191 *
192 * @see FileView#isTraversable
193 */
194 public Boolean isTraversable(File f) {
195 // if (some_reason) {
196 // return Boolean.FALSE;
197 // }
198 return null; // Use default from FileSystemView
199 };
200
201 }
202