Added popup menu on right click to remove items from the playlist.

This commit is contained in:
neviyn 2016-02-16 02:06:55 +00:00
parent 24070231b6
commit 0ba7ba8760
3 changed files with 66 additions and 19 deletions

View File

@ -15,26 +15,10 @@
</grid>
</constraints>
<properties>
<dividerLocation value="350"/>
<dividerLocation value="240"/>
</properties>
<border type="none"/>
<children>
<scrollpane id="2b209">
<constraints>
<splitpane position="left"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6b9fd" class="javax.swing.JTree" binding="libraryView">
<constraints/>
<properties>
<rootVisible value="true"/>
<showsRootHandles value="false"/>
</properties>
</component>
</children>
</scrollpane>
<scrollpane id="359a1">
<constraints>
<splitpane position="right"/>
@ -50,6 +34,33 @@
</component>
</children>
</scrollpane>
<grid id="1f7af" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<splitpane position="left"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c14a2" class="javax.swing.JComboBox" binding="libraryDisplayType">
<constraints border-constraint="North"/>
<properties/>
</component>
<scrollpane id="2b209">
<constraints border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="6b9fd" class="javax.swing.JTree" binding="libraryView">
<constraints/>
<properties>
<rootVisible value="true"/>
<showsRootHandles value="false"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
</children>
</splitpane>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar">

View File

@ -22,9 +22,12 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
private JSlider seekBar;
private JSlider volumeSlider;
private JMenuBar menuBar;
private JComboBox libraryDisplayType;
private Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
public static void main(String[] args) {
JFrame frame = new JFrame();
@ -58,7 +61,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
//Action Listeners
playButton.addActionListener(e -> {
if (playList.getSelectedColumn() == -1) {
if (playList.getRowCount() > 0) {
player.playSong(playlistTableModel.getFirst().get());
}
});
@ -67,8 +70,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
stopButton.addActionListener(e -> player.stop());
nextButton.addActionListener(e -> playNextSong());
volumeSlider.addChangeListener(e -> player.setVolume(((JSlider)e.getSource()).getValue()));
playList.setComponentPopupMenu(createPlaylistPopup());
populateMenuBar();
refreshLibrary();
}
/**
@ -230,4 +234,20 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
menuBar.add(tools);
}
private JPopupMenu createPlaylistPopup(){
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Remove");
menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows()));
popupMenu.add(menuItem);
return popupMenu;
}
private Map<String, Runnable> createDisplayVariantMap(){
Map<String, Runnable> value = new HashMap<>();
value.put("Song", () -> Gateway.listAllSongs().get());
return value;
}
}

View File

@ -3,8 +3,11 @@ package musicplayer;
import musicplayer.model.Song;
import javax.swing.table.AbstractTableModel;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class PlaylistTableModel extends AbstractTableModel {
@ -78,4 +81,17 @@ public class PlaylistTableModel extends AbstractTableModel {
public int getSongIndex(Song song){
return songList.indexOf(song);
}
public void removeSong(int index){
if(songList.size() > index && index >= 0){
songList.remove(index);
fireTableRowsDeleted(index, index);
}
}
public void removeSong(int[] index){
List<Integer> indices = IntStream.of(index).boxed().collect(Collectors.toList());
Collections.reverse(indices);
indices.forEach(this::removeSong);
}
}