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