001/*****************************************************************************
002 * Copyright by The HDF Group.                                               *
003 * Copyright by the Board of Trustees of the University of Illinois.         *
004 * All rights reserved.                                                      *
005 *                                                                           *
006 * This file is part of the HDF Java Products distribution.                  *
007 * The full copyright notice, including terms governing use, modification,   *
008 * and redistribution, is contained in the COPYING file, which can be found  *
009 * at the root of the source code distribution tree,                         *
010 * or in https://www.hdfgroup.org/licenses.                                  *
011 * If you do not have access to either file, you may request a copy from     *
012 * help@hdfgroup.org.                                                        *
013 ****************************************************************************/
014
015package hdf.view;
016
017import java.util.Enumeration;
018import java.util.Hashtable;
019import java.util.StringTokenizer;
020
021/**
022 * A convenience implementation of FileFilter that filters out all files except
023 * for those type extensions that it knows about.
024 *
025 * @author Jordan T. Henderson
026 * @version 2.4 4/27/2016
027 */
028public class DefaultFileFilter {
029    private Hashtable<String, DefaultFileFilter> filters = null;
030    private String description                           = null;
031    private String fullDescription                       = null;
032    private boolean useExtensionsInDescription           = true;
033
034    /**
035     * Creates a file filter. If no filters are added, then all files are
036     * accepted.
037     *
038     * @see #addExtension
039     */
040    public DefaultFileFilter() { this.filters = new Hashtable<>(); }
041
042    /**
043     * Creates a file filter that accepts files with the given extension.
044     * Example: new DefaultFileFilter("jpg");
045     *
046     * @see #addExtension
047     *
048     * @param extension the file extension to filter on
049     */
050    public DefaultFileFilter(String extension) { this(extension, null); }
051
052    /**
053     * Creates a file filter that accepts the given file type. Example: new
054     * DefaultFileFilter("jpg", "JPEG Image Images");
055     *
056     * Note that the "." before the extension is not needed. If provided, it
057     * will be ignored.
058     *
059     * @see #addExtension
060     *
061     * @param extension the file extension to filter on
062     * @param description the file extension full description
063     */
064    public DefaultFileFilter(String extension, String description)
065    {
066        this();
067        if (extension != null) {
068            addExtension(extension);
069        }
070        if (description != null) {
071            setDescription(description);
072        }
073    }
074
075    /**
076     * Creates a file filter from the given string array. Example: new
077     * DefaultFileFilter(String {"gif", "jpg"});
078     *
079     * Note that the "." before the extension is not needed and will be ignored.
080     *
081     * @see #addExtension
082     *
083     * @param filters
084     *          the list of filter names
085     */
086    public DefaultFileFilter(String[] filters) { this(filters, null); }
087
088    /**
089     * Creates a file filter from the given string array and description.
090     * Example: new DefaultFileFilter(String {"gif", "jpg"},
091     * "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     * @param filters
098     *          the list of filter names
099     * @param description
100     *          the name of the filter list
101     */
102    public DefaultFileFilter(String[] filters, String description)
103    {
104        this();
105        for (int i = 0; i < filters.length; i++) {
106            // add filters one by one
107            addExtension(filters[i]);
108        }
109        if (description != null) {
110            setDescription(description);
111        }
112    }
113
114    /**
115     * Get the file extensions associated with this DefaultFileFilter
116     *
117     * @return the file extensions associated with this DefaultFileFilter
118     */
119    public String getExtensions()
120    {
121        Enumeration<String> extensions = filters.keys();
122        String extString               = "";
123
124        while (extensions.hasMoreElements()) {
125            extString += "*." + extensions.nextElement();
126            if (extensions.hasMoreElements()) {
127                extString += ";";
128            }
129        }
130
131        return extString;
132    }
133
134    /**
135     * Adds a filetype "dot" extension to filter against.
136     *
137     * For example: the following code will create a filter that filters out all
138     * files except those that end in ".jpg" and ".tif":
139     *
140     * DefaultFileFilter filter = new DefaultFileFilter();
141     * filter.addExtension("jpg"); filter.addExtension("tif"); or
142     * filter.addExtension("jpg, tif");
143     *
144     * Note that the "." before the extension is not needed and will be ignored.
145     *
146     * @param extension the file extension to add to the file filter
147     */
148    public void addExtension(String extension)
149    {
150        if (filters == null) {
151            filters = new Hashtable<>(5);
152        }
153
154        String ext         = null;
155        StringTokenizer st = new StringTokenizer(extension, ",");
156        while (st.hasMoreElements()) {
157            ext = st.nextToken().trim();
158            filters.put(ext.toLowerCase(), this);
159        }
160
161        fullDescription = null;
162    }
163
164    /**
165     * Get the human readable description of this filter. For example: "JPEG and GIF Image Files (*.jpg,
166     * *.gif)"
167     *
168     * @return the human readable description of this filter.
169     */
170    public String getDescription()
171    {
172        if (fullDescription == null) {
173            if ((description == null) || isExtensionListInDescription()) {
174                fullDescription = description == null ? "(" : description + " (";
175                // build the description from the extension list
176                Enumeration<String> extensions = filters.keys();
177                if (extensions != null) {
178
179                    if (!extensions.hasMoreElements()) {
180                        fullDescription = null;
181                        return null;
182                    }
183
184                    fullDescription += "." + extensions.nextElement();
185                    while (extensions.hasMoreElements()) {
186                        fullDescription += ", "
187                                           + "." + extensions.nextElement();
188                    }
189                }
190                fullDescription += ")";
191            }
192            else {
193                fullDescription = description;
194            }
195        }
196        return fullDescription;
197    }
198
199    /**
200     * Sets the human readable description of this filter. For example:
201     * filter.setDescription("Gif and JPG Images");
202     *
203     * @param description the full description of the file filter
204     */
205    public void setDescription(String description)
206    {
207        this.description = description;
208        fullDescription  = null;
209    }
210
211    /**
212     * Determines whether the extension list (.jpg, .gif, etc) should show up in
213     * the human readable description.
214     *
215     * Only relevent if a description was provided in the constructor or using
216     * setDescription();
217     *
218     * @param b the show state of the extension list
219     */
220    public void setExtensionListInDescription(boolean b)
221    {
222        useExtensionsInDescription = b;
223        fullDescription            = null;
224    }
225
226    /**
227     * Check if the extension list (.jpg, .gif, etc) should show up in the human readable description.
228     *
229     * @return whether the extension list (.jpg, .gif, etc) should show up in the human readable description.
230     *
231     *         Only relevent if a description was provided in the constructor or using setDescription();
232     */
233    public boolean isExtensionListInDescription() { return useExtensionsInDescription; }
234
235    /**
236     * Get the file filter for HDF4/5 file
237     *
238     * @return a file filter for HDF4/5 file.
239     */
240    public static DefaultFileFilter getFileFilter()
241    {
242        // update extensions
243        String fileExtensions = ViewProperties.getFileExtension();
244
245        DefaultFileFilter filter = new DefaultFileFilter();
246        filter.setDescription("HDF & more");
247
248        filter.addExtension(fileExtensions);
249
250        return filter;
251    }
252
253    /**
254     * Get a file filter for NetCDF3 file
255     *
256     * @return a file filter for NetCDF3 file.
257     */
258    public static DefaultFileFilter getFileFilterNetCDF3()
259    {
260        DefaultFileFilter filter = new DefaultFileFilter();
261        filter.addExtension("nc");
262        filter.setDescription("NetCDF3 files");
263
264        return filter;
265    }
266
267    /**
268     * Get a file filter for HDF4 file
269     *
270     * @return a file filter for HDF4 file.
271     */
272    public static DefaultFileFilter getFileFilterHDF4()
273    {
274        DefaultFileFilter filter = new DefaultFileFilter();
275        filter.addExtension("hdf");
276        filter.addExtension("h4");
277        filter.addExtension("hdf4");
278        filter.setDescription("HDF4 files");
279
280        return filter;
281    }
282
283    /**
284     * Get a file filter for HDF5 file
285     *
286     * @return a file filter for HDF5 file.
287     */
288    public static DefaultFileFilter getFileFilterHDF5()
289    {
290        DefaultFileFilter filter = new DefaultFileFilter();
291        filter.addExtension("h5");
292        filter.addExtension("hdf5");
293        filter.setDescription("HDF5 files");
294
295        return filter;
296    }
297
298    /**
299     * Get a file filter for JPEG image files
300     *
301     * @return a file filter for JPEG image files.
302     */
303    public static DefaultFileFilter getFileFilterJPEG()
304    {
305        DefaultFileFilter filter = new DefaultFileFilter();
306        filter.addExtension("jpg");
307        filter.addExtension("jpeg");
308        filter.addExtension("jpe");
309        filter.addExtension("jif");
310        filter.addExtension("jfif");
311        filter.addExtension("jfi");
312        filter.setDescription("JPEG images");
313
314        return filter;
315    }
316
317    /**
318     * Get a file filter for TIFF image files
319     *
320     * @return a file filter for TIFF image files.
321     */
322    public static DefaultFileFilter getFileFilterTIFF()
323    {
324        DefaultFileFilter filter = new DefaultFileFilter();
325        filter.addExtension("tif");
326        filter.addExtension("tiff");
327        filter.setDescription("TIFF images");
328
329        return filter;
330    }
331
332    /**
333     * Get a file filter for PNG image files
334     *
335     * @return a file filter for PNG image files.
336     */
337    public static DefaultFileFilter getFileFilterPNG()
338    {
339        DefaultFileFilter filter = new DefaultFileFilter();
340        filter.addExtension("png");
341        filter.setDescription("PNG images");
342
343        return filter;
344    }
345
346    /**
347     * Get a file filter for BMP image files
348     *
349     * @return a file filter for BMP image files.
350     */
351    public static DefaultFileFilter getFileFilterBMP()
352    {
353        DefaultFileFilter filter = new DefaultFileFilter();
354        filter.addExtension("bmp");
355        filter.addExtension("dib");
356        filter.setDescription("BMP images");
357
358        return filter;
359    }
360
361    /**
362     * Get a file filter for GIF image files
363     *
364     * @return a file filter for GIF image files.
365     */
366    public static DefaultFileFilter getFileFilterGIF()
367    {
368        DefaultFileFilter filter = new DefaultFileFilter();
369        filter.addExtension("gif");
370        filter.setDescription("GIF images");
371
372        return filter;
373    }
374
375    /**
376     * Get a file filter for GIF, JPEG, BMP, or PNG image files
377     *
378     * @return a file filter for GIF, JPEG, BMP, or PNG image files.
379     */
380    public static DefaultFileFilter getImageFileFilter()
381    {
382        DefaultFileFilter filter = new DefaultFileFilter();
383        filter.addExtension("jpg");
384        filter.addExtension("jpeg");
385        filter.addExtension("jpe");
386        filter.addExtension("jif");
387        filter.addExtension("jfif");
388        filter.addExtension("jfi");
389        filter.addExtension("png");
390        filter.addExtension("gif");
391        filter.addExtension("bmp");
392        filter.addExtension("dib");
393        filter.setDescription("GIF, JPEG, BMP, or PNG images");
394
395        return filter;
396    }
397
398    /**
399     * Get a file filter for text file
400     *
401     * @return a file filter for text file.
402     */
403    public static DefaultFileFilter getFileFilterText()
404    {
405        DefaultFileFilter filter = new DefaultFileFilter();
406        filter.addExtension("txt");
407        filter.addExtension("text");
408        filter.setDescription("Text");
409
410        return filter;
411    }
412
413    /**
414     * Get a file filter for binary file
415     *
416     * @return a file filter for binary file.
417     */
418    public static DefaultFileFilter getFileFilterBinary()
419    {
420        DefaultFileFilter filter = new DefaultFileFilter();
421        filter.addExtension("bin");
422        filter.setDescription("Binary");
423
424        return filter;
425    }
426}