General tidying and access tightening.

This commit is contained in:
neviyn 2016-02-29 02:55:31 +00:00
parent 00a4355025
commit 322896bc31
11 changed files with 112 additions and 44 deletions

View File

@ -9,14 +9,17 @@ import java.util.stream.Collectors;
public final class ConfigManager { public final class ConfigManager {
static final String settingsFilename = "settings.cfg"; private static final String settingsFilename = "settings.cfg";
static final File propertiesFile = new File(settingsFilename); private static final File propertiesFile = new File(settingsFilename);
static final Properties librarySettings = new Properties(); private static final Properties librarySettings = new Properties();
// Config Keys // Config Keys
static final String foldersKey = "libraryFolders"; private static final String foldersKey = "libraryFolders";
static final String databaseKey = "databaseDir"; private static final String databaseKey = "databaseDir";
static final String libraryDisplayKey = "libraryDisplayKey"; private static final String libraryDisplayKey = "libraryDisplayKey";
/**
* @return List of directories used for music library indexing.
*/
public static List<File> getLibraryDirectories(){ public static List<File> getLibraryDirectories(){
try(FileInputStream inputStream = new FileInputStream(propertiesFile)){ try(FileInputStream inputStream = new FileInputStream(propertiesFile)){
librarySettings.load(inputStream); librarySettings.load(inputStream);
@ -33,6 +36,10 @@ public final class ConfigManager {
return new ArrayList<>(); 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<File> folderList){ public static void saveLibraryDirectories(List<File> folderList){
librarySettings.setProperty(foldersKey, folderList.stream().map(File::toString).collect(Collectors.joining(","))); librarySettings.setProperty(foldersKey, folderList.stream().map(File::toString).collect(Collectors.joining(",")));
try(FileWriter fileWriter = new FileWriter(settingsFilename)){ 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() { public static String getDatabaseConnectionString() {
try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { try (FileInputStream inputStream = new FileInputStream(propertiesFile)) {
librarySettings.load(inputStream); 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. return "jdbc:hsqldb:mem:."; // If not set use an in-memory database for now.
} }
public static void setDatabaseFile(String fileName){ /**
librarySettings.setProperty(databaseKey, fileName); * @return Index of the last library display mode used.
try(FileWriter fileWriter = new FileWriter(settingsFilename)){ */
librarySettings.store(fileWriter, "");
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getLastDisplayTypeIndex(){ public static int getLastDisplayTypeIndex(){
try (FileInputStream inputStream = new FileInputStream(propertiesFile)) { try (FileInputStream inputStream = new FileInputStream(propertiesFile)) {
librarySettings.load(inputStream); librarySettings.load(inputStream);
@ -73,6 +77,10 @@ public final class ConfigManager {
return 0; return 0;
} }
/**
* Set the stored index of the last library display mode used.
* @param index Display mode index.
*/
public static void setLastDisplayTypeIndex(Integer index){ public static void setLastDisplayTypeIndex(Integer index){
librarySettings.setProperty(libraryDisplayKey, index.toString()); librarySettings.setProperty(libraryDisplayKey, index.toString());
try(FileWriter fileWriter = new FileWriter(settingsFilename)){ try(FileWriter fileWriter = new FileWriter(settingsFilename)){

View File

@ -7,9 +7,9 @@ import java.awt.BorderLayout;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
public class LibraryConfigGUI { class LibraryConfigGUI {
private JList listLibraryFolders; private JList listLibraryFolders;
private LibraryListModel listModel = new LibraryListModel(); private final LibraryListModel listModel = new LibraryListModel();
private JPanel mainPanel; private JPanel mainPanel;
public LibraryConfigGUI(){ public LibraryConfigGUI(){

View File

@ -7,9 +7,9 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreeCellRenderer;
import java.awt.*; import java.awt.*;
public class LibraryTreeCellRenderer implements TreeCellRenderer { class LibraryTreeCellRenderer implements TreeCellRenderer {
private JLabel label; private final JLabel label;
LibraryTreeCellRenderer() { LibraryTreeCellRenderer() {
label = new JLabel(); label = new JLabel();

View File

@ -13,16 +13,13 @@ import org.jaudiotagger.tag.TagException;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; 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)"; private static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)";
static Thread updaterThread; private static Thread updaterThread;
/** /**
* Add all songs contained with all paths in rootDirectory to the database. * Add all songs contained with all paths in rootDirectory to the database.

View File

@ -11,14 +11,14 @@ import org.gstreamer.elements.PlayBin2;
import java.io.File; import java.io.File;
import java.util.Optional; import java.util.Optional;
public class Player { class Player {
private PlayBin2 playBin; private final PlayBin2 playBin;
private InternalThread internalThread; private InternalThread internalThread;
private Thread thread; private Thread thread;
private Song currentSong; private Song currentSong;
private PlayerCallbackInterface callbackInterface; private final PlayerCallbackInterface callbackInterface;
private int currentVolume = 100; private int currentVolume = 100;
/** /**

View File

@ -25,18 +25,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface { public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
public JPanel mainPanel; private JPanel mainPanel;
private JTree libraryView = new JTree(); private final JTree libraryView = new JTree();
private JTable playList = new JTable(); private final JTable playList = new JTable();
private JSlider seekBar; private JSlider seekBar;
private JComboBox libraryDisplayType = new JComboBox(); private final JComboBox libraryDisplayType = new JComboBox();
private Player player = new Player(this); private final Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private final PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private static DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode();
private boolean libraryUpdating = false; private boolean libraryUpdating = false;
private AtomicBoolean seeking = new AtomicBoolean(false); private final AtomicBoolean seeking = new AtomicBoolean(false);
private Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap(); private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
public PlayerGUI() { public PlayerGUI() {
createUI(); createUI();
@ -186,7 +186,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
/** /**
* Refresh the library with songs in the currently selected display format. * Refresh the library with songs in the currently selected display format.
*/ */
public void refreshLibrary() { private void refreshLibrary() {
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Refreshing Library..."); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Refreshing Library...");

View File

@ -2,8 +2,6 @@ package musicplayer.callbacks;
import musicplayer.model.Song; import musicplayer.model.Song;
import java.util.Optional;
public interface PlayerCallbackInterface { public interface PlayerCallbackInterface {
/** /**

View File

@ -15,7 +15,7 @@ import java.util.Properties;
* Singleton for managing connection to Hibernate. * Singleton for managing connection to Hibernate.
*/ */
public class DatabaseManager { public class DatabaseManager {
private static DatabaseManager ourInstance = new DatabaseManager(); private static final DatabaseManager ourInstance = new DatabaseManager();
private SessionFactory sessionFactory; private SessionFactory sessionFactory;

View File

@ -35,7 +35,7 @@ public class Song implements Comparable<Song> {
private int discNumber; private int discNumber;
@Transient @Transient
private static int discNumberComparator = 1000; private static final int discNumberComparator = 1000;
protected Song() { protected Song() {
} }

View File

@ -19,6 +19,10 @@ public class LibraryListModel extends AbstractListModel {
return libraryFolders.get(index); 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){ public void addFolder(File folder){
if(folder.exists() && folder.isDirectory()) { if(folder.exists() && folder.isDirectory()) {
libraryFolders.add(folder); libraryFolders.add(folder);
@ -26,14 +30,26 @@ public class LibraryListModel extends AbstractListModel {
} }
} }
/**
* @return List of directories currently used for library indexing.
*/
public List<File> currentFolderList(){ public List<File> currentFolderList(){
return new ArrayList<>(libraryFolders); // Copy? Don't want modification via here. 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){ public boolean contains(File file){
return libraryFolders.contains(file); return libraryFolders.contains(file);
} }
/**
* Set the list contained in libraryFolders.
* @param folderList New directory list.
*/
public void setFolderList(List<File> folderList){ public void setFolderList(List<File> folderList){
if(folderList != null && folderList.size() > 0) if(folderList != null && folderList.size() > 0)
libraryFolders = new ArrayList<>(folderList); libraryFolders = new ArrayList<>(folderList);
@ -42,6 +58,10 @@ public class LibraryListModel extends AbstractListModel {
fireContentsChanged(this, 0, libraryFolders.size() == 0 ? 0 : libraryFolders.size() - 1); 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){ public void removeFile(File file){
libraryFolders.remove(file); libraryFolders.remove(file);
fireContentsChanged(this, 0, libraryFolders.size() == 0 ? 0 : libraryFolders.size() - 1); fireContentsChanged(this, 0, libraryFolders.size() == 0 ? 0 : libraryFolders.size() - 1);

View File

@ -10,7 +10,7 @@ import java.util.stream.IntStream;
public class PlaylistTableModel extends AbstractTableModel { public class PlaylistTableModel extends AbstractTableModel {
private List<Song> songList; private List<Song> songList;
private int playingRow = -1; private int playingRow = -1; // -1 means no song is currently playing.
public PlaylistTableModel(List<Song> songList){ public PlaylistTableModel(List<Song> songList){
this.songList = songList; this.songList = songList;
@ -58,31 +58,56 @@ public class PlaylistTableModel extends AbstractTableModel {
return name; return name;
} }
/**
* Add a song to the current playlist.
* @param song Song to add to the playlist.
*/
public void addSong(Song song){ public void addSong(Song song){
songList.add(song); songList.add(song);
fireTableDataChanged(); fireTableDataChanged();
} }
/**
* @return The first song in the playlist.
*/
public Optional<Song> getFirst(){ public Optional<Song> getFirst(){
return songList.size() > 0 ? Optional.of(songList.get(0)) : Optional.<Song>empty(); return songList.size() > 0 ? Optional.of(songList.get(0)) : Optional.<Song>empty();
} }
/**
* Get the next song in the playlist. (Wraps around)
* @param song The current song.
* @return The song after the current song.
*/
public Optional<Song> nextSong(Song song){ public Optional<Song> nextSong(Song song){
int index = songList.indexOf(song) + 1; int index = songList.indexOf(song) + 1;
if(index >= songList.size()) index = 0; if(index >= songList.size()) index = 0;
return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); 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<Song> previous(Song song){ public Optional<Song> previous(Song song){
int index = songList.indexOf(song) - 1; int index = songList.indexOf(song) - 1;
if(index < 0) index = songList.size() - 1; if(index < 0) index = songList.size() - 1;
return songList.size() > 0 ? Optional.of(songList.get(index)) : Optional.empty(); 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){ public int getSongIndex(Song song){
return songList.indexOf(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){ public void removeSong(int index){
if(songList.size() > index && index >= 0){ if(songList.size() > index && index >= 0){
songList.remove(index); 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){ public void removeSong(int[] index){
List<Integer> indices = IntStream.of(index).boxed().collect(Collectors.toList()); List<Integer> 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); indices.forEach(this::removeSong);
} }
/**
* Remove a song from the playlist.
* @param song Song to be removed.
*/
public void removeSong(Song song){ public void removeSong(Song song){
songList.remove(song); songList.remove(song);
fireTableDataChanged(); fireTableDataChanged();
} }
/**
* Remove all songs from the playlist.
*/
public void removeAll(){ public void removeAll(){
songList.clear(); songList.clear();
fireTableDataChanged(); 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<Song> getSong(int index){ public Optional<Song> getSong(int index){
return songList.size() > 0 && index >= 0 && index < songList.size() ? Optional.of(songList.get(index)) : Optional.empty(); 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){ public void setPlayingRow(int index){
playingRow = index; playingRow = index;
fireTableDataChanged(); fireTableDataChanged();