From 86196780233d449d781951fbda7675969c85e30b Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 22 Mar 2016 14:32:37 +0000 Subject: [PATCH] Fixed playlist row selection behaviour on playback stop. --- .../musicplayer/player/GStreamerPlayer.java | 2 +- .../java/musicplayer/playlist/IPlaylist.java | 1 + .../musicplayer/playlist/JTablePlaylist.java | 20 ++++++++++++------ .../playlist/JTablePlaylistTest.java | 21 +++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/musicplayer/player/GStreamerPlayer.java b/src/main/java/musicplayer/player/GStreamerPlayer.java index c2ed7c6..df9d145 100644 --- a/src/main/java/musicplayer/player/GStreamerPlayer.java +++ b/src/main/java/musicplayer/player/GStreamerPlayer.java @@ -125,7 +125,7 @@ public class GStreamerPlayer implements IPlayer{ internalThread.stop(); thread = null; resetSeek(); - playlist.setPlayingSong(currentSong); + playlist.setStopped(); } } diff --git a/src/main/java/musicplayer/playlist/IPlaylist.java b/src/main/java/musicplayer/playlist/IPlaylist.java index 6f32d6b..c41947e 100644 --- a/src/main/java/musicplayer/playlist/IPlaylist.java +++ b/src/main/java/musicplayer/playlist/IPlaylist.java @@ -20,5 +20,6 @@ public interface IPlaylist { void deleteAll(); boolean isEmpty(); void setPlayingSong(Song song); + void setStopped(); } diff --git a/src/main/java/musicplayer/playlist/JTablePlaylist.java b/src/main/java/musicplayer/playlist/JTablePlaylist.java index 9c1411a..cf03fb4 100644 --- a/src/main/java/musicplayer/playlist/JTablePlaylist.java +++ b/src/main/java/musicplayer/playlist/JTablePlaylist.java @@ -4,10 +4,7 @@ import musicplayer.model.Song; import javax.swing.*; import javax.swing.table.AbstractTableModel; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -117,10 +114,21 @@ public class JTablePlaylist extends JScrollPane implements IPlaylist { @Override public void setPlayingSong(Song song){ - int index = playlistTableModel.getSongIndex(song); + final int index = getIndex(song); + playlistTableModel.setPlayingRow(index); if(index >= 0) SwingUtilities.invokeLater(() -> playList.setRowSelectionInterval(index, index)); - playlistTableModel.setPlayingRow(index); + } + + @Override + public void setStopped(){ + if(getActive().isPresent()) { + int index = getIndex(getActive().get()); + playlistTableModel.setPlayingRow(-1); + SwingUtilities.invokeLater(() -> playList.setRowSelectionInterval(index, index)); + } + else + playlistTableModel.setPlayingRow(-1); } private class PlaylistTableModel extends AbstractTableModel { diff --git a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java index bc968e0..d9cfab3 100644 --- a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java +++ b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java @@ -6,15 +6,23 @@ import musicplayer.model.Song; import org.junit.Before; import org.junit.Test; +import javax.swing.*; + +import java.lang.reflect.Field; + import static org.junit.Assert.*; public class JTablePlaylistTest { JTablePlaylist playlist; + JTable innerTable; @Before public void setUp() throws Exception { playlist = new JTablePlaylist(); + Field jTable = JTablePlaylist.class.getDeclaredField("playList"); + jTable.setAccessible(true); + innerTable = (JTable)jTable.get(playlist); } @Test @@ -109,6 +117,8 @@ public class JTablePlaylistTest { playlist.addSong(testSong); playlist.setPlayingSong(testSong); assertEquals(testSong, playlist.getActive().get()); + Thread.sleep(10); // Wait for Swing thread update propagation + assertEquals(playlist.getIndex(testSong), innerTable.getSelectedRow()); } @Test @@ -116,6 +126,17 @@ public class JTablePlaylistTest { Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); playlist.setPlayingSong(testSong); assertFalse(playlist.getActive().isPresent()); + assertEquals(-1, innerTable.getSelectedRow()); + } + + @Test + public void testSetStopped() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.addSong(testSong); + playlist.setPlayingSong(testSong); + playlist.setStopped(); + assertEquals(testSong, playlist.getActive().get()); + assertEquals(playlist.getIndex(testSong), innerTable.getSelectedRow()); } @Test