Skip to content

Commit

Permalink
redid filechooser manager to hold on to file chooser references
Browse files Browse the repository at this point in the history
  • Loading branch information
kunstmusik committed Oct 6, 2023
1 parent 84bef85 commit 343123b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 90 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ information.

# CHANGE LOG

## [2.9.2] - 2023-xx-xx

### FIX

* Redid filechooser manager to hold on to Filechoosers so that they will
maintain last state (i.e., last file or folder selected) between uses

## [2.9.1] - 2023-09-10

### FIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -12,9 +13,11 @@

public class FileChooserManager {

private HashMap<Object, DialogInfoSet> dialogInfoSets
private HashMap<Object, FileDialog> fileDialogs
= new HashMap<>();

private FilenameFilter directoryFileNameFilter = (dir, file) -> new File(dir, file).isDirectory();

// private FileChooser fileChooser;
private static FileChooserManager instance = null;

Expand All @@ -26,39 +29,39 @@ public static FileChooserManager getDefault() {
}

public void addFilter(Object fileChooserId, FileFilter filter) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.filters.add(filter);
// DialogInfoSet temp = getDialogInfoSet(fileChooserId);
// temp.filters.add(filter);
var dialog = getDialog(fileChooserId);
// FIXME
// dialog.setFilenameFilter((dir, name) ->
// return filter.accept(new File(dir, name)));
}

public void setSelectedFile(Object fileChooserId, File f) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.currentDirectory = null;
temp.selectedFile = f;
getDialog(fileChooserId).setFile(f.getAbsolutePath());
}

public void setCurrentDirectory(Object fileChooserId, File f) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.selectedFile = null;
temp.currentDirectory = f;
getDialog(fileChooserId).setDirectory(
f.isDirectory() ? f.getAbsolutePath()
: f.getParent());
}

public void setDialogTitle(Object fileChooserId, String title) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.dialogTitle = title;
getDialog(fileChooserId).setTitle(title);
}

public void setMultiSelectionEnabled(Object fileChooserId, boolean val) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.isMultiSelect = val;
getDialog(fileChooserId).setMultipleMode(val);
}

public void setDirectoryChooser(Object fileChooserId, boolean isDirectoriesOnly) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
temp.directoriesOnly = isDirectoriesOnly;
getDialog(fileChooserId).setFilenameFilter(
directoryFileNameFilter);
}

public List<File> showOpenDialog(Object fileChooserId, Component parent) {
final DialogInfoSet temp = getDialogInfoSet(fileChooserId);
final var dialog = getDialog(fileChooserId);
final List<File> retVal = new ArrayList<>();

final boolean isMac = System.getProperty("os.name").toLowerCase().startsWith(
Expand All @@ -68,36 +71,16 @@ public List<File> showOpenDialog(Object fileChooserId, Component parent) {
final boolean isSwingEDT = SwingUtilities.isEventDispatchThread();

Runnable r = () -> {
// USE AWT IMPL ON MAC DUE TO ISSUES WITH FILE CHOOSER
// THAT APPEAR TO BE INTRODUCED BY MacOS
java.awt.FileDialog ff = new java.awt.FileDialog((Frame) SwingUtilities.windowForComponent(parent));

ff.setFilenameFilter((dir, name)
-> true
);

ff.setMode(FileDialog.LOAD);
ff.setTitle(temp.dialogTitle);
ff.setMultipleMode(temp.isMultiSelect);

if (temp.currentDirectory != null) {
ff.setDirectory(temp.currentDirectory.getAbsolutePath());
} else if (temp.selectedFile != null) {
if(temp.selectedFile.getParentFile() != null) {
ff.setDirectory(temp.selectedFile.getParentFile().getAbsolutePath());
}
}
if (temp.selectedFile != null) {
ff.setFile(temp.selectedFile.getName());
}

if (temp.directoriesOnly) {
dialog.setMode(FileDialog.LOAD);

if (dialog.getFilenameFilter() == directoryFileNameFilter) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
}

ff.setVisible(true);
dialog.setVisible(true);

final File[] files = ff.getFiles();
final File[] files = dialog.getFiles();

System.setProperty("apple.awt.fileDialogForDirectories", "false");

Expand All @@ -119,36 +102,20 @@ public List<File> showOpenDialog(Object fileChooserId, Component parent) {
}

public File showSaveDialog(Object fileChooserId, Component parent) {
DialogInfoSet temp = getDialogInfoSet(fileChooserId);
final var dialog = getDialog(fileChooserId);
final boolean isSwingEDT = SwingUtilities.isEventDispatchThread();

final List<File> retVal = new ArrayList<>();

// USE AWT IMPL ON MAC DUE TO ISSUES WITH FILE CHOOSER
// THAT APPEAR TO BE INTRODUCED BY MacOS
Runnable r = () -> {
java.awt.FileDialog ff = new java.awt.FileDialog((Frame) SwingUtilities.windowForComponent(parent));

ff.setFilenameFilter((File dir, String name)
-> true
);

ff.setMode(FileDialog.SAVE);
ff.setTitle(temp.dialogTitle);
// ff.setMultipleMode(temp.isMultiSelect);
dialog.setMode(FileDialog.SAVE);

if (temp.currentDirectory != null) {
ff.setDirectory(temp.currentDirectory.getAbsolutePath());
} else if (temp.selectedFile != null) {
ff.setDirectory(temp.selectedFile.getParentFile().getAbsolutePath());
}
if (temp.selectedFile != null) {
ff.setFile(temp.selectedFile.getName());
}

ff.setVisible(true);
dialog.setVisible(true);

final File[] files = ff.getFiles();
final File[] files = dialog.getFiles();

if (files != null && files.length > 0) {
retVal.add(files[0]);
Expand All @@ -165,42 +132,38 @@ public File showSaveDialog(Object fileChooserId, Component parent) {
return retVal.size() == 0 ? null : retVal.get(0);
}

private DialogInfoSet getDialogInfoSet(Object fileChooserId) {
if (dialogInfoSets.containsKey(fileChooserId)) {
DialogInfoSet infoSet = dialogInfoSets.get(fileChooserId);
return infoSet;
} else {
DialogInfoSet temp = new DialogInfoSet();

temp.selectedFile = new File(System.getProperty("user.home"));
temp.currentDirectory = new File(System.getProperty("user.home"));
private FileDialog getDialog(Object fileChooserId) {
var dialog = fileDialogs.get(fileChooserId);

dialogInfoSets.put(fileChooserId, temp);
if (dialog == null) {
dialog = new FileDialog((Frame) null);
dialog.setDirectory(System.getProperty("user.home"));

return temp;
fileDialogs.put(fileChooserId, dialog);
}
return dialog;
}

public boolean isDialogDefined(Object fileChooserId) {
return dialogInfoSets.containsKey(fileChooserId);
return fileDialogs.containsKey(fileChooserId);

}

public static class DialogInfoSet {

List<FileFilter> filters = new ArrayList<>();

File selectedFile;

File currentDirectory;

File[] selectedFiles;

String dialogTitle = "Select File";

boolean directoriesOnly = false;

boolean isMultiSelect = false;

}
// public static class DialogInfoSet {
//
// List<FileFilter> filters = new ArrayList<>();
//
// File selectedFile;
//
// File currentDirectory;
//
// File[] selectedFiles;
//
// String dialogTitle = "Select File";
//
// boolean directoriesOnly = false;
//
// boolean isMultiSelect = false;
//
// }
}

0 comments on commit 343123b

Please sign in to comment.