X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FJalviewFileChooser.java;fp=src%2Fjalview%2Fio%2FJalviewFileChooser.java;h=271ac53943248cbdc84b3aa50fce707ec1ee0bbc;hb=3dce97f35c416e0cc6f833df95544819b05059da;hp=7a21c16cd52622d58e384572f2bd6a30fe61544c;hpb=4a0a4a012676c3ebee341d667eab07ea60c5770e;p=jalview.git 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)