001/*
002 * Licensed Materials - Property of IBM
003 * IBM SDK/J2RE 1.2
004 * Copyright IBM Corp. 1999  All Rights Reserved
005 * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
006 */
007
008package ca.bc.webarts.widgets;
009
010import java.io.File;
011import java.util.Hashtable;
012import java.util.Enumeration;
013import javax.swing.*;
014import javax.swing.filechooser.*;
015
016/**
017 * A convenience implementation of FileFilter that filters out
018 * all files except for those type extensions that it knows about.
019 *
020 * Extensions are of the type ".foo", which is typically found on
021 * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
022 *
023 * Example - create a new filter that filerts out all files
024 * but gif and jpg image files:
025 *
026 *     JFileChooser chooser = new JFileChooser();
027 *     ExampleFileFilter filter = new ExampleFileFilter(
028 *                   new String{"gif", "jpg"}, "JPEG & GIF Images")
029 *     chooser.addChoosableFileFilter(filter);
030 *     chooser.showOpenDialog(this);
031 *
032 * @version 1.9 04/23/99
033 * @author Jeff Dinkins
034 */
035public class ExampleFileFilter extends FileFilter {
036
037    private static String TYPE_UNKNOWN = "Type Unknown";
038    private static String HIDDEN_FILE = "Hidden File";
039
040    private Hashtable filters = null;
041    private String description = null;
042    private String fullDescription = null;
043    private boolean useExtensionsInDescription = true;
044
045    /**
046     * Creates a file filter. If no filters are added, then all
047     * files are accepted.
048     *
049     * @see #addExtension
050     */
051    public ExampleFileFilter() {
052  this.filters = new Hashtable();
053    }
054
055    /**
056     * Creates a file filter that accepts files with the given extension.
057     * Example: new ExampleFileFilter("jpg");
058     *
059     * @see #addExtension
060     */
061    public ExampleFileFilter(String extension) {
062  this(extension,null);
063    }
064
065    /**
066     * Creates a file filter that accepts the given file type.
067     * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
068     *
069     * Note that the "." before the extension is not needed. If
070     * provided, it will be ignored.
071     *
072     * @see #addExtension
073     */
074    public ExampleFileFilter(String extension, String description) {
075  this();
076  if(extension!=null) addExtension(extension);
077  if(description!=null) setDescription(description);
078    }
079
080    /**
081     * Creates a file filter from the given string array.
082     * Example: new ExampleFileFilter(String {"gif", "jpg"});
083     *
084     * Note that the "." before the extension is not needed adn
085     * will be ignored.
086     *
087     * @see #addExtension
088     */
089    public ExampleFileFilter(String[] filters) {
090  this(filters, null);
091    }
092
093    /**
094     * Creates a file filter from the given string array and description.
095     * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
096     *
097     * Note that the "." before the extension is not needed and will be ignored.
098     *
099     * @see #addExtension
100     */
101    public ExampleFileFilter(String[] filters, String description) {
102  this();
103  for (int i = 0; i < filters.length; i++) {
104      // add filters one by one
105      addExtension(filters[i]);
106  }
107  if(description!=null) setDescription(description);
108    }
109
110    /**
111     * Return true if this file should be shown in the directory pane,
112     * false if it shouldn't.
113     *
114     * Files that begin with "." are ignored.
115     *
116     * @see #getExtension
117     * @see FileFilter#accepts
118     */
119    public boolean accept(File f) {
120  if(f != null) {
121      if(f.isDirectory()) {
122    return true;
123      }
124      String extension = getExtension(f);
125      if(extension != null && 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     *   ExampleFileFilter filter = new ExampleFileFilter();
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    /**
171     * Returns the human readable description of this filter. For
172     * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
173     *
174     * @see setDescription
175     * @see setExtensionListInDescription
176     * @see isExtensionListInDescription
177     * @see FileFilter#getDescription
178     */
179    public String getDescription() {
180  if(fullDescription == null) {
181      if(description == null || isExtensionListInDescription()) {
182    fullDescription = description==null ? "(" : description + " (";
183    // build the description from the extension list
184    Enumeration extensions = filters.keys();
185    if(extensions != null) {
186        fullDescription += "." + (String) extensions.nextElement();
187        while (extensions.hasMoreElements()) {
188      fullDescription += ", " + (String) extensions.nextElement();
189        }
190    }
191    fullDescription += ")";
192      } else {
193    fullDescription = description;
194      }
195  }
196  return fullDescription;
197    }
198
199    /**
200     * Sets the human readable description of this filter. For
201     * example: filter.setDescription("Gif and JPG Images");
202     *
203     * @see setDescription
204     * @see setExtensionListInDescription
205     * @see isExtensionListInDescription
206     */
207    public void setDescription(String description) {
208  this.description = description;
209  fullDescription = null;
210    }
211
212    /**
213     * Determines whether the extension list (.jpg, .gif, etc) should
214     * show up in the human readable description.
215     *
216     * Only relevent if a description was provided in the constructor
217     * or using setDescription();
218     *
219     * @see getDescription
220     * @see setDescription
221     * @see isExtensionListInDescription
222     */
223    public void setExtensionListInDescription(boolean b) {
224  useExtensionsInDescription = b;
225  fullDescription = null;
226    }
227
228    /**
229     * Returns whether the extension list (.jpg, .gif, etc) should
230     * show up in the human readable description.
231     *
232     * Only relevent if a description was provided in the constructor
233     * or using setDescription();
234     *
235     * @see getDescription
236     * @see setDescription
237     * @see setExtensionListInDescription
238     */
239    public boolean isExtensionListInDescription() {
240  return useExtensionsInDescription;
241    }
242}