JAL-2988 workaround: overload getSelectedFile() because calling setSelectedFile(...
authorJim Procter <jprocter@issues.jalview.org>
Fri, 7 Sep 2018 13:23:16 +0000 (14:23 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Fri, 7 Sep 2018 13:23:16 +0000 (14:23 +0100)
src/jalview/io/JalviewFileChooser.java

index cb42662..2ba7bbf 100755 (executable)
@@ -278,6 +278,19 @@ public class JalviewFileChooser extends JFileChooser
     return null;
   }
 
+  File ourselectedFile = null;
+
+  @Override
+  public File getSelectedFile()
+  {
+    File selfile = super.getSelectedFile();
+    if (selfile == null && ourselectedFile != null)
+    {
+      return ourselectedFile;
+    }
+    return selfile;
+  }
+
   @Override
   public int showSaveDialog(Component parent) throws HeadlessException
   {
@@ -287,22 +300,24 @@ public class JalviewFileChooser extends JFileChooser
 
     this.setSelectedFile(null);
     int ret = showDialog(parent, MessageManager.getString("action.save"));
+    ourselectedFile = getSelectedFile();
 
     if (getFileFilter() instanceof JalviewFileFilter)
     {
       JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
 
-      if (!jvf.accept(getSelectedFile()))
+      if (!jvf.accept(ourselectedFile))
       {
-        String withExtension = getSelectedFile() + "."
+        String withExtension = getSelectedFile().getName() + "."
                 + jvf.getAcceptableExtension();
-        setSelectedFile(new File(withExtension));
+        ourselectedFile = (new File(getCurrentDirectory(), withExtension));
+        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)
-            && getSelectedFile().exists())
+            && ourselectedFile.exists())
     {
       int confirm = JvOptionPane.showConfirmDialog(parent,
               MessageManager.getString("label.overwrite_existing_file"),