2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.gui;
|
|
27 |
|
|
28 |
import java.io.File;
|
|
29 |
import java.util.Hashtable;
|
|
30 |
import java.util.Enumeration;
|
|
31 |
import javax.swing.*;
|
|
32 |
import javax.swing.filechooser.*;
|
|
33 |
|
|
34 |
//### Renamed from 'ExampleFileFilter.java' provided with Swing demos.
|
|
35 |
|
|
36 |
/**
|
|
37 |
* A convenience implementation of FileFilter that filters out
|
|
38 |
* all files except for those type extensions that it knows about.
|
|
39 |
*
|
|
40 |
* Extensions are of the type ".foo", which is typically found on
|
|
41 |
* Windows and Unix boxes, but not on Macinthosh. Case is ignored.
|
|
42 |
*
|
|
43 |
* Example - create a new filter that filerts out all files
|
|
44 |
* but gif and jpg image files:
|
|
45 |
*
|
|
46 |
* JFileChooser chooser = new JFileChooser();
|
|
47 |
* ExampleFileFilter filter = new ExampleFileFilter(
|
|
48 |
* new String{"gif", "jpg"}, "JPEG & GIF Images")
|
|
49 |
* chooser.addChoosableFileFilter(filter);
|
|
50 |
* chooser.showOpenDialog(this);
|
|
51 |
*
|
|
52 |
* @author Jeff Dinkins
|
|
53 |
*/
|
|
54 |
|
|
55 |
public class JDBFileFilter extends FileFilter {
|
|
56 |
|
|
57 |
private static String TYPE_UNKNOWN = "Type Unknown";
|
|
58 |
private static String HIDDEN_FILE = "Hidden File";
|
|
59 |
|
|
60 |
private Hashtable<String, JDBFileFilter> filters = null;
|
|
61 |
private String description = null;
|
|
62 |
private String fullDescription = null;
|
|
63 |
private boolean useExtensionsInDescription = true;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Creates a file filter. If no filters are added, then all
|
|
67 |
* files are accepted.
|
|
68 |
*
|
|
69 |
* @see #addExtension
|
|
70 |
*/
|
|
71 |
public JDBFileFilter() {
|
|
72 |
this.filters = new Hashtable<String, JDBFileFilter>();
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Creates a file filter that accepts files with the given extension.
|
|
77 |
* Example: new JDBFileFilter("jpg");
|
|
78 |
*
|
|
79 |
* @see #addExtension
|
|
80 |
*/
|
|
81 |
public JDBFileFilter(String extension) {
|
|
82 |
this(extension,null);
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Creates a file filter that accepts the given file type.
|
|
87 |
* Example: new JDBFileFilter("jpg", "JPEG Image Images");
|
|
88 |
*
|
|
89 |
* Note that the "." before the extension is not needed. If
|
|
90 |
* provided, it will be ignored.
|
|
91 |
*
|
|
92 |
* @see #addExtension
|
|
93 |
*/
|
|
94 |
public JDBFileFilter(String extension, String description) {
|
|
95 |
this();
|
|
96 |
if(extension!=null) addExtension(extension);
|
|
97 |
if(description!=null) setDescription(description);
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Creates a file filter from the given string array.
|
|
102 |
* Example: new JDBFileFilter(String {"gif", "jpg"});
|
|
103 |
*
|
|
104 |
* Note that the "." before the extension is not needed adn
|
|
105 |
* will be ignored.
|
|
106 |
*
|
|
107 |
* @see #addExtension
|
|
108 |
*/
|
|
109 |
public JDBFileFilter(String[] filters) {
|
|
110 |
this(filters, null);
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Creates a file filter from the given string array and description.
|
|
115 |
* Example: new JDBFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
|
|
116 |
*
|
|
117 |
* Note that the "." before the extension is not needed and will be ignored.
|
|
118 |
*
|
|
119 |
* @see #addExtension
|
|
120 |
*/
|
|
121 |
public JDBFileFilter(String[] filters, String description) {
|
|
122 |
this();
|
|
123 |
for (int i = 0; i < filters.length; i++) {
|
|
124 |
// add filters one by one
|
|
125 |
addExtension(filters[i]);
|
|
126 |
}
|
|
127 |
if(description!=null) setDescription(description);
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Return true if this file should be shown in the directory pane,
|
|
132 |
* false if it shouldn't.
|
|
133 |
*
|
|
134 |
* Files that begin with "." are ignored.
|
|
135 |
*
|
|
136 |
* @see #getExtension
|
|
137 |
* @see FileFilter#accepts
|
|
138 |
*/
|
|
139 |
public boolean accept(File f) {
|
|
140 |
if(f != null) {
|
|
141 |
if(f.isDirectory()) {
|
|
142 |
return true;
|
|
143 |
}
|
|
144 |
String extension = getExtension(f);
|
|
145 |
if(extension != null && filters.get(getExtension(f)) != null) {
|
|
146 |
return true;
|
|
147 |
};
|
|
148 |
}
|
|
149 |
return false;
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Return the extension portion of the file's name .
|
|
154 |
*
|
|
155 |
* @see #getExtension
|
|
156 |
* @see FileFilter#accept
|
|
157 |
*/
|
|
158 |
public String getExtension(File f) {
|
|
159 |
if(f != null) {
|
|
160 |
String filename = f.getName();
|
|
161 |
int i = filename.lastIndexOf('.');
|
|
162 |
if(i>0 && i<filename.length()-1) {
|
|
163 |
return filename.substring(i+1).toLowerCase();
|
|
164 |
};
|
|
165 |
}
|
|
166 |
return null;
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Adds a filetype "dot" extension to filter against.
|
|
171 |
*
|
|
172 |
* For example: the following code will create a filter that filters
|
|
173 |
* out all files except those that end in ".jpg" and ".tif":
|
|
174 |
*
|
|
175 |
* JDBFileFilter filter = new JDBFileFilter();
|
|
176 |
* filter.addExtension("jpg");
|
|
177 |
* filter.addExtension("tif");
|
|
178 |
*
|
|
179 |
* Note that the "." before the extension is not needed and will be ignored.
|
|
180 |
*/
|
|
181 |
public void addExtension(String extension) {
|
|
182 |
if(filters == null) {
|
|
183 |
filters = new Hashtable<String, JDBFileFilter>(5);
|
|
184 |
}
|
|
185 |
filters.put(extension.toLowerCase(), this);
|
|
186 |
fullDescription = null;
|
|
187 |
}
|
|
188 |
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Returns the human readable description of this filter. For
|
|
192 |
* example: "JPEG and GIF Image Files (*.jpg, *.gif)"
|
|
193 |
*
|
|
194 |
* @see setDescription
|
|
195 |
* @see setExtensionListInDescription
|
|
196 |
* @see isExtensionListInDescription
|
|
197 |
* @see FileFilter#getDescription
|
|
198 |
*/
|
|
199 |
public String getDescription() {
|
|
200 |
if(fullDescription == null) {
|
|
201 |
if(description == null || isExtensionListInDescription()) {
|
|
202 |
fullDescription = description==null ? "(" : description + " (";
|
|
203 |
// build the description from the extension list
|
51
|
204 |
Enumeration<String> extensions = filters.keys();
|
2
|
205 |
if(extensions != null) {
|
51
|
206 |
fullDescription += "." + extensions.nextElement();
|
2
|
207 |
while (extensions.hasMoreElements()) {
|
51
|
208 |
fullDescription += ", " + extensions.nextElement();
|
2
|
209 |
}
|
|
210 |
}
|
|
211 |
fullDescription += ")";
|
|
212 |
} else {
|
|
213 |
fullDescription = description;
|
|
214 |
}
|
|
215 |
}
|
|
216 |
return fullDescription;
|
|
217 |
}
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Sets the human readable description of this filter. For
|
|
221 |
* example: filter.setDescription("Gif and JPG Images");
|
|
222 |
*
|
|
223 |
* @see setDescription
|
|
224 |
* @see setExtensionListInDescription
|
|
225 |
* @see isExtensionListInDescription
|
|
226 |
*/
|
|
227 |
public void setDescription(String description) {
|
|
228 |
this.description = description;
|
|
229 |
fullDescription = null;
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Determines whether the extension list (.jpg, .gif, etc) should
|
|
234 |
* show up in the human readable description.
|
|
235 |
*
|
|
236 |
* Only relevent if a description was provided in the constructor
|
|
237 |
* or using setDescription();
|
|
238 |
*
|
|
239 |
* @see getDescription
|
|
240 |
* @see setDescription
|
|
241 |
* @see isExtensionListInDescription
|
|
242 |
*/
|
|
243 |
public void setExtensionListInDescription(boolean b) {
|
|
244 |
useExtensionsInDescription = b;
|
|
245 |
fullDescription = null;
|
|
246 |
}
|
|
247 |
|
|
248 |
/**
|
|
249 |
* Returns whether the extension list (.jpg, .gif, etc) should
|
|
250 |
* show up in the human readable description.
|
|
251 |
*
|
|
252 |
* Only relevent if a description was provided in the constructor
|
|
253 |
* or using setDescription();
|
|
254 |
*
|
|
255 |
* @see getDescription
|
|
256 |
* @see setDescription
|
|
257 |
* @see setExtensionListInDescription
|
|
258 |
*/
|
|
259 |
public boolean isExtensionListInDescription() {
|
|
260 |
return useExtensionsInDescription;
|
|
261 |
}
|
|
262 |
}
|