001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: // This code is repackaged after the demo from Sun Microsystems (in the Java3D demo packages) /demo/jfc/FileChooserDemo/ExampleFileFilter.java
034: // Site http://java.sun.com/
035: // Email
036: package com.db.utils;
037:
038: /*
039: * @(#)BasicFileFilter.java 1.12 01/12/03
040: *
041: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
042: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
043: */
044:
045: import java.io.File;
046: import java.util.Hashtable;
047: import java.util.Enumeration;
048: import javax.swing.*;
049: import javax.swing.filechooser.*;
050:
051: /**
052: * A convenience implementation of FileFilter that filters out
053: * all files except for those type extensions that it knows about.
054: *
055: * Extensions are of the type ".foo", which is typically found on
056: * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
057: *
058: * Example - create a new filter that filerts out all files
059: * but gif and jpg image files:
060: *
061: * JFileChooser chooser = new JFileChooser();
062: * BasicFileFilter filter = new BasicFileFilter(
063: * new String{"gif", "jpg"}, "JPEG & GIF Images")
064: * chooser.addChoosableFileFilter(filter);
065: * chooser.showOpenDialog(this);
066: *
067: * @version 1.12 12/03/01
068: * @author Jeff Dinkins
069: */
070: public class BasicFileFilter extends FileFilter {
071:
072: private static String TYPE_UNKNOWN = "Type Unknown";
073: private static String HIDDEN_FILE = "Hidden File";
074:
075: private Hashtable filters = null;
076: private String description = null;
077: private String fullDescription = null;
078: private boolean useExtensionsInDescription = true;
079:
080: /**
081: * Creates a file filter. If no filters are added, then all
082: * files are accepted.
083: *
084: * @see #addExtension
085: */
086: public BasicFileFilter() {
087: this .filters = new Hashtable();
088: }
089:
090: /**
091: * Creates a file filter that accepts files with the given extension.
092: * Example: new BasicFileFilter("jpg");
093: *
094: * @see #addExtension
095: */
096: public BasicFileFilter(String extension) {
097: this (extension, null);
098: }
099:
100: /**
101: * Creates a file filter that accepts the given file type.
102: * Example: new BasicFileFilter("jpg", "JPEG Image Images");
103: *
104: * Note that the "." before the extension is not needed. If
105: * provided, it will be ignored.
106: *
107: * @see #addExtension
108: */
109: public BasicFileFilter(String extension, String description) {
110: this ();
111: if (extension != null)
112: addExtension(extension);
113: if (description != null)
114: setDescription(description);
115: }
116:
117: /**
118: * Creates a file filter from the given string array.
119: * Example: new BasicFileFilter(String {"gif", "jpg"});
120: *
121: * Note that the "." before the extension is not needed adn
122: * will be ignored.
123: *
124: * @see #addExtension
125: */
126: public BasicFileFilter(String[] filters) {
127: this (filters, null);
128: }
129:
130: /**
131: * Creates a file filter from the given string array and description.
132: * Example: new BasicFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
133: *
134: * Note that the "." before the extension is not needed and will be ignored.
135: *
136: * @see #addExtension
137: */
138: public BasicFileFilter(String[] filters, String description) {
139: this ();
140: for (int i = 0; i < filters.length; i++) {
141: // add filters one by one
142: addExtension(filters[i]);
143: }
144: if (description != null)
145: setDescription(description);
146: }
147:
148: /**
149: * Return true if this file should be shown in the directory pane,
150: * false if it shouldn't.
151: *
152: * Files that begin with "." are ignored.
153: *
154: * @see #getExtension
155: * @see FileFilter#accepts
156: */
157: public boolean accept(File f) {
158: if (f != null) {
159: if (f.isDirectory()) {
160: return true;
161: }
162: String extension = getExtension(f);
163: if (extension != null
164: && filters.get(getExtension(f)) != null) {
165: return true;
166: }
167: ;
168: }
169: return false;
170: }
171:
172: /**
173: * Return the extension portion of the file's name .
174: *
175: * @see #getExtension
176: * @see FileFilter#accept
177: */
178: public String getExtension(File f) {
179: if (f != null) {
180: String filename = f.getName();
181: int i = filename.lastIndexOf('.');
182: if (i > 0 && i < filename.length() - 1) {
183: return filename.substring(i + 1).toLowerCase();
184: }
185: ;
186: }
187: return null;
188: }
189:
190: /**
191: * Adds a filetype "dot" extension to filter against.
192: *
193: * For example: the following code will create a filter that filters
194: * out all files except those that end in ".jpg" and ".tif":
195: *
196: * BasicFileFilter filter = new BasicFileFilter();
197: * filter.addExtension("jpg");
198: * filter.addExtension("tif");
199: *
200: * Note that the "." before the extension is not needed and will be ignored.
201: */
202: public void addExtension(String extension) {
203: if (filters == null) {
204: filters = new Hashtable(5);
205: }
206: filters.put(extension.toLowerCase(), this );
207: fullDescription = null;
208: }
209:
210: /**
211: * Returns the human readable description of this filter. For
212: * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
213: *
214: * @see setDescription
215: * @see setExtensionListInDescription
216: * @see isExtensionListInDescription
217: * @see FileFilter#getDescription
218: */
219: public String getDescription() {
220: if (fullDescription == null) {
221: if (description == null || isExtensionListInDescription()) {
222: fullDescription = description == null ? "("
223: : description + " (";
224: // build the description from the extension list
225: Enumeration extensions = filters.keys();
226: if (extensions != null) {
227: fullDescription += "."
228: + (String) extensions.nextElement();
229: while (extensions.hasMoreElements()) {
230: fullDescription += ", ."
231: + (String) extensions.nextElement();
232: }
233: }
234: fullDescription += ")";
235: } else {
236: fullDescription = description;
237: }
238: }
239: return fullDescription;
240: }
241:
242: /**
243: * Sets the human readable description of this filter. For
244: * example: filter.setDescription("Gif and JPG Images");
245: *
246: * @see setDescription
247: * @see setExtensionListInDescription
248: * @see isExtensionListInDescription
249: */
250: public void setDescription(String description) {
251: this .description = description;
252: fullDescription = null;
253: }
254:
255: /**
256: * Determines whether the extension list (.jpg, .gif, etc) should
257: * show up in the human readable description.
258: *
259: * Only relevent if a description was provided in the constructor
260: * or using setDescription();
261: *
262: * @see getDescription
263: * @see setDescription
264: * @see isExtensionListInDescription
265: */
266: public void setExtensionListInDescription(boolean b) {
267: useExtensionsInDescription = b;
268: fullDescription = null;
269: }
270:
271: /**
272: * Returns whether the extension list (.jpg, .gif, etc) should
273: * show up in the human readable description.
274: *
275: * Only relevent if a description was provided in the constructor
276: * or using setDescription();
277: *
278: * @see getDescription
279: * @see setDescription
280: * @see setExtensionListInDescription
281: */
282: public boolean isExtensionListInDescription() {
283: return useExtensionsInDescription;
284: }
285: }
|