JAL-3169 leave file browser open on Cancel in overwrite check
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 10 Dec 2018 11:25:44 +0000 (11:25 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 10 Dec 2018 11:25:44 +0000 (11:25 +0000)
src/jalview/io/JalviewFileChooser.java

index 7a21c16..271ac53 100755 (executable)
@@ -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)