From 3dce97f35c416e0cc6f833df95544819b05059da Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 10 Dec 2018 11:25:44 +0000 Subject: [PATCH] JAL-3169 leave file browser open on Cancel in overwrite check --- src/jalview/io/JalviewFileChooser.java | 53 +++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 7a21c16..271ac53 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -295,14 +295,29 @@ public class JalviewFileChooser extends JFileChooser public int showSaveDialog(Component parent) throws HeadlessException { this.setAccessory(null); + this.setSelectedFile(null); + return super.showSaveDialog(parent); + } - setDialogType(SAVE_DIALOG); + /** + * If doing a Save, and an existing file is chosen or entered, prompt for + * confirmation of overwrite. Proceed if Yes, else leave the file chooser + * open. + * + * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite + */ + @Override + public void approveSelection() + { + if (getDialogType() != SAVE_DIALOG) + { + super.approveSelection(); + return; + } - this.setSelectedFile(null); - int ret = showDialog(parent, MessageManager.getString("action.save")); ourselectedFile = getSelectedFile(); - if (getSelectedFile() == null) + if (ourselectedFile == null) { // Workaround for Java 9,10 on OSX - no selected file, but there is a // filename typed in @@ -319,11 +334,15 @@ public class JalviewFileChooser extends JFileChooser "Unexpected exception when trying to get filename."); x.printStackTrace(); } + // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND + // THE + // USER PROMPTED FOR A NEW FILENAME } if (ourselectedFile == null) { - return JalviewFileChooser.CANCEL_OPTION; + return; } + if (getFileFilter() instanceof JalviewFileFilter) { JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); @@ -336,23 +355,21 @@ public class JalviewFileChooser extends JFileChooser setSelectedFile(ourselectedFile); } } - // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE - // USER PROMPTED FOR A NEW FILENAME - if ((ret == JalviewFileChooser.APPROVE_OPTION) - && ourselectedFile.exists()) - { - int confirm = JvOptionPane.showConfirmDialog(parent, - MessageManager.getString("label.overwrite_existing_file"), - MessageManager.getString("label.file_already_exists"), - JvOptionPane.YES_NO_OPTION); - if (confirm != JvOptionPane.YES_OPTION) + if (ourselectedFile.exists()) { - ret = JalviewFileChooser.CANCEL_OPTION; + int confirm = JvOptionPane.showConfirmDialog(this, + MessageManager.getString("label.overwrite_existing_file"), + MessageManager.getString("label.file_already_exists"), + JvOptionPane.YES_NO_OPTION); + + if (confirm != JvOptionPane.YES_OPTION) + { + return; + } } - } - return ret; + super.approveSelection(); } void recentListSelectionChanged(Object selection) -- 1.7.10.2