From b6dd75b9791d4f61b9636ae808e158075cca96df Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 11 Dec 2019 22:08:43 +0100 Subject: [PATCH] Implemented saving the last accessed file system path When loading or saving a game file, the file chooser dialog opens inside the directory that has been last accessed by the user. In case of no previous access, the home directory is used instead. Closes #14 --- src/dev/kske/chess/ui/DialogUtil.java | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/dev/kske/chess/ui/DialogUtil.java b/src/dev/kske/chess/ui/DialogUtil.java index 0621666..b8dc9b1 100644 --- a/src/dev/kske/chess/ui/DialogUtil.java +++ b/src/dev/kske/chess/ui/DialogUtil.java @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.prefs.Preferences; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; @@ -32,22 +33,34 @@ public class DialogUtil { private DialogUtil() {} + /** + * Saves the last accessed folder for loading and saving game files. + */ + private static Preferences preferences = Preferences.userNodeForPackage(DialogUtil.class); + public static void showFileSelectionDialog(Component parent, Consumer> action, Collection filters) { - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); - fileChooser.setAcceptAllFileFilterUsed(false); - filters.forEach(fileChooser::addChoosableFileFilter); - if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) action.accept(Arrays.asList(fileChooser.getSelectedFile())); + JFileChooser fileChooser = createFileChooser(filters); + if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { + action.accept(Arrays.asList(fileChooser.getSelectedFile())); + preferences.put("path", fileChooser.getSelectedFile().getParent()); + } } public static void showFileSaveDialog(Component parent, Consumer action, Collection filters) { + JFileChooser fileChooser = createFileChooser(filters); + if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) { + action.accept(new File(fileChooser.getSelectedFile().getAbsolutePath() + "." + + ((FileNameExtensionFilter) fileChooser.getFileFilter()).getExtensions()[0])); + preferences.put("path", fileChooser.getSelectedFile().getParent()); + } + } + + private static JFileChooser createFileChooser(Collection filters) { JFileChooser fileChooser = new JFileChooser(); - fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); + fileChooser.setCurrentDirectory(new File(preferences.get("path", System.getProperty("user.home")))); fileChooser.setAcceptAllFileFilterUsed(false); filters.forEach(fileChooser::addChoosableFileFilter); - if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) action.accept( - new File(fileChooser.getSelectedFile().getAbsolutePath() + "." - + ((FileNameExtensionFilter) fileChooser.getFileFilter()).getExtensions()[0])); + return fileChooser; } public static void showGameConfigurationDialog(Component parent, BiConsumer action) {