1 /*
2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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 Oracle 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  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
42 import javax.swing.*;
43 import javax.swing.filechooser.*;
44 import java.io.File;
45 import java.util.HashMap;
46 import java.util.Map;
47 
48 
49 /**
50  * A convenience implementation of the FileView interface that
51  * manages name, icon, traversable, and file type information.
52  *
53  * This implementation will work well with file systems that use
54  * "dot" extensions to indicate file type. For example: "picture.gif"
55  * as a gif image.
56  *
57  * If the java.io.File ever contains some of this information, such as
58  * file type, icon, and hidden file inforation, this implementation may
59  * become obsolete. At minimum, it should be rewritten at that time to
60  * use any new type information provided by java.io.File
61  *
62  * Example:
63  *    JFileChooser chooser = new JFileChooser();
64  *    fileView = new ExampleFileView();
65  *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
66  *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
67  *    chooser.setFileView(fileView);
68  *
69  * @author Jeff Dinkins
70  */
71 public class ExampleFileView extends FileView {
72 
73     private final Map<String, Icon> icons = new HashMap<String, Icon>();
74     private final Map<File, String> fileDescriptions =
75             new HashMap<File, String>();
76     private final Map<String, String> typeDescriptions =
77             new HashMap<String, String>();
78 
79     /**
80      * The name of the file.  Do nothing special here. Let
81      * the system file view handle this.
82      * @see FileView#getName
83      */
84     @Override
getName(File f)85     public String getName(File f) {
86         return null;
87     }
88 
89     /**
90      * Adds a human readable description of the file.
91      */
putDescription(File f, String fileDescription)92     public void putDescription(File f, String fileDescription) {
93         fileDescriptions.put(f, fileDescription);
94     }
95 
96     /**
97      * A human readable description of the file.
98      *
99      * @see FileView#getDescription
100      */
101     @Override
getDescription(File f)102     public String getDescription(File f) {
103         return fileDescriptions.get(f);
104     }
105 
106     /**
107      * Adds a human readable type description for files. Based on "dot"
108      * extension strings, e.g: ".gif". Case is ignored.
109      */
putTypeDescription(String extension, String typeDescription)110     public void putTypeDescription(String extension, String typeDescription) {
111         typeDescriptions.put(extension, typeDescription);
112     }
113 
114     /**
115      * Adds a human readable type description for files of the type of
116      * the passed in file. Based on "dot" extension strings, e.g: ".gif".
117      * Case is ignored.
118      */
putTypeDescription(File f, String typeDescription)119     public void putTypeDescription(File f, String typeDescription) {
120         putTypeDescription(getExtension(f), typeDescription);
121     }
122 
123     /**
124      * A human readable description of the type of the file.
125      *
126      * @see FileView#getTypeDescription
127      */
128     @Override
getTypeDescription(File f)129     public String getTypeDescription(File f) {
130         return typeDescriptions.get(getExtension(f));
131     }
132 
133     /**
134      * Convenience method that returns the "dot" extension for the
135      * given file.
136      */
getExtension(File f)137     private String getExtension(File f) {
138         String name = f.getName();
139         if (name != null) {
140             int extensionIndex = name.lastIndexOf('.');
141             if (extensionIndex < 0) {
142                 return null;
143             }
144             return name.substring(extensionIndex + 1).toLowerCase();
145         }
146         return null;
147     }
148 
149     /**
150      * Adds an icon based on the file type "dot" extension
151      * string, e.g: ".gif". Case is ignored.
152      */
putIcon(String extension, Icon icon)153     public void putIcon(String extension, Icon icon) {
154         icons.put(extension, icon);
155     }
156 
157     /**
158      * Icon that reperesents this file. Default implementation returns
159      * null. You might want to override this to return something more
160      * interesting.
161      *
162      * @see FileView#getIcon
163      */
164     @Override
getIcon(File f)165     public Icon getIcon(File f) {
166         Icon icon = null;
167         String extension = getExtension(f);
168         if (extension != null) {
169             icon = icons.get(extension);
170         }
171         return icon;
172     }
173 
174     /**
175      * Whether the directory is traversable or not. Generic implementation
176      * returns true for all directories and special folders.
177      *
178      * You might want to subtype ExampleFileView to do somethimg more interesting,
179      * such as recognize compound documents directories; in such a case you might
180      * return a special icon for the directory that makes it look like a regular
181      * document, and return false for isTraversable to not allow users to
182      * descend into the directory.
183      *
184      * @see FileView#isTraversable
185      */
186     @Override
isTraversable(File f)187     public Boolean isTraversable(File f) {
188         // if (some_reason) {
189         //    return Boolean.FALSE;
190         // }
191         return null;    // Use default from FileSystemView
192     }
193 }
194