From ff7611b70f230fdd65b64935da10a79cea72ce28 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Wed, 9 Mar 2011 13:21:17 +0000
Subject: [PATCH] LMS-2102 Expand the options available in the file chooser
 utils.

SVN: 20265
---
 .../cisd/common/gui/FileChooserUtils.java     | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/common/source/java/ch/systemsx/cisd/common/gui/FileChooserUtils.java b/common/source/java/ch/systemsx/cisd/common/gui/FileChooserUtils.java
index ea3ed6ac66d..62895adb65a 100644
--- a/common/source/java/ch/systemsx/cisd/common/gui/FileChooserUtils.java
+++ b/common/source/java/ch/systemsx/cisd/common/gui/FileChooserUtils.java
@@ -85,6 +85,37 @@ public class FileChooserUtils
 
     }
 
+    /**
+     * Let the user choose a file or directory. Start the selection process in
+     * <var>initialDirectoryOrNull</var> (defaulted to the home directory if it is null). The
+     * windows will be shown relative to <var>parentFrame</var>.
+     * 
+     * @return The new file or directory if the user approved the selection or <code>null</code> if
+     *         the user cancelled the selection.
+     */
+    public static File tryChooseFileOrDirectory(Frame parentFrame, File initialDirectoryOrNull)
+    {
+
+        // We can't use the awt file chooser to select files *or* directories (it only allows
+        // selection of one or the other).
+        File initialDirectory =
+                (initialDirectoryOrNull != null) ? initialDirectoryOrNull : new File(
+                        System.getProperty("user.home"));
+
+        final JFileChooser fileChooser = new JFileChooser(initialDirectory);
+        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
+        fileChooser.setDialogTitle("Select a directory or file");
+        final int returnVal = fileChooser.showOpenDialog(parentFrame);
+        if (returnVal == JFileChooser.APPROVE_OPTION)
+        {
+            return fileChooser.getSelectedFile();
+        } else
+        {
+            return null;
+        }
+
+    }
+
     private static String getTitle(boolean chooseDirectories)
     {
         return "Select a " + (chooseDirectories ? "Directory" : "File");
-- 
GitLab