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