diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index b932eac..97b6e2f 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -9,6 +9,7 @@ import musicplayer.model.Song; import musicplayer.swingmodels.PlaylistTableModel; import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; @@ -18,6 +19,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.util.*; import java.util.List; @@ -419,6 +421,38 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf // Add everything to the menu bar itself 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; } diff --git a/src/main/java/musicplayer/model/Song.java b/src/main/java/musicplayer/model/Song.java index 68063b5..5ff216c 100644 --- a/src/main/java/musicplayer/model/Song.java +++ b/src/main/java/musicplayer/model/Song.java @@ -132,4 +132,11 @@ public class Song implements Comparable, IDBType { public int compareTo(Song o) { 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()); + } } diff --git a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java index 2ba3696..386e8e8 100644 --- a/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java +++ b/src/main/java/musicplayer/swingmodels/PlaylistTableModel.java @@ -3,6 +3,7 @@ package musicplayer.swingmodels; import musicplayer.model.Song; import javax.swing.table.AbstractTableModel; +import java.io.*; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -136,6 +137,13 @@ public class PlaylistTableModel extends AbstractTableModel { fireTableDataChanged(); } + /** + * @return Is the playlist currently empty. + */ + public boolean isEmpty(){ + return songList.isEmpty(); + } + /** * Remove all songs from the playlist. */ @@ -161,4 +169,21 @@ public class PlaylistTableModel extends AbstractTableModel { playingRow = index; 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()); + } + } + } }