General tidying and access tightening.
This commit is contained in:
parent
00a4355025
commit
322896bc31
@ -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<File> 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<File> 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)){
|
||||
|
@ -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(){
|
||||
|
@ -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();
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -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<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
|
||||
private final Map<String, Runnable> 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...");
|
||||
|
@ -2,8 +2,6 @@ package musicplayer.callbacks;
|
||||
|
||||
import musicplayer.model.Song;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PlayerCallbackInterface {
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
||||
|
@ -35,7 +35,7 @@ public class Song implements Comparable<Song> {
|
||||
private int discNumber;
|
||||
|
||||
@Transient
|
||||
private static int discNumberComparator = 1000;
|
||||
private static final int discNumberComparator = 1000;
|
||||
|
||||
protected Song() {
|
||||
}
|
||||
|
@ -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<File> 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<File> 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);
|
||||
|
@ -10,7 +10,7 @@ import java.util.stream.IntStream;
|
||||
public class PlaylistTableModel extends AbstractTableModel {
|
||||
|
||||
private List<Song> songList;
|
||||
private int playingRow = -1;
|
||||
private int playingRow = -1; // -1 means no song is currently playing.
|
||||
|
||||
public PlaylistTableModel(List<Song> 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<Song> getFirst(){
|
||||
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){
|
||||
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<Song> 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<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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Song> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user