Added library display variant selection and application.

This commit is contained in:
neviyn 2016-02-24 18:36:16 +00:00
parent 427c648e33
commit e8255c5b32
3 changed files with 28 additions and 6 deletions

View File

@ -19,7 +19,7 @@
</properties>
<border type="none"/>
<children>
<scrollpane id="359a1">
<scrollpane id="359a1" binding="playlistScroll">
<constraints>
<splitpane position="right"/>
</constraints>

View File

@ -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<Album, List<Song>> libraryData) {
TreeMap<Album, List<Song>> 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<Song> 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<String, Runnable> createDisplayVariantMap(){
Map<String, Runnable> 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;
}

View File

@ -100,6 +100,11 @@ public class PlaylistTableModel extends AbstractTableModel {
fireTableDataChanged();
}
public void removeAll(){
songList.clear();
fireTableDataChanged();
}
public Optional<Song> getSong(int index){
return songList.size() > 0 && index >= 0 && index < songList.size() ? Optional.of(songList.get(index)) : Optional.empty();
}