Source code: ledestin/swing/ExampleFileFilter.java
1 package ledestin.swing;
2
3 import java.io.File;
4 import java.util.Hashtable;
5 import java.util.Enumeration;
6 import javax.swing.*;
7 import javax.swing.filechooser.*;
8
9 /**
10 * A convenience implementation of FileFilter that filters out
11 * all files except for those type extensions that it knows about.
12 *
13 * Extensions are of the type ".foo", which is typically found on
14 * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
15 *
16 * Example - create a new filter that filerts out all files
17 * but gif and jpg image files:
18 *
19 * JFileChooser chooser = new JFileChooser();
20 * ExampleFileFilter filter = new ExampleFileFilter(
21 * new String{"gif", "jpg"}, "JPEG & GIF Images")
22 * chooser.addChoosableFileFilter(filter);
23 * chooser.showOpenDialog(this);
24 *
25 * @version 1.9 04/23/99
26 * @author Jeff Dinkins
27 */
28 public class ExampleFileFilter extends FileFilter {
29
30 private static String TYPE_UNKNOWN = "Type Unknown";
31 private static String HIDDEN_FILE = "Hidden File";
32
33 private Hashtable filters = null;
34 private String description = null;
35 private String fullDescription = null;
36 private boolean useExtensionsInDescription = true;
37
38 /**
39 * Creates a file filter. If no filters are added, then all
40 * files are accepted.
41 *
42 * @see #addExtension
43 */
44 public ExampleFileFilter() {
45 this.filters = new Hashtable();
46 }
47
48 /**
49 * Creates a file filter that accepts files with the given extension.
50 * Example: new ExampleFileFilter("jpg");
51 *
52 * @see #addExtension
53 */
54 public ExampleFileFilter(String extension) {
55 this(extension,null);
56 }
57
58 /**
59 * Creates a file filter that accepts the given file type.
60 * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
61 *
62 * Note that the "." before the extension is not needed. If
63 * provided, it will be ignored.
64 *
65 * @see #addExtension
66 */
67 public ExampleFileFilter(String extension, String description) {
68 this();
69 if(extension!=null) addExtension(extension);
70 if(description!=null) setDescription(description);
71 }
72
73 /**
74 * Creates a file filter from the given string array.
75 * Example: new ExampleFileFilter(String {"gif", "jpg"});
76 *
77 * Note that the "." before the extension is not needed adn
78 * will be ignored.
79 *
80 * @see #addExtension
81 */
82 public ExampleFileFilter(String[] filters) {
83 this(filters, null);
84 }
85
86 /**
87 * Creates a file filter from the given string array and description.
88 * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
89 *
90 * Note that the "." before the extension is not needed and will be ignored.
91 *
92 * @see #addExtension
93 */
94 public ExampleFileFilter(String[] filters, String description) {
95 this();
96 for (int i = 0; i < filters.length; i++) {
97 // add filters one by one
98 addExtension(filters[i]);
99 }
100 if(description!=null) setDescription(description);
101 }
102
103 /**
104 * Return true if this file should be shown in the directory pane,
105 * false if it shouldn't.
106 *
107 * Files that begin with "." are ignored.
108 *
109 * @see #getExtension
110 * @see FileFilter#accepts
111 */
112 public boolean accept(File f) {
113 if(f != null) {
114 if(f.isDirectory()) {
115 return true;
116 }
117 String extension = getExtension(f);
118 if(extension != null && filters.get(getExtension(f)) != null) {
119 return true;
120 };
121 }
122 return false;
123 }
124
125 /**
126 * Return the extension portion of the file's name .
127 *
128 * @see #getExtension
129 * @see FileFilter#accept
130 */
131 public String getExtension(File f) {
132 if(f != null) {
133 String filename = f.getName();
134 int i = filename.lastIndexOf('.');
135 if(i>0 && i<filename.length()-1) {
136 return filename.substring(i+1).toLowerCase();
137 };
138 }
139 return null;
140 }
141
142 /**
143 * Adds a filetype "dot" extension to filter against.
144 *
145 * For example: the following code will create a filter that filters
146 * out all files except those that end in ".jpg" and ".tif":
147 *
148 * ExampleFileFilter filter = new ExampleFileFilter();
149 * filter.addExtension("jpg");
150 * filter.addExtension("tif");
151 *
152 * Note that the "." before the extension is not needed and will be ignored.
153 */
154 public void addExtension(String extension) {
155 if(filters == null) {
156 filters = new Hashtable(5);
157 }
158 filters.put(extension.toLowerCase(), this);
159 fullDescription = null;
160 }
161
162
163 /**
164 * Returns the human readable description of this filter. For
165 * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
166 *
167 * @see setDescription
168 * @see setExtensionListInDescription
169 * @see isExtensionListInDescription
170 * @see FileFilter#getDescription
171 */
172 public String getDescription() {
173 if(fullDescription == null) {
174 if(description == null || isExtensionListInDescription()) {
175 fullDescription = description==null ? "(" : description + " (";
176 // build the description from the extension list
177 Enumeration extensions = filters.keys();
178 if(extensions != null) {
179 fullDescription += "." + (String) extensions.nextElement();
180 while (extensions.hasMoreElements()) {
181 fullDescription += ", " + (String) extensions.nextElement();
182 }
183 }
184 fullDescription += ")";
185 } else {
186 fullDescription = description;
187 }
188 }
189 return fullDescription;
190 }
191
192 /**
193 * Sets the human readable description of this filter. For
194 * example: filter.setDescription("Gif and JPG Images");
195 *
196 * @see setDescription
197 * @see setExtensionListInDescription
198 * @see isExtensionListInDescription
199 */
200 public void setDescription(String description) {
201 this.description = description;
202 fullDescription = null;
203 }
204
205 /**
206 * Determines whether the extension list (.jpg, .gif, etc) should
207 * show up in the human readable description.
208 *
209 * Only relevent if a description was provided in the constructor
210 * or using setDescription();
211 *
212 * @see getDescription
213 * @see setDescription
214 * @see isExtensionListInDescription
215 */
216 public void setExtensionListInDescription(boolean b) {
217 useExtensionsInDescription = b;
218 fullDescription = null;
219 }
220
221 /**
222 * Returns whether the extension list (.jpg, .gif, etc) should
223 * show up in the human readable description.
224 *
225 * Only relevent if a description was provided in the constructor
226 * or using setDescription();
227 *
228 * @see getDescription
229 * @see setDescription
230 * @see setExtensionListInDescription
231 */
232 public boolean isExtensionListInDescription() {
233 return useExtensionsInDescription;
234 }
235 }