001/**
002 * 
003 */
004package icy.gui.dialog;
005
006import icy.file.FileUtil;
007import icy.main.Icy;
008import icy.system.thread.ThreadUtil;
009
010import java.io.File;
011
012import javax.swing.JFileChooser;
013
014/**
015 * Simple dialog to let user select a file or a folder for open operation.
016 * 
017 * @author Stephane
018 */
019public class OpenDialog
020{
021    private static class OpenDialogRunner implements Runnable
022    {
023        private final String title;
024        private final String defaultDir;
025        private final String defaultName;
026        private final String extension;
027
028        private JFileChooser dialog;
029        String result;
030
031        public OpenDialogRunner(String title, String defaultDir, String defaultName, String extension)
032        {
033            super();
034
035            this.title = title;
036            this.defaultDir = defaultDir;
037            this.defaultName = defaultName;
038            this.extension = extension;
039        }
040
041        @Override
042        public void run()
043        {
044            result = null;
045
046            final String defaultFileName;
047
048            if ((defaultName != null) && (extension != null))
049                defaultFileName = FileUtil.setExtension(defaultName, extension);
050            else
051                defaultFileName = defaultName;
052
053            if (dialog == null)
054                dialog = new JFileChooser();
055
056            dialog.setDialogTitle(title);
057
058            if (defaultDir != null)
059                dialog.setCurrentDirectory(new File(defaultDir));
060
061            if (defaultFileName != null)
062                dialog.setSelectedFile(new File(defaultFileName));
063
064            final int returnVal = dialog.showOpenDialog(null);
065
066            if (returnVal != JFileChooser.APPROVE_OPTION)
067                return;
068
069            final File f = dialog.getSelectedFile();
070            if (!f.exists())
071                return;
072
073            result = f.getAbsolutePath();
074        }
075    }
076
077    /**
078     * Displays a file load dialog, using the specified default directory and file name and
079     * extension
080     */
081    public static String chooseFile(String title, String defaultDir, String defaultName, String extension)
082    {
083        final OpenDialogRunner runner = new OpenDialogRunner(title, defaultDir, defaultName, extension);
084
085        // no result in headless
086        if (Icy.getMainInterface().isHeadLess())
087            return null;
088
089        ThreadUtil.invokeNow(runner);
090
091        return runner.result;
092    }
093
094    /**
095     * Displays a file load dialog, using the specified default directory and file name
096     */
097    public static String chooseFile(String title, String defaultDir, String defaultName)
098    {
099        return chooseFile(title, defaultDir, defaultName, null);
100    }
101
102    /**
103     * Displays a file load dialog, using the specified default directory and file name
104     */
105    public static String chooseFile(String defaultDir, String defaultName)
106    {
107        return chooseFile("Load file...", defaultDir, defaultName);
108    }
109
110    /**
111     * Displays a file load dialog
112     */
113    public static String chooseFile()
114    {
115        return chooseFile(null, null);
116    }
117
118}