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