001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.dialog;
020
021import icy.file.FileUtil;
022import icy.main.Icy;
023import icy.preferences.GeneralPreferences;
024import icy.system.thread.ThreadUtil;
025
026import java.io.File;
027
028import javax.swing.JFileChooser;
029import javax.swing.JOptionPane;
030
031/**
032 * Simple dialog to let user select a file for save operation.
033 * 
034 * @author stephane
035 */
036public class SaveDialog
037{
038    private static class SaveDialogRunner implements Runnable
039    {
040        private final String title;
041        private final String defaultDir;
042        private final String defaultName;
043        private final String extension;
044
045        private JFileChooser dialog;
046        String result;
047
048        public SaveDialogRunner(String title, String defaultDir, String defaultName, String extension)
049        {
050            super();
051
052            this.title = title;
053            this.defaultDir = defaultDir;
054            this.defaultName = defaultName;
055            this.extension = extension;
056        }
057
058        @Override
059        public void run()
060        {
061            result = null;
062
063            final String defaultFileName;
064
065            if ((defaultName != null) && (extension != null))
066                defaultFileName = FileUtil.setExtension(defaultName, extension);
067            else
068                defaultFileName = defaultName;
069
070            if (dialog == null)
071                dialog = new JFileChooser();
072
073            dialog.setDialogTitle(title);
074
075            if (defaultDir != null)
076                dialog.setCurrentDirectory(new File(defaultDir));
077
078            if (defaultFileName != null)
079                dialog.setSelectedFile(new File(defaultFileName));
080
081            final int returnVal = dialog.showSaveDialog(null);
082
083            if (returnVal != JFileChooser.APPROVE_OPTION)
084                return;
085
086            final File f = dialog.getSelectedFile();
087            if (f.exists())
088            {
089                final int ret = JOptionPane.showConfirmDialog(dialog,
090                        "The file " + f.getName() + " already exists. \nWould you like to replace it ?", "Replace ?",
091                        JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
092
093                if (ret != JOptionPane.OK_OPTION)
094                    return;
095            }
096
097            result = f.getAbsolutePath();
098        }
099    }
100
101    /**
102     * Displays a file save dialog, using the specified default directory and file name and
103     * extension
104     */
105    public static String chooseFile(String title, String defaultDir, String defaultName, String extension)
106    {
107        final SaveDialogRunner runner = new SaveDialogRunner(title, defaultDir, defaultName, extension);
108
109        // no result in headless
110        if (Icy.getMainInterface().isHeadLess())
111            return null;
112
113        ThreadUtil.invokeNow(runner);
114
115        return runner.result;
116    }
117
118    /**
119     * Displays a file save dialog, using the specified default directory and file name
120     */
121    public static String chooseFile(String title, String defaultDir, String defaultName)
122    {
123        return chooseFile(title, defaultDir, defaultName, null);
124    }
125
126    /**
127     * Displays a file save dialog, using the specified default directory and file name
128     */
129    public static String chooseFile(String defaultDir, String defaultName)
130    {
131        return chooseFile("Save file...", defaultDir, defaultName);
132    }
133
134    /**
135     * Displays a file save dialog
136     */
137    public static String chooseFile()
138    {
139        return chooseFile(null, null);
140    }
141
142    /**
143     * Displays a file save dialog pointing to default <i>result</i> folder, using the specified default file name and
144     * extension.<br>
145     * If filename is validated the <i>result</i> folder is updated to chosen location folder.
146     */
147    public static String chooseFileForResult(String title, String defaultName, String extension)
148    {
149        // get the global result folder
150        final String dir = GeneralPreferences.getResultFolder();
151        // create it if needed
152        FileUtil.createDir(dir);
153
154        final String result = chooseFile(title, dir, defaultName, extension);
155
156        if (result != null)
157            // update result folder
158            GeneralPreferences.setResultFolder(FileUtil.getDirectory(result));
159
160        return result;
161    }
162    
163    /**
164     * Displays a file save dialog pointing to default <i>result</i> folder, using the specified default file name and
165     * extension.<br>
166     * If filename is validated the <i>result</i> folder is updated to chosen location folder.
167     */
168    public static String chooseFileForResult(String defaultName, String extension)
169    {
170        return chooseFileForResult("Save file...", defaultName, extension);
171    }
172    
173    /**
174     * Displays a file save dialog pointing to default <i>result</i> folder, using the specified default file name.<br>
175     * If filename is validated the <i>result</i> folder is updated to chosen location folder.
176     */
177    public static String chooseFileForResult(String defaultName)
178    {
179        return chooseFileForResult("Save file...", defaultName, null);
180    }
181}