    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 files COPYING and Copyright.html. *
    009 * COPYING can be found at the root of the source code distribution tree.    *
    010 * Or, see https://support.hdfgroup.org/products/licenses.html               *
    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() {
    041        this.filters = new Hashtable<>();
    042    }
    043
    044    /**
    045     * Creates a file filter that accepts files with the given extension.
    046     * Example: new DefaultFileFilter("jpg");
    047     *
    048     * @see #addExtension
    049     *
    050     * @param extension the file extension to filter on
    051     */
    052    public DefaultFileFilter(String extension) {
    053        this(extension, null);
    054    }
    055
    056    /**
    057     * Creates a file filter that accepts the given file type. Example: new
    058     * DefaultFileFilter("jpg", "JPEG Image Images");
    059     *
    060     * Note that the "." before the extension is not needed. If provided, it
    061     * will be ignored.
    062     *
    063     * @see #addExtension
    064     *
    065     * @param extension the file extension to filter on
    066     * @param description the file extension full description
    067     */
    068    public DefaultFileFilter(String extension, String description) {
    069        this();
    070        if (extension != null) {
    071            addExtension(extension);
    072        }
    073        if (description != null) {
    074            setDescription(description);
    075        }
    076    }
    077
    078    /**
    079     * Creates a file filter from the given string array. Example: new
    080     * DefaultFileFilter(String {"gif", "jpg"});
    081     *
    082     * Note that the "." before the extension is not needed and will be ignored.
    083     *
    084     * @see #addExtension
    085     *
    086     * @param filters
    087     *          the list of filter names
    088     */
    089    public DefaultFileFilter(String[] filters) {
    090        this(filters, null);
    091    }
    092
    093    /**
    094     * Creates a file filter from the given string array and description.
    095     * Example: new DefaultFileFilter(String {"gif", "jpg"},
    096     * "Gif and JPG Images");
    097     *
    098     * Note that the "." before the extension is not needed and will be ignored.
    099     *
    100     * @see #addExtension
    101     *
    102     * @param filters
    103     *          the list of filter names
    104     * @param description
    105     *          the name of the filter list
    106     */
    107    public DefaultFileFilter(String[] filters, String description) {
    108        this();
    109        for (int i = 0; i < filters.length; i++) {
    110            // add filters one by one
    111            addExtension(filters[i]);
    112        }
    113        if (description != null) {
    114            setDescription(description);
    115        }
    116    }
    117
    118    /**
    119     * @return the file extensions associated with this DefaultFileFilter
    120     */
    121    public String getExtensions() {
    122        Enumeration<String> extensions = filters.keys();
    123        String extString = "";
    124
    125        while (extensions.hasMoreElements()) {
    126            extString += "*." + extensions.nextElement() + ";";
    127        }
    128
    129        return extString;
    130    }
    131
    132    /**
    133     * Adds a filetype "dot" extension to filter against.
    134     *
    135     * For example: the following code will create a filter that filters out all
    136     * files except those that end in ".jpg" and ".tif":
    137     *
    138     * DefaultFileFilter filter = new DefaultFileFilter();
    139     * filter.addExtension("jpg"); filter.addExtension("tif"); or
    140     * filter.addExtension("jpg, tif");
    141     *
    142     * Note that the "." before the extension is not needed and will be ignored.
    143     *
    144     * @param extension the file extension to add to the file filter
    145     */
    146    public void addExtension(String extension) {
    147        if (filters == null) {
    148            filters = new Hashtable<>(5);
    149        }
    150
    151        String ext = null;
    152        StringTokenizer st = new StringTokenizer(extension, ",");
    153        while (st.hasMoreElements()) {
    154            ext = st.nextToken().trim();
    155            filters.put(ext.toLowerCase(), this);
    156        }
    157
    158        fullDescription = null;
    159    }
    160
    161    /**
    162     * @return the human readable description of this filter. For example:
    163     * "JPEG and GIF Image Files (*.jpg, *.gif)"
    164     */
    165    public String getDescription() {
    166        if (fullDescription == null) {
    167            if ((description == null) || isExtensionListInDescription()) {
    168                fullDescription = description == null ? "(" : description
    169                        + " (";
    170                // build the description from the extension list
    171                Enumeration<String> extensions = filters.keys();
    172                if (extensions != null) {
    173
    174                    if (!extensions.hasMoreElements()) {
    175                        fullDescription = null;
    176                        return null;
    177                    }
    178
    179                    fullDescription += "." + extensions.nextElement();
    180                    while (extensions.hasMoreElements()) {
    181                        fullDescription += ", "
    182                                + "." + extensions.nextElement();
    183                    }
    184                }
    185                fullDescription += ")";
    186            }
    187            else {
    188                fullDescription = description;
    189            }
    190        }
    191        return fullDescription;
    192    }
    193
    194    /**
    195     * Sets the human readable description of this filter. For example:
    196     * filter.setDescription("Gif and JPG Images");
    197     *
    198     * @param description the full description of the file filter
    199     */
    200    public void setDescription(String description) {
    201        this.description = description;
    202        fullDescription = null;
    203    }
    204
    205    /**
    206     * Determines whether the extension list (.jpg, .gif, etc) should show up in
    207     * the human readable description.
    208     *
    209     * Only relevent if a description was provided in the constructor or using
    210     * setDescription();
    211     *
    212     * @param b the show state of the extension list
    213     */
    214    public void setExtensionListInDescription(boolean b) {
    215        useExtensionsInDescription = b;
    216        fullDescription = null;
    217    }
    218
    219    /**
    220     * @return whether the extension list (.jpg, .gif, etc) should show up in
    221     * the human readable description.
    222     *
    223     * Only relevent if a description was provided in the constructor or using
    224     * setDescription();
    225     */
    226    public boolean isExtensionListInDescription() {
    227        return useExtensionsInDescription;
    228    }
    229
    230    /** @return a file filter for HDF4/5 file. */
    231    public static DefaultFileFilter getFileFilter() {
    232        // update extensions
    233        String fileExtensions = ViewProperties.getFileExtension();
    234
    235        DefaultFileFilter filter = new DefaultFileFilter();
    236        filter.setDescription("HDF & more");
    237
    238        filter.addExtension(fileExtensions);
    239
    240        return filter;
    241    }
    242
    243    /** @return a file filter for NetCDF3 file. */
    244    public static DefaultFileFilter getFileFilterNetCDF3() {
    245        DefaultFileFilter filter = new DefaultFileFilter();
    246        filter.addExtension("nc");
    247        filter.setDescription("NetCDF3 files");
    248
    249        return filter;
    250    }
    251
    252    /** @return a file filter for HDF4 file. */
    253    public static DefaultFileFilter getFileFilterHDF4() {
    254        DefaultFileFilter filter = new DefaultFileFilter();
    255        filter.addExtension("hdf");
    256        filter.addExtension("h4");
    257        filter.addExtension("hdf4");
    258        filter.setDescription("HDF4 files");
    259
    260        return filter;
    261    }
    262
    263    /** @return a file filter for HDF5 file. */
    264    public static DefaultFileFilter getFileFilterHDF5() {
    265        DefaultFileFilter filter = new DefaultFileFilter();
    266        filter.addExtension("h5");
    267        filter.addExtension("hdf5");
    268        filter.setDescription("HDF5 files");
    269
    270        return filter;
    271    }
    272
    273    /** @return a file filter for JPEG image files. */
    274    public static DefaultFileFilter getFileFilterJPEG() {
    275        DefaultFileFilter filter = new DefaultFileFilter();
    276        filter.addExtension("jpg");
    277        filter.addExtension("jpeg");
    278        filter.addExtension("jpe");
    279        filter.addExtension("jif");
    280        filter.addExtension("jfif");
    281        filter.addExtension("jfi");
    282        filter.setDescription("JPEG images");
    283
    284        return filter;
    285    }
    286
    287    /** @return a file filter for TIFF image files. */
    288    public static DefaultFileFilter getFileFilterTIFF() {
    289        DefaultFileFilter filter = new DefaultFileFilter();
    290        filter.addExtension("tif");
    291        filter.addExtension("tiff");
    292        filter.setDescription("TIFF images");
    293
    294        return filter;
    295    }
    296
    297    /** @return a file filter for PNG image files. */
    298    public static DefaultFileFilter getFileFilterPNG() {
    299        DefaultFileFilter filter = new DefaultFileFilter();
    300        filter.addExtension("png");
    301        filter.setDescription("PNG images");
    302
    303        return filter;
    304    }
    305
    306    /** @return a file filter for BMP image files. */
    307    public static DefaultFileFilter getFileFilterBMP() {
    308        DefaultFileFilter filter = new DefaultFileFilter();
    309        filter.addExtension("bmp");
    310        filter.addExtension("dib");
    311        filter.setDescription("BMP images");
    312
    313        return filter;
    314    }
    315
    316    /** @return a file filter for GIF image files. */
    317    public static DefaultFileFilter getFileFilterGIF() {
    318        DefaultFileFilter filter = new DefaultFileFilter();
    319        filter.addExtension("gif");
    320        filter.setDescription("GIF images");
    321
    322        return filter;
    323    }
    324
    325    /** @return a file filter for GIF, JPEG, BMP, or PNG image files. */
    326    public static DefaultFileFilter getImageFileFilter() {
    327        DefaultFileFilter filter = new DefaultFileFilter();
    328        filter.addExtension("jpg");
    329        filter.addExtension("jpeg");
    330        filter.addExtension("jpe");
    331        filter.addExtension("jif");
    332        filter.addExtension("jfif");
    333        filter.addExtension("jfi");
    334        filter.addExtension("png");
    335        filter.addExtension("gif");
    336        filter.addExtension("bmp");
    337        filter.addExtension("dib");
    338        filter.setDescription("GIF, JPEG, BMP, or PNG images");
    339
    340        return filter;
    341    }
    342
    343    /** @return a file filter for text file. */
    344    public static DefaultFileFilter getFileFilterText() {
    345        DefaultFileFilter filter = new DefaultFileFilter();
    346        filter.addExtension("txt");
    347        filter.addExtension("text");
    348        filter.setDescription("Text");
    349
    350        return filter;
    351    }
    352
    353    /** @return a file filter for binary file. */
    354    public static DefaultFileFilter getFileFilterBinary() {
    355        DefaultFileFilter filter = new DefaultFileFilter();
    356        filter.addExtension("bin");
    357        filter.setDescription("Binary");
    358
    359        return filter;
    360    }
    361}