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