Merge branch 'Jalview-BH/JAL-3026-JAL-3063-JAXB' into trialMerge
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index 809985d..c90ecdc 100755 (executable)
@@ -309,6 +309,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;
+  }
 
   Component saveparent;
   RunResponse overwriteCheck = new RunResponse(
@@ -317,6 +330,31 @@ public class JalviewFileChooser extends JFileChooser
     @Override
     public void run()
     {
+    ourselectedFile = getSelectedFile();
+
+    if (getSelectedFile() == null)
+    {
+      // Workaround for Java 9,10 on OSX - no selected file, but there is a
+      // filename typed in
+      try
+      {
+        String filename = ((BasicFileChooserUI) getUI()).getFileName();
+        if (filename != null && filename.length() > 0)
+        {
+          ourselectedFile = new File(getCurrentDirectory(), filename);
+        }
+      } catch (Throwable x)
+      {
+        System.err.println(
+                "Unexpected exception when trying to get filename.");
+        x.printStackTrace();
+      }
+    }
+    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)
@@ -378,6 +416,10 @@ public class JalviewFileChooser extends JFileChooser
     }
 
     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"));