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