Source code: com/arranger/jarl/ui/ExampleFileFilter.java
1 package com.arranger.jarl.ui;
2
3 /*
4 * Copyright (c) 2002 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * -Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * -Redistribution in binary form must reproduct the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Sun Microsystems, Inc. or the names of contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind. ALL
22 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
25 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
26 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
27 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
31 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended for
34 * use in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 */
37
38 /*
39 * @(#)ExampleFileFilter.java 1.13 02/06/13
40 */
41
42
43 import javax.swing.filechooser.FileFilter;
44 import java.io.File;
45 import java.util.Enumeration;
46 import java.util.Hashtable;
47
48 /**
49 * A convenience implementation of FileFilter that filters out
50 * all files except for those type extensions that it knows about.
51 *
52 * Extensions are of the type ".foo", which is typically found on
53 * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
54 *
55 * Example - create a new filter that filerts out all files
56 * but gif and jpg image files:
57 *
58 * JFileChooser chooser = new JFileChooser();
59 * ExampleFileFilter filter = new ExampleFileFilter(
60 * new String{"gif", "jpg"}, "JPEG & GIF Images")
61 * chooser.addChoosableFileFilter(filter);
62 * chooser.showOpenDialog(this);
63 *
64 * @version 1.13 06/13/02
65 * @author Jeff Dinkins
66 */
67 public class ExampleFileFilter extends FileFilter {
68
69 /*private static String TYPE_UNKNOWN = "Type Unknown";
70 private static String HIDDEN_FILE = "Hidden File";*/
71
72 private Hashtable filters = null;
73 private String description = null;
74 private String fullDescription = null;
75 private boolean useExtensionsInDescription = true;
76
77 /**
78 * Creates a file filter. If no filters are added, then all
79 * files are accepted.
80 *
81 * @see #addExtension
82 */
83 public ExampleFileFilter() {
84 this.filters = new Hashtable();
85 }
86
87 /**
88 * Creates a file filter that accepts files with the given extension.
89 * Example: new ExampleFileFilter("jpg");
90 *
91 * @see #addExtension
92 */
93 public ExampleFileFilter(String extension) {
94 this(extension, null);
95 }
96
97 /**
98 * Creates a file filter that accepts the given file type.
99 * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
100 *
101 * Note that the "." before the extension is not needed. If
102 * provided, it will be ignored.
103 *
104 * @see #addExtension
105 */
106 public ExampleFileFilter(String extension, String description) {
107 this();
108 if (extension != null) addExtension(extension);
109 if (description != null) setDescription(description);
110 }
111
112 /**
113 * Creates a file filter from the given string array.
114 * Example: new ExampleFileFilter(String {"gif", "jpg"});
115 *
116 * Note that the "." before the extension is not needed adn
117 * will be ignored.
118 *
119 * @see #addExtension
120 */
121 public ExampleFileFilter(String[] filters) {
122 this(filters, null);
123 }
124
125 /**
126 * Creates a file filter from the given string array and description.
127 * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
128 *
129 * Note that the "." before the extension is not needed and will be ignored.
130 *
131 * @see #addExtension
132 */
133 public ExampleFileFilter(String[] filters, String description) {
134 this();
135 for (int i = 0; i < filters.length; i++) {
136 // add filters one by one
137 addExtension(filters[i]);
138 }
139 if (description != null) setDescription(description);
140 }
141
142 /**
143 * Return true if this file should be shown in the directory pane,
144 * false if it shouldn't.
145 *
146 * Files that begin with "." are ignored.
147 *
148 * @see #getExtension
149 * @see FileFilter#accept
150 */
151 public boolean accept(File f) {
152 if (f != null) {
153 if (f.isDirectory()) {
154 return true;
155 }
156 String extension = getExtension(f);
157 if (extension != null && filters.get(getExtension(f)) != null) {
158 return true;
159 }
160 ;
161 }
162 return false;
163 }
164
165 /**
166 * Return the extension portion of the file's name .
167 *
168 * @see #getExtension
169 * @see FileFilter#accept
170 */
171 public String getExtension(File f) {
172 if (f != null) {
173 String filename = f.getName();
174 int i = filename.lastIndexOf('.');
175 if (i > 0 && i < filename.length() - 1) {
176 return filename.substring(i + 1).toLowerCase();
177 }
178 ;
179 }
180 return null;
181 }
182
183 /**
184 * Adds a filetype "dot" extension to filter against.
185 *
186 * For example: the following code will create a filter that filters
187 * out all files except those that end in ".jpg" and ".tif":
188 *
189 * ExampleFileFilter filter = new ExampleFileFilter();
190 * filter.addExtension("jpg");
191 * filter.addExtension("tif");
192 *
193 * Note that the "." before the extension is not needed and will be ignored.
194 */
195 public void addExtension(String extension) {
196 if (filters == null) {
197 filters = new Hashtable(5);
198 }
199 filters.put(extension.toLowerCase(), this);
200 fullDescription = null;
201 }
202
203
204 /**
205 * Returns the human readable description of this filter. For
206 * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
207 *
208 * @see #setDescription
209 * @see #setExtensionListInDescription
210 * @see #isExtensionListInDescription
211 * @see FileFilter#getDescription
212 */
213 public String getDescription() {
214 if (fullDescription == null) {
215 if (description == null || isExtensionListInDescription()) {
216 fullDescription = description == null ? "(" : description + " (";
217 // build the description from the extension list
218 Enumeration extensions = filters.keys();
219 if (extensions != null) {
220 fullDescription += "." + (String)extensions.nextElement();
221 while (extensions.hasMoreElements()) {
222 fullDescription += ", ." + (String)extensions.nextElement();
223 }
224 }
225 fullDescription += ")";
226 } else {
227 fullDescription = description;
228 }
229 }
230 return fullDescription;
231 }
232
233 /**
234 * Sets the human readable description of this filter. For
235 * example: filter.setDescription("Gif and JPG Images");
236 *
237 * @see #setDescription
238 * @see #setExtensionListInDescription
239 * @see #isExtensionListInDescription
240 */
241 public void setDescription(String description) {
242 this.description = description;
243 fullDescription = null;
244 }
245
246 /**
247 * Determines whether the extension list (.jpg, .gif, etc) should
248 * show up in the human readable description.
249 *
250 * Only relevent if a description was provided in the constructor
251 * or using setDescription();
252 *
253 * @see #getDescription
254 * @see #setDescription
255 * @see #isExtensionListInDescription
256 */
257 public void setExtensionListInDescription(boolean b) {
258 useExtensionsInDescription = b;
259 fullDescription = null;
260 }
261
262 /**
263 * Returns whether the extension list (.jpg, .gif, etc) should
264 * show up in the human readable description.
265 *
266 * Only relevent if a description was provided in the constructor
267 * or using setDescription();
268 *
269 * @see #getDescription
270 * @see #setDescription
271 * @see #setExtensionListInDescription
272 */
273 public boolean isExtensionListInDescription() {
274 return useExtensionsInDescription;
275 }
276 }