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