001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/loadercontrol/ExampleFileFilter.java,v 1.1 2005/04/20 21:05:06 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.loadercontrol;
019:
020: import java.io.File;
021: import java.util.Hashtable;
022: import java.util.Enumeration;
023: import javax.swing.*;
024: import javax.swing.filechooser.*;
025:
026: /**
027: * A convenience implementation of FileFilter that filters out
028: * all files except for those type extensions that it knows about.
029: *
030: * Extensions are of the type ".foo", which is typically found on
031: * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
032: *
033: * Example - create a new filter that filerts out all files
034: * but gif and jpg image files:
035: *
036: * JFileChooser chooser = new JFileChooser();
037: * ExampleFileFilter filter = new ExampleFileFilter(
038: * new String{"gif", "jpg"}, "JPEG & GIF Images")
039: * chooser.addChoosableFileFilter(filter);
040: * chooser.showOpenDialog(this);
041: *
042: * @version 1.9 04/23/99
043: * @author Jeff Dinkins
044: */
045: public class ExampleFileFilter extends FileFilter {
046:
047: private static String TYPE_UNKNOWN = "Type Unknown";
048: private static String HIDDEN_FILE = "Hidden File";
049:
050: private Hashtable filters = null;
051: private String description = null;
052: private String fullDescription = null;
053: private boolean useExtensionsInDescription = true;
054:
055: /**
056: * Creates a file filter. If no filters are added, then all
057: * files are accepted.
058: *
059: * @see #addExtension
060: */
061: public ExampleFileFilter() {
062: this .filters = new Hashtable();
063: }
064:
065: /**
066: * Creates a file filter that accepts files with the given extension.
067: * Example: new ExampleFileFilter("jpg");
068: *
069: * @see #addExtension
070: */
071: public ExampleFileFilter(String extension) {
072: this (extension, null);
073: }
074:
075: /**
076: * Creates a file filter that accepts the given file type.
077: * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
078: *
079: * Note that the "." before the extension is not needed. If
080: * provided, it will be ignored.
081: *
082: * @see #addExtension
083: */
084: public ExampleFileFilter(String extension, String description) {
085: this ();
086: if (extension != null)
087: addExtension(extension);
088: if (description != null)
089: setDescription(description);
090: }
091:
092: /**
093: * Creates a file filter from the given string array.
094: * Example: new ExampleFileFilter(String {"gif", "jpg"});
095: *
096: * Note that the "." before the extension is not needed adn
097: * will be ignored.
098: *
099: * @see #addExtension
100: */
101: public ExampleFileFilter(String[] filters) {
102: this (filters, null);
103: }
104:
105: /**
106: * Creates a file filter from the given string array and description.
107: * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
108: *
109: * Note that the "." before the extension is not needed and will be ignored.
110: *
111: * @see #addExtension
112: */
113: public ExampleFileFilter(String[] filters, String description) {
114: this ();
115: for (int i = 0; i < filters.length; i++) {
116: // add filters one by one
117: addExtension(filters[i]);
118: }
119: if (description != null)
120: setDescription(description);
121: }
122:
123: /**
124: * Return true if this file should be shown in the directory pane,
125: * false if it shouldn't.
126: *
127: * Files that begin with "." are ignored.
128: *
129: * @see #getExtension
130: * @see FileFilter#accepts
131: */
132: public boolean accept(File f) {
133: if (f != null) {
134: if (f.isDirectory()) {
135: return true;
136: }
137: String extension = getExtension(f);
138: if (extension != null
139: && filters.get(getExtension(f)) != null) {
140: return true;
141: }
142: ;
143: }
144: return false;
145: }
146:
147: /**
148: * Return the extension portion of the file's name .
149: *
150: * @see #getExtension
151: * @see FileFilter#accept
152: */
153: public String getExtension(File f) {
154: if (f != null) {
155: String filename = f.getName();
156: int i = filename.lastIndexOf('.');
157: if (i > 0 && i < filename.length() - 1) {
158: return filename.substring(i + 1).toLowerCase();
159: }
160: ;
161: }
162: return null;
163: }
164:
165: /**
166: * Adds a filetype "dot" extension to filter against.
167: *
168: * For example: the following code will create a filter that filters
169: * out all files except those that end in ".jpg" and ".tif":
170: *
171: * ExampleFileFilter filter = new ExampleFileFilter();
172: * filter.addExtension("jpg");
173: * filter.addExtension("tif");
174: *
175: * Note that the "." before the extension is not needed and will be ignored.
176: */
177: public void addExtension(String extension) {
178: if (filters == null) {
179: filters = new Hashtable(5);
180: }
181: filters.put(extension.toLowerCase(), this );
182: fullDescription = null;
183: }
184:
185: /**
186: * Returns the human readable description of this filter. For
187: * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
188: *
189: * @see setDescription
190: * @see setExtensionListInDescription
191: * @see isExtensionListInDescription
192: * @see FileFilter#getDescription
193: */
194: public String getDescription() {
195: if (fullDescription == null) {
196: if (description == null || isExtensionListInDescription()) {
197: fullDescription = description == null ? "("
198: : description + " (";
199: // build the description from the extension list
200: Enumeration extensions = filters.keys();
201: if (extensions != null) {
202: fullDescription += "."
203: + (String) extensions.nextElement();
204: while (extensions.hasMoreElements()) {
205: fullDescription += ", "
206: + (String) extensions.nextElement();
207: }
208: }
209: fullDescription += ")";
210: } else {
211: fullDescription = description;
212: }
213: }
214: return fullDescription;
215: }
216:
217: /**
218: * Sets the human readable description of this filter. For
219: * example: filter.setDescription("Gif and JPG Images");
220: *
221: * @see setDescription
222: * @see setExtensionListInDescription
223: * @see isExtensionListInDescription
224: */
225: public void setDescription(String description) {
226: this .description = description;
227: fullDescription = null;
228: }
229:
230: /**
231: * Determines whether the extension list (.jpg, .gif, etc) should
232: * show up in the human readable description.
233: *
234: * Only relevent if a description was provided in the constructor
235: * or using setDescription();
236: *
237: * @see getDescription
238: * @see setDescription
239: * @see isExtensionListInDescription
240: */
241: public void setExtensionListInDescription(boolean b) {
242: useExtensionsInDescription = b;
243: fullDescription = null;
244: }
245:
246: /**
247: * Returns whether the extension list (.jpg, .gif, etc) should
248: * show up in the human readable description.
249: *
250: * Only relevent if a description was provided in the constructor
251: * or using setDescription();
252: *
253: * @see getDescription
254: * @see setDescription
255: * @see setExtensionListInDescription
256: */
257: public boolean isExtensionListInDescription() {
258: return useExtensionsInDescription;
259: }
260: }
|