Merge branch 'feature/JAL-3063JAXB' into Jalview-BH/JAL-3026-JAL-3063-JAXB
[jalview.git] / src / jalview / io / JalviewFileChooser.java
index 65cb603..c90ecdc 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;
@@ -192,8 +193,14 @@ public class JalviewFileChooser extends JFileChooser
 
   public void openDialog(Component parent)
   {
+    runner.resetResponses();
     int value = showOpenDialog(this);
-    runner.run(value);
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(value);
+    }
   }
 
   /**
@@ -302,45 +309,127 @@ public class JalviewFileChooser extends JFileChooser
     }
     return null;
   }
+  
+  File ourselectedFile = null;
 
   @Override
-  public int showSaveDialog(Component parent) throws HeadlessException
+  public File getSelectedFile()
   {
-    this.setAccessory(null);
-
-    setDialogType(SAVE_DIALOG);
-
-    int ret = showDialog(parent, MessageManager.getString("action.save"));
-
+    File selfile = super.getSelectedFile();
+    if (selfile == null && ourselectedFile != null)
+    {
+      return ourselectedFile;
+    }
+    return selfile;
+  }
 
-    if (getFileFilter() instanceof JalviewFileFilter)
+  Component saveparent;
+  RunResponse overwriteCheck = new RunResponse(
+          JalviewFileChooser.APPROVE_OPTION)
+  {
+    @Override
+    public void run()
     {
-      JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
+    ourselectedFile = getSelectedFile();
 
-      if (!jvf.accept(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)
       {
-        String withExtension = getSelectedFile() + "."
-                + jvf.getAcceptableExtension();
-        setSelectedFile(new File(withExtension));
+        System.err.println(
+                "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 ((ret == JalviewFileChooser.APPROVE_OPTION)
-            && getSelectedFile().exists())
+    if (ourselectedFile == null)
     {
-      int confirm = JvOptionPane.showConfirmDialog(parent,
-              MessageManager.getString("label.overwrite_existing_file"),
-              MessageManager.getString("label.file_already_exists"),
-              JvOptionPane.YES_NO_OPTION);
+      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)
+      {
+        JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
 
-      if (confirm != JvOptionPane.YES_OPTION)
+        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
+       */
       {
-        ret = JalviewFileChooser.CANCEL_OPTION;
+        if (getSelectedFile().exists())
+        {
+          // JAL-3048 - may not need to raise this for browser saves
+
+          // 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);
+          }
+        }
       }
+    };
+  };
+
+  @Override
+  public int showSaveDialog(Component parent) throws HeadlessException
+  {
+    this.setAccessory(null);
+
+    /*
+     * Save dialog is opened until user picks a file format 
+     */
+    if (!runner.isRegistered(overwriteCheck))
+    {
+      // first call for this instance
+      runner.firstResponse(overwriteCheck);
+    }
+    else
+    {
+      // reset response flags
+      runner.resetResponses();
     }
 
-    return ret;
+    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"));
+    /**
+     * @j2sNative
+     */
+    {
+      runner.firstRun(value);
+    }
+    return value;
   }
 
   void recentListSelectionChanged(Object selection)