KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > ExampleFileView


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 package demo.swingset;
40 import swingwtx.swing.*;
41 import swingwtx.swing.filechooser.*;
42
43 import java.io.File JavaDoc;
44 import java.util.Hashtable JavaDoc;
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 Object JavaDoc {
70     private Hashtable JavaDoc icons = new Hashtable JavaDoc(5);
71     private Hashtable JavaDoc fileDescriptions = new Hashtable JavaDoc(5);
72     private Hashtable JavaDoc typeDescriptions = new Hashtable JavaDoc(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 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(fileDescription, f);
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(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 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      * Conveinience method that returnsa 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 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 JavaDoc isHidden(File JavaDoc f) {
173     String JavaDoc 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 JavaDoc isTraversable(File JavaDoc f) {
194     // if (some_reason) {
195
// return Boolean.FALSE;
196
// }
197
return null; // Use default from FileSystemView
198
};
199
200 }
201
Popular Tags