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> </grid>
</constraints> </constraints>
<properties> <properties>
<dividerLocation value="350"/> <dividerLocation value="240"/>
</properties> </properties>
<border type="none"/> <border type="none"/>
<children> <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"> <scrollpane id="359a1">
<constraints> <constraints>
<splitpane position="right"/> <splitpane position="right"/>
@ -50,6 +34,33 @@
</component> </component>
</children> </children>
</scrollpane> </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> </children>
</splitpane> </splitpane>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar"> <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 seekBar;
private JSlider volumeSlider; private JSlider volumeSlider;
private JMenuBar menuBar; private JMenuBar menuBar;
private JComboBox libraryDisplayType;
private Player player = new Player(this); private Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame(); JFrame frame = new JFrame();
@ -58,7 +61,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
//Action Listeners //Action Listeners
playButton.addActionListener(e -> { playButton.addActionListener(e -> {
if (playList.getSelectedColumn() == -1) { if (playList.getRowCount() > 0) {
player.playSong(playlistTableModel.getFirst().get()); player.playSong(playlistTableModel.getFirst().get());
} }
}); });
@ -67,8 +70,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
stopButton.addActionListener(e -> player.stop()); stopButton.addActionListener(e -> player.stop());
nextButton.addActionListener(e -> playNextSong()); nextButton.addActionListener(e -> playNextSong());
volumeSlider.addChangeListener(e -> player.setVolume(((JSlider)e.getSource()).getValue())); volumeSlider.addChangeListener(e -> player.setVolume(((JSlider)e.getSource()).getValue()));
playList.setComponentPopupMenu(createPlaylistPopup());
populateMenuBar(); populateMenuBar();
refreshLibrary();
} }
/** /**
@ -230,4 +234,20 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
menuBar.add(tools); 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 musicplayer.model.Song;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class PlaylistTableModel extends AbstractTableModel { public class PlaylistTableModel extends AbstractTableModel {
@ -78,4 +81,17 @@ public class PlaylistTableModel extends AbstractTableModel {
public int getSongIndex(Song song){ public int getSongIndex(Song song){
return songList.indexOf(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);
}
} }