From 833b0e8d60b46c17dad52d31f4896363f29499bc Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 2 Jul 2018 15:36:26 +0100 Subject: [PATCH] =?utf8?q?JAL-3048=20AlignFrame=20->=20Save=20dialog=20refac?= =?utf8?q?tored=20-=20patched=20=E2=80=98File=20exists=20-=20Overwrite=20=3F?= =?utf8?q?=E2=80=99=20dialog=20to=20be=20ignored=20in=20javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/jalview/gui/AlignFrame.java | 46 ++++++++-------- src/jalview/io/JalviewFileChooser.java | 93 ++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 51 deletions(-) diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 4695ed7..e45dc5d 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -88,6 +88,7 @@ import jalview.schemes.ColourSchemes; import jalview.schemes.ResidueColourScheme; import jalview.schemes.TCoffeeColourScheme; import jalview.util.MessageManager; +import jalview.util.dialogrunner.RunResponse; import jalview.viewmodel.AlignmentViewport; import jalview.viewmodel.ViewportRanges; import jalview.ws.DBRefFetcher; @@ -1122,42 +1123,41 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, String format = currentFileFormat == null ? null : currentFileFormat.getName(); - JalviewFileChooser chooser = JalviewFileChooser + final JalviewFileChooser chooser = JalviewFileChooser .forWrite(Cache.getProperty("LAST_DIRECTORY"), format); - + final AlignFrame us = this; chooser.setFileView(new JalviewFileView()); chooser.setDialogTitle( MessageManager.getString("label.save_alignment_to_file")); chooser.setToolTipText(MessageManager.getString("action.save")); - int value = chooser.showSaveDialog(this); - - - if (value == JalviewFileChooser.APPROVE_OPTION) + chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION) { - currentFileFormat = chooser.getSelectedFormat(); - while (currentFileFormat == null) - { - JvOptionPane.showInternalMessageDialog(Desktop.desktop, - MessageManager.getString( - "label.select_file_format_before_saving"), - MessageManager.getString("label.file_format_not_specified"), - JvOptionPane.WARNING_MESSAGE); + @Override + public void run() + { currentFileFormat = chooser.getSelectedFormat(); - value = chooser.showSaveDialog(this); - if (value != JalviewFileChooser.APPROVE_OPTION) + while (currentFileFormat == null) { - return; + JvOptionPane.showInternalMessageDialog(Desktop.desktop, + MessageManager.getString( + "label.select_file_format_before_saving"), + MessageManager + .getString("label.file_format_not_specified"), + JvOptionPane.WARNING_MESSAGE); + currentFileFormat = chooser.getSelectedFormat(); + chooser.showSaveDialog(us); } - } - fileName = chooser.getSelectedFile().getPath(); + fileName = chooser.getSelectedFile().getPath(); - Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName()); + Cache.setProperty("DEFAULT_FILE_FORMAT", + currentFileFormat.getName()); - Cache.setProperty("LAST_DIRECTORY", fileName); - saveAlignment(fileName, currentFileFormat); - } + Cache.setProperty("LAST_DIRECTORY", fileName); + saveAlignment(fileName, currentFileFormat); + } + }).showSaveDialog(this); } public boolean saveAlignment(String file, FileFormatI format) diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 65cb603..cf72ffb 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -26,6 +26,7 @@ import jalview.gui.JvOptionPane; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.util.dialogrunner.DialogRunnerI; +import jalview.util.dialogrunner.Response; import jalview.util.dialogrunner.RunResponse; import java.awt.Component; @@ -303,44 +304,80 @@ public class JalviewFileChooser extends JFileChooser return null; } - @Override - public int showSaveDialog(Component parent) throws HeadlessException + Component saveparent; + RunResponse overwriteCheck = new RunResponse( + JalviewFileChooser.APPROVE_OPTION) { - this.setAccessory(null); + @Override + public void run() + { + // 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(); - setDialogType(SAVE_DIALOG); + 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); + + // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE + // USER PROMPTED FOR A NEW FILENAME + /** + * @j2sNative + */ + { + if (getSelectedFile().exists()) + { + // TODO JAL-3048 - may not need to raise this for browser saves - int ret = showDialog(parent, MessageManager.getString("action.save")); + // 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); + } + } + } + }; + }; - if (getFileFilter() instanceof JalviewFileFilter) - { - JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter(); + @Override + public int showSaveDialog(Component parent) throws HeadlessException + { + this.setAccessory(null); - if (!jvf.accept(getSelectedFile())) - { - String withExtension = getSelectedFile() + "." - + jvf.getAcceptableExtension(); - setSelectedFile(new File(withExtension)); - } + /* + * Save dialog is opened until user picks a file format + */ + if (!runner.isRegistered(overwriteCheck)) + { + // first call for this instance + runner.firstResponse(overwriteCheck); } - // 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) - && getSelectedFile().exists()) + else { - 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) - { - ret = JalviewFileChooser.CANCEL_OPTION; - } + // reset response flags + runner.resetResponses(); } - return ret; + setDialogType(SAVE_DIALOG); + saveparent = parent; + + int value = showDialog(parent, MessageManager.getString("action.save")); + + runner.run(value); + return value; } void recentListSelectionChanged(Object selection) -- 1.7.10.2