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