Skip to content

Commit

Permalink
Added filter names and extension to save dialogs
Browse files Browse the repository at this point in the history
On Windows and Linux, this change ensures that the picked filename has
the given file extension. If the user manually adds a file extension
in the input field, the default extension is appended.

On macOS, the users is asked if he wants to use a non-standard file
extension in case it has been changed.

Issue: #3826
  • Loading branch information
buchen committed Mar 8, 2024
1 parent a87922c commit 15daeae
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ private File pickFile(Shell shell, String extension, String fileNameProposal)
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
dialog.setOverwrite(true);

// set filter names and extension to make sure the file name keeps the
// right extension.
dialog.setFilterNames(new String[] { Messages.LabelPortfolioPerformanceFile });
dialog.setFilterExtensions(new String[] { "*." + extension }); //$NON-NLS-1$

// if an extension is given, make sure the file name proposal has the
// right extension in the save as dialog
if (extension != null && !fileNameProposal.endsWith('.' + extension))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class AbstractCSVExporter

protected abstract Shell getShell();

private static final String[] FILTER_NAMES = { "CSV (*.csv)" }; //$NON-NLS-1$
private static final String[] FILTER_NAMES = { Messages.CSVImportLabelFileCSV };
private static final String[] FILTER_EXTS = { "*.csv" }; //$NON-NLS-1$

protected abstract void writeToFile(File file) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ private SimpleAction.Runnable exportToJSON(String filename, List<TransactionPair
{
return action -> {
FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.SAVE);
dialog.setFilterNames(new String[] { Messages.CSVConfigCSVImportLabelFileJSON });
dialog.setFilterExtensions(new String[] { "*.json" }); //$NON-NLS-1$

dialog.setFileName(TextUtil.sanitizeFilename(filename));
dialog.setOverwrite(true);
String name = dialog.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ public void run()
public void run()
{
FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.SAVE);
fileDialog.setFilterNames(new String[] { Messages.CSVImportLabelFileCSV });
fileDialog.setFilterExtensions(new String[] { "*.csv" }); //$NON-NLS-1$

fileDialog.setFileName(TextUtil.sanitizeFilename(security.getName() + ".csv")); //$NON-NLS-1$
fileDialog.setOverwrite(true);
String fileName = fileDialog.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ private void onConfigSaved()
private void onConfigExport()
{
FileDialog dialog = new FileDialog(getControl().getShell(), SWT.SAVE);
dialog.setFilterNames(new String[] { Messages.CSVConfigCSVImportLabelFileJSON });
dialog.setFilterExtensions(new String[] { "*.json" }); //$NON-NLS-1$

String proposal = TextUtil.sanitizeFilename(currentConfigLabel != null ? currentConfigLabel : "csv-config"); //$NON-NLS-1$
dialog.setFileName(proposal + ".json");//$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ else if (exportItem instanceof String string)
name = string;

FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
dialog.setFilterNames(new String[] { Messages.CSVImportLabelFileCSV });
dialog.setFilterExtensions(new String[] { "*.csv" }); //$NON-NLS-1$
dialog.setOverwrite(true);
if (name != null)
dialog.setFileName(TextUtil.sanitizeFilename(name + ".csv")); //$NON-NLS-1$
Expand Down

0 comments on commit 15daeae

Please sign in to comment.