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