From 4e668954a987ba80e3f438bf75cf9bc9a2b2dd92 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Fri, 26 Feb 2016 15:43:21 +0000 Subject: [PATCH] Decoupled config stuff from LibraryUtils, now configuring hibernate with database directory that can be changed during runtime. --- src/main/java/musicplayer/ConfigManager.java | 63 +++++++++++++++++++ .../java/musicplayer/LibraryConfigGUI.java | 4 +- src/main/java/musicplayer/LibraryUtils.java | 34 ---------- src/main/java/musicplayer/Player.java | 4 +- src/main/java/musicplayer/PlayerGUI.java | 23 +++---- .../callbacks/PlayerCallbackInterface.java | 4 +- .../java/musicplayer/db/DatabaseManager.java | 10 ++- src/main/resources/hibernate.cfg.xml | 27 -------- 8 files changed, 85 insertions(+), 84 deletions(-) create mode 100644 src/main/java/musicplayer/ConfigManager.java delete mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/src/main/java/musicplayer/ConfigManager.java b/src/main/java/musicplayer/ConfigManager.java new file mode 100644 index 0000000..b7e2a62 --- /dev/null +++ b/src/main/java/musicplayer/ConfigManager.java @@ -0,0 +1,63 @@ +package musicplayer; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.stream.Collectors; + +public final class ConfigManager { + + static final String settingsFilename = "settings.cfg"; + static final File propertiesFile = new File(settingsFilename); + static final Properties librarySettings = new Properties(); + // Config Keys + static final String foldersKey = "libraryFolders"; + static final String databaseKey = "databaseDir"; + + public static List getLibraryDirectories(){ + try(FileInputStream inputStream = new FileInputStream(propertiesFile)){ + librarySettings.load(inputStream); + if(librarySettings.containsKey(foldersKey)){ + return Arrays.asList(librarySettings.getProperty(foldersKey).split(",")) + .stream().map(File::new).filter(File::exists).collect(Collectors.toList()); + } + } catch (IOException ex) { + propertiesFile.getParentFile().mkdirs(); + try { + propertiesFile.createNewFile(); + } catch (IOException ignored) {} + } + return new ArrayList<>(); + } + + public static void saveLibraryDirectories(List folderList){ + librarySettings.setProperty(foldersKey, folderList.stream().map(File::toString).collect(Collectors.joining(","))); + try(FileWriter fileWriter = new FileWriter(settingsFilename)){ + librarySettings.store(fileWriter, ""); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static String getDatabaseConnectionString() { + try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { + librarySettings.load(inputStream); + if (librarySettings.containsKey(databaseKey)) { + return "jdbc:hsqldb:file:" + librarySettings.getProperty(databaseKey) + ";shutdown=true"; + } + } catch (IOException ignored) { + } + return "jdbc:hsqldb:mem:."; // If not set use an in-memory database for now. + } + + public static void setDatabaseFile(String fileName){ + librarySettings.setProperty(databaseKey, fileName); + try(FileWriter fileWriter = new FileWriter(settingsFilename)){ + librarySettings.store(fileWriter, ""); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/musicplayer/LibraryConfigGUI.java b/src/main/java/musicplayer/LibraryConfigGUI.java index 2326d50..d6c2a17 100644 --- a/src/main/java/musicplayer/LibraryConfigGUI.java +++ b/src/main/java/musicplayer/LibraryConfigGUI.java @@ -42,12 +42,12 @@ public class LibraryConfigGUI { } private void updateLibraryListContents(){ - listModel.setFolderList(LibraryUtils.getLibraryDirectories()); + listModel.setFolderList(ConfigManager.getLibraryDirectories()); } private void saveLibraryList(){ List folders = listModel.currentFolderList(); - LibraryUtils.saveLibrarySettings(folders); + ConfigManager.saveLibraryDirectories(folders); } private void addNew(){ diff --git a/src/main/java/musicplayer/LibraryUtils.java b/src/main/java/musicplayer/LibraryUtils.java index 1f8361f..5eb1d00 100644 --- a/src/main/java/musicplayer/LibraryUtils.java +++ b/src/main/java/musicplayer/LibraryUtils.java @@ -22,40 +22,6 @@ import java.util.stream.Collectors; public final class LibraryUtils { static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; - static final Properties librarySettings = new Properties(); - static final String settingsFilename = "settings.cfg"; - static final File propertiesFile = new File(settingsFilename); - private static List libraryDirectories; - static final String foldersKey = "libraryFolders"; - - public static List getLibraryDirectories(){ - loadLibrarySettings(); // Make sure libraryDirectories matches the stored version. - return libraryDirectories; - } - - public static void loadLibrarySettings(){ - try(FileInputStream inputStream = new FileInputStream(propertiesFile)){ - librarySettings.load(inputStream); - if(librarySettings.containsKey(foldersKey)){ - libraryDirectories = Arrays.asList(librarySettings.getProperty(foldersKey).split(",")) - .stream().map(File::new).filter(File::exists).collect(Collectors.toList()); - } - } catch (IOException ex) { - propertiesFile.getParentFile().mkdirs(); - try { - propertiesFile.createNewFile(); - } catch (IOException ignored) {} - } - } - - public static void saveLibrarySettings(List folderList){ - librarySettings.setProperty(foldersKey, folderList.stream().map(File::toString).collect(Collectors.joining(","))); - try(FileWriter fileWriter = new FileWriter(settingsFilename)){ - librarySettings.store(fileWriter, ""); - } catch (IOException e) { - e.printStackTrace(); - } - } public static void processSongsWithCallback(List rootDirectory, LibraryCallbackInterface callbackInterface){ Thread thread = new Thread(() -> { diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index b811155..173e047 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -37,7 +37,7 @@ public class Player { } catch (InterruptedException e) { e.printStackTrace(); } - playSong(callbackInterface.getNextSong(currentSong)); + callbackInterface.playNextSong(); }); callbackThing.start(); }); @@ -57,8 +57,8 @@ public class Player { callbackInterface.setSongHighlighting(currentSong); File songFile = currentSong.getSongFile(); if (!songFile.exists()) { + callbackInterface.playNextSong(); callbackInterface.removeInvalidSong(currentSong); - playSong(callbackInterface.getNextSong(currentSong)); return; } playBin.setURI(songFile.toURI()); diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index dd35094..ccdec90 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -24,7 +24,7 @@ import java.util.stream.Collectors; public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface { public JPanel mainPanel; - private JTree libraryView; + private JTree libraryView = new JTree(); private JTable playList = new JTable(); private JSlider seekBar; private JComboBox libraryDisplayType = new JComboBox(); @@ -33,12 +33,13 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private static DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private boolean libraryUpdating = false; - private Map libraryDisplayVariants = createDisplayVariantMap(); + private Map libraryDisplayVariants; public PlayerGUI() { createUI(); DatabaseManager.init(); resetTree(); + libraryDisplayVariants = createDisplayVariantMap(); libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); Thread seekBarUpdater = new Thread(() -> { boolean running = true; @@ -135,17 +136,6 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf playList.revalidate(); } - /** - * Get the next song in the playlist. - * - * @param currentSong The song that is currently selected. - * @return The next song to be selected. - */ - @Override - public Optional getNextSong(Song currentSong) { - return playlistTableModel.nextSong(currentSong); - } - /** * Set the highlighted song in the playlist to this song. * @@ -163,7 +153,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf */ public void playNextSong() { SwingUtilities.invokeLater(() -> seekBar.setValue(0)); - player.playSong(getNextSong(player.getCurrentSong())); + player.playSong(playlistTableModel.nextSong(player.getCurrentSong())); } /** @@ -213,7 +203,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf addNodeToTreeModel(model, parentNode, newNode); addNodeToTreeModel(model, parentNode, updatingNode); libraryView.setModel(model); - List dirs = LibraryUtils.getLibraryDirectories().stream().map(File::toPath).collect(Collectors.toList()); + List dirs = ConfigManager.getLibraryDirectories().stream().map(File::toPath).collect(Collectors.toList()); LibraryUtils.processSongsWithCallback(dirs, this); } @@ -281,7 +271,6 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf } private JTree createLibraryArea() { - libraryView = new JTree(); libraryView.setRootVisible(false); libraryView.setToggleClickCount(1); libraryView.setCellRenderer(new LibraryTreeCellRenderer()); @@ -296,6 +285,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf playList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); playList.getColumnModel().getColumn(0).setPreferredWidth(10); playList.getColumnModel().getColumn(0).setMaxWidth(10); + playList.getColumnModel().getColumn(1).setPreferredWidth(30); + playList.getColumnModel().getColumn(1).setMaxWidth(30); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("Remove"); menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows())); diff --git a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java index f081b91..fe15404 100644 --- a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java +++ b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java @@ -6,8 +6,6 @@ import java.util.Optional; public interface PlayerCallbackInterface { - Optional getNextSong(Song currentSong); - void setSongHighlighting(Song playingSong); void setSeekBarDuration(int seconds); @@ -16,4 +14,6 @@ public interface PlayerCallbackInterface { void playerStopped(); + void playNextSong(); + } diff --git a/src/main/java/musicplayer/db/DatabaseManager.java b/src/main/java/musicplayer/db/DatabaseManager.java index 3bfd7e4..1c78fff 100644 --- a/src/main/java/musicplayer/db/DatabaseManager.java +++ b/src/main/java/musicplayer/db/DatabaseManager.java @@ -1,5 +1,6 @@ package musicplayer.db; +import musicplayer.ConfigManager; import musicplayer.model.Album; import musicplayer.model.Artist; import musicplayer.model.Song; @@ -31,7 +32,14 @@ public class DatabaseManager { public static void init() { if (getInstance().sessionFactory != null) getInstance().sessionFactory.close(); - getInstance().sessionFactory = new Configuration().configure() + Properties properties = new Properties(); + properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); + properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver"); + properties.put("hibernate.connection.url", ConfigManager.getDatabaseConnectionString()); + properties.put("hibernate.hbm2ddl.auto", "update"); + properties.put("hibernate.enable_lazy_load_no_trans", "true"); + getInstance().sessionFactory = new Configuration() + .addProperties(properties) .addAnnotatedClass(Album.class) .addAnnotatedClass(Artist.class) .addAnnotatedClass(Song.class) diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml deleted file mode 100644 index 370c017..0000000 --- a/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - org.hibernate.dialect.HSQLDialect - - - org.hsqldb.jdbc.JDBCDriver - - - jdbc:hsqldb:file:E:/dev/testdb;shutdown=true - - - root - - - root123 - - - update - - - true - - - \ No newline at end of file