diff --git a/src/main/java/listfix/model/playlists/Playlist.java b/src/main/java/listfix/model/playlists/Playlist.java index eb3e5637..f6ffc429 100644 --- a/src/main/java/listfix/model/playlists/Playlist.java +++ b/src/main/java/listfix/model/playlists/Playlist.java @@ -282,24 +282,28 @@ public enum SortIx * @return Playlist which contains selected entries */ public Playlist getSublist(int[] rows) throws IOException + { + return getSublist(Arrays.stream(rows).mapToObj(_entries::get).collect(Collectors.toList())); + } + + public Playlist getSublist(Collection entries) throws IOException { final io.github.borewit.lizzy.playlist.Playlist newPlaylist = new io.github.borewit.lizzy.playlist.Playlist(); final Sequence rootSequence = newPlaylist.getRootSequence(); - for (int i : rows) + for (PlaylistEntry entry : entries) { String srcUri; - if (_entries.get(i) instanceof FilePlaylistEntry) + if (entry instanceof FilePlaylistEntry) { // Normalize to absolute path - Path trackPath = ((FilePlaylistEntry) _entries.get(i)).trackPath; + Path trackPath = ((FilePlaylistEntry) entry).trackPath; srcUri = trackPath.isAbsolute() ? trackPath.toString() : this.playlistPath.getParent().resolve(trackPath).toString(); } else { - srcUri = _entries.get(i).getMedia().getSource().toString(); + srcUri = entry.getMedia().getSource().toString(); } Media media = new Media(new Content(srcUri)); - _entries.get(i).getMedia(); rootSequence.addComponent(media); } diff --git a/src/main/java/listfix/view/controls/PlaylistEditCtrl.java b/src/main/java/listfix/view/controls/PlaylistEditCtrl.java index f52a9615..96542bc2 100644 --- a/src/main/java/listfix/view/controls/PlaylistEditCtrl.java +++ b/src/main/java/listfix/view/controls/PlaylistEditCtrl.java @@ -583,40 +583,14 @@ private void savePlaylist() private void playSelectedEntries() { + List playlistEntries = this.getSelectedPlaylistEntries().stream() + .filter(entry -> entry.isFound() || entry.isURL()) + .collect(Collectors.toList()); + try { - int[] rows; - if (_uiTable.getSelectedRowCount() > 0) - { - rows = _uiTable.getSelectedRows(); - } - else - { - rows = new int[_uiTable.getRowCount()]; - for (int r = 0; r < rows.length; r++) - { - rows[r] = r; - } - } - - for (int r = 0; r < rows.length; r++) - { - rows[r] = _uiTable.convertRowIndexToModel(rows[r]); - } - - // Get a list of the selected rows that aren't missing, effectively stripping those entries from the selection - List rowList = new ArrayList<>(); - for (int i : rows) - { - if (_playlist.get(i).isFound() || _playlist.get(i).isURL()) - { - rowList.add(i); - } - } - rows = ArrayFunctions.integerListToArray(rowList); - // Get a temp playlist - Playlist tempList = _playlist.getSublist(rows); + Playlist tempList = _playlist.getSublist(playlistEntries); // Sanity check, don't launch an empty list if (tempList.size() > 0) @@ -1128,7 +1102,6 @@ private void _miNewPlaylistFromSelectedActionPerformed() { throw new RuntimeException("Failed to create new playlist", e); } - } private JButton _btnAdd; @@ -1331,9 +1304,9 @@ else if (entry instanceof FilePlaylistEntry) private boolean selectedRowsContainFoundEntry() { - for (int row : _uiTable.getSelectedRows()) + for (PlaylistEntry playlistEntry : this.getSelectedPlaylistEntries()) { - if (_playlist.get(_uiTable.convertRowIndexToModel(row)).isFound() || _playlist.get(_uiTable.convertRowIndexToModel(row)).isURL()) + if (playlistEntry.isFound() || playlistEntry.isURL()) { return true; } @@ -1341,6 +1314,12 @@ private boolean selectedRowsContainFoundEntry() return false; } + public List getSelectedPlaylistEntries() { + return Arrays.stream(_uiTable.getSelectedRows()) + .mapToObj(row -> _playlist.get(_uiTable.convertRowIndexToModel(row))) + .collect(Collectors.toList()); + } + private void initPlaylistTable() { _uiTable.setDefaultRenderer(Integer.class, new IntRenderer()); @@ -1368,7 +1347,7 @@ private void initPlaylistTable() _btnDelete.setEnabled(hasSelected); _btnUp.setEnabled(_isSortedByFileIx && hasSelected && _uiTable.getSelectedRow() > 0); _btnDown.setEnabled(_isSortedByFileIx && hasSelected && _uiTable.getSelectedRow() < _uiTable.getRowCount() - 1); - _btnPlay.setEnabled(_playlist != null && (_uiTable.getSelectedRow() < 0 || (_uiTable.getSelectedRows().length > 0 && selectedRowsContainFoundEntry()))); + _btnPlay.setEnabled(_playlist != null && (_uiTable.getSelectedRow() < 0 || (_uiTable.getSelectedRows().length > 0 && this.selectedRowsContainFoundEntry()))); _btnReload.setEnabled(_playlist != null && _playlist.isModified()); _btnSave.setEnabled(_playlist != null); _btnNextMissing.setEnabled(_playlist != null && _playlist.getMissingCount() > 0);