From 322896bc3151ea7bfc2e1063dfe02d1e20cbda6f Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 29 Feb 2016 02:55:31 +0000 Subject: [PATCH] General tidying and access tightening. --- src/main/java/musicplayer/ConfigManager.java | 38 ++++++++------ .../java/musicplayer/LibraryConfigGUI.java | 4 +- .../musicplayer/LibraryTreeCellRenderer.java | 4 +- src/main/java/musicplayer/LibraryUtils.java | 9 ++-- src/main/java/musicplayer/Player.java | 6 +-- src/main/java/musicplayer/PlayerGUI.java | 20 ++++---- .../callbacks/PlayerCallbackInterface.java | 2 - .../java/musicplayer/db/DatabaseManager.java | 2 +- src/main/java/musicplayer/model/Song.java | 2 +- .../swingmodels/LibraryListModel.java | 20 ++++++++ .../swingmodels/PlaylistTableModel.java | 49 ++++++++++++++++++- 11 files changed, 112 insertions(+), 44 deletions(-) diff --git a/src/main/java/musicplayer/ConfigManager.java b/src/main/java/musicplayer/ConfigManager.java index 019b91b..c5ba5b4 100644 --- a/src/main/java/musicplayer/ConfigManager.java +++ b/src/main/java/musicplayer/ConfigManager.java @@ -9,14 +9,17 @@ 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(); + private static final String settingsFilename = "settings.cfg"; + private static final File propertiesFile = new File(settingsFilename); + private static final Properties librarySettings = new Properties(); // Config Keys - static final String foldersKey = "libraryFolders"; - static final String databaseKey = "databaseDir"; - static final String libraryDisplayKey = "libraryDisplayKey"; + private static final String foldersKey = "libraryFolders"; + private static final String databaseKey = "databaseDir"; + private static final String libraryDisplayKey = "libraryDisplayKey"; + /** + * @return List of directories used for music library indexing. + */ public static List getLibraryDirectories(){ try(FileInputStream inputStream = new FileInputStream(propertiesFile)){ librarySettings.load(inputStream); @@ -33,6 +36,10 @@ public final class ConfigManager { return new ArrayList<>(); } + /** + * Update the list of folders used for music library indexing. + * @param folderList Complete list of music folders. + */ public static void saveLibraryDirectories(List folderList){ librarySettings.setProperty(foldersKey, folderList.stream().map(File::toString).collect(Collectors.joining(","))); try(FileWriter fileWriter = new FileWriter(settingsFilename)){ @@ -42,6 +49,9 @@ public final class ConfigManager { } } + /** + * @return String used to connect to hibernate database. + */ public static String getDatabaseConnectionString() { try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { librarySettings.load(inputStream); @@ -53,15 +63,9 @@ public final class ConfigManager { 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(); - } - } - + /** + * @return Index of the last library display mode used. + */ public static int getLastDisplayTypeIndex(){ try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { librarySettings.load(inputStream); @@ -73,6 +77,10 @@ public final class ConfigManager { return 0; } + /** + * Set the stored index of the last library display mode used. + * @param index Display mode index. + */ public static void setLastDisplayTypeIndex(Integer index){ librarySettings.setProperty(libraryDisplayKey, index.toString()); try(FileWriter fileWriter = new FileWriter(settingsFilename)){ diff --git a/src/main/java/musicplayer/LibraryConfigGUI.java b/src/main/java/musicplayer/LibraryConfigGUI.java index d6c2a17..e6f6436 100644 --- a/src/main/java/musicplayer/LibraryConfigGUI.java +++ b/src/main/java/musicplayer/LibraryConfigGUI.java @@ -7,9 +7,9 @@ import java.awt.BorderLayout; import java.io.File; import java.util.List; -public class LibraryConfigGUI { +class LibraryConfigGUI { private JList listLibraryFolders; - private LibraryListModel listModel = new LibraryListModel(); + private final LibraryListModel listModel = new LibraryListModel(); private JPanel mainPanel; public LibraryConfigGUI(){ diff --git a/src/main/java/musicplayer/LibraryTreeCellRenderer.java b/src/main/java/musicplayer/LibraryTreeCellRenderer.java index 381ebb3..af849fe 100644 --- a/src/main/java/musicplayer/LibraryTreeCellRenderer.java +++ b/src/main/java/musicplayer/LibraryTreeCellRenderer.java @@ -7,9 +7,9 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeCellRenderer; import java.awt.*; -public class LibraryTreeCellRenderer implements TreeCellRenderer { +class LibraryTreeCellRenderer implements TreeCellRenderer { - private JLabel label; + private final JLabel label; LibraryTreeCellRenderer() { label = new JLabel(); diff --git a/src/main/java/musicplayer/LibraryUtils.java b/src/main/java/musicplayer/LibraryUtils.java index 0eaf0c6..7945fc4 100644 --- a/src/main/java/musicplayer/LibraryUtils.java +++ b/src/main/java/musicplayer/LibraryUtils.java @@ -13,16 +13,13 @@ import org.jaudiotagger.tag.TagException; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import java.util.Optional; -import java.util.Properties; -import java.util.stream.Collectors; -public final class LibraryUtils { +final class LibraryUtils { - static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; - static Thread updaterThread; + private static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; + private static Thread updaterThread; /** * Add all songs contained with all paths in rootDirectory to the database. diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index a3aca37..d371005 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -11,14 +11,14 @@ import org.gstreamer.elements.PlayBin2; import java.io.File; import java.util.Optional; -public class Player { +class Player { - private PlayBin2 playBin; + private final PlayBin2 playBin; private InternalThread internalThread; private Thread thread; private Song currentSong; - private PlayerCallbackInterface callbackInterface; + private final PlayerCallbackInterface callbackInterface; private int currentVolume = 100; /** diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 6203674..ee6f0fd 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -25,18 +25,18 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface { - public JPanel mainPanel; - private JTree libraryView = new JTree(); - private JTable playList = new JTable(); + private JPanel mainPanel; + private final JTree libraryView = new JTree(); + private final JTable playList = new JTable(); private JSlider seekBar; - private JComboBox libraryDisplayType = new JComboBox(); - private Player player = new Player(this); - private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); - private static DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); + private final JComboBox libraryDisplayType = new JComboBox(); + private final Player player = new Player(this); + private final PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); + private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private boolean libraryUpdating = false; - private AtomicBoolean seeking = new AtomicBoolean(false); + private final AtomicBoolean seeking = new AtomicBoolean(false); - private Map libraryDisplayVariants = createDisplayVariantMap(); + private final Map libraryDisplayVariants = createDisplayVariantMap(); public PlayerGUI() { createUI(); @@ -186,7 +186,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf /** * Refresh the library with songs in the currently selected display format. */ - public void refreshLibrary() { + private void refreshLibrary() { DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Refreshing Library..."); diff --git a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java index 9243866..f829a06 100644 --- a/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java +++ b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java @@ -2,8 +2,6 @@ package musicplayer.callbacks; import musicplayer.model.Song; -import java.util.Optional; - public interface PlayerCallbackInterface { /** diff --git a/src/main/java/musicplayer/db/DatabaseManager.java b/src/main/java/musicplayer/db/DatabaseManager.java index 1c78fff..976cd07 100644 --- a/src/main/java/musicplayer/db/DatabaseManager.java +++ b/src/main/java/musicplayer/db/DatabaseManager.java @@ -15,7 +15,7 @@ import java.util.Properties; * Singleton for managing connection to Hibernate. */ public class DatabaseManager { - private static DatabaseManager ourInstance = new DatabaseManager(); + private static final DatabaseManager ourInstance = new DatabaseManager(); private SessionFactory sessionFactory; diff --git a/src/main/java/musicplayer/model/Song.java b/src/main/java/musicplayer/model/Song.java index 7bf88d5..513f333 100644 --- a/src/main/java/musicplayer/model/Song.java +++ b/src/main/java/musicplayer/model/Song.java @@ -35,7 +35,7 @@ public class Song implements Comparable { private int discNumber; @Transient - private static int discNumberComparator = 1000; + private static final int discNumberComparator = 1000; protected Song() { } diff --git a/src/main/java/musicplayer/swingmodels/LibraryListModel.java b/src/main/java/musicplayer/swingmodels/LibraryListModel.java index 4a0c78e..b0a4fc5 100644 --- a/src/main/java/musicplayer/swingmodels/LibraryListModel.java +++ b/src/main/java/musicplayer/swingmodels/LibraryListModel.java @@ -19,6 +19,10 @@ public class LibraryListModel extends AbstractListModel { return libraryFolders.get(index); } + /** + * Add a directory to the list of directories used for library indexing. + * @param folder Directory to add. + */ public void addFolder(File folder){ if(folder.exists() && folder.isDirectory()) { libraryFolders.add(folder); @@ -26,14 +30,26 @@ public class LibraryListModel extends AbstractListModel { } } + /** + * @return List of directories currently used for library indexing. + */ public List currentFolderList(){ return new ArrayList<>(libraryFolders); // Copy? Don't want modification via here. } + /** + * Check if this File already exists in libraryFolders. + * @param file File to find. + * @return Does file exist in libraryFolders. + */ public boolean contains(File file){ return libraryFolders.contains(file); } + /** + * Set the list contained in libraryFolders. + * @param folderList New directory list. + */ public void setFolderList(List folderList){ if(folderList != null && folderList.size() > 0) libraryFolders = new ArrayList<>(folderList); @@ -42,6 +58,10 @@ public class LibraryListModel extends AbstractListModel { fireContentsChanged(this, 0, libraryFolders.size() == 0 ? 0 : libraryFolders.size() - 1); } + /** + * Remove a directory from libraryFolders. + * @param file Directory to be removed. + */ public void removeFile(File file){ libraryFolders.remove(file); fireContentsChanged(this, 0, libraryFolders.size() == 0 ? 0 : libraryFolders.size() - 1); diff --git a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java index 41a2f1f..351c8b3 100644 --- a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java +++ b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java @@ -10,7 +10,7 @@ import java.util.stream.IntStream; public class PlaylistTableModel extends AbstractTableModel { private List songList; - private int playingRow = -1; + private int playingRow = -1; // -1 means no song is currently playing. public PlaylistTableModel(List songList){ this.songList = songList; @@ -58,31 +58,56 @@ public class PlaylistTableModel extends AbstractTableModel { return name; } + /** + * Add a song to the current playlist. + * @param song Song to add to the playlist. + */ public void addSong(Song song){ songList.add(song); fireTableDataChanged(); } + /** + * @return The first song in the playlist. + */ public Optional getFirst(){ return songList.size() > 0 ? Optional.of(songList.get(0)) : Optional.empty(); } + /** + * Get the next song in the playlist. (Wraps around) + * @param song The current song. + * @return The song after the current song. + */ public Optional nextSong(Song song){ int index = songList.indexOf(song) + 1; if(index >= songList.size()) index = 0; return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); } + /** + * Get the previous song in the playlist. (Wraps around) + * @param song The current song. + * @return The song before the current song. + */ public Optional previous(Song song){ int index = songList.indexOf(song) - 1; if(index < 0) index = songList.size() - 1; return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); } + /** + * @param song Song to find in the current playlist. + * @return Index of song in the playlist. + */ public int getSongIndex(Song song){ return songList.indexOf(song); } + /** + * Remove the song at index from the playlist. + * @param index List index of the song to be removed. + */ public void removeSong(int index){ if(songList.size() > index && index >= 0){ songList.remove(index); @@ -90,26 +115,46 @@ public class PlaylistTableModel extends AbstractTableModel { } } + /** + * Remove a number of songs from the playlist. + * @param index Indices of the songs to be removed. + */ public void removeSong(int[] index){ List indices = IntStream.of(index).boxed().collect(Collectors.toList()); - Collections.reverse(indices); + Collections.reverse(indices); // Do removals in reverse order to prevent mis-deletion. indices.forEach(this::removeSong); } + /** + * Remove a song from the playlist. + * @param song Song to be removed. + */ public void removeSong(Song song){ songList.remove(song); fireTableDataChanged(); } + /** + * Remove all songs from the playlist. + */ public void removeAll(){ songList.clear(); fireTableDataChanged(); } + /** + * Get a Song from the playlist based on its index. + * @param index Index of the Song to get. + * @return Song object at index. + */ public Optional getSong(int index){ return songList.size() > 0 && index >= 0 && index < songList.size() ? Optional.of(songList.get(index)) : Optional.empty(); } + /** + * Set the row at index to have a playing icon. + * @param index Row index to display as playing. + */ public void setPlayingRow(int index){ playingRow = index; fireTableDataChanged();