Added functionality to save playlists.

This commit is contained in:
neviyn 2016-03-07 16:28:34 +00:00
parent 36cc0da306
commit 148eb38bba
3 changed files with 66 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import musicplayer.model.Song;
import musicplayer.swingmodels.PlaylistTableModel; import musicplayer.swingmodels.PlaylistTableModel;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode; import javax.swing.tree.TreeNode;
@ -18,6 +19,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
@ -419,6 +421,38 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
// Add everything to the menu bar itself // Add everything to the menu bar itself
menuBar.add(tools); menuBar.add(tools);
JMenu playlistTools = new JMenu("Playlist");
menuItem = new JMenuItem("Clear All");
menuItem.addActionListener((e) -> {
playlistTableModel.removeAll();
player.stop();
});
playlistTools.add(menuItem);
menuItem = new JMenuItem("Save Playlist");
menuItem.addActionListener(e -> {
if(!playlistTableModel.isEmpty()) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser.setSelectedFile(new File("playlist.m3u"));
fileChooser.setFileFilter(new FileNameExtensionFilter("m3u playlist", "m3u"));
if (fileChooser.showSaveDialog(mainPanel) == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().toString();
if (!filename.endsWith(".m3u"))
filename += ".m3u";
try {
playlistTableModel.writePlaylistToFile(new File(filename));
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage() + "\n" + Arrays.toString(e1.getStackTrace()), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
});
playlistTools.add(menuItem);
// Add everything to the menu bar itself
menuBar.add(playlistTools);
return menuBar; return menuBar;
} }

View File

@ -132,4 +132,11 @@ public class Song implements Comparable<Song>, IDBType {
public int compareTo(Song o) { public int compareTo(Song o) {
return (trackNumber - o.getTrackNumber()) + ((discNumber - o.getDiscNumber()) * discNumberComparator); return (trackNumber - o.getTrackNumber()) + ((discNumber - o.getDiscNumber()) * discNumberComparator);
} }
/**
* @return String that can be used to represent this song in an m3u format playlist file.
*/
public String getM3UFormatString(){
return String.format("#EXTINF:0,%s - %s\n%s\n", getArtist().getName(), getTitle(), getSongFile().getAbsolutePath());
}
} }

View File

@ -3,6 +3,7 @@ package musicplayer.swingmodels;
import musicplayer.model.Song; import musicplayer.model.Song;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.io.*;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -136,6 +137,13 @@ public class PlaylistTableModel extends AbstractTableModel {
fireTableDataChanged(); fireTableDataChanged();
} }
/**
* @return Is the playlist currently empty.
*/
public boolean isEmpty(){
return songList.isEmpty();
}
/** /**
* Remove all songs from the playlist. * Remove all songs from the playlist.
*/ */
@ -161,4 +169,21 @@ public class PlaylistTableModel extends AbstractTableModel {
playingRow = index; playingRow = index;
fireTableDataChanged(); fireTableDataChanged();
} }
public String getM3UPlaylist(){
StringBuilder output = new StringBuilder("#EXTM3U\n");
songList.forEach(x -> output.append(x.getM3UFormatString()));
return output.toString();
}
public void writePlaylistToFile(File targetFile) throws IOException {
if(!targetFile.exists())
targetFile.createNewFile();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) {
writer.write("#EXTM3U\n");
for(Song song : songList){
writer.write(song.getM3UFormatString());
}
}
}
} }