root / tags / v1_0_2_Build_904 / libraries / libIverUtiles / src / com / iver / utiles / swing / fileFilter / ExampleFileFilter.java @ 10724
History | View | Annotate | Download (8.38 KB)
1 | 7033 | ppiqueras | package com.iver.utiles.swing.fileFilter; |
---|---|---|---|
2 | |||
3 | /*
|
||
4 | * Copyright (c) 2003 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.14 03/01/23
|
||
40 | */
|
||
41 | import java.io.File; |
||
42 | 8765 | jjdelcerro | import java.util.Enumeration; |
43 | 7033 | ppiqueras | import java.util.Hashtable; |
44 | |||
45 | 8765 | jjdelcerro | import javax.swing.filechooser.FileFilter; |
46 | |||
47 | 7033 | ppiqueras | /**
|
48 | * A convenience implementation of FileFilter that filters out
|
||
49 | * all files except for those type extensions that it knows about.
|
||
50 | *
|
||
51 | * Extensions are of the type ".foo", which is typically found on
|
||
52 | * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
|
||
53 | *
|
||
54 | * Example - create a new filter that filerts out all files
|
||
55 | * but gif and jpg image files:
|
||
56 | *
|
||
57 | * JFileChooser chooser = new JFileChooser();
|
||
58 | * ExampleFileFilter filter = new ExampleFileFilter(
|
||
59 | * new String{"gif", "jpg"}, "JPEG & GIF Images")
|
||
60 | * chooser.addChoosableFileFilter(filter);
|
||
61 | * chooser.showOpenDialog(this);
|
||
62 | *
|
||
63 | * @version 1.14 01/23/03
|
||
64 | * @author Jeff Dinkins
|
||
65 | */
|
||
66 | public class ExampleFileFilter extends FileFilter { |
||
67 | |||
68 | private static String TYPE_UNKNOWN = "Type Unknown"; |
||
69 | private static String HIDDEN_FILE = "Hidden File"; |
||
70 | |||
71 | private Hashtable filters = null; |
||
72 | private String description = null; |
||
73 | private String fullDescription = null; |
||
74 | private boolean useExtensionsInDescription = true; |
||
75 | |||
76 | /**
|
||
77 | * Creates a file filter. If no filters are added, then all
|
||
78 | * files are accepted.
|
||
79 | *
|
||
80 | * @see #addExtension
|
||
81 | */
|
||
82 | public ExampleFileFilter() {
|
||
83 | this.filters = new Hashtable(); |
||
84 | } |
||
85 | |||
86 | /**
|
||
87 | * Creates a file filter that accepts files with the given extension.
|
||
88 | * Example: new ExampleFileFilter("jpg");
|
||
89 | *
|
||
90 | * @see #addExtension
|
||
91 | */
|
||
92 | public ExampleFileFilter(String extension) { |
||
93 | this(extension,null); |
||
94 | } |
||
95 | |||
96 | /**
|
||
97 | * Creates a file filter that accepts the given file type.
|
||
98 | * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
|
||
99 | *
|
||
100 | * Note that the "." before the extension is not needed. If
|
||
101 | * provided, it will be ignored.
|
||
102 | *
|
||
103 | * @see #addExtension
|
||
104 | */
|
||
105 | public ExampleFileFilter(String extension, String description) { |
||
106 | this();
|
||
107 | if(extension!=null) addExtension(extension); |
||
108 | if(description!=null) setDescription(description); |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * Creates a file filter from the given string array.
|
||
113 | * Example: new ExampleFileFilter(String {"gif", "jpg"});
|
||
114 | *
|
||
115 | * Note that the "." before the extension is not needed adn
|
||
116 | * will be ignored.
|
||
117 | *
|
||
118 | * @see #addExtension
|
||
119 | */
|
||
120 | public ExampleFileFilter(String[] filters) { |
||
121 | this(filters, null); |
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * Creates a file filter from the given string array and description.
|
||
126 | * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
|
||
127 | *
|
||
128 | * Note that the "." before the extension is not needed and will be ignored.
|
||
129 | *
|
||
130 | * @see #addExtension
|
||
131 | */
|
||
132 | public ExampleFileFilter(String[] filters, String description) { |
||
133 | this();
|
||
134 | for (int i = 0; i < filters.length; i++) { |
||
135 | // add filters one by one
|
||
136 | addExtension(filters[i]); |
||
137 | } |
||
138 | if(description!=null) setDescription(description); |
||
139 | } |
||
140 | |||
141 | /**
|
||
142 | * Return true if this file should be shown in the directory pane,
|
||
143 | * false if it shouldn't.
|
||
144 | *
|
||
145 | * Files that begin with "." are ignored.
|
||
146 | *
|
||
147 | * @see #getExtension
|
||
148 | * @see FileFilter#accepts
|
||
149 | */
|
||
150 | public boolean accept(File f) { |
||
151 | if(f != null) { |
||
152 | if(f.isDirectory()) {
|
||
153 | return true; |
||
154 | } |
||
155 | String extension = getExtension(f);
|
||
156 | if(extension != null && filters.get(getExtension(f)) != null) { |
||
157 | return true; |
||
158 | }; |
||
159 | } |
||
160 | return false; |
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Return the extension portion of the file's name .
|
||
165 | *
|
||
166 | * @see #getExtension
|
||
167 | * @see FileFilter#accept
|
||
168 | */
|
||
169 | public String getExtension(File f) { |
||
170 | if(f != null) { |
||
171 | String filename = f.getName();
|
||
172 | int i = filename.lastIndexOf('.'); |
||
173 | if(i>0 && i<filename.length()-1) { |
||
174 | return filename.substring(i+1).toLowerCase(); |
||
175 | }; |
||
176 | } |
||
177 | return null; |
||
178 | } |
||
179 | |||
180 | /**
|
||
181 | * Adds a filetype "dot" extension to filter against.
|
||
182 | *
|
||
183 | * For example: the following code will create a filter that filters
|
||
184 | * out all files except those that end in ".jpg" and ".tif":
|
||
185 | *
|
||
186 | * ExampleFileFilter filter = new ExampleFileFilter();
|
||
187 | * filter.addExtension("jpg");
|
||
188 | * filter.addExtension("tif");
|
||
189 | *
|
||
190 | * Note that the "." before the extension is not needed and will be ignored.
|
||
191 | */
|
||
192 | public void addExtension(String extension) { |
||
193 | if(filters == null) { |
||
194 | filters = new Hashtable(5); |
||
195 | } |
||
196 | filters.put(extension.toLowerCase(), this);
|
||
197 | fullDescription = null;
|
||
198 | } |
||
199 | |||
200 | |||
201 | /**
|
||
202 | * Returns the human readable description of this filter. For
|
||
203 | * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
|
||
204 | *
|
||
205 | * @see setDescription
|
||
206 | * @see setExtensionListInDescription
|
||
207 | * @see isExtensionListInDescription
|
||
208 | * @see FileFilter#getDescription
|
||
209 | */
|
||
210 | public String getDescription() { |
||
211 | if(fullDescription == null) { |
||
212 | if(description == null || isExtensionListInDescription()) { |
||
213 | fullDescription = description==null ? "(" : description + " ("; |
||
214 | // build the description from the extension list
|
||
215 | Enumeration extensions = filters.keys();
|
||
216 | if(extensions != null) { |
||
217 | fullDescription += "." + (String) extensions.nextElement(); |
||
218 | while (extensions.hasMoreElements()) {
|
||
219 | fullDescription += ", ." + (String) extensions.nextElement(); |
||
220 | } |
||
221 | } |
||
222 | fullDescription += ")";
|
||
223 | } else {
|
||
224 | fullDescription = description; |
||
225 | } |
||
226 | } |
||
227 | return fullDescription;
|
||
228 | } |
||
229 | |||
230 | /**
|
||
231 | * Sets the human readable description of this filter. For
|
||
232 | * example: filter.setDescription("Gif and JPG Images");
|
||
233 | *
|
||
234 | * @see setDescription
|
||
235 | * @see setExtensionListInDescription
|
||
236 | * @see isExtensionListInDescription
|
||
237 | */
|
||
238 | public void setDescription(String description) { |
||
239 | this.description = description;
|
||
240 | fullDescription = null;
|
||
241 | } |
||
242 | |||
243 | /**
|
||
244 | * Determines whether the extension list (.jpg, .gif, etc) should
|
||
245 | * show up in the human readable description.
|
||
246 | *
|
||
247 | * Only relevent if a description was provided in the constructor
|
||
248 | * or using setDescription();
|
||
249 | *
|
||
250 | * @see getDescription
|
||
251 | * @see setDescription
|
||
252 | * @see isExtensionListInDescription
|
||
253 | */
|
||
254 | public void setExtensionListInDescription(boolean b) { |
||
255 | useExtensionsInDescription = b; |
||
256 | fullDescription = null;
|
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * Returns whether the extension list (.jpg, .gif, etc) should
|
||
261 | * show up in the human readable description.
|
||
262 | *
|
||
263 | * Only relevent if a description was provided in the constructor
|
||
264 | * or using setDescription();
|
||
265 | *
|
||
266 | * @see getDescription
|
||
267 | * @see setDescription
|
||
268 | * @see setExtensionListInDescription
|
||
269 | */
|
||
270 | public boolean isExtensionListInDescription() { |
||
271 | return useExtensionsInDescription;
|
||
272 | } |
||
273 | } |
||
274 |