Skip to content

Commit

Permalink
Merge 6b57540 into b8c8627
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit authored Oct 26, 2023
2 parents b8c8627 + 6b57540 commit fb88fd9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 63 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ repositories {
dependencies {

// Provides christophedelory.playlist.*
implementation 'io.github.borewit:lizzy:3.0.1'
implementation 'io.github.borewit:lizzy:4.0.0'

// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
implementation 'org.apache.logging.log4j:log4j-core:2.21.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void serialize(Playlist playlist, OutputStream outputStream) throw
SpecificPlaylist specificPlaylist = LizzyPlaylistUtil.toPlaylist(playlistFormat, playlist);
try
{
specificPlaylist.writeTo(outputStream, null);
specificPlaylist.writeTo(outputStream);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/LizzyPlaylistUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static SpecificPlaylist writeNewPlaylist(Playlist playlist, Path path, Pl
try (OutputStream outputStream = Files.newOutputStream(path, options))
{
SpecificPlaylist specificPlaylist = specificPlaylistFactory.getProvider(playlistFormat).toSpecificPlaylist(playlist);
specificPlaylist.writeTo(outputStream, null);
specificPlaylist.writeTo(outputStream);
return specificPlaylist;
}
}
Expand Down
46 changes: 28 additions & 18 deletions src/main/java/listfix/model/playlists/Playlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.borewit.lizzy.content.Content;
import io.github.borewit.lizzy.playlist.*;
import listfix.config.IMediaLibrary;
import listfix.io.FileUtils;
import listfix.io.IPlaylistOptions;
import listfix.io.playlists.LizzyPlaylistUtil;
import listfix.model.BatchMatchItem;
Expand All @@ -17,6 +18,7 @@
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.File;
import java.io.IOException;
Expand All @@ -32,7 +34,7 @@

public class Playlist
{
private static final String HOME_DIR = System.getProperty("user.home");
private static final String DEFAULT_SAVE_FOLDER = FileSystemView.getFileSystemView().getDefaultDirectory().getPath();

private static int NEW_LIST_COUNT = -1;

Expand All @@ -47,21 +49,22 @@ public class Playlist
private int _urlCount;
private int _missingCount;
private boolean isModified;
private boolean _isNew;
private boolean isUnsaved;
private static final Logger _logger = LogManager.getLogger(Playlist.class);

private final IPlaylistOptions playListOptions;

private final List<IPlaylistModifiedListener> _listeners = new ArrayList<>();

private static final PlaylistFormat defaultPlaylistFormat = PlaylistFormat.m3u;
private static final String defaultPlaylistExtension = "m3u8";

public static Playlist load(Path playlistPath, IProgressObserver<String> observer, IPlaylistOptions playListOptions) throws IOException
{
SpecificPlaylist specificPlaylist = LizzyPlaylistUtil.readPlaylist(playlistPath);
Playlist playlist = new Playlist(playlistPath, playListOptions, specificPlaylist);
playlist.load(observer);
playlist._isNew = false;
playlist.isUnsaved = false;
return playlist;
}

Expand Down Expand Up @@ -93,12 +96,18 @@ public static Playlist makeTemporaryPlaylist(IPlaylistOptions playListOptions, i
return newPlaylist;
}

public static Playlist makeNewPersistentPlaylist(IPlaylistOptions playListOptions) throws IOException
public static Playlist makeNewPersistentPlaylist(String extension, IPlaylistOptions playListOptions) throws IOException, PlaylistProviderNotFoundException
{
final Path path = getNewPlaylistFilename();
final io.github.borewit.lizzy.playlist.Playlist playlist = new io.github.borewit.lizzy.playlist.Playlist();
SpecificPlaylist specificPlaylist = LizzyPlaylistUtil.writeNewPlaylist(playlist, path, defaultPlaylistFormat);
return new Playlist(path, playListOptions, specificPlaylist);
final Path path = getNewPlaylistFilename(extension);
SpecificPlaylistFactory specificPlaylistFactory = SpecificPlaylistFactory.getInstance();
List<SpecificPlaylistProvider> providers = specificPlaylistFactory.findProvidersByExtension(extension);
if (!providers.isEmpty())
{
final io.github.borewit.lizzy.playlist.Playlist playlist = new io.github.borewit.lizzy.playlist.Playlist();
SpecificPlaylist specificPlaylist = providers.get(0).toSpecificPlaylist(playlist);
return new Playlist(path, playListOptions, specificPlaylist);
}
throw new PlaylistProviderNotFoundException("Could not find a playlist provided for " + extension);
}

/**
Expand All @@ -111,6 +120,7 @@ public Playlist(Path playlistPath, IPlaylistOptions playListOptions, SpecificPla
this.playlistPath = playlistPath;
this.playListOptions = playListOptions;
this.specificPlaylist = playlist;
this.isUnsaved = true;
this.isModified = false;
toPlaylistEntries(this._entries, playlist.toPlaylist().getRootSequence());
refreshStatus();
Expand Down Expand Up @@ -166,7 +176,7 @@ private void loadPlaylistEntries(List<PlaylistEntry> playlistEntries, Path playl
final InputStream observableInputStream = observer == null ? is : new ObservableInputStream(is, Files.size(playlistPath), observer);
try
{
this.specificPlaylist = specificPlaylistProvider.readFrom(observableInputStream, null);
this.specificPlaylist = specificPlaylistProvider.readFrom(observableInputStream);
if (this.specificPlaylist != null)
{
toPlaylistEntries(playlistEntries, this.specificPlaylist.toPlaylist().getRootSequence());
Expand Down Expand Up @@ -250,7 +260,7 @@ protected void resetInternalStateAfterSave(IProgressObserver<String> observer)
// change original _entries
replaceEntryListContents(_entries, _originalEntries);
this.isModified = false;
_isNew = false;
this.isUnsaved = false;

// set entries unfixed if we're being watched...
// (otherwise writing out a temp file for playback, at least right now)
Expand Down Expand Up @@ -475,7 +485,7 @@ public void play()

public boolean isNew()
{
return this._isNew;
return this.isUnsaved;
}

public void replace(int index, PlaylistEntry newEntry)
Expand Down Expand Up @@ -985,8 +995,7 @@ public final void save(PlaylistFormat format, IProgressObserver<String> observer
OutputStream observableOutputStream = observer == null ? os : new ObservableOutputStream(os, currentFileSize, observer);
try
{
final String encoding = this.playlistPath.toString().toLowerCase().endsWith(".m3u8") ? "UTF-8" : null;
this.specificPlaylist.writeTo(observableOutputStream, encoding);
this.specificPlaylist.writeTo(observableOutputStream);
}
catch (Exception e)
{
Expand All @@ -1010,12 +1019,12 @@ private void quickSave() throws IOException
*/
public void reload(IProgressObserver<String> observer) throws IOException
{
if (_isNew)
if (isUnsaved)
{
this.playlistPath = getNewPlaylistFilename();
this.playlistPath = getNewPlaylistFilename(defaultPlaylistExtension);
this.playlistPath.toFile().deleteOnExit();
this.isModified = false;
_isNew = true;
isUnsaved = true;
_entries.clear();
_originalEntries.clear();
}
Expand All @@ -1030,13 +1039,14 @@ else if (Files.exists(this.playlistPath))
refreshStatus();
}

public static synchronized Path getNewPlaylistFilename()
public static synchronized Path getNewPlaylistFilename(String pathOrExtension)
{
final String extension = pathOrExtension.contains(".") ? FileUtils.getFileExtension(pathOrExtension) : pathOrExtension;
Path path;
do
{
++NEW_LIST_COUNT;
path = Path.of(HOME_DIR, "Untitled-" + NEW_LIST_COUNT + ".m3u8");
path = Path.of(DEFAULT_SAVE_FOLDER, "Untitled-" + NEW_LIST_COUNT + "." + extension);
}
while (Files.exists(path));
return path;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package listfix.model.playlists;

public class PlaylistProviderNotFoundException extends Exception
{
public PlaylistProviderNotFoundException(String message) {
super(message);
}

public PlaylistProviderNotFoundException(Throwable cause) {
super(cause);
}

public PlaylistProviderNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
3 changes: 2 additions & 1 deletion src/main/java/listfix/swing/JPlaylistComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setPlaylist(Playlist playlist)
}

this.closableTabComponent.setTitle(playlist.getPath().getFileName().toString());
this.closableTabComponent.setTooltip(playlist.toString());
this.closableTabComponent.setTooltip(playlist.getPath().toString());

playlist.addModifiedListener(this.playlistListener);

Expand All @@ -75,6 +75,7 @@ public void setPlaylist(Playlist playlist)
private void onPlaylistModified(Playlist list)
{
this.closableTabComponent.setTitle(playlist.getPath().getFileName().toString());
this.closableTabComponent.setTooltip(playlist.getPath().toString());
this.setIcon(this.getIconForPlaylist());
}

Expand Down
39 changes: 24 additions & 15 deletions src/main/java/listfix/view/GUIScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import listfix.model.PlaylistHistory;
import listfix.model.playlists.Playlist;
import listfix.model.playlists.PlaylistFactory;
import listfix.model.playlists.PlaylistProviderNotFoundException;
import listfix.swing.IDocumentChangeListener;
import listfix.swing.JPlaylistComponent;
import listfix.swing.JDocumentTabbedPane;
Expand Down Expand Up @@ -65,6 +66,8 @@ public final class GUIScreen extends JFrame implements IListFixGui

private static final String applicationVersion = getBuildNumber();

private static final String DefaultPlaylistFormatExtension = ".m3u8";

/**
* The components should only be enabled when 1 or more playlists are loaded
*/
Expand Down Expand Up @@ -750,8 +753,8 @@ public void keyPressed(KeyEvent evt)
_verticalPanel.add(_spacerPanel);

_newIconButton.setIcon(getImageIcon("icon_new_file.png")); // NOI18N
_newIconButton.setText("New Playlist");
_newIconButton.setToolTipText("New Playlist");
_newIconButton.setText("New playlist");
_newIconButton.setToolTipText("Create new empty playlist");
_newIconButton.setAlignmentY(0.0F);
_newIconButton.setFocusable(false);
_newIconButton.setHorizontalTextPosition(SwingConstants.CENTER);
Expand Down Expand Up @@ -815,8 +818,8 @@ public void keyPressed(KeyEvent evt)

_closeAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK));
_closeAllMenuItem.setMnemonic('A');
_closeAllMenuItem.setText("Close All");
_closeAllMenuItem.setToolTipText("Closes All Open Playlists");
_closeAllMenuItem.setText("Close all");
_closeAllMenuItem.setToolTipText("Closes all open playlists");
_closeAllMenuItem.addActionListener(evt -> this._playlistTabbedPane.closeAll());
_fileMenu.add(_closeAllMenuItem);
_fileMenu.add(jSeparator1);
Expand All @@ -829,22 +832,22 @@ public void keyPressed(KeyEvent evt)

_saveAsMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK));
_saveAsMenuItem.setMnemonic('V');
_saveAsMenuItem.setText("Save As");
_saveAsMenuItem.setText("Save as");
_saveAsMenuItem.addActionListener(evt -> _saveAsMenuItemActionPerformed());
_fileMenu.add(_saveAsMenuItem);

_saveAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK));
_saveAllMenuItem.setMnemonic('S');
_saveAllMenuItem.setText("Save All");
_saveAllMenuItem.setToolTipText("Save All Open Playlists");
_saveAllMenuItem.setToolTipText("Save all open playlists");
_saveAllMenuItem.addActionListener(evt -> _saveAllMenuItemActionPerformed());
_fileMenu.add(_saveAllMenuItem);
_fileMenu.add(jSeparator2);

_miReload.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
_miReload.setMnemonic('R');
_miReload.setText("Reload");
_miReload.setToolTipText("Reloads The Currently Open Playlist");
_miReload.setToolTipText("Reloads the current open playlist");
_miReload.addActionListener(evt -> _miReloadActionPerformed());
_fileMenu.add(_miReload);

Expand Down Expand Up @@ -1658,22 +1661,29 @@ private void cleanupOnTabClose(JPlaylistComponent playlistComponent)

private void confirmCloseApp()
{
final Optional<Playlist> hasModifiedPlaylist = this._playlistTabbedPane.getPlaylistEditors().stream()
final Collection<Playlist> modifiedPlaylists = this._playlistTabbedPane.getPlaylistEditors().stream()
.map(PlaylistEditCtrl::getPlaylist)
.filter(Playlist::isModified)
.findAny();
.collect(Collectors.toList());

if (hasModifiedPlaylist.isPresent()) {
if (!modifiedPlaylists.isEmpty()) {
Object[] options =
{
"Discard Changes and Exit", "Cancel"
"Discard changes and exit", "Cancel"
};
int rc = JOptionPane.showOptionDialog(this, new JTransparentTextArea("You have unsaved changes. Do you really want to discard these changes and exit?"), "Confirm Close",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if (rc == JOptionPane.NO_OPTION)
{
return;
}
else if (rc == JOptionPane.YES_OPTION)
{
// Close unsaved playlists to prevent to prevent those from being re-opened next run
modifiedPlaylists.stream().filter(Playlist::isNew).forEach(unsavedPlaylist -> {
this._playlistTabbedPane.remove(unsavedPlaylist);
});
}
}

// Store open playlists
Expand All @@ -1685,7 +1695,7 @@ private void confirmCloseApp()
List<String> playlistPaths = appState.getPlaylistsOpened();
playlistPaths.clear();
playlistPaths.addAll(openPlaylists);
appState.setActivePlaylistIndex(_playlistTabbedPane.getSelectedIndex());
appState.setActivePlaylistIndex(openPlaylists.isEmpty() ? null : _playlistTabbedPane.getSelectedIndex());

try
{
Expand Down Expand Up @@ -1916,7 +1926,7 @@ private void _newIconButtonActionPerformed()
{
try
{
_currentPlaylist = Playlist.makeNewPersistentPlaylist(this.getOptions());
_currentPlaylist = Playlist.makeNewPersistentPlaylist(DefaultPlaylistFormatExtension, this.getOptions());
PlaylistEditCtrl editor = new PlaylistEditCtrl(this);
editor.setPlaylist(_currentPlaylist);
_playlistTabbedPane.openPlaylist(editor, _currentPlaylist);
Expand All @@ -1930,15 +1940,14 @@ private void _newIconButtonActionPerformed()
((CardLayout) _playlistPanel.getLayout()).show(_playlistPanel, "_docTabPanel");
}
}
catch (IOException ex)
catch (IOException | PlaylistProviderNotFoundException ex)
{
_logger.error("Error creating a new playlist", ex);
JOptionPane.showMessageDialog(this,
new JTransparentTextArea(ExStack.textFormatErrorForUser("Sorry, there was an error creating a new playlist. Please try again, or file a bug report.", ex.getCause())),
"New Playlist Error",
JOptionPane.ERROR_MESSAGE);
}

}

private void _closeMenuItemActionPerformed()
Expand Down
Loading

0 comments on commit fb88fd9

Please sign in to comment.