Example usage for javax.swing.filechooser FileNameExtensionFilter getExtensions

List of usage examples for javax.swing.filechooser FileNameExtensionFilter getExtensions

Introduction

In this page you can find the example usage for javax.swing.filechooser FileNameExtensionFilter getExtensions.

Prototype

public String[] getExtensions() 

Source Link

Document

Returns the set of file name extensions files are tested against.

Usage

From source file:com.opendoorlogistics.speedregions.excelshp.app.FileBrowserPanel.java

private static boolean matchesFilter(FileNameExtensionFilter filter, String path) {
    String[] exts = filter.getExtensions();
    for (String ext : exts) { // check if it already has a valid extension
        if (TextUtils.equalsStd(FilenameUtils.getExtension(path), ext)) {
            return true;
        }//from  ww  w . java 2  s  .c  om
    }
    return false;
}

From source file:com.opendoorlogistics.core.utils.ui.FileBrowserPanel.java

private static boolean matchesFilter(FileNameExtensionFilter filter, String path) {
    String[] exts = filter.getExtensions();
    for (String ext : exts) { // check if it already has a valid extension
        if (Strings.equalsStd(FilenameUtils.getExtension(path), ext)) {
            return true;
        }//from   w  ww .j a  v a2  s .  com
    }
    return false;
}

From source file:org.pgptool.gui.ui.tools.browsefs.SaveFileChooserDialog.java

private String enforceExtension(String filePathName, JFileChooser ofd) {
    FileFilter fileExtFilter = ofd.getFileFilter();
    if (fileExtFilter == ofd.getAcceptAllFileFilter()) {
        return filePathName;
    }/*  www  . j a va 2s  .  c  o  m*/
    FileNameExtensionFilter fileNameExtensionFilter = (FileNameExtensionFilter) fileExtFilter;
    String ext = fileNameExtensionFilter.getExtensions()[0];
    if (!ext.equalsIgnoreCase(FilenameUtils.getExtension(filePathName))) {
        filePathName += "." + ext;
    }
    return filePathName;
}

From source file:org.yccheok.jstock.gui.Utils.java

private static File promptOpenJFileChooser(FileNameExtensionFilter... fileNameExtensionFilters) {
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    final JFileChooser chooser = new JFileChooser(jStockOptions.getLastFileIODirectory());
    chooser.setAcceptAllFileFilterUsed(false);
    for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
        chooser.addChoosableFileFilter(fileNameExtensionFilter);
    }//from  www . j  a  va2  s  . c  o m
    final java.util.Map<String, FileNameExtensionFilter> map = new HashMap<String, FileNameExtensionFilter>();
    for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
        map.put(fileNameExtensionFilter.getDescription(), fileNameExtensionFilter);
    }
    final FileNameExtensionFilter filter = map.get(jStockOptions.getLastSavedFileNameExtensionDescription());
    if (filter != null) {
        chooser.setFileFilter(filter);
    }
    int returnVal = chooser.showOpenDialog(JStock.instance());

    if (returnVal != JFileChooser.APPROVE_OPTION) {
        return null;
    }

    File file = chooser.getSelectedFile();
    if (file == null || !file.exists()) {
        return null;
    }
    final String parent = chooser.getSelectedFile().getParent();
    if (parent != null) {
        jStockOptions.setLastFileIODirectory(parent);
    }
    final String extension = Utils.getFileExtension(file);
    for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
        final String[] extensions = fileNameExtensionFilter.getExtensions();
        if (extensions.length <= 0) {
            continue;
        }
        if (extension.equals(extensions[0])) {
            jStockOptions.setLastFileNameExtensionDescription(fileNameExtensionFilter.getDescription());
            return file;
        }
    }
    return null;
}

From source file:org.yccheok.jstock.gui.Utils.java

private static File promptSaveJFileChooser(String suggestedFileName,
        FileNameExtensionFilter... fileNameExtensionFilters) {
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    final JFileChooser chooser = new JFileChooser(jStockOptions.getLastFileIODirectory());
    chooser.setAcceptAllFileFilterUsed(false);
    for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
        chooser.addChoosableFileFilter(fileNameExtensionFilter);
    }//from   w w  w.  ja va  2 s. c om
    chooser.setSelectedFile(new File(suggestedFileName));
    final java.util.Map<String, FileNameExtensionFilter> map = new HashMap<String, FileNameExtensionFilter>();
    for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
        map.put(fileNameExtensionFilter.getDescription(), fileNameExtensionFilter);
    }
    final FileNameExtensionFilter filter = map.get(jStockOptions.getLastSavedFileNameExtensionDescription());
    if (filter != null) {
        chooser.setFileFilter(filter);
    }
    while (true) {
        final int returnVal = chooser.showSaveDialog(JStock.instance());
        if (returnVal != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        File file = chooser.getSelectedFile();
        if (file == null) {
            return null;
        }
        // Ensure the saved file is in correct extension. If user provide correct
        // file extension explicitly, leave it as is. If not, mutate the filename.
        final String extension = Utils.getFileExtension(file);
        boolean found = false;
        root: for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
            String[] extensions = fileNameExtensionFilter.getExtensions();
            for (String e : extensions) {
                if (e.equals(extension)) {
                    found = true;
                    break root;
                }
            }
        }
        if (!found) {
            for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
                String[] extensions = fileNameExtensionFilter.getExtensions();
                if (extensions.length <= 0) {
                    continue;
                }
                final String e = extensions[0];
                if (chooser.getFileFilter().getDescription().equals(fileNameExtensionFilter.getDescription())) {
                    if (e.startsWith(".")) {
                        file = new File(file.getAbsolutePath() + e);
                    } else {
                        file = new File(file.getAbsolutePath() + "." + e);
                    }
                    break;
                }
            }
        }
        if (file.exists()) {
            final String output = MessageFormat
                    .format(MessagesBundle.getString("question_message_replace_old_template"), file.getName());

            final int result = javax.swing.JOptionPane.showConfirmDialog(JStock.instance(), output,
                    MessagesBundle.getString("question_title_replace_old"),
                    javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE);
            if (result != javax.swing.JOptionPane.YES_OPTION) {
                continue;
            }
        }

        final String parent = chooser.getSelectedFile().getParent();
        if (parent != null) {
            jStockOptions.setLastFileIODirectory(parent);
        }
        final String e = Utils.getFileExtension(file);
        for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
            String[] extensions = fileNameExtensionFilter.getExtensions();
            if (extensions.length <= 0) {
                continue;
            }
            if (e.equals(extensions[0])) {
                jStockOptions.setLastFileNameExtensionDescription(fileNameExtensionFilter.getDescription());
                break;
            }
        }
        return file;
    }
}