Skip to content

Commit

Permalink
Improve "Save as" JFileChooser behaviour
Browse files Browse the repository at this point in the history
Preserve the filename the user typed in, when he changes the playlist type (extension).
  • Loading branch information
Borewit committed Nov 6, 2023
1 parent 4797675 commit 7ec0687
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/main/java/listfix/view/GUIScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import listfix.model.playlists.PlaylistFactory;
import listfix.model.playlists.PlaylistProviderNotFoundException;
import listfix.swing.IDocumentChangeListener;
import listfix.swing.JPlaylistComponent;
import listfix.swing.JDocumentTabbedPane;
import listfix.swing.JPlaylistComponent;
import listfix.util.ArrayFunctions;
import listfix.util.ExStack;
import listfix.util.FileTypeSearch;
import listfix.view.controls.JTransparentTextArea;
import listfix.view.controls.PlaylistEditCtrl;
import listfix.view.dialogs.*;
import listfix.view.dialogs.AppOptionsDialog;
import listfix.view.dialogs.FolderChooser;
import listfix.view.dialogs.ProgressDialog;
import listfix.view.support.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -33,6 +35,7 @@
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicFileChooserUI;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
Expand Down Expand Up @@ -90,7 +93,8 @@ public GUIScreen()
postInitComponents();
}

public static String getBuildNumber() {
public static String getBuildNumber()
{
URL url = getResourceUrl("/META-INF/MANIFEST.MF");
try
{
Expand Down Expand Up @@ -402,17 +406,18 @@ private void configureFileAndFolderChoosers()
_savePlaylistAsFileChooser.addPropertyChangeListener(propertyChangeEvent -> {
if (_savePlaylistAsFileChooser.isVisible() && propertyChangeEvent.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
{
System.out.printf("Selected file = %s\n", _savePlaylistAsFileChooser.getSelectedFile());
// We want the last filename the user entered
String currentFileName = ((BasicFileChooserUI) _savePlaylistAsFileChooser.getUI()).getFileName();
System.out.printf("Selected file = %s\n", currentFileName);
SpecificPlaylistFileFilter fileFilter = (SpecificPlaylistFileFilter) propertyChangeEvent.getNewValue();
final File selectedFile = _savePlaylistAsFileChooser.getSelectedFile();
if (selectedFile != null)
if (currentFileName != null)
{
File userFile = new File(_savePlaylistAsFileChooser.getCurrentDirectory(), currentFileName);
// Current selected file is not compliant with FileFilter, let's adjust it
if (!fileFilter.getContentType().accept(selectedFile))
if (!fileFilter.getContentType().accept(userFile))
{
String curName = selectedFile.getName();
String nameWithoutExtension = FileUtils.getExtension(selectedFile.getName()).map(
ext -> curName.substring(0, curName.lastIndexOf("."))).orElse(curName);
String nameWithoutExtension = FileUtils.getExtension(currentFileName).map(
ext -> currentFileName.substring(0, currentFileName.lastIndexOf("."))).orElse(currentFileName);
String newName = nameWithoutExtension + fileFilter.getContentType().getExtensions()[0];
_savePlaylistAsFileChooser.setSelectedFile(new File(newName));
}
Expand Down Expand Up @@ -1182,7 +1187,7 @@ private void openPlaylistFoldersFromPlaylistTree()

private void openPlaylistLocation(Playlist playList)
{
this.openFileLocation(playList.getPath());
this.openFileLocation(playList.getPath());
}

private void openFileLocation(Path path)
Expand Down Expand Up @@ -1579,9 +1584,11 @@ private void updateTabTitleForPlaylist(Playlist list, JPlaylistComponent comp)

public void runClosestMatchOnAllTabs()
{
for (PlaylistEditCtrl ctrl : this._playlistTabbedPane.getPlaylistEditors()) {
for (PlaylistEditCtrl ctrl : this._playlistTabbedPane.getPlaylistEditors())
{
this._playlistTabbedPane.setActivePlaylist(ctrl.getPlaylist());
if (!ctrl.locateMissingFiles() || !ctrl.bulkFindClosestMatches()) {
if (!ctrl.locateMissingFiles() || !ctrl.bulkFindClosestMatches())
{
break;
}
}
Expand Down Expand Up @@ -1667,7 +1674,8 @@ private void confirmCloseApp()
.filter(Playlist::isModified)
.collect(Collectors.toList());

if (!modifiedPlaylists.isEmpty()) {
if (!modifiedPlaylists.isEmpty())
{
Object[] options =
{
"Discard changes and exit", "Cancel"
Expand Down

0 comments on commit 7ec0687

Please sign in to comment.