Skip to content

Commit

Permalink
Improve repair playlist process
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 22, 2023
1 parent 794abb6 commit 97613cc
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 134 deletions.
9 changes: 1 addition & 8 deletions src/main/java/listfix/model/BatchMatchItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,17 @@ public class BatchMatchItem
/**
* Playlist position
*/
private final int _entryIx;
private final PlaylistEntry _entry;
private final List<PotentialPlaylistEntryMatch> matches;
private PotentialPlaylistEntryMatch selectedMatch;

public BatchMatchItem(int ix, PlaylistEntry entry, List<PotentialPlaylistEntryMatch> matches)
public BatchMatchItem(PlaylistEntry entry, List<PotentialPlaylistEntryMatch> matches)
{
this._entryIx = ix;
this._entry = entry;
this.matches = matches;
this.selectedMatch = matches.size() > 0 ? matches.get(0) : null;
}

public int getEntryIx()
{
return this._entryIx;
}

public PlaylistEntry getEntry()
{
return _entry;
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/listfix/model/playlists/Playlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,23 +578,21 @@ public void changeEntryFileName(int ix, String newName)
* @param mediaLibrary Media library used to reference existing media files
* @param observer Progress observer
*/
public List<Integer> repair(IMediaLibrary mediaLibrary, IProgressObserver<String> observer)
public List<PlaylistEntry> repair(IMediaLibrary mediaLibrary, IProgressObserver<String> observer)
{
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(this._entries.size());

List<Integer> fixed = new ArrayList<>();
for (int ix = 0; ix < _entries.size(); ix++)
{
final long start = System.currentTimeMillis();

List<PlaylistEntry> fixed = _entries.stream().filter(entry -> {
if (observer.getCancelled())
{
_logger.info(markerPlaylistRepair, "Observer cancelled, quit repair");
return null;
return false;
}
progress.stepCompleted();

PlaylistEntry entry = this._entries.get(ix);

final boolean caseInsensitiveExactMatching = this.playListOptions.getCaseInsensitiveExactMatching();
final boolean relativePaths = this.playListOptions.getSavePlaylistsWithRelativePaths();

Expand All @@ -607,8 +605,8 @@ public List<Integer> repair(IMediaLibrary mediaLibrary, IProgressObserver<String
_logger.debug(markerPlaylistRepair, "Found " + fileEntry.getTrackPath());
if (filePlaylistEntry.updatePathToMediaLibraryIfFoundOutside(mediaLibrary, caseInsensitiveExactMatching, relativePaths))
{
fixed.add(ix);
refreshStatus();
this.refreshStatus();
return true;
}
}
else
Expand All @@ -618,13 +616,16 @@ public List<Integer> repair(IMediaLibrary mediaLibrary, IProgressObserver<String
if (entry.isFound())
{
_logger.debug(markerPlaylistRepair, "Found & repaired file entry " + fileEntry.getTrackPath());
fixed.add(ix);
refreshStatus();
this.refreshStatus();
return true;
}
}
}
}
_logger.info(markerPlaylistRepair, "Completed.");
return false;
}).collect(Collectors.toList());

long timeElapsed = System.currentTimeMillis() - start;
_logger.info("Repaired playlist in " + timeElapsed + " ms.");
return fixed;
}

Expand Down Expand Up @@ -694,7 +695,6 @@ public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Coll
progress.setTotal(entries.size());

List<BatchMatchItem> fixed = new ArrayList<>();
int ix = 0;
for (PlaylistEntry entry : entries)
{
if (observer.getCancelled()) return null;
Expand All @@ -703,10 +703,9 @@ public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Coll
List<PotentialPlaylistEntryMatch> matches = entry.findClosestMatches(libraryFiles, null, this.playListOptions);
if (!matches.isEmpty())
{
fixed.add(new BatchMatchItem(ix, entry, matches));
fixed.add(new BatchMatchItem(entry, matches));
}
}
ix++;
progress.stepCompleted();
}

Expand Down
148 changes: 48 additions & 100 deletions src/main/java/listfix/view/controls/PlaylistEditCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import listfix.view.IListFixGui;
import listfix.view.dialogs.*;
import listfix.view.support.IPlaylistModifiedListener;
import listfix.view.support.ImageIcons;

import listfix.view.support.ProgressWorker;
import listfix.view.support.ZebraJTable;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -58,6 +58,7 @@ public class PlaylistEditCtrl extends JPanel
private final FolderChooser _destDirFileChooser = new FolderChooser();
private Playlist _playlist;
protected final IListFixGui listFixGui;
private boolean refreshPending = false;

private final IPlaylistModifiedListener listener = this::onPlaylistModified;

Expand Down Expand Up @@ -90,7 +91,25 @@ private void onPlaylistModified(Playlist list)
_btnPlay.setEnabled(_playlist != null);
_btnNextMissing.setEnabled(_playlist != null && _playlist.getMissingCount() > 0);
_btnPrevMissing.setEnabled(_playlist != null && _playlist.getMissingCount() > 0);
getTableModel().fireTableDataChanged();
this.fireTableDataChanged();
}

/**
* Call fireTableDataChanged() on the GUI thread, and do not call when a pending call is present
*/
private void fireTableDataChanged() {
if (!refreshPending) {
synchronized (this.playlistTableModel) {
refreshPending = true;
SwingUtilities.invokeLater(() -> {
synchronized (this.playlistTableModel)
{
refreshPending = false;
}
getTableModel().fireTableDataChanged();
});
}
}
}

private void addItems()
Expand Down Expand Up @@ -222,19 +241,19 @@ private void moveSelectedRowsDown()
}

/**
* Locate missing files
* Repair playlist
* @return false if cancelled, otherwise true
*/
public boolean locateMissingFiles()
{
_logger.debug(markerRepair, "Start locateMissingFiles()");
ProgressWorker<List<Integer>, String> worker = new ProgressWorker<>()
ProgressWorker<List<PlaylistEntry>, String> worker = new ProgressWorker<>()
{
@Override
protected List<Integer> doInBackground()
protected List<PlaylistEntry> doInBackground()
{
_logger.debug(markerRepairWorker, "Start repairing in background....");
List<Integer> result = _playlist.repair(PlaylistEditCtrl.this.getMediaLibrary(), this);
List<PlaylistEntry> result = _playlist.repair(PlaylistEditCtrl.this.getMediaLibrary(), this);
_logger.debug(markerRepairWorker, "Repair completed.");
return result;
}
Expand All @@ -247,12 +266,13 @@ protected void done()
{
_logger.debug(markerRepair, "Updating UI-table...");
_uiTable.clearSelection();
List<Integer> fixed = this.get();
for (Integer fixIx : fixed)

for (Integer fixIx : this.getIndexList(this.get()))
{
int viewIx = _uiTable.convertRowIndexToView(fixIx);
_uiTable.addRowSelectionInterval(viewIx, viewIx);
}
playlistTableModel.fireTableDataChanged();
_logger.debug(markerRepair, "Completed updating UI-table");
}
catch (CancellationException | InterruptedException exception)
Expand All @@ -264,13 +284,29 @@ protected void done()
_logger.error(markerRepair, "Error processing missing files", ex);
}
}

private List<Integer> getIndexList(List<PlaylistEntry> fixedEntries) {
final List<Integer> fixedIndexes = new LinkedList<>();
final List<PlaylistEntry> copiedList = new LinkedList<>(fixedEntries);

// Lookup the playlist index of the fixed playlist entries
for (int fixIx = 0; fixIx < _playlist.size() && !copiedList.isEmpty(); ++fixIx) {
if (_playlist.get(fixIx) == copiedList.get(0)) {
copiedList.remove(0);
fixedIndexes.add(fixIx);
}
}
assert fixedIndexes.size() == fixedEntries.size();
return fixedIndexes;
}
};

ProgressDialog pd = new ProgressDialog(getParentFrame(), true, worker, "Repairing...");
pd.setVisible(true); // Wait until the worker completed
return !worker.isCancelled();
}


private void reorderList()
{
Playlist.SortIx sortIx = Playlist.SortIx.None;
Expand Down Expand Up @@ -771,7 +807,8 @@ public void mouseClicked(MouseEvent evt)
_uiTableScrollPane.setBorder(BorderFactory.createEtchedBorder());

_uiTable.setAutoCreateRowSorter(true);
_uiTable.setModel(new PlaylistTableModel());

_uiTable.setModel(playlistTableModel);
_uiTable.setDragEnabled(true);
_uiTable.setFillsViewportHeight(true);
_uiTable.setGridColor(new Color(153, 153, 153));
Expand Down Expand Up @@ -1113,6 +1150,7 @@ private void _miNewPlaylistFromSelectedActionPerformed()
private JMenuItem _miFindClosest;
private JMenuItem _miReplace;
private JPopupMenu _playlistEntryRightClickMenu;
private final PlaylistTableModel playlistTableModel= new PlaylistTableModel();
private ZebraJTable _uiTable;
private JScrollPane _uiTableScrollPane;

Expand All @@ -1134,7 +1172,7 @@ public void setPlaylist(Playlist list, boolean force)
}
_playlist = list;

((PlaylistTableModel) _uiTable.getModel()).fireTableDataChanged();
this.playlistTableModel.changePlaylist(_playlist);

boolean hasPlaylist = _playlist != null;

Expand Down Expand Up @@ -1695,96 +1733,6 @@ private void initFolderChooser()
_destDirFileChooser.setDialogTitle("Choose a destination directory...");
}

private class PlaylistTableModel extends AbstractTableModel
{
@Override
public int getRowCount()
{
if (_playlist != null)
{
return _playlist.size();
}
else
{
return 0;
}
}

@Override
public int getColumnCount()
{
return 4;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
PlaylistEntry entry = _playlist.get(rowIndex);
switch (columnIndex)
{
case 0 ->
{
return rowIndex + 1;
}
case 1 ->
{
if (entry.isURL())
{
return ImageIcons.IMG_URL;
}
else if (entry.isFixed())
{
return ImageIcons.IMG_FIXED;
}
else if (entry.isFound())
{
return ImageIcons.IMG_FOUND;
}
else
{
return ImageIcons.IMG_MISSING;
}
}
case 2 ->
{
return entry.getTrackFileName();
}
case 3 ->
{
return entry.getTrackFolder();
}
default ->
{
return null;
}
}
}

@Override
public String getColumnName(int column)
{
return switch (column)
{
case 0 -> "#";
case 1 -> "";
case 2 -> "File Name";
case 3 -> "Location";
default -> null;
};
}

@Override
public Class<?> getColumnClass(int columnIndex)
{
return switch (columnIndex)
{
case 0 -> Integer.class;
case 1 -> ImageIcon.class;
default -> Object.class;
};
}
}

private static class IntRenderer extends DefaultTableCellRenderer
{
IntRenderer()
Expand Down
Loading

0 comments on commit 97613cc

Please sign in to comment.