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 ea3ed6ac66d08869ae9488a3e415655dbca8b462..62895adb65a4e4b32cc3800b6848171b9e2be126 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");