svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / fileFilter / ExampleFileFilter.java @ 40561
History | View | Annotate | Download (9.32 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (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 General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils.swing.fileFilter; |
25 |
|
26 |
/*
|
27 |
* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
|
28 |
*
|
29 |
* Redistribution and use in source and binary forms, with or without
|
30 |
* modification, are permitted provided that the following conditions
|
31 |
* are met:
|
32 |
*
|
33 |
* -Redistributions of source code must retain the above copyright
|
34 |
* notice, this list of conditions and the following disclaimer.
|
35 |
*
|
36 |
* -Redistribution in binary form must reproduct the above copyright
|
37 |
* notice, this list of conditions and the following disclaimer in
|
38 |
* the documentation and/or other materials provided with the distribution.
|
39 |
*
|
40 |
* Neither the name of Sun Microsystems, Inc. or the names of contributors
|
41 |
* may be used to endorse or promote products derived from this software
|
42 |
* without specific prior written permission.
|
43 |
*
|
44 |
* This software is provided "AS IS," without a warranty of any kind. ALL
|
45 |
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
|
46 |
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
47 |
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
|
48 |
* BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
|
49 |
* OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
|
50 |
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
|
51 |
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
|
52 |
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
|
53 |
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
|
54 |
* IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
55 |
*
|
56 |
* You acknowledge that Software is not designed, licensed or intended for
|
57 |
* use in the design, construction, operation or maintenance of any nuclear
|
58 |
* facility.
|
59 |
*/
|
60 |
|
61 |
/*
|
62 |
* @(#)ExampleFileFilter.java 1.14 03/01/23
|
63 |
*/
|
64 |
import java.io.File; |
65 |
import java.util.Enumeration; |
66 |
import java.util.Hashtable; |
67 |
|
68 |
import javax.swing.filechooser.FileFilter; |
69 |
|
70 |
/**
|
71 |
* A convenience implementation of FileFilter that filters out
|
72 |
* all files except for those type extensions that it knows about.
|
73 |
*
|
74 |
* Extensions are of the type ".foo", which is typically found on
|
75 |
* Windows and Unix boxes, but not on Macinthosh. Case is ignored.
|
76 |
*
|
77 |
* Example - create a new filter that filerts out all files
|
78 |
* but gif and jpg image files:
|
79 |
*
|
80 |
* JFileChooser chooser = new JFileChooser();
|
81 |
* ExampleFileFilter filter = new ExampleFileFilter(
|
82 |
* new String{"gif", "jpg"}, "JPEG & GIF Images")
|
83 |
* chooser.addChoosableFileFilter(filter);
|
84 |
* chooser.showOpenDialog(this);
|
85 |
*
|
86 |
* @version 1.14 01/23/03
|
87 |
* @author Jeff Dinkins
|
88 |
*/
|
89 |
public class ExampleFileFilter extends FileFilter { |
90 |
|
91 |
private static String TYPE_UNKNOWN = "Type Unknown"; |
92 |
private static String HIDDEN_FILE = "Hidden File"; |
93 |
|
94 |
private Hashtable filters = null; |
95 |
private String description = null; |
96 |
private String fullDescription = null; |
97 |
private boolean useExtensionsInDescription = true; |
98 |
|
99 |
/**
|
100 |
* Creates a file filter. If no filters are added, then all
|
101 |
* files are accepted.
|
102 |
*
|
103 |
* @see #addExtension
|
104 |
*/
|
105 |
public ExampleFileFilter() {
|
106 |
this.filters = new Hashtable(); |
107 |
} |
108 |
|
109 |
/**
|
110 |
* Creates a file filter that accepts files with the given extension.
|
111 |
* Example: new ExampleFileFilter("jpg");
|
112 |
*
|
113 |
* @see #addExtension
|
114 |
*/
|
115 |
public ExampleFileFilter(String extension) { |
116 |
this(extension,null); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Creates a file filter that accepts the given file type.
|
121 |
* Example: new ExampleFileFilter("jpg", "JPEG Image Images");
|
122 |
*
|
123 |
* Note that the "." before the extension is not needed. If
|
124 |
* provided, it will be ignored.
|
125 |
*
|
126 |
* @see #addExtension
|
127 |
*/
|
128 |
public ExampleFileFilter(String extension, String description) { |
129 |
this();
|
130 |
if(extension!=null) addExtension(extension); |
131 |
if(description!=null) setDescription(description); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Creates a file filter from the given string array.
|
136 |
* Example: new ExampleFileFilter(String {"gif", "jpg"});
|
137 |
*
|
138 |
* Note that the "." before the extension is not needed adn
|
139 |
* will be ignored.
|
140 |
*
|
141 |
* @see #addExtension
|
142 |
*/
|
143 |
public ExampleFileFilter(String[] filters) { |
144 |
this(filters, null); |
145 |
} |
146 |
|
147 |
/**
|
148 |
* Creates a file filter from the given string array and description.
|
149 |
* Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
|
150 |
*
|
151 |
* Note that the "." before the extension is not needed and will be ignored.
|
152 |
*
|
153 |
* @see #addExtension
|
154 |
*/
|
155 |
public ExampleFileFilter(String[] filters, String description) { |
156 |
this();
|
157 |
for (int i = 0; i < filters.length; i++) { |
158 |
// add filters one by one
|
159 |
addExtension(filters[i]); |
160 |
} |
161 |
if(description!=null) setDescription(description); |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Return true if this file should be shown in the directory pane,
|
166 |
* false if it shouldn't.
|
167 |
*
|
168 |
* Files that begin with "." are ignored.
|
169 |
*
|
170 |
* @see #getExtension
|
171 |
* @see FileFilter#accepts
|
172 |
*/
|
173 |
public boolean accept(File f) { |
174 |
if(f != null) { |
175 |
if(f.isDirectory()) {
|
176 |
return true; |
177 |
} |
178 |
String extension = getExtension(f);
|
179 |
if(extension != null && filters.get(getExtension(f)) != null) { |
180 |
return true; |
181 |
}; |
182 |
} |
183 |
return false; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Return the extension portion of the file's name .
|
188 |
*
|
189 |
* @see #getExtension
|
190 |
* @see FileFilter#accept
|
191 |
*/
|
192 |
public String getExtension(File f) { |
193 |
if(f != null) { |
194 |
String filename = f.getName();
|
195 |
int i = filename.lastIndexOf('.'); |
196 |
if(i>0 && i<filename.length()-1) { |
197 |
return filename.substring(i+1).toLowerCase(); |
198 |
}; |
199 |
} |
200 |
return null; |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Adds a filetype "dot" extension to filter against.
|
205 |
*
|
206 |
* For example: the following code will create a filter that filters
|
207 |
* out all files except those that end in ".jpg" and ".tif":
|
208 |
*
|
209 |
* ExampleFileFilter filter = new ExampleFileFilter();
|
210 |
* filter.addExtension("jpg");
|
211 |
* filter.addExtension("tif");
|
212 |
*
|
213 |
* Note that the "." before the extension is not needed and will be ignored.
|
214 |
*/
|
215 |
public void addExtension(String extension) { |
216 |
if(filters == null) { |
217 |
filters = new Hashtable(5); |
218 |
} |
219 |
filters.put(extension.toLowerCase(), this);
|
220 |
fullDescription = null;
|
221 |
} |
222 |
|
223 |
|
224 |
/**
|
225 |
* Returns the human readable description of this filter. For
|
226 |
* example: "JPEG and GIF Image Files (*.jpg, *.gif)"
|
227 |
*
|
228 |
* @see setDescription
|
229 |
* @see setExtensionListInDescription
|
230 |
* @see isExtensionListInDescription
|
231 |
* @see FileFilter#getDescription
|
232 |
*/
|
233 |
public String getDescription() { |
234 |
if(fullDescription == null) { |
235 |
if(description == null || isExtensionListInDescription()) { |
236 |
fullDescription = description==null ? "(" : description + " ("; |
237 |
// build the description from the extension list
|
238 |
Enumeration extensions = filters.keys();
|
239 |
if(extensions != null) { |
240 |
fullDescription += "." + (String) extensions.nextElement(); |
241 |
while (extensions.hasMoreElements()) {
|
242 |
fullDescription += ", ." + (String) extensions.nextElement(); |
243 |
} |
244 |
} |
245 |
fullDescription += ")";
|
246 |
} else {
|
247 |
fullDescription = description; |
248 |
} |
249 |
} |
250 |
return fullDescription;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* Sets the human readable description of this filter. For
|
255 |
* example: filter.setDescription("Gif and JPG Images");
|
256 |
*
|
257 |
* @see setDescription
|
258 |
* @see setExtensionListInDescription
|
259 |
* @see isExtensionListInDescription
|
260 |
*/
|
261 |
public void setDescription(String description) { |
262 |
this.description = description;
|
263 |
fullDescription = null;
|
264 |
} |
265 |
|
266 |
/**
|
267 |
* Determines whether the extension list (.jpg, .gif, etc) should
|
268 |
* show up in the human readable description.
|
269 |
*
|
270 |
* Only relevent if a description was provided in the constructor
|
271 |
* or using setDescription();
|
272 |
*
|
273 |
* @see getDescription
|
274 |
* @see setDescription
|
275 |
* @see isExtensionListInDescription
|
276 |
*/
|
277 |
public void setExtensionListInDescription(boolean b) { |
278 |
useExtensionsInDescription = b; |
279 |
fullDescription = null;
|
280 |
} |
281 |
|
282 |
/**
|
283 |
* Returns whether the extension list (.jpg, .gif, etc) should
|
284 |
* show up in the human readable description.
|
285 |
*
|
286 |
* Only relevent if a description was provided in the constructor
|
287 |
* or using setDescription();
|
288 |
*
|
289 |
* @see getDescription
|
290 |
* @see setDescription
|
291 |
* @see setExtensionListInDescription
|
292 |
*/
|
293 |
public boolean isExtensionListInDescription() { |
294 |
return useExtensionsInDescription;
|
295 |
} |
296 |
} |
297 |
|
298 |
|