Java JFileChooser Filter completeFileExtension(File file, FileFilter filter)

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

Description

complete File Extension

License

Open Source License

Declaration

public static File completeFileExtension(File file, FileFilter filter) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static File completeFileExtension(File file, FileFilter filter) {
        if ((file != null) && (filter != null)) {
            if (filter instanceof FileNameExtensionFilter) {
                String[] exts = ((FileNameExtensionFilter) filter)
                        .getExtensions();
                String fName = file.getName();
                if ((exts != null) && (fName != null)) {
                    int dotPos = fName.indexOf('.');
                    if ((exts.length == 1)
                            && ((dotPos < 0) || (dotPos == (fName.length() - 1)))) {
                        if (dotPos < 0) {
                            fName += ".";
                        }//  w  w w.j  a v a2  s . co m
                        fName += exts[0];
                        File parent = file.getParentFile();
                        if (parent != null) {
                            file = new File(parent, fName);
                        } else {
                            file = new File(fName);
                        }
                    }
                }
            }
        }
        return file;
    }
}

Related

  1. accept(File f, Collection filters)
  2. createFileFilter(final String description, final String[] extensions)
  3. createFileFilter(final String description, String extension)
  4. createFileFilter(final String ext, final String desc)
  5. createFileFilter(final String filter)