Java JFileChooser getPropertiesFile(boolean saving, String startName, String extension, String description)

Here you can find the source of getPropertiesFile(boolean saving, String startName, String extension, String description)

Description

Opens a file chooser and returns the selected file for open or close operations

License

Open Source License

Parameter

Parameter Description
owner owner for JFileChooser
loading true = load a file; false = save a file
extension extension of the properties file
description description for extension

Return

selected file

Declaration

public static File getPropertiesFile(boolean saving, String startName,
        String extension, String description) 

Method Source Code

//package com.java2s;
/*/*ww w.ja v a2 s  .  co m*/
 * Copyright (C) 2008 Michael Romankiewicz
 * microm at users.sourceforge.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Opens a file chooser and returns the selected file for open or close operations 
     * @param owner owner for JFileChooser
     * @param loading true = load a file; false = save a file
     * @param extension extension of the properties file
     * @param description description for extension
     * @return selected file
     */
    public static File getPropertiesFile(boolean saving, String startName,
            String extension, String description) {
        JFileChooser fc = createFileChooser(true, startName, saving);
        final String finalExtension = extension;
        final String finalDescription = description;

        // FileFilter
        if (extension != null && extension.length() > 0) {
            FileFilter ff = new FileFilter() {
                public boolean accept(File f) {
                    return f.isDirectory()
                            || f.getName().toLowerCase()
                                    .endsWith(finalExtension);
                }

                public String getDescription() {
                    return finalDescription;
                }
            };
            fc.setFileFilter(ff);
        }

        int returnVal = -1;
        if (saving) {
            returnVal = fc.showSaveDialog(null);
        } else {
            returnVal = fc.showOpenDialog(null);
        }

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            if (fc.getSelectedFile().getAbsolutePath()
                    .endsWith("." + finalExtension)) {
                return fc.getSelectedFile();
            } else {
                return new File(fc.getSelectedFile().getAbsolutePath()
                        + "." + finalExtension);
            }
        } else {
            return null;
        }

    }

    /**
     * Create a file chooser
     * @param fileSelect true for using as file selection box, false for using as directory selection box 
     * @param startDirectory initial directory 
     * @param saveDialog true for using as save dialog, false for using as open dialog 
     * @return created file chooser
     */
    private static JFileChooser createFileChooser(boolean fileSelect,
            String startDirectory, boolean saveDialog) {
        if (startDirectory == null) {
            startDirectory = "";
        }
        JFileChooser fc = new JFileChooser(startDirectory);
        fc.setSelectedFile(new File(startDirectory));
        fc.setCurrentDirectory(fc.getSelectedFile());
        fc.enableInputMethods(false);
        fc.setFileHidingEnabled(true);
        if (fileSelect)
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        else
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        if (saveDialog)
            fc.setDialogType(JFileChooser.SAVE_DIALOG);
        else
            fc.setDialogType(JFileChooser.OPEN_DIALOG);

        return fc;
    }
}

Related

  1. getLoadFiles(String message, File openDefaultDirectory, String description, String... extensions)
  2. getNewFileChooser()
  3. getOpenFile(String defaultPath)
  4. getOpenFile(String name, String currentDirectory)
  5. getOpenFile(String title)
  6. getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)
  7. getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
  8. getSelectedFiles(final JFileChooser chooser)
  9. getSelectedFiles(JFileChooser chooser)