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