JAL-3048 AlignFrame -> Save dialog refactored - patched ‘File exists - Overwrite...
authorJim Procter <jprocter@issues.jalview.org>
Mon, 2 Jul 2018 14:36:26 +0000 (15:36 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Mon, 2 Jul 2018 14:36:26 +0000 (15:36 +0100)
src/jalview/gui/AlignFrame.java
src/jalview/io/JalviewFileChooser.java

index 4695ed7..e45dc5d 100644 (file)
@@ -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)
index 65cb603..cf72ffb 100755 (executable)
@@ -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)