View Javadoc

1   package org.astrogrid.ui;
2   
3   // A copy of the example FileFilter implementation that is provided as an
4   // example in the SDK demos, but not in the library - MCH
5   
6   /*
7    * @(#)ExampleFileFilter.java 1.9 99/04/23
8    *
9    * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
10   *
11   * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
12   * modify and redistribute this software in source and binary code form,
13   * provided that i) this copyright notice and license appear on all copies of
14   * the software; and ii) Licensee does not utilize the software in a manner
15   * which is disparaging to Sun.
16   *
17   * This software is provided "AS IS," without a warranty of any kind. ALL
18   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
19   * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
20   * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
21   * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
22   * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
23   * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
24   * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
25   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
26   * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
27   * POSSIBILITY OF SUCH DAMAGES.
28   *
29   * This software is not designed or intended for use in on-line control of
30   * aircraft, air traffic, aircraft navigation or aircraft communications; or in
31   * the design, construction, operation or maintenance of any nuclear
32   * facility. Licensee represents and warrants that it will not use or
33   * redistribute the Software for such purposes.
34   */
35  import java.io.File;
36  import java.util.Hashtable;
37  import java.util.Enumeration;
38  import javax.swing.filechooser.*;
39  
40  /***
41   * A convenience implementation of FileFilter that filters out
42   * all files except for those type extensions that it knows about.
43   *
44   * Extensions are of the type ".foo", which is typically found on
45   * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
46   *
47   * Example - create a new filter that filerts out all files
48   * but gif and jpg image files:
49   *
50   *     JFileChooser chooser = new JFileChooser();
51   *     ExampleFileFilter filter = new ExampleFileFilter(
52   *                   new String{"gif", "jpg"}, "JPEG & GIF Images")
53   *     chooser.addChoosableFileFilter(filter);
54   *     chooser.showOpenDialog(this);
55   *
56   * @version 1.9 04/23/99
57   * @author Jeff Dinkins
58   */
59  public class ExtensionFileFilter extends FileFilter {
60     
61     private static String TYPE_UNKNOWN = "Type Unknown";
62     private static String HIDDEN_FILE = "Hidden File";
63     
64     private Hashtable filters = null;
65     private String description = null;
66     private String fullDescription = null;
67     private boolean useExtensionsInDescription = true;
68     
69     /***
70     * Creates a file filter. If no filters are added, then all
71     * files are accepted.
72     *
73     * @see #addExtension
74     */
75     public ExtensionFileFilter() {
76       this.filters = new Hashtable();
77     }
78     /***
79     * Creates a file filter from the given string array.
80     * Example: new ExampleFileFilter(String {"gif", "jpg"});
81     *
82     * Note that the "." before the extension is not needed adn
83     * will be ignored.
84     *
85     * @see #addExtension
86     */
87     public ExtensionFileFilter(String[] filters) {
88       this(filters, null);
89     }
90     /***
91     * Creates a file filter from the given string array and description.
92     * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
93     *
94     * Note that the "." before the extension is not needed and will be ignored.
95     *
96     * @see #addExtension
97     */
98     public ExtensionFileFilter(String[] filters, String description) {
99       this();
100      for (int i = 0; i < filters.length; i++) {
101        // add filters one by one
102        addExtension(filters[i]);
103      }
104      if(description!=null) setDescription(description);
105    }
106    /***
107    * Creates a file filter that accepts files with the given extension.
108    * Example: new ExampleFileFilter("jpg");
109    *
110    * @see #addExtension
111    */
112    public ExtensionFileFilter(String extension) {
113      this(extension,null);
114    }
115    /***
116    * Creates a file filter that accepts the given file type.
117    * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
118    *
119    * Note that the "." before the extension is not needed. If
120    * provided, it will be ignored.
121    *
122    * @see #addExtension
123    */
124    public ExtensionFileFilter(String extension, String description) {
125      this();
126      if(extension!=null) addExtension(extension);
127      if(description!=null) setDescription(description);
128    }
129    /***
130    * Return true if this file should be shown in the directory pane,
131    * false if it shouldn't.
132    *
133    * Files that begin with "." are ignored.
134    *
135    * @see #getExtension
136    * @see FileFilter#accepts
137    */
138    public boolean accept(File f) {
139      if(f != null) {
140        if(f.isDirectory()) {
141          return true;
142        }
143        String extension = getExtension(f);
144        if(extension != null && filters.get(getExtension(f)) != null) {
145          return true;
146        };
147      }
148      return false;
149    }
150    /***
151    * Adds a filetype "dot" extension to filter against.
152    *
153    * For example: the following code will create a filter that filters
154    * out all files except those that end in ".jpg" and ".tif":
155    *
156    *   ExampleFileFilter filter = new ExampleFileFilter();
157    *   filter.addExtension("jpg");
158    *   filter.addExtension("tif");
159    *
160    * Note that the "." before the extension is not needed and will be ignored.
161    */
162    public void addExtension(String extension) {
163      if(filters == null) {
164        filters = new Hashtable(5);
165      }
166      filters.put(extension.toLowerCase(), this);
167      fullDescription = null;
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 ? "(" : description + " (";
182          // build the description from the extension list
183          Enumeration extensions = filters.keys();
184          if(extensions != null) {
185             fullDescription += "." + (String) extensions.nextElement();
186             while (extensions.hasMoreElements()) {
187               fullDescription += ", " + (String) extensions.nextElement();
188             }
189          }
190          fullDescription += ")";
191        } else {
192          fullDescription = description;
193        }
194      }
195      return fullDescription;
196    }
197    /***
198    * Return the extension portion of the file's name .
199    *
200    * @see #getExtension
201    * @see FileFilter#accept
202    */
203    public String getExtension(File f) {
204      if(f != null) {
205        String filename = f.getName();
206        int i = filename.lastIndexOf('.');
207        if(i>0 && i<filename.length()-1) {
208          return filename.substring(i+1).toLowerCase();
209        };
210      }
211      return null;
212    }
213    /***
214    * Returns 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 setExtensionListInDescription
223    */
224    public boolean isExtensionListInDescription() {
225      return useExtensionsInDescription;
226    }
227    /***
228    * Sets the human readable description of this filter. For
229    * example: filter.setDescription("Gif and JPG Images");
230    *
231    * @see setDescription
232    * @see setExtensionListInDescription
233    * @see isExtensionListInDescription
234    */
235    public void setDescription(String description) {
236      this.description = description;
237      fullDescription = null;
238    }
239    /***
240    * Determines whether the extension list (.jpg, .gif, etc) should
241    * show up in the human readable description.
242    *
243    * Only relevent if a description was provided in the constructor
244    * or using setDescription();
245    *
246    * @see getDescription
247    * @see setDescription
248    * @see isExtensionListInDescription
249    */
250    public void setExtensionListInDescription(boolean b) {
251      useExtensionsInDescription = b;
252      fullDescription = null;
253    }
254 }