diff --git a/src/main/java/musicplayer/PlayerGUI.form b/src/main/java/musicplayer/PlayerGUI.form index b7a9585..e2d11ee 100644 --- a/src/main/java/musicplayer/PlayerGUI.form +++ b/src/main/java/musicplayer/PlayerGUI.form @@ -19,7 +19,7 @@ - + diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 31e79c6..baf15ac 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -26,6 +26,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private JMenuBar menuBar; private JComboBox libraryDisplayType; private JLabel volumeValue; + private JScrollPane playlistScroll; private Player player = new Player(this); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); @@ -48,6 +49,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf libraryView.setCellRenderer(new LibraryTreeCellRenderer()); playList.setModel(playlistTableModel); resetTree(); + libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); volumeSlider.setValue(player.getVolume()); setVolumeValue(player.getVolume()); Thread seekBarUpdater = new Thread(() -> { @@ -64,6 +66,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf seekBarUpdater.start(); //Action Listeners + libraryDisplayType.addItemListener(e ->{ + if(e.getStateChange() == ItemEvent.SELECTED) + libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run(); + }); playButton.addActionListener(e -> { if (playList.getRowCount() > 0) { if(playList.getSelectedRowCount() > 0) @@ -81,6 +87,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf setVolumeValue(player.getVolume()); }); playList.setComponentPopupMenu(createPlaylistPopup()); + playlistScroll.setComponentPopupMenu(createPlaylistEmptyAreaPopup()); populateMenuBar(); refreshLibrary(); } @@ -105,9 +112,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @param libraryData Map of albums with a lists of associated songs. */ private void populateLibrary(Map> libraryData) { + TreeMap> sortedData = new TreeMap<>(libraryData); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); - libraryData.forEach((k, v) -> { + sortedData.forEach((k, v) -> { DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k); addNodeToTreeModel(model, parentNode, albumNode); new TreeSet<>(v).forEach(x -> addNodeToTreeModel(model, albumNode, new DefaultMutableTreeNode(x))); @@ -120,6 +128,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @param libraryData List of songs. */ private void populateLibrary(List libraryData) { + Collections.sort(libraryData); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); libraryData.forEach(x -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(x))); @@ -195,9 +204,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf addNodeToTreeModel(model, parentNode, newNode); libraryView.setModel(model); Thread populateThread = new Thread(() -> { - TreeMap treeMap = new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get()); - //SwingUtilities.invokeLater(() -> populateLibrary(treeMap)); - populateLibrary(treeMap); + libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run(); }); populateThread.start(); } @@ -259,9 +266,19 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf return popupMenu; } + private JPopupMenu createPlaylistEmptyAreaPopup(){ + JPopupMenu popupMenu = new JPopupMenu(); + JMenuItem menuItem = new JMenuItem("Clear all"); + menuItem.addActionListener((e) -> playlistTableModel.removeAll()); + + popupMenu.add(menuItem); + return popupMenu; + } + private Map createDisplayVariantMap(){ Map value = new HashMap<>(); - value.put("Song", () -> Gateway.listAllSongs().get()); + value.put("Song", () -> Gateway.listAllSongs().ifPresent(this::populateLibrary)); + value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary)); return value; } diff --git a/src/main/java/musicplayer/PlaylistTableModel.java b/src/main/java/musicplayer/PlaylistTableModel.java index bd699b9..a3a52ee 100644 --- a/src/main/java/musicplayer/PlaylistTableModel.java +++ b/src/main/java/musicplayer/PlaylistTableModel.java @@ -100,6 +100,11 @@ public class PlaylistTableModel extends AbstractTableModel { fireTableDataChanged(); } + public void removeAll(){ + songList.clear(); + fireTableDataChanged(); + } + public Optional getSong(int index){ return songList.size() > 0 && index >= 0 && index < songList.size() ? Optional.of(songList.get(index)) : Optional.empty(); }