From: gmungoc Date: Mon, 17 Dec 2018 16:45:11 +0000 (+0000) Subject: Merge branch 'feature/JAL-3169cancelOverwrite' into trialMerge X-Git-Tag: Release_2_11_4_0~45^2~18^2~350^2~1 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=4b527101d4272edd16a94be664bc3b4761bbd185;p=jalview.git Merge branch 'feature/JAL-3169cancelOverwrite' into trialMerge Conflicts: src/jalview/io/JalviewFileChooser.java --- 4b527101d4272edd16a94be664bc3b4761bbd185 diff --cc src/jalview/io/JalviewFileChooser.java index b7eeaab,6b0eba8..2419bef --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@@ -62,10 -58,7 +62,10 @@@ import javax.swing.plaf.basic.BasicFile * */ public class JalviewFileChooser extends JFileChooser - implements PropertyChangeListener, DialogRunnerI ++ implements PropertyChangeListener, DialogRunnerI { + DialogRunner runner = new DialogRunner<>(this); + /** * Factory method to return a file chooser that offers readable alignment file * formats @@@ -331,16 -293,33 +331,35 @@@ return selfile; } - Component saveparent; - RunResponse overwriteCheck = new RunResponse( - JalviewFileChooser.APPROVE_OPTION) + @Override + public int showSaveDialog(Component parent) throws HeadlessException { - @Override - public void run() + this.setAccessory(null); ++ // Java 9,10,11 on OSX - clear selected file so name isn't auto populated + this.setSelectedFile(null); ++ + return super.showSaveDialog(parent); + } + + /** + * 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; + } + 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 @@@ -360,89 -342,36 +382,36 @@@ } if (ourselectedFile == null) { - returned = new Response(JalviewFileChooser.CANCEL_OPTION); return; } - // JBP Note - this code was executed regardless of 'SAVE' being pressed - // need to see if there were side effects - if (getFileFilter() instanceof JalviewFileFilter) - { - JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); - if (!jvf.accept(getSelectedFile())) - { - String withExtension = getSelectedFile() + "." - + jvf.getAcceptableExtension(); - setSelectedFile(new File(withExtension)); - } - } - // All good, so we continue to save - returned = new Response(JalviewFileChooser.APPROVE_OPTION); + if (getFileFilter() instanceof JalviewFileFilter) + { + JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); - // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE - // USER PROMPTED FOR A NEW FILENAME - /** - * @j2sNative - */ + if (!jvf.accept(ourselectedFile)) { - if (getSelectedFile().exists()) - { - // JAL-3048 - may not need to raise this for browser saves - - // yes/no cancel - int confirm = JvOptionPane.showConfirmDialog(saveparent, - MessageManager.getString("label.overwrite_existing_file"), - MessageManager.getString("label.file_already_exists"), - JvOptionPane.YES_NO_OPTION); - - if (confirm != JvOptionPane.YES_OPTION) - { - returned = new Response(JalviewFileChooser.CANCEL_OPTION); - } - } + String withExtension = getSelectedFile().getName() + "." + + jvf.getAcceptableExtension(); + ourselectedFile = (new File(getCurrentDirectory(), withExtension)); + setSelectedFile(ourselectedFile); } - }; - }; - - /** - * Overridden for JalviewJS compatibility: only one thread in Javascript, - * so we can't wait for user choice in another thread and then perform the - * desired action - */ - @Override - public int showSaveDialog(Component parent) throws HeadlessException - { - this.setAccessory(null); - - /* - * Save dialog is opened until user picks a file format - */ - if (!runner.isRegistered(overwriteCheck)) - { - // first call for this instance - runner.firstResponse(overwriteCheck); } - else - { - // reset response flags - runner.resetResponses(); - } - - setDialogType(SAVE_DIALOG); - - // Java 9,10,11 on OSX - clear selected file so name isn't auto populated - this.setSelectedFile(null); - - saveparent = parent; - int value = showDialog(parent, MessageManager.getString("action.save")); - /** - * @j2sNative - */ + if (ourselectedFile.exists()) - { - int confirm = JvOptionPane.showConfirmDialog(this, - MessageManager.getString("label.overwrite_existing_file"), - MessageManager.getString("label.file_already_exists"), - JvOptionPane.YES_NO_OPTION); + { - runner.firstRun(value); ++ 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; - } ++ if (confirm != JvOptionPane.YES_OPTION) ++ { ++ return; + } + } - return value; + + super.approveSelection(); } void recentListSelectionChanged(Object selection)