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