Java JFileChooser Filter fileNameAutoExtend(FileFilter filter, File file)

Here you can find the source of fileNameAutoExtend(FileFilter filter, File file)

Description

If the user does not input the extension specified by the file filter, automatically augment the file name with the specified extension.

License

LGPL

Declaration

public static String fileNameAutoExtend(FileFilter filter, File file) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**/*from w ww  . j  av  a2  s.c  o  m*/
     * If the user does not input the extension specified by the file filter, automatically augment the file name with the specified extension.
     */
    public static String fileNameAutoExtend(FileFilter filter, File file) {
        if (filter == null)
            return file.getAbsolutePath();
        String description = filter.getDescription().toLowerCase();
        String extension = getExtensionInLowerCase(file);
        String filename = file.getAbsolutePath();
        if (extension != null) {
            if (!filter.accept(file)) {
                filename = file.getAbsolutePath().concat(".").concat(description);
            }
        } else {
            filename = file.getAbsolutePath().concat(".").concat(description);
        }
        return filename;
    }

    /** @return the extension of a file name in lower case */
    public static String getExtensionInLowerCase(File file) {
        if (file == null || file.isDirectory())
            return null;
        String extension = getSuffix(file.getName());
        if (extension != null)
            return extension.toLowerCase();
        return null;
    }

    /** @return the extension of a file name */
    public static String getSuffix(String filename) {
        String extension = null;
        int index = filename.lastIndexOf('.');
        if (index >= 1 && index < filename.length() - 1) {
            extension = filename.substring(index + 1);
        }
        return extension;
    }
}

Related

  1. createFileFilter(final String ext, final String desc)
  2. createFileFilter(final String filter)
  3. createFileFilter(final String filterName, final String... extensionPatterns)
  4. createFileFilter(String text, String[]... suffixes)
  5. fileEndsInValidExtension(File file, FileNameExtensionFilter filter)
  6. getBinaryFileFilter()
  7. getCustomFileFilter(String desc, String[] validExtensions)
  8. getFileChooserFilter(java.util.List allowedExtensions)
  9. getFileFilter( String text, String... formats)