From f97a302c79ce8d50520711b732adc0ad2266a555 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Fri, 26 Feb 2016 22:00:33 +0000 Subject: [PATCH] Now remembers the last library display mode that was selected. --- src/main/java/musicplayer/ConfigManager.java | 21 ++++++++++++++++++++ src/main/java/musicplayer/PlayerGUI.java | 14 +++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/musicplayer/ConfigManager.java b/src/main/java/musicplayer/ConfigManager.java index cec5730..019b91b 100644 --- a/src/main/java/musicplayer/ConfigManager.java +++ b/src/main/java/musicplayer/ConfigManager.java @@ -15,6 +15,7 @@ public final class ConfigManager { // Config Keys static final String foldersKey = "libraryFolders"; static final String databaseKey = "databaseDir"; + static final String libraryDisplayKey = "libraryDisplayKey"; public static List getLibraryDirectories(){ try(FileInputStream inputStream = new FileInputStream(propertiesFile)){ @@ -60,4 +61,24 @@ public final class ConfigManager { e.printStackTrace(); } } + + public static int getLastDisplayTypeIndex(){ + try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { + librarySettings.load(inputStream); + if (librarySettings.containsKey(libraryDisplayKey)) { + return Integer.parseInt(librarySettings.getProperty(libraryDisplayKey)); + } + } catch (IOException ignored) { + } + return 0; + } + + public static void setLastDisplayTypeIndex(Integer index){ + librarySettings.setProperty(libraryDisplayKey, index.toString()); + try(FileWriter fileWriter = new FileWriter(settingsFilename)){ + librarySettings.store(fileWriter, ""); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index c2ff89f..d22582f 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -33,14 +33,12 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private static DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private boolean libraryUpdating = false; - private Map libraryDisplayVariants; + private Map libraryDisplayVariants = createDisplayVariantMap(); public PlayerGUI() { createUI(); DatabaseManager.init(); resetTree(); - libraryDisplayVariants = createDisplayVariantMap(); - libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); Thread seekBarUpdater = new Thread(() -> { boolean running = true; while (running) { @@ -53,7 +51,6 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf } }); seekBarUpdater.start(); - createMenuBar(); refreshLibrary(); } @@ -256,9 +253,14 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf final JPanel libraryAndComboboxPane = new JPanel(); libraryAndComboboxPane.setLayout(new BorderLayout(0, 0)); libraryAndPlaylistPane.setLeftComponent(libraryAndComboboxPane); + libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); + libraryDisplayType.setSelectedIndex(ConfigManager.getLastDisplayTypeIndex()); libraryDisplayType.addItemListener(e -> { - if (!libraryUpdating && e.getStateChange() == ItemEvent.SELECTED) - libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run(); + if (e.getStateChange() == ItemEvent.SELECTED) { + ConfigManager.setLastDisplayTypeIndex(libraryDisplayType.getSelectedIndex()); + if(!libraryUpdating) + libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run(); + } }); libraryAndComboboxPane.add(libraryDisplayType, BorderLayout.NORTH); final JScrollPane libraryPane = new JScrollPane();